blob: 10304517fff73bbcadda715e7b65f11ee82e032e [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"
John Reckd7db4d72015-05-20 07:18:55 -070026#include "utils/GLUtils.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080027
28#include <utils/Log.h>
29#include <utils/String8.h>
Chet Haasedd78cca2010-10-22 18:59:26 -070030
31namespace android {
Chet Haasedd78cca2010-10-22 18:59:26 -070032namespace uirenderer {
33
Chris Craik96a5c4c2015-01-27 15:46:35 -080034Caches* Caches::sInstance = nullptr;
35
Chet Haasedd78cca2010-10-22 18:59:26 -070036///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070037// Macros
38///////////////////////////////////////////////////////////////////////////////
39
40#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000041 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070042#else
43 #define FLUSH_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070047// Constructors/destructor
48///////////////////////////////////////////////////////////////////////////////
49
Chris Craik96a5c4c2015-01-27 15:46:35 -080050Caches::Caches(RenderState& renderState)
Chris Craik117bdbc2015-02-05 10:12:38 -080051 : gradientCache(mExtensions)
52 , patchCache(renderState)
53 , programCache(mExtensions)
Chris Craik44eb2c02015-01-29 09:45:09 -080054 , dither(*this)
Chris Craik96a5c4c2015-01-27 15:46:35 -080055 , mRenderState(&renderState)
Chris Craik96a5c4c2015-01-27 15:46:35 -080056 , mInitialized(false) {
57 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guy8ff6b9e2011-11-09 20:10:18 -080058 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070059 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070060 initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -070061 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080062 initExtensions();
Chet Haasedd78cca2010-10-22 18:59:26 -070063}
64
Romain Guy3b748a42013-04-17 18:54:38 -070065bool Caches::init() {
66 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080067
John Reckfbc8df02014-11-14 16:18:41 -080068 ATRACE_NAME("Caches::init");
69
Chris Craikd41c4d82015-01-05 15:51:13 -080070 mRegionMesh = nullptr;
Chris Craik6c15ffa2015-02-02 13:50:55 -080071 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080072
Romain Guy54c1a642012-09-27 17:55:46 -070073 mFunctorsCount = 0;
74
Chris Craik96a5c4c2015-01-27 15:46:35 -080075 patchCache.init();
Romain Guy3b748a42013-04-17 18:54:38 -070076
Romain Guy8ff6b9e2011-11-09 20:10:18 -080077 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -070078
Chris Craik44eb2c02015-01-29 09:45:09 -080079 mPixelBufferState = new PixelBufferState();
80 mTextureState = new TextureState();
Romain Guy8aa195d2013-06-04 18:00:09 -070081
Romain Guy3b748a42013-04-17 18:54:38 -070082 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080083}
84
Romain Guyb1d0a4e2012-07-13 18:25:35 -070085void Caches::initFont() {
86 fontRenderer = GammaFontRenderer::createRenderer();
87}
88
Romain Guydfa10462012-05-12 16:18:58 -070089void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -080090 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -070091 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -080092
Chris Craikff785832013-03-08 13:12:16 -080093 startMark = glPushGroupMarkerEXT;
94 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -070095 } else {
96 eventMark = eventMarkNull;
97 startMark = startMarkNull;
98 endMark = endMarkNull;
99 }
Romain Guydfa10462012-05-12 16:18:58 -0700100}
101
102void Caches::initConstraints() {
Romain Guydfa10462012-05-12 16:18:58 -0700103 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
104}
105
Romain Guyf9f00162013-05-09 11:50:12 -0700106void Caches::initStaticProperties() {
107 gpuPixelBuffersEnabled = false;
108
109 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700110 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700111 char property[PROPERTY_VALUE_MAX];
112 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
113 gpuPixelBuffersEnabled = !strcmp(property, "true");
114 }
115 }
116}
117
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800118void Caches::terminate() {
119 if (!mInitialized) return;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800120 mRegionMesh.release();
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800121
122 fboCache.clear();
123
124 programCache.clear();
Chris Craik34725682015-02-05 09:06:30 -0800125 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800126
Romain Guy3b748a42013-04-17 18:54:38 -0700127 patchCache.clear();
128
Romain Guy46bfc482013-08-16 18:38:29 -0700129 clearGarbage();
130
Chris Craik44eb2c02015-01-29 09:45:09 -0800131 delete mPixelBufferState;
132 mPixelBufferState = nullptr;
133 delete mTextureState;
134 mTextureState = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800135 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700136}
137
Chris Craik6c15ffa2015-02-02 13:50:55 -0800138void Caches::setProgram(const ProgramDescription& description) {
139 setProgram(programCache.get(description));
140}
141
142void Caches::setProgram(Program* program) {
143 if (!program || !program->isInUse()) {
144 if (mProgram) {
145 mProgram->remove();
146 }
147 if (program) {
148 program->use();
149 }
150 mProgram = program;
151 }
152}
153
Romain Guy5b3b3522010-10-27 18:57:51 -0700154///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800155// Debug
156///////////////////////////////////////////////////////////////////////////////
157
Romain Guy627c6fd2013-08-21 11:53:18 -0700158uint32_t Caches::getOverdrawColor(uint32_t amount) const {
159 static uint32_t sOverdrawColors[2][4] = {
160 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
161 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
162 };
163 if (amount < 1) amount = 1;
164 if (amount > 4) amount = 4;
Chris Craik2507c342015-05-04 14:36:49 -0700165
166 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
167 return sOverdrawColors[overdrawColorIndex][amount - 1];
Romain Guy627c6fd2013-08-21 11:53:18 -0700168}
169
Romain Guyc15008e2010-11-10 11:59:15 -0800170void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700171 String8 stringLog;
172 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000173 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700174}
175
176void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700177 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700178 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
179 log.appendFormat(" TextureCache %8d / %8d\n",
180 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700181 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
182 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700183 if (mRenderState) {
184 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800185 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck0e89e2b2014-10-31 14:49:06 -0700186 it != mRenderState->mActiveLayers.end(); it++) {
187 const Layer* layer = *it;
188 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
189 layer->getWidth(), layer->getHeight(),
Chris Craikf27133d2015-02-19 09:51:53 -0800190 layer->isTextureLayer(), layer->getTextureId(),
John Reck0e89e2b2014-10-31 14:49:06 -0700191 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800192 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700193 }
194 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
195 memused, mRenderState->mActiveLayers.size());
196 total += memused;
197 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800198 log.appendFormat(" RenderBufferCache %8d / %8d\n",
199 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700200 log.appendFormat(" GradientCache %8d / %8d\n",
201 gradientCache.getSize(), gradientCache.getMaxSize());
202 log.appendFormat(" PathCache %8d / %8d\n",
203 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700204 log.appendFormat(" TessellationCache %8d / %8d\n",
205 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700206 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800207 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700208 log.appendFormat(" PatchCache %8d / %8d\n",
209 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700210 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700211 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
212 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
213 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
214 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
215 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
216 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800217 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700218 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700219 log.appendFormat(" FboCache %8d / %8d\n",
220 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800221
Romain Guyc15008e2010-11-10 11:59:15 -0800222 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800223 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800224 total += gradientCache.getSize();
225 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700226 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800227 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700228 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700229 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700230 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
231 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800232 }
233
Chet Haase9c1e23b2011-03-24 10:51:31 -0700234 log.appendFormat("Total memory usage:\n");
235 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800236}
237
238///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800239// Memory management
240///////////////////////////////////////////////////////////////////////////////
241
242void Caches::clearGarbage() {
243 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800244 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700245 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800246}
247
Romain Guybdf76092011-07-18 15:00:43 -0700248void Caches::flush(FlushMode mode) {
249 FLUSH_LOGD("Flushing caches (mode %d)", mode);
250
Romain Guyb0a41ed2013-08-16 14:44:38 -0700251 // We must stop tasks before clearing caches
252 if (mode > kFlushMode_Layers) {
253 tasks.stop();
254 }
255
Romain Guybdf76092011-07-18 15:00:43 -0700256 switch (mode) {
257 case kFlushMode_Full:
258 textureCache.clear();
259 patchCache.clear();
260 dropShadowCache.clear();
261 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700262 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700263 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700264 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700265 // fall through
266 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700267 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700268 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700269 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700270 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700271 // fall through
272 case kFlushMode_Layers:
273 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800274 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700275 break;
276 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700277
278 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700279 glFinish();
John Reckd7db4d72015-05-20 07:18:55 -0700280 // Errors during cleanup should be considered non-fatal, dump them and
281 // and move on. TODO: All errors or just errors like bad surface?
282 GLUtils::dumpGLErrors();
Romain Guybdf76092011-07-18 15:00:43 -0700283}
284
Romain Guyfe48f652010-11-11 15:36:56 -0800285///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700286// Tiling
287///////////////////////////////////////////////////////////////////////////////
288
Romain Guyf735c8e2013-01-31 17:45:55 -0800289void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Chris Craik2507c342015-05-04 14:36:49 -0700290 if (mExtensions.hasTiledRendering() && !Properties::debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800291 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700292 }
293}
294
295void Caches::endTiling() {
Chris Craik2507c342015-05-04 14:36:49 -0700296 if (mExtensions.hasTiledRendering() && !Properties::debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700297 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700298 }
299}
300
Romain Guy54c1a642012-09-27 17:55:46 -0700301bool Caches::hasRegisteredFunctors() {
302 return mFunctorsCount > 0;
303}
304
305void Caches::registerFunctors(uint32_t functorCount) {
306 mFunctorsCount += functorCount;
307}
308
309void Caches::unregisterFunctors(uint32_t functorCount) {
310 if (functorCount > mFunctorsCount) {
311 mFunctorsCount = 0;
312 } else {
313 mFunctorsCount -= functorCount;
314 }
315}
316
Romain Guy85ef80d2012-09-13 20:26:50 -0700317///////////////////////////////////////////////////////////////////////////////
318// Regions
319///////////////////////////////////////////////////////////////////////////////
320
Romain Guy5b3b3522010-10-27 18:57:51 -0700321TextureVertex* Caches::getRegionMesh() {
322 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
323 if (!mRegionMesh) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800324 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700325 }
326
Chris Craik51d6a3d2014-12-22 17:16:56 -0800327 return mRegionMesh.get();
Romain Guy5b3b3522010-10-27 18:57:51 -0700328}
329
Chris Craikba9b6132013-12-15 17:10:19 -0800330///////////////////////////////////////////////////////////////////////////////
331// Temporary Properties
332///////////////////////////////////////////////////////////////////////////////
333
Chet Haasedd78cca2010-10-22 18:59:26 -0700334}; // namespace uirenderer
335}; // namespace android