blob: ff713133e3896bc64ea7fddd374e91ac7c1248db [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
Chet Haasedd78cca2010-10-22 18:59:26 -070017#include "Caches.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080018
Tom Hudson2dc236b2014-10-15 15:46:42 -040019#include "GammaFontRenderer.h"
Romain Guy09b7c912011-02-02 20:28:09 -080020#include "LayerRenderer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080021#include "Properties.h"
22#include "renderstate/RenderState.h"
ztenghui63d41ab2014-02-14 13:13:41 -080023#include "ShadowTessellator.h"
John Reckd7db4d72015-05-20 07:18:55 -070024#include "utils/GLUtils.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080025
26#include <utils/Log.h>
27#include <utils/String8.h>
Chet Haasedd78cca2010-10-22 18:59:26 -070028
29namespace android {
Chet Haasedd78cca2010-10-22 18:59:26 -070030namespace uirenderer {
31
Chris Craik96a5c4c2015-01-27 15:46:35 -080032Caches* Caches::sInstance = nullptr;
33
Chet Haasedd78cca2010-10-22 18:59:26 -070034///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070035// Macros
36///////////////////////////////////////////////////////////////////////////////
37
38#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000039 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070040#else
41 #define FLUSH_LOGD(...)
42#endif
43
44///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070045// Constructors/destructor
46///////////////////////////////////////////////////////////////////////////////
47
Chris Craik96a5c4c2015-01-27 15:46:35 -080048Caches::Caches(RenderState& renderState)
Chris Craik117bdbc2015-02-05 10:12:38 -080049 : gradientCache(mExtensions)
50 , patchCache(renderState)
51 , programCache(mExtensions)
Chris Craik44eb2c02015-01-29 09:45:09 -080052 , dither(*this)
Chris Craik96a5c4c2015-01-27 15:46:35 -080053 , mRenderState(&renderState)
Chris Craik96a5c4c2015-01-27 15:46:35 -080054 , mInitialized(false) {
55 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guy8ff6b9e2011-11-09 20:10:18 -080056 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070057 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070058 initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -070059 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080060 initExtensions();
Chet Haasedd78cca2010-10-22 18:59:26 -070061}
62
Romain Guy3b748a42013-04-17 18:54:38 -070063bool Caches::init() {
64 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080065
John Reckfbc8df02014-11-14 16:18:41 -080066 ATRACE_NAME("Caches::init");
67
Chris Craikd41c4d82015-01-05 15:51:13 -080068 mRegionMesh = nullptr;
Chris Craik6c15ffa2015-02-02 13:50:55 -080069 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080070
Chris Craik96a5c4c2015-01-27 15:46:35 -080071 patchCache.init();
Romain Guy3b748a42013-04-17 18:54:38 -070072
Romain Guy8ff6b9e2011-11-09 20:10:18 -080073 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -070074
Chris Craik44eb2c02015-01-29 09:45:09 -080075 mPixelBufferState = new PixelBufferState();
76 mTextureState = new TextureState();
Romain Guy8aa195d2013-06-04 18:00:09 -070077
Romain Guy3b748a42013-04-17 18:54:38 -070078 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080079}
80
Romain Guyb1d0a4e2012-07-13 18:25:35 -070081void Caches::initFont() {
82 fontRenderer = GammaFontRenderer::createRenderer();
83}
84
Romain Guydfa10462012-05-12 16:18:58 -070085void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -080086 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -070087 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -080088
Chris Craikff785832013-03-08 13:12:16 -080089 startMark = glPushGroupMarkerEXT;
90 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -070091 } else {
92 eventMark = eventMarkNull;
93 startMark = startMarkNull;
94 endMark = endMarkNull;
95 }
Romain Guydfa10462012-05-12 16:18:58 -070096}
97
98void Caches::initConstraints() {
Romain Guydfa10462012-05-12 16:18:58 -070099 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
100}
101
Romain Guyf9f00162013-05-09 11:50:12 -0700102void Caches::initStaticProperties() {
103 gpuPixelBuffersEnabled = false;
104
105 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700106 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700107 char property[PROPERTY_VALUE_MAX];
108 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
109 gpuPixelBuffersEnabled = !strcmp(property, "true");
110 }
111 }
112}
113
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800114void Caches::terminate() {
115 if (!mInitialized) return;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800116 mRegionMesh.release();
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800117
118 fboCache.clear();
119
120 programCache.clear();
Chris Craik34725682015-02-05 09:06:30 -0800121 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800122
Romain Guy3b748a42013-04-17 18:54:38 -0700123 patchCache.clear();
124
Romain Guy46bfc482013-08-16 18:38:29 -0700125 clearGarbage();
126
Chris Craik44eb2c02015-01-29 09:45:09 -0800127 delete mPixelBufferState;
128 mPixelBufferState = nullptr;
129 delete mTextureState;
130 mTextureState = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800131 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700132}
133
Chris Craik6c15ffa2015-02-02 13:50:55 -0800134void Caches::setProgram(const ProgramDescription& description) {
135 setProgram(programCache.get(description));
136}
137
138void Caches::setProgram(Program* program) {
139 if (!program || !program->isInUse()) {
140 if (mProgram) {
141 mProgram->remove();
142 }
143 if (program) {
144 program->use();
145 }
146 mProgram = program;
147 }
148}
149
Romain Guy5b3b3522010-10-27 18:57:51 -0700150///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800151// Debug
152///////////////////////////////////////////////////////////////////////////////
153
Romain Guy627c6fd2013-08-21 11:53:18 -0700154uint32_t Caches::getOverdrawColor(uint32_t amount) const {
155 static uint32_t sOverdrawColors[2][4] = {
156 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
157 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
158 };
159 if (amount < 1) amount = 1;
160 if (amount > 4) amount = 4;
Chris Craik2507c342015-05-04 14:36:49 -0700161
162 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
163 return sOverdrawColors[overdrawColorIndex][amount - 1];
Romain Guy627c6fd2013-08-21 11:53:18 -0700164}
165
Romain Guyc15008e2010-11-10 11:59:15 -0800166void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700167 String8 stringLog;
168 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000169 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700170}
171
172void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700173 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700174 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
175 log.appendFormat(" TextureCache %8d / %8d\n",
176 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700177 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
178 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700179 if (mRenderState) {
180 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800181 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck0e89e2b2014-10-31 14:49:06 -0700182 it != mRenderState->mActiveLayers.end(); it++) {
183 const Layer* layer = *it;
184 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
185 layer->getWidth(), layer->getHeight(),
Chris Craikf27133d2015-02-19 09:51:53 -0800186 layer->isTextureLayer(), layer->getTextureId(),
John Reck0e89e2b2014-10-31 14:49:06 -0700187 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800188 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700189 }
190 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
191 memused, mRenderState->mActiveLayers.size());
192 total += memused;
193 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800194 log.appendFormat(" RenderBufferCache %8d / %8d\n",
195 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700196 log.appendFormat(" GradientCache %8d / %8d\n",
197 gradientCache.getSize(), gradientCache.getMaxSize());
198 log.appendFormat(" PathCache %8d / %8d\n",
199 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700200 log.appendFormat(" TessellationCache %8d / %8d\n",
201 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700202 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800203 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700204 log.appendFormat(" PatchCache %8d / %8d\n",
205 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700206 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700207 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
208 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
209 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
210 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
211 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
212 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800213 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700214 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700215 log.appendFormat(" FboCache %8d / %8d\n",
216 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800217
Romain Guyc15008e2010-11-10 11:59:15 -0800218 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800219 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800220 total += gradientCache.getSize();
221 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700222 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800223 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700224 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700225 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700226 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
227 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800228 }
229
Chet Haase9c1e23b2011-03-24 10:51:31 -0700230 log.appendFormat("Total memory usage:\n");
231 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800232}
233
234///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800235// Memory management
236///////////////////////////////////////////////////////////////////////////////
237
238void Caches::clearGarbage() {
239 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800240 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700241 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800242}
243
Romain Guybdf76092011-07-18 15:00:43 -0700244void Caches::flush(FlushMode mode) {
245 FLUSH_LOGD("Flushing caches (mode %d)", mode);
246
Romain Guybdf76092011-07-18 15:00:43 -0700247 switch (mode) {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700248 case FlushMode::Full:
Romain Guybdf76092011-07-18 15:00:43 -0700249 textureCache.clear();
250 patchCache.clear();
251 dropShadowCache.clear();
252 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700253 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700254 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700255 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700256 // fall through
Chris Craikb9ce116d2015-08-20 15:14:06 -0700257 case FlushMode::Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700258 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700259 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700260 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700261 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700262 // fall through
Chris Craikb9ce116d2015-08-20 15:14:06 -0700263 case FlushMode::Layers:
Romain Guy6d7475d2011-07-27 16:28:21 -0700264 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800265 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700266 break;
267 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700268
269 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700270 glFinish();
John Reckd7db4d72015-05-20 07:18:55 -0700271 // Errors during cleanup should be considered non-fatal, dump them and
272 // and move on. TODO: All errors or just errors like bad surface?
273 GLUtils::dumpGLErrors();
Romain Guybdf76092011-07-18 15:00:43 -0700274}
275
Romain Guyfe48f652010-11-11 15:36:56 -0800276///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700277// Regions
278///////////////////////////////////////////////////////////////////////////////
279
Romain Guy5b3b3522010-10-27 18:57:51 -0700280TextureVertex* Caches::getRegionMesh() {
281 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
282 if (!mRegionMesh) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800283 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700284 }
285
Chris Craik51d6a3d2014-12-22 17:16:56 -0800286 return mRegionMesh.get();
Romain Guy5b3b3522010-10-27 18:57:51 -0700287}
288
Chris Craikba9b6132013-12-15 17:10:19 -0800289///////////////////////////////////////////////////////////////////////////////
290// Temporary Properties
291///////////////////////////////////////////////////////////////////////////////
292
Chet Haasedd78cca2010-10-22 18:59:26 -0700293}; // namespace uirenderer
294}; // namespace android