2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "GammaFontRenderer.h"
20 #include "LayerRenderer.h"
21 #include "Properties.h"
22 #include "renderstate/RenderState.h"
23 #include "ShadowTessellator.h"
24 #include "utils/GLUtils.h"
26 #include <cutils/properties.h>
27 #include <utils/Log.h>
28 #include <utils/String8.h>
31 namespace uirenderer {
33 Caches* Caches::sInstance = nullptr;
35 ///////////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////////
40 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
42 #define FLUSH_LOGD(...)
45 ///////////////////////////////////////////////////////////////////////////////
46 // Constructors/destructor
47 ///////////////////////////////////////////////////////////////////////////////
49 Caches::Caches(RenderState& renderState)
50 : gradientCache(mExtensions)
51 , patchCache(renderState)
52 , programCache(mExtensions)
54 , mRenderState(&renderState)
55 , mInitialized(false) {
56 INIT_LOGD("Creating OpenGL renderer caches");
59 initStaticProperties();
64 if (mInitialized) return false;
66 ATRACE_NAME("Caches::init");
68 mRegionMesh = nullptr;
75 mPixelBufferState = new PixelBufferState();
76 mTextureState = new TextureState();
77 mTextureState->constructTexture(*this);
82 void Caches::initExtensions() {
83 if (mExtensions.hasDebugMarker()) {
84 eventMark = glInsertEventMarkerEXT;
86 startMark = glPushGroupMarkerEXT;
87 endMark = glPopGroupMarkerEXT;
89 eventMark = eventMarkNull;
90 startMark = startMarkNull;
91 endMark = endMarkNull;
95 void Caches::initConstraints() {
96 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
99 void Caches::initStaticProperties() {
100 // OpenGL ES 3.0+ specific features
101 gpuPixelBuffersEnabled = mExtensions.hasPixelBufferObjects()
102 && property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
105 void Caches::terminate() {
106 if (!mInitialized) return;
107 mRegionMesh.reset(nullptr);
111 programCache.clear();
118 delete mPixelBufferState;
119 mPixelBufferState = nullptr;
120 delete mTextureState;
121 mTextureState = nullptr;
122 mInitialized = false;
125 void Caches::setProgram(const ProgramDescription& description) {
126 setProgram(programCache.get(description));
129 void Caches::setProgram(Program* program) {
130 if (!program || !program->isInUse()) {
141 ///////////////////////////////////////////////////////////////////////////////
143 ///////////////////////////////////////////////////////////////////////////////
145 uint32_t Caches::getOverdrawColor(uint32_t amount) const {
146 static uint32_t sOverdrawColors[2][4] = {
147 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
148 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
150 if (amount < 1) amount = 1;
151 if (amount > 4) amount = 4;
153 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
154 return sOverdrawColors[overdrawColorIndex][amount - 1];
157 void Caches::dumpMemoryUsage() {
159 dumpMemoryUsage(stringLog);
160 ALOGD("%s", stringLog.string());
163 void Caches::dumpMemoryUsage(String8 &log) {
165 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
166 log.appendFormat(" TextureCache %8d / %8d\n",
167 textureCache.getSize(), textureCache.getMaxSize());
168 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
169 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
172 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
173 it != mRenderState->mActiveLayers.end(); it++) {
174 const Layer* layer = *it;
175 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
176 layer->getWidth(), layer->getHeight(),
177 layer->isTextureLayer(), layer->getTextureId(),
178 layer->getFbo(), layer->getStrongCount());
179 memused += layer->getWidth() * layer->getHeight() * 4;
181 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
182 memused, mRenderState->mActiveLayers.size());
185 log.appendFormat(" RenderBufferCache %8d / %8d\n",
186 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
187 log.appendFormat(" GradientCache %8d / %8d\n",
188 gradientCache.getSize(), gradientCache.getMaxSize());
189 log.appendFormat(" PathCache %8d / %8d\n",
190 pathCache.getSize(), pathCache.getMaxSize());
191 log.appendFormat(" TessellationCache %8d / %8d\n",
192 tessellationCache.getSize(), tessellationCache.getMaxSize());
193 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
194 dropShadowCache.getMaxSize());
195 log.appendFormat(" PatchCache %8d / %8d\n",
196 patchCache.getSize(), patchCache.getMaxSize());
198 const uint32_t sizeA8 = fontRenderer.getFontRendererSize(GL_ALPHA);
199 const uint32_t sizeRGBA = fontRenderer.getFontRendererSize(GL_RGBA);
200 log.appendFormat(" FontRenderer A8 %8d / %8d\n", sizeA8, sizeA8);
201 log.appendFormat(" FontRenderer RGBA %8d / %8d\n", sizeRGBA, sizeRGBA);
202 log.appendFormat(" FontRenderer total %8d / %8d\n", sizeA8 + sizeRGBA,
205 log.appendFormat("Other:\n");
206 log.appendFormat(" FboCache %8d / %8d\n",
207 fboCache.getSize(), fboCache.getMaxSize());
209 total += textureCache.getSize();
210 total += renderBufferCache.getSize();
211 total += gradientCache.getSize();
212 total += pathCache.getSize();
213 total += tessellationCache.getSize();
214 total += dropShadowCache.getSize();
215 total += patchCache.getSize();
216 total += fontRenderer.getFontRendererSize(GL_ALPHA);
217 total += fontRenderer.getFontRendererSize(GL_RGBA);
219 log.appendFormat("Total memory usage:\n");
220 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
223 ///////////////////////////////////////////////////////////////////////////////
225 ///////////////////////////////////////////////////////////////////////////////
227 void Caches::clearGarbage() {
228 textureCache.clearGarbage();
229 pathCache.clearGarbage();
230 patchCache.clearGarbage();
233 void Caches::flush(FlushMode mode) {
234 FLUSH_LOGD("Flushing caches (mode %d)", mode);
237 case FlushMode::Full:
238 textureCache.clear();
240 dropShadowCache.clear();
241 gradientCache.clear();
242 fontRenderer.clear();
246 case FlushMode::Moderate:
247 fontRenderer.flush();
248 textureCache.flush();
250 tessellationCache.clear();
252 case FlushMode::Layers:
254 renderBufferCache.clear();
260 // Errors during cleanup should be considered non-fatal, dump them and
261 // and move on. TODO: All errors or just errors like bad surface?
262 GLUtils::dumpGLErrors();
265 ///////////////////////////////////////////////////////////////////////////////
267 ///////////////////////////////////////////////////////////////////////////////
269 TextureVertex* Caches::getRegionMesh() {
270 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
272 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
275 return mRegionMesh.get();
278 ///////////////////////////////////////////////////////////////////////////////
279 // Temporary Properties
280 ///////////////////////////////////////////////////////////////////////////////
282 }; // namespace uirenderer
283 }; // namespace android