2010-07-19 18:43:02 -07:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-11-28 17:35:51 -08:00
|
|
|
#include <utils/JenkinsHash.h>
|
2010-09-08 18:04:33 -07:00
|
|
|
|
2012-08-08 16:05:42 -07:00
|
|
|
#include "Caches.h"
|
2011-01-21 21:14:15 -08:00
|
|
|
#include "Debug.h"
|
2010-07-19 18:43:02 -07:00
|
|
|
#include "GradientCache.h"
|
2010-08-23 21:05:08 -07:00
|
|
|
#include "Properties.h"
|
2017-07-17 09:55:02 -07:00
|
|
|
#include "DeviceInfo.h"
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2015-11-03 10:09:59 -08:00
|
|
|
#include <cutils/properties.h>
|
|
|
|
|
2010-07-19 18:43:02 -07:00
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
2012-07-30 14:47:51 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Functions
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static inline T min(T a, T b) {
|
|
|
|
return a < b ? a : b;
|
|
|
|
}
|
|
|
|
|
2012-11-28 17:35:51 -08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Cache entry
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
hash_t GradientCacheEntry::hash() const {
|
|
|
|
uint32_t hash = JenkinsHashMix(0, count);
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
hash = JenkinsHashMix(hash, android::hash_type(colors[i]));
|
|
|
|
hash = JenkinsHashMix(hash, android::hash_type(positions[i]));
|
|
|
|
}
|
|
|
|
return JenkinsHashWhiten(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GradientCacheEntry::compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
|
|
|
|
int deltaInt = int(lhs.count) - int(rhs.count);
|
|
|
|
if (deltaInt != 0) return deltaInt;
|
|
|
|
|
2014-12-22 17:16:56 -08:00
|
|
|
deltaInt = memcmp(lhs.colors.get(), rhs.colors.get(), lhs.count * sizeof(uint32_t));
|
2012-11-28 17:35:51 -08:00
|
|
|
if (deltaInt != 0) return deltaInt;
|
|
|
|
|
2014-12-22 17:16:56 -08:00
|
|
|
return memcmp(lhs.positions.get(), rhs.positions.get(), lhs.count * sizeof(float));
|
2012-11-28 17:35:51 -08:00
|
|
|
}
|
|
|
|
|
2010-07-19 18:43:02 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Constructors/destructor
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-07-17 09:55:02 -07:00
|
|
|
GradientCache::GradientCache(const Extensions& extensions)
|
2015-02-05 10:12:38 -08:00
|
|
|
: mCache(LruCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity)
|
|
|
|
, mSize(0)
|
2017-07-17 09:55:02 -07:00
|
|
|
, mMaxSize(MB(1))
|
2015-02-05 10:12:38 -08:00
|
|
|
, mUseFloatTexture(extensions.hasFloatTextures())
|
Linear blending, step 1
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
2016-09-28 17:34:42 -07:00
|
|
|
, mHasNpot(extensions.hasNPot())
|
2017-02-27 11:00:04 -08:00
|
|
|
, mHasLinearBlending(extensions.hasLinearBlending()) {
|
2017-07-17 09:55:02 -07:00
|
|
|
mMaxTextureSize = DeviceInfo::get()->maxTextureSize();
|
2012-07-20 11:36:03 -07:00
|
|
|
|
2010-08-23 21:05:08 -07:00
|
|
|
mCache.setOnEntryRemovedListener(this);
|
2010-07-19 18:43:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
GradientCache::~GradientCache() {
|
|
|
|
mCache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Size management
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
uint32_t GradientCache::getSize() {
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GradientCache::getMaxSize() {
|
|
|
|
return mMaxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Callbacks
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-10-17 10:30:55 -07:00
|
|
|
void GradientCache::operator()(GradientCacheEntry&, Texture*& texture) {
|
2011-08-01 18:56:21 -07:00
|
|
|
if (texture) {
|
2015-11-10 12:19:17 -08:00
|
|
|
mSize -= texture->objectSize();
|
2013-06-06 14:02:54 -07:00
|
|
|
texture->deleteTexture();
|
2010-07-19 18:43:02 -07:00
|
|
|
delete texture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Caching
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-07-30 14:47:51 -07:00
|
|
|
Texture* GradientCache::get(uint32_t* colors, float* positions, int count) {
|
|
|
|
GradientCacheEntry gradient(colors, positions, count);
|
2011-08-01 18:56:21 -07:00
|
|
|
Texture* texture = mCache.get(gradient);
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2011-08-01 18:56:21 -07:00
|
|
|
if (!texture) {
|
2012-07-30 14:47:51 -07:00
|
|
|
texture = addLinearGradient(gradient, colors, positions, count);
|
2010-11-11 15:36:56 -08:00
|
|
|
}
|
2011-08-01 18:56:21 -07:00
|
|
|
|
|
|
|
return texture;
|
2010-11-11 15:36:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GradientCache::clear() {
|
2010-07-19 18:43:02 -07:00
|
|
|
mCache.clear();
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:47:51 -07:00
|
|
|
void GradientCache::getGradientInfo(const uint32_t* colors, const int count,
|
|
|
|
GradientInfo& info) {
|
2012-08-08 16:05:42 -07:00
|
|
|
uint32_t width = 256 * (count - 1);
|
|
|
|
|
2013-04-12 16:32:05 -07:00
|
|
|
// If the npot extension is not supported we cannot use non-clamp
|
|
|
|
// wrap modes. We therefore find the nearest largest power of 2
|
|
|
|
// unless width is already a power of 2
|
|
|
|
if (!mHasNpot && (width & (width - 1)) != 0) {
|
|
|
|
width = 1 << (32 - __builtin_clz(width));
|
2012-08-08 16:05:42 -07:00
|
|
|
}
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2012-08-08 16:05:42 -07:00
|
|
|
bool hasAlpha = false;
|
2012-07-30 14:47:51 -07:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (((colors[i] >> 24) & 0xff) < 255) {
|
|
|
|
hasAlpha = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2012-07-30 14:47:51 -07:00
|
|
|
info.width = min(width, uint32_t(mMaxTextureSize));
|
|
|
|
info.hasAlpha = hasAlpha;
|
|
|
|
}
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2012-07-30 14:47:51 -07:00
|
|
|
Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient,
|
|
|
|
uint32_t* colors, float* positions, int count) {
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2012-07-30 14:47:51 -07:00
|
|
|
GradientInfo info;
|
|
|
|
getGradientInfo(colors, count, info);
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2015-02-23 13:07:57 -08:00
|
|
|
Texture* texture = new Texture(Caches::getInstance());
|
2012-07-30 14:47:51 -07:00
|
|
|
texture->blend = info.hasAlpha;
|
|
|
|
texture->generation = 1;
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2016-02-05 13:03:47 -08:00
|
|
|
// Assume the cache is always big enough
|
2015-11-10 12:19:17 -08:00
|
|
|
const uint32_t size = info.width * 2 * bytesPerPixel();
|
2013-01-03 14:22:40 -08:00
|
|
|
while (getSize() + size > mMaxSize) {
|
2016-02-05 13:03:47 -08:00
|
|
|
LOG_ALWAYS_FATAL_IF(!mCache.removeOldest(),
|
|
|
|
"Ran out of things to remove from the cache? getSize() = %" PRIu32
|
|
|
|
", size = %" PRIu32 ", mMaxSize = %" PRIu32 ", width = %" PRIu32,
|
|
|
|
getSize(), size, mMaxSize, info.width);
|
2010-07-19 18:43:02 -07:00
|
|
|
}
|
|
|
|
|
2015-11-10 12:19:17 -08:00
|
|
|
generateTexture(colors, positions, info.width, 2, texture);
|
2010-07-19 18:43:02 -07:00
|
|
|
|
|
|
|
mSize += size;
|
2016-02-11 13:22:25 -08:00
|
|
|
LOG_ALWAYS_FATAL_IF((int)size != texture->objectSize(),
|
2016-02-11 14:35:08 -08:00
|
|
|
"size != texture->objectSize(), size %" PRIu32 ", objectSize %d"
|
|
|
|
" width = %" PRIu32 " bytesPerPixel() = %zu",
|
2016-02-11 13:22:25 -08:00
|
|
|
size, texture->objectSize(), info.width, bytesPerPixel());
|
2011-08-01 18:56:21 -07:00
|
|
|
mCache.put(gradient, texture);
|
2010-07-19 18:43:02 -07:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2013-04-05 11:17:55 -07:00
|
|
|
size_t GradientCache::bytesPerPixel() const {
|
|
|
|
// We use 4 channels (RGBA)
|
Linear blending, step 1
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
2016-09-28 17:34:42 -07:00
|
|
|
return 4 * (mUseFloatTexture ? /* fp16 */ 2 : sizeof(uint8_t));
|
2013-04-05 11:17:55 -07:00
|
|
|
}
|
|
|
|
|
Linear blending, step 1
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
2016-09-28 17:34:42 -07:00
|
|
|
size_t GradientCache::sourceBytesPerPixel() const {
|
|
|
|
// We use 4 channels (RGBA) and upload from floats (not half floats)
|
|
|
|
return 4 * (mUseFloatTexture ? sizeof(float) : sizeof(uint8_t));
|
2013-04-05 11:17:55 -07:00
|
|
|
}
|
|
|
|
|
2016-10-12 12:14:07 -07:00
|
|
|
void GradientCache::mixBytes(const FloatColor& start, const FloatColor& end,
|
|
|
|
float amount, uint8_t*& dst) const {
|
2013-04-05 11:17:55 -07:00
|
|
|
float oppAmount = 1.0f - amount;
|
2016-12-12 18:21:32 -08:00
|
|
|
float a = start.a * oppAmount + end.a * amount;
|
2017-03-31 12:09:24 -04:00
|
|
|
*dst++ = uint8_t(OECF(start.r * oppAmount + end.r * amount) * 255.0f);
|
|
|
|
*dst++ = uint8_t(OECF(start.g * oppAmount + end.g * amount) * 255.0f);
|
|
|
|
*dst++ = uint8_t(OECF(start.b * oppAmount + end.b * amount) * 255.0f);
|
2016-12-12 18:21:32 -08:00
|
|
|
*dst++ = uint8_t(a * 255.0f);
|
2013-04-05 11:17:55 -07:00
|
|
|
}
|
|
|
|
|
2016-10-12 12:14:07 -07:00
|
|
|
void GradientCache::mixFloats(const FloatColor& start, const FloatColor& end,
|
|
|
|
float amount, uint8_t*& dst) const {
|
2013-04-05 11:17:55 -07:00
|
|
|
float oppAmount = 1.0f - amount;
|
2016-12-12 18:21:32 -08:00
|
|
|
float a = start.a * oppAmount + end.a * amount;
|
2013-04-05 11:17:55 -07:00
|
|
|
float* d = (float*) dst;
|
2016-10-12 18:29:06 -07:00
|
|
|
#ifdef ANDROID_ENABLE_LINEAR_BLENDING
|
2017-03-16 12:24:55 -07:00
|
|
|
// We want to stay linear
|
2017-03-31 12:09:24 -04:00
|
|
|
*d++ = (start.r * oppAmount + end.r * amount);
|
|
|
|
*d++ = (start.g * oppAmount + end.g * amount);
|
|
|
|
*d++ = (start.b * oppAmount + end.b * amount);
|
2016-10-12 12:14:07 -07:00
|
|
|
#else
|
2017-03-31 12:09:24 -04:00
|
|
|
*d++ = OECF(start.r * oppAmount + end.r * amount);
|
|
|
|
*d++ = OECF(start.g * oppAmount + end.g * amount);
|
|
|
|
*d++ = OECF(start.b * oppAmount + end.b * amount);
|
2016-10-12 12:14:07 -07:00
|
|
|
#endif
|
2016-12-12 18:21:32 -08:00
|
|
|
*d++ = a;
|
2013-04-05 11:17:55 -07:00
|
|
|
dst += 4 * sizeof(float);
|
|
|
|
}
|
|
|
|
|
2015-11-10 12:19:17 -08:00
|
|
|
void GradientCache::generateTexture(uint32_t* colors, float* positions,
|
|
|
|
const uint32_t width, const uint32_t height, Texture* texture) {
|
Linear blending, step 1
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
2016-09-28 17:34:42 -07:00
|
|
|
const GLsizei rowBytes = width * sourceBytesPerPixel();
|
2015-11-10 12:19:17 -08:00
|
|
|
uint8_t pixels[rowBytes * height];
|
2012-07-30 14:47:51 -07:00
|
|
|
|
2013-04-05 11:17:55 -07:00
|
|
|
static ChannelMixer gMixers[] = {
|
2016-12-12 18:21:32 -08:00
|
|
|
// colors are stored gamma-encoded
|
|
|
|
&android::uirenderer::GradientCache::mixBytes,
|
|
|
|
// colors are stored in linear (linear blending on)
|
|
|
|
// or gamma-encoded (linear blending off)
|
|
|
|
&android::uirenderer::GradientCache::mixFloats,
|
2013-04-05 11:17:55 -07:00
|
|
|
};
|
|
|
|
ChannelMixer mix = gMixers[mUseFloatTexture];
|
2012-07-30 14:47:51 -07:00
|
|
|
|
Linear blending, step 1
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
2016-09-28 17:34:42 -07:00
|
|
|
FloatColor start;
|
2017-03-31 12:09:24 -04:00
|
|
|
start.set(colors[0]);
|
2012-07-30 14:47:51 -07:00
|
|
|
|
Linear blending, step 1
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
2016-09-28 17:34:42 -07:00
|
|
|
FloatColor end;
|
2017-03-31 12:09:24 -04:00
|
|
|
end.set(colors[1]);
|
2012-07-30 14:47:51 -07:00
|
|
|
|
2013-04-05 11:17:55 -07:00
|
|
|
int currentPos = 1;
|
|
|
|
float startPos = positions[0];
|
|
|
|
float distance = positions[1] - startPos;
|
|
|
|
|
|
|
|
uint8_t* dst = pixels;
|
2012-07-30 14:47:51 -07:00
|
|
|
for (uint32_t x = 0; x < width; x++) {
|
|
|
|
float pos = x / float(width - 1);
|
|
|
|
if (pos > positions[currentPos]) {
|
2013-04-05 11:17:55 -07:00
|
|
|
start = end;
|
|
|
|
startPos = positions[currentPos];
|
2012-07-30 14:47:51 -07:00
|
|
|
|
|
|
|
currentPos++;
|
|
|
|
|
2017-03-31 12:09:24 -04:00
|
|
|
end.set(colors[currentPos]);
|
2013-04-05 11:17:55 -07:00
|
|
|
distance = positions[currentPos] - startPos;
|
2012-07-30 14:47:51 -07:00
|
|
|
}
|
|
|
|
|
2013-04-05 11:17:55 -07:00
|
|
|
float amount = (pos - startPos) / distance;
|
|
|
|
(this->*mix)(start, end, amount, dst);
|
2010-07-19 18:43:02 -07:00
|
|
|
}
|
|
|
|
|
2013-04-05 11:17:55 -07:00
|
|
|
memcpy(pixels + rowBytes, pixels, rowBytes);
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2013-04-05 11:17:55 -07:00
|
|
|
if (mUseFloatTexture) {
|
2016-01-19 11:46:52 -08:00
|
|
|
texture->upload(GL_RGBA16F, width, height, GL_RGBA, GL_FLOAT, pixels);
|
2013-04-05 11:17:55 -07:00
|
|
|
} else {
|
2017-02-27 11:00:04 -08:00
|
|
|
GLint internalFormat = mHasLinearBlending ? GL_SRGB8_ALPHA8 : GL_RGBA;
|
Linear blending, step 1
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
2016-09-28 17:34:42 -07:00
|
|
|
texture->upload(internalFormat, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
2013-04-05 11:17:55 -07:00
|
|
|
}
|
2010-07-19 18:43:02 -07:00
|
|
|
|
2011-12-12 18:14:06 -08:00
|
|
|
texture->setFilter(GL_LINEAR);
|
|
|
|
texture->setWrap(GL_CLAMP_TO_EDGE);
|
2010-07-19 18:43:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace uirenderer
|
|
|
|
}; // namespace android
|