Romain Guy 3b748a44c6 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-05-02 13:32:09 -07:00

250 lines
7.5 KiB
C++

/*
* 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.
*/
#define LOG_TAG "OpenGLRenderer"
#include <cmath>
#include <utils/Log.h>
#include "Caches.h"
#include "Patch.h"
#include "Properties.h"
#include "UvMapper.h"
namespace android {
namespace uirenderer {
///////////////////////////////////////////////////////////////////////////////
// Constructors/destructor
///////////////////////////////////////////////////////////////////////////////
Patch::Patch(): verticesCount(0), indexCount(0), hasEmptyQuads(false) {
}
Patch::~Patch() {
}
///////////////////////////////////////////////////////////////////////////////
// Vertices management
///////////////////////////////////////////////////////////////////////////////
uint32_t Patch::getSize() const {
return verticesCount * sizeof(TextureVertex);
}
TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight,
float left, float top, float right, float bottom, const Res_png_9patch* patch) {
UvMapper mapper;
return createMesh(bitmapWidth, bitmapHeight, left, top, right, bottom, mapper, patch);
}
TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight,
float left, float top, float right, float bottom,
const UvMapper& mapper, const Res_png_9patch* patch) {
const uint32_t* colors = &patch->colors[0];
const int8_t numColors = patch->numColors;
mColorKey = 0;
int8_t emptyQuads = 0;
if (uint8_t(numColors) < sizeof(uint32_t) * 4) {
for (int8_t i = 0; i < numColors; i++) {
if (colors[i] == 0x0) {
emptyQuads++;
mColorKey |= 0x1 << i;
}
}
}
hasEmptyQuads = emptyQuads > 0;
uint32_t xCount = patch->numXDivs;
uint32_t yCount = patch->numYDivs;
uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 4;
if (maxVertices == 0) return NULL;
TextureVertex* vertices = new TextureVertex[maxVertices];
TextureVertex* vertex = vertices;
const int32_t* xDivs = patch->xDivs;
const int32_t* yDivs = patch->yDivs;
const uint32_t xStretchCount = (xCount + 1) >> 1;
const uint32_t yStretchCount = (yCount + 1) >> 1;
float stretchX = 0.0f;
float stretchY = 0.0f;
float rescaleX = 1.0f;
float rescaleY = 1.0f;
if (xStretchCount > 0) {
uint32_t stretchSize = 0;
for (uint32_t i = 1; i < xCount; i += 2) {
stretchSize += xDivs[i] - xDivs[i - 1];
}
const float xStretchTex = stretchSize;
const float fixed = bitmapWidth - stretchSize;
const float xStretch = fmaxf(right - left - fixed, 0.0f);
stretchX = xStretch / xStretchTex;
rescaleX = fixed == 0.0f ? 0.0f : fminf(fmaxf(right - left, 0.0f) / fixed, 1.0f);
}
if (yStretchCount > 0) {
uint32_t stretchSize = 0;
for (uint32_t i = 1; i < yCount; i += 2) {
stretchSize += yDivs[i] - yDivs[i - 1];
}
const float yStretchTex = stretchSize;
const float fixed = bitmapHeight - stretchSize;
const float yStretch = fmaxf(bottom - top - fixed, 0.0f);
stretchY = yStretch / yStretchTex;
rescaleY = fixed == 0.0f ? 0.0f : fminf(fmaxf(bottom - top, 0.0f) / fixed, 1.0f);
}
uint32_t quadCount = 0;
float previousStepY = 0.0f;
float y1 = 0.0f;
float y2 = 0.0f;
float v1 = 0.0f;
mUvMapper = mapper;
for (uint32_t i = 0; i < yCount; i++) {
float stepY = yDivs[i];
const float segment = stepY - previousStepY;
if (i & 1) {
y2 = y1 + floorf(segment * stretchY + 0.5f);
} else {
y2 = y1 + segment * rescaleY;
}
float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
float v2 = fmax(0.0f, stepY - vOffset) / bitmapHeight;
v1 += vOffset / bitmapHeight;
if (stepY > 0.0f) {
generateRow(xDivs, xCount, vertex, y1, y2, v1, v2, stretchX, rescaleX,
right - left, bitmapWidth, quadCount);
}
y1 = y2;
v1 = stepY / bitmapHeight;
previousStepY = stepY;
}
if (previousStepY != bitmapHeight) {
y2 = bottom - top;
generateRow(xDivs, xCount, vertex, y1, y2, v1, 1.0f, stretchX, rescaleX,
right - left, bitmapWidth, quadCount);
}
return vertices;
}
void Patch::generateRow(const int32_t* xDivs, uint32_t xCount, TextureVertex*& vertex,
float y1, float y2, float v1, float v2, float stretchX, float rescaleX,
float width, float bitmapWidth, uint32_t& quadCount) {
float previousStepX = 0.0f;
float x1 = 0.0f;
float x2 = 0.0f;
float u1 = 0.0f;
// Generate the row quad by quad
for (uint32_t i = 0; i < xCount; i++) {
float stepX = xDivs[i];
const float segment = stepX - previousStepX;
if (i & 1) {
x2 = x1 + floorf(segment * stretchX + 0.5f);
} else {
x2 = x1 + segment * rescaleX;
}
float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
float u2 = fmax(0.0f, stepX - uOffset) / bitmapWidth;
u1 += uOffset / bitmapWidth;
if (stepX > 0.0f) {
generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount);
}
x1 = x2;
u1 = stepX / bitmapWidth;
previousStepX = stepX;
}
if (previousStepX != bitmapWidth) {
x2 = width;
generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount);
}
}
void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
float u1, float v1, float u2, float v2, uint32_t& quadCount) {
const uint32_t oldQuadCount = quadCount;
quadCount++;
if (x1 < 0.0f) x1 = 0.0f;
if (x2 < 0.0f) x2 = 0.0f;
if (y1 < 0.0f) y1 = 0.0f;
if (y2 < 0.0f) y2 = 0.0f;
// Skip degenerate and transparent (empty) quads
if (((mColorKey >> oldQuadCount) & 0x1) || x1 >= x2 || y1 >= y2) {
#if DEBUG_PATCHES_EMPTY_VERTICES
PATCH_LOGD(" quad %d (empty)", oldQuadCount);
PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);
PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2);
#endif
return;
}
// Record all non empty quads
if (hasEmptyQuads) {
Rect bounds(x1, y1, x2, y2);
quads.add(bounds);
}
mUvMapper.map(u1, v1, u2, v2);
TextureVertex::set(vertex++, x1, y1, u1, v1);
TextureVertex::set(vertex++, x2, y1, u2, v1);
TextureVertex::set(vertex++, x1, y2, u1, v2);
TextureVertex::set(vertex++, x2, y2, u2, v2);
verticesCount += 4;
indexCount += 6;
#if DEBUG_PATCHES_VERTICES
PATCH_LOGD(" quad %d", oldQuadCount);
PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);
PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2);
#endif
}
}; // namespace uirenderer
}; // namespace android