NOTE: Linear blending is currently disabled in this CL as the feature is still a work in progress Android currently performs all blending (any kind of linear math on colors really) on gamma-encoded colors. Since Android assumes that the default color space is sRGB, all bitmaps and colors are encoded with the sRGB Opto-Electronic Conversion Function (OECF, which can be approximated with a power function). Since the power curve is not linear, our linear math is incorrect. The result is that we generate colors that tend to be too dark; this affects blending but also anti-aliasing, gradients, blurs, etc. The solution is to convert gamma-encoded colors back to linear space before doing any math on them, using the sRGB Electo-Optical Conversion Function (EOCF). This is achieved in different ways in different parts of the pipeline: - Using hardware conversions when sampling from OpenGL textures or writing into OpenGL frame buffers - Using software conversion functions, to translate app-supplied colors to and from sRGB - Using Skia's color spaces Any type of processing on colors must roughly ollow these steps: [sRGB input]->EOCF->[linear data]->[processing]->OECF->[sRGB output] For the sRGB color space, the conversion functions are defined as follows: OECF(linear) := linear <= 0.0031308 ? linear * 12.92 : (pow(linear, 1/2.4) * 1.055) - 0.055 EOCF(srgb) := srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4) The EOCF is simply the reciprocal of the OECF. While it is highly recommended to use the exact sRGB conversion functions everywhere possible, it is sometimes useful or beneficial to rely on approximations: - pow(x,2.2) and pow(x,1/2.2) - x^2 and sqrt(x) The latter is particularly useful in fragment shaders (for instance to apply dithering in sRGB space), especially if the sqrt() can be replaced with an inversesqrt(). Here is a fairly exhaustive list of modifications implemented in this CL: - Set TARGET_ENABLE_LINEAR_BLENDING := false in BoardConfig.mk to disable linear blending. This is only for GLES 2.0 GPUs with no hardware sRGB support. This flag is currently assumed to be false (see note above) - sRGB writes are disabled when entering a functor (WebView). This will need to be fixed at some point - Skia bitmaps are created with the sRGB color space - Bitmaps using a 565 config are expanded to 888 - Linear blending is disabled when entering a functor - External textures are not properly sampled (see below) - Gradients are interpolated in linear space - Texture-based dithering was replaced with analytical dithering - Dithering is done in the quantization color space, which is why we must do EOCF(OECF(color)+dither) - Text is now gamma corrected differently depending on the luminance of the source pixel. The asumption is that a bright pixel will be blended on a dark background and the other way around. The source alpha is gamma corrected to thicken dark on bright and thin bright on dark to match the intended design of fonts. This also matches the behavior of popular design/drawing applications - Removed the asset atlas. It did not contain anything useful and could not be sampled in sRGB without a yet-to-be-defined GL extension - The last column of color matrices is converted to linear space because its value are added to linear colors Missing features: - Resource qualifier? - Regeneration of goldeng images for automated tests - Handle alpha8/grey8 properly - Disable sRGB write for layers with external textures Test: Manual testing while work in progress Bug: 29940137 Change-Id: I6a07b15ab49b554377cd33a36b6d9971a15e9a0b
278 lines
8.4 KiB
C++
278 lines
8.4 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.
|
|
*/
|
|
|
|
#include "Caches.h"
|
|
|
|
#include "GammaFontRenderer.h"
|
|
#include "Layer.h"
|
|
#include "Properties.h"
|
|
#include "renderstate/RenderState.h"
|
|
#include "ShadowTessellator.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(mExtensions)
|
|
, patchCache(renderState)
|
|
, programCache(mExtensions)
|
|
, mRenderState(&renderState)
|
|
, mInitialized(false) {
|
|
INIT_LOGD("Creating OpenGL renderer caches");
|
|
init();
|
|
initConstraints();
|
|
initStaticProperties();
|
|
initExtensions();
|
|
}
|
|
|
|
bool Caches::init() {
|
|
if (mInitialized) return false;
|
|
|
|
ATRACE_NAME("Caches::init");
|
|
|
|
mRegionMesh = nullptr;
|
|
mProgram = nullptr;
|
|
|
|
mInitialized = true;
|
|
|
|
mPixelBufferState = new PixelBufferState();
|
|
mTextureState = new TextureState();
|
|
mTextureState->constructTexture(*this);
|
|
|
|
return true;
|
|
}
|
|
|
|
void Caches::initExtensions() {
|
|
if (mExtensions.hasDebugMarker()) {
|
|
eventMark = glInsertEventMarkerEXT;
|
|
|
|
startMark = glPushGroupMarkerEXT;
|
|
endMark = glPopGroupMarkerEXT;
|
|
} else {
|
|
eventMark = eventMarkNull;
|
|
startMark = startMarkNull;
|
|
endMark = endMarkNull;
|
|
}
|
|
}
|
|
|
|
void Caches::initConstraints() {
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
|
|
}
|
|
|
|
void Caches::initStaticProperties() {
|
|
// OpenGL ES 3.0+ specific features
|
|
gpuPixelBuffersEnabled = mExtensions.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;
|
|
|
|
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.appendFormat(" Layer size %dx%d; texid=%u refs=%d\n",
|
|
layer->getWidth(), layer->getHeight(),
|
|
layer->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();
|
|
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() {
|
|
textureCache.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
|