275 lines
8.6 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Caches.h"
#include "GammaFontRenderer.h"
#include "GlLayer.h"
#include "Properties.h"
#include "ShadowTessellator.h"
#include "renderstate/RenderState.h"
#ifdef BUGREPORT_FONT_CACHE_USAGE
#include "font/FontCacheHistoryTracker.h"
#endif
#include "utils/GLUtils.h"
#include <cutils/properties.h>
#include <utils/Log.h>
#include <utils/String8.h>
namespace android {
namespace uirenderer {
Caches* Caches::sInstance = nullptr;
///////////////////////////////////////////////////////////////////////////////
// Macros
///////////////////////////////////////////////////////////////////////////////
#if DEBUG_CACHE_FLUSH
#define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
#else
#define FLUSH_LOGD(...)
#endif
///////////////////////////////////////////////////////////////////////////////
// Constructors/destructor
///////////////////////////////////////////////////////////////////////////////
Caches::Caches(RenderState& renderState)
: gradientCache(extensions())
, patchCache(renderState)
, programCache(extensions())
, mRenderState(&renderState)
, mInitialized(false) {
INIT_LOGD("Creating OpenGL renderer caches");
init();
initConstraints();
initStaticProperties();
initExtensions();
}
Pack preloaded framework assets in a texture atlas When the Android runtime starts, the system preloads a series of assets in the Zygote process. These assets are shared across all processes. Unfortunately, each one of these assets is later uploaded in its own OpenGL texture, once per process. This wastes memory and generates unnecessary OpenGL state changes. This CL introduces an asset server that provides an atlas to all processes. Note: bitmaps used by skia shaders are *not* sampled from the atlas. It's an uncommon use case and would require extra texture transforms in the GL shaders. WHAT IS THE ASSETS ATLAS The "assets atlas" is a single, shareable graphic buffer that contains all the system's preloaded bitmap drawables (this includes 9-patches.) The atlas is made of two distinct objects: the graphic buffer that contains the actual pixels and the map which indicates where each preloaded bitmap can be found in the atlas (essentially a pair of x and y coordinates.) HOW IS THE ASSETS ATLAS GENERATED Because we need to support a wide variety of devices and because it is easy to change the list of preloaded drawables, the atlas is generated at runtime, during the startup phase of the system process. There are several steps that lead to the atlas generation: 1. If the device is booting for the first time, or if the device was updated, we need to find the best atlas configuration. To do so, the atlas service tries a number of width, height and algorithm variations that allows us to pack as many assets as possible while using as little memory as possible. Once a best configuration is found, it gets written to disk in /data/system/framework_atlas 2. Given a best configuration (algorithm variant, dimensions and number of bitmaps that can be packed in the atlas), the atlas service packs all the preloaded bitmaps into a single graphic buffer object. 3. The packing is done using Skia in a temporary native bitmap. The Skia bitmap is then copied into the graphic buffer using OpenGL ES to benefit from texture swizzling. HOW PROCESSES USE THE ATLAS Whenever a process' hardware renderer initializes its EGL context, it queries the atlas service for the graphic buffer and the map. It is important to remember that both the context and the map will be valid for the lifetime of the hardware renderer (if the system process goes down, all apps get killed as well.) Every time the hardware renderer needs to render a bitmap, it first checks whether the bitmap can be found in the assets atlas. When the bitmap is part of the atlas, texture coordinates are remapped appropriately before rendering. Change-Id: I8eaecf53e7f6a33d90da3d0047c5ceec89ea3af0
2013-04-17 18:54:38 -07:00
bool Caches::init() {
if (mInitialized) return false;
ATRACE_NAME("Caches::init");
mRegionMesh = nullptr;
mProgram = nullptr;
mInitialized = true;
Pack preloaded framework assets in a texture atlas When the Android runtime starts, the system preloads a series of assets in the Zygote process. These assets are shared across all processes. Unfortunately, each one of these assets is later uploaded in its own OpenGL texture, once per process. This wastes memory and generates unnecessary OpenGL state changes. This CL introduces an asset server that provides an atlas to all processes. Note: bitmaps used by skia shaders are *not* sampled from the atlas. It's an uncommon use case and would require extra texture transforms in the GL shaders. WHAT IS THE ASSETS ATLAS The "assets atlas" is a single, shareable graphic buffer that contains all the system's preloaded bitmap drawables (this includes 9-patches.) The atlas is made of two distinct objects: the graphic buffer that contains the actual pixels and the map which indicates where each preloaded bitmap can be found in the atlas (essentially a pair of x and y coordinates.) HOW IS THE ASSETS ATLAS GENERATED Because we need to support a wide variety of devices and because it is easy to change the list of preloaded drawables, the atlas is generated at runtime, during the startup phase of the system process. There are several steps that lead to the atlas generation: 1. If the device is booting for the first time, or if the device was updated, we need to find the best atlas configuration. To do so, the atlas service tries a number of width, height and algorithm variations that allows us to pack as many assets as possible while using as little memory as possible. Once a best configuration is found, it gets written to disk in /data/system/framework_atlas 2. Given a best configuration (algorithm variant, dimensions and number of bitmaps that can be packed in the atlas), the atlas service packs all the preloaded bitmaps into a single graphic buffer object. 3. The packing is done using Skia in a temporary native bitmap. The Skia bitmap is then copied into the graphic buffer using OpenGL ES to benefit from texture swizzling. HOW PROCESSES USE THE ATLAS Whenever a process' hardware renderer initializes its EGL context, it queries the atlas service for the graphic buffer and the map. It is important to remember that both the context and the map will be valid for the lifetime of the hardware renderer (if the system process goes down, all apps get killed as well.) Every time the hardware renderer needs to render a bitmap, it first checks whether the bitmap can be found in the assets atlas. When the bitmap is part of the atlas, texture coordinates are remapped appropriately before rendering. Change-Id: I8eaecf53e7f6a33d90da3d0047c5ceec89ea3af0
2013-04-17 18:54:38 -07:00
mPixelBufferState = new PixelBufferState();
mTextureState = new TextureState();
mTextureState->constructTexture(*this);
Pack preloaded framework assets in a texture atlas When the Android runtime starts, the system preloads a series of assets in the Zygote process. These assets are shared across all processes. Unfortunately, each one of these assets is later uploaded in its own OpenGL texture, once per process. This wastes memory and generates unnecessary OpenGL state changes. This CL introduces an asset server that provides an atlas to all processes. Note: bitmaps used by skia shaders are *not* sampled from the atlas. It's an uncommon use case and would require extra texture transforms in the GL shaders. WHAT IS THE ASSETS ATLAS The "assets atlas" is a single, shareable graphic buffer that contains all the system's preloaded bitmap drawables (this includes 9-patches.) The atlas is made of two distinct objects: the graphic buffer that contains the actual pixels and the map which indicates where each preloaded bitmap can be found in the atlas (essentially a pair of x and y coordinates.) HOW IS THE ASSETS ATLAS GENERATED Because we need to support a wide variety of devices and because it is easy to change the list of preloaded drawables, the atlas is generated at runtime, during the startup phase of the system process. There are several steps that lead to the atlas generation: 1. If the device is booting for the first time, or if the device was updated, we need to find the best atlas configuration. To do so, the atlas service tries a number of width, height and algorithm variations that allows us to pack as many assets as possible while using as little memory as possible. Once a best configuration is found, it gets written to disk in /data/system/framework_atlas 2. Given a best configuration (algorithm variant, dimensions and number of bitmaps that can be packed in the atlas), the atlas service packs all the preloaded bitmaps into a single graphic buffer object. 3. The packing is done using Skia in a temporary native bitmap. The Skia bitmap is then copied into the graphic buffer using OpenGL ES to benefit from texture swizzling. HOW PROCESSES USE THE ATLAS Whenever a process' hardware renderer initializes its EGL context, it queries the atlas service for the graphic buffer and the map. It is important to remember that both the context and the map will be valid for the lifetime of the hardware renderer (if the system process goes down, all apps get killed as well.) Every time the hardware renderer needs to render a bitmap, it first checks whether the bitmap can be found in the assets atlas. When the bitmap is part of the atlas, texture coordinates are remapped appropriately before rendering. Change-Id: I8eaecf53e7f6a33d90da3d0047c5ceec89ea3af0
2013-04-17 18:54:38 -07:00
return true;
}
void Caches::initExtensions() {
if (extensions().hasDebugMarker()) {
eventMark = glInsertEventMarkerEXT;
startMark = glPushGroupMarkerEXT;
endMark = glPopGroupMarkerEXT;
} else {
eventMark = eventMarkNull;
startMark = startMarkNull;
endMark = endMarkNull;
}
}
void Caches::initConstraints() {
maxTextureSize = DeviceInfo::get()->maxTextureSize();
}
void Caches::initStaticProperties() {
// OpenGL ES 3.0+ specific features
gpuPixelBuffersEnabled = extensions().hasPixelBufferObjects() &&
property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
}
void Caches::terminate() {
if (!mInitialized) return;
mRegionMesh.reset(nullptr);
fboCache.clear();
programCache.clear();
mProgram = nullptr;
Pack preloaded framework assets in a texture atlas When the Android runtime starts, the system preloads a series of assets in the Zygote process. These assets are shared across all processes. Unfortunately, each one of these assets is later uploaded in its own OpenGL texture, once per process. This wastes memory and generates unnecessary OpenGL state changes. This CL introduces an asset server that provides an atlas to all processes. Note: bitmaps used by skia shaders are *not* sampled from the atlas. It's an uncommon use case and would require extra texture transforms in the GL shaders. WHAT IS THE ASSETS ATLAS The "assets atlas" is a single, shareable graphic buffer that contains all the system's preloaded bitmap drawables (this includes 9-patches.) The atlas is made of two distinct objects: the graphic buffer that contains the actual pixels and the map which indicates where each preloaded bitmap can be found in the atlas (essentially a pair of x and y coordinates.) HOW IS THE ASSETS ATLAS GENERATED Because we need to support a wide variety of devices and because it is easy to change the list of preloaded drawables, the atlas is generated at runtime, during the startup phase of the system process. There are several steps that lead to the atlas generation: 1. If the device is booting for the first time, or if the device was updated, we need to find the best atlas configuration. To do so, the atlas service tries a number of width, height and algorithm variations that allows us to pack as many assets as possible while using as little memory as possible. Once a best configuration is found, it gets written to disk in /data/system/framework_atlas 2. Given a best configuration (algorithm variant, dimensions and number of bitmaps that can be packed in the atlas), the atlas service packs all the preloaded bitmaps into a single graphic buffer object. 3. The packing is done using Skia in a temporary native bitmap. The Skia bitmap is then copied into the graphic buffer using OpenGL ES to benefit from texture swizzling. HOW PROCESSES USE THE ATLAS Whenever a process' hardware renderer initializes its EGL context, it queries the atlas service for the graphic buffer and the map. It is important to remember that both the context and the map will be valid for the lifetime of the hardware renderer (if the system process goes down, all apps get killed as well.) Every time the hardware renderer needs to render a bitmap, it first checks whether the bitmap can be found in the assets atlas. When the bitmap is part of the atlas, texture coordinates are remapped appropriately before rendering. Change-Id: I8eaecf53e7f6a33d90da3d0047c5ceec89ea3af0
2013-04-17 18:54:38 -07:00
patchCache.clear();
clearGarbage();
delete mPixelBufferState;
mPixelBufferState = nullptr;
delete mTextureState;
mTextureState = nullptr;
mInitialized = false;
}
void Caches::setProgram(const ProgramDescription& description) {
setProgram(programCache.get(description));
}
void Caches::setProgram(Program* program) {
if (!program || !program->isInUse()) {
if (mProgram) {
mProgram->remove();
}
if (program) {
program->use();
}
mProgram = program;
}
}
///////////////////////////////////////////////////////////////////////////////
// Debug
///////////////////////////////////////////////////////////////////////////////
uint32_t Caches::getOverdrawColor(uint32_t amount) const {
static uint32_t sOverdrawColors[2][4] = {{0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000},
{0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000}};
if (amount < 1) amount = 1;
if (amount > 4) amount = 4;
int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
return sOverdrawColors[overdrawColorIndex][amount - 1];
}
void Caches::dumpMemoryUsage() {
String8 stringLog;
dumpMemoryUsage(stringLog);
ALOGD("%s", stringLog.string());
}
void Caches::dumpMemoryUsage(String8& log) {
uint32_t total = 0;
log.appendFormat("Current memory usage / total memory usage (bytes):\n");
log.appendFormat(" TextureCache %8d / %8d\n", textureCache.getSize(),
textureCache.getMaxSize());
if (mRenderState) {
int memused = 0;
for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
it != mRenderState->mActiveLayers.end(); it++) {
const Layer* layer = *it;
LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL);
const GlLayer* glLayer = static_cast<const GlLayer*>(layer);
log.appendFormat(" GlLayer size %dx%d; texid=%u refs=%d\n", layer->getWidth(),
layer->getHeight(), glLayer->getTextureId(), layer->getStrongCount());
memused += layer->getWidth() * layer->getHeight() * 4;
}
log.appendFormat(" Layers total %8d (numLayers = %zu)\n", memused,
mRenderState->mActiveLayers.size());
total += memused;
}
log.appendFormat(" RenderBufferCache %8d / %8d\n", renderBufferCache.getSize(),
renderBufferCache.getMaxSize());
log.appendFormat(" GradientCache %8d / %8d\n", gradientCache.getSize(),
gradientCache.getMaxSize());
log.appendFormat(" PathCache %8d / %8d\n", pathCache.getSize(),
pathCache.getMaxSize());
log.appendFormat(" TessellationCache %8d / %8d\n", tessellationCache.getSize(),
tessellationCache.getMaxSize());
log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
dropShadowCache.getMaxSize());
log.appendFormat(" PatchCache %8d / %8d\n", patchCache.getSize(),
patchCache.getMaxSize());
fontRenderer.dumpMemoryUsage(log);
log.appendFormat("Other:\n");
log.appendFormat(" FboCache %8d / %8d\n", fboCache.getSize(),
fboCache.getMaxSize());
total += textureCache.getSize();
total += renderBufferCache.getSize();
total += gradientCache.getSize();
total += pathCache.getSize();
total += tessellationCache.getSize();
total += dropShadowCache.getSize();
Pack preloaded framework assets in a texture atlas When the Android runtime starts, the system preloads a series of assets in the Zygote process. These assets are shared across all processes. Unfortunately, each one of these assets is later uploaded in its own OpenGL texture, once per process. This wastes memory and generates unnecessary OpenGL state changes. This CL introduces an asset server that provides an atlas to all processes. Note: bitmaps used by skia shaders are *not* sampled from the atlas. It's an uncommon use case and would require extra texture transforms in the GL shaders. WHAT IS THE ASSETS ATLAS The "assets atlas" is a single, shareable graphic buffer that contains all the system's preloaded bitmap drawables (this includes 9-patches.) The atlas is made of two distinct objects: the graphic buffer that contains the actual pixels and the map which indicates where each preloaded bitmap can be found in the atlas (essentially a pair of x and y coordinates.) HOW IS THE ASSETS ATLAS GENERATED Because we need to support a wide variety of devices and because it is easy to change the list of preloaded drawables, the atlas is generated at runtime, during the startup phase of the system process. There are several steps that lead to the atlas generation: 1. If the device is booting for the first time, or if the device was updated, we need to find the best atlas configuration. To do so, the atlas service tries a number of width, height and algorithm variations that allows us to pack as many assets as possible while using as little memory as possible. Once a best configuration is found, it gets written to disk in /data/system/framework_atlas 2. Given a best configuration (algorithm variant, dimensions and number of bitmaps that can be packed in the atlas), the atlas service packs all the preloaded bitmaps into a single graphic buffer object. 3. The packing is done using Skia in a temporary native bitmap. The Skia bitmap is then copied into the graphic buffer using OpenGL ES to benefit from texture swizzling. HOW PROCESSES USE THE ATLAS Whenever a process' hardware renderer initializes its EGL context, it queries the atlas service for the graphic buffer and the map. It is important to remember that both the context and the map will be valid for the lifetime of the hardware renderer (if the system process goes down, all apps get killed as well.) Every time the hardware renderer needs to render a bitmap, it first checks whether the bitmap can be found in the assets atlas. When the bitmap is part of the atlas, texture coordinates are remapped appropriately before rendering. Change-Id: I8eaecf53e7f6a33d90da3d0047c5ceec89ea3af0
2013-04-17 18:54:38 -07:00
total += patchCache.getSize();
total += fontRenderer.getSize();
log.appendFormat("Total memory usage:\n");
log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
#ifdef BUGREPORT_FONT_CACHE_USAGE
fontRenderer.getFontRenderer().historyTracker().dump(log);
#endif
}
///////////////////////////////////////////////////////////////////////////////
// Memory management
///////////////////////////////////////////////////////////////////////////////
void Caches::clearGarbage() {
pathCache.clearGarbage();
patchCache.clearGarbage();
}
void Caches::flush(FlushMode mode) {
FLUSH_LOGD("Flushing caches (mode %d)", mode);
switch (mode) {
case FlushMode::Full:
textureCache.clear();
patchCache.clear();
dropShadowCache.clear();
gradientCache.clear();
fontRenderer.clear();
fboCache.clear();
// fall through
case FlushMode::Moderate:
fontRenderer.flush();
textureCache.flush();
pathCache.clear();
tessellationCache.clear();
// fall through
case FlushMode::Layers:
renderBufferCache.clear();
break;
}
clearGarbage();
glFinish();
// Errors during cleanup should be considered non-fatal, dump them and
// and move on. TODO: All errors or just errors like bad surface?
GLUtils::dumpGLErrors();
}
///////////////////////////////////////////////////////////////////////////////
// Regions
///////////////////////////////////////////////////////////////////////////////
TextureVertex* Caches::getRegionMesh() {
// Create the mesh, 2 triangles and 4 vertices per rectangle in the region
if (!mRegionMesh) {
mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
}
return mRegionMesh.get();
}
///////////////////////////////////////////////////////////////////////////////
// Temporary Properties
///////////////////////////////////////////////////////////////////////////////
}; // namespace uirenderer
}; // namespace android