2015-02-02 18:39:33 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 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 "GlopBuilder.h"
|
|
|
|
|
|
|
|
#include "Caches.h"
|
2017-01-09 14:15:41 -05:00
|
|
|
#include "GlLayer.h"
|
2015-02-02 18:39:33 -08:00
|
|
|
#include "Glop.h"
|
2016-07-06 16:10:09 -07:00
|
|
|
#include "Layer.h"
|
2015-02-02 18:39:33 -08:00
|
|
|
#include "Matrix.h"
|
2015-03-03 09:54:14 -08:00
|
|
|
#include "Patch.h"
|
2016-07-06 16:10:09 -07:00
|
|
|
#include "PathCache.h"
|
2015-02-05 10:12:38 -08:00
|
|
|
#include "SkiaShader.h"
|
|
|
|
#include "Texture.h"
|
|
|
|
#include "VertexBuffer.h"
|
2017-11-03 10:12:19 -07:00
|
|
|
#include "renderstate/MeshState.h"
|
|
|
|
#include "renderstate/RenderState.h"
|
|
|
|
#include "utils/PaintUtils.h"
|
2015-02-02 18:39:33 -08:00
|
|
|
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
#include <SkPaint.h>
|
|
|
|
|
2015-06-12 11:28:52 -07:00
|
|
|
#define DEBUG_GLOP_BUILDER 0
|
|
|
|
|
|
|
|
#if DEBUG_GLOP_BUILDER
|
2015-02-02 18:39:33 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
#define TRIGGER_STAGE(stageFlag) \
|
|
|
|
LOG_ALWAYS_FATAL_IF((stageFlag)&mStageFlags, "Stage %d cannot be run twice", (stageFlag)); \
|
2015-02-11 13:17:06 -08:00
|
|
|
mStageFlags = static_cast<StageFlags>(mStageFlags | (stageFlag))
|
2015-02-05 10:12:38 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
#define REQUIRE_STAGES(requiredFlags) \
|
2015-02-11 13:17:06 -08:00
|
|
|
LOG_ALWAYS_FATAL_IF((mStageFlags & (requiredFlags)) != (requiredFlags), \
|
2017-11-03 10:12:19 -07:00
|
|
|
"not prepared for current stage")
|
2015-02-09 18:58:32 -08:00
|
|
|
|
2015-06-12 11:28:52 -07:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define TRIGGER_STAGE(stageFlag) ((void)0)
|
|
|
|
#define REQUIRE_STAGES(requiredFlags) ((void)0)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace uirenderer {
|
|
|
|
|
2015-02-13 17:47:21 -08:00
|
|
|
static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) {
|
2015-02-24 13:46:29 -08:00
|
|
|
quadVertex[0] = {0, 0, uvs.left, uvs.top};
|
|
|
|
quadVertex[1] = {1, 0, uvs.right, uvs.top};
|
|
|
|
quadVertex[2] = {0, 1, uvs.left, uvs.bottom};
|
|
|
|
quadVertex[3] = {1, 1, uvs.right, uvs.bottom};
|
2015-02-11 13:17:06 -08:00
|
|
|
}
|
|
|
|
|
2015-02-02 18:39:33 -08:00
|
|
|
GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop)
|
2017-11-03 10:12:19 -07:00
|
|
|
: mRenderState(renderState), mCaches(caches), mShader(nullptr), mOutGlop(outGlop) {
|
2015-02-05 10:12:38 -08:00
|
|
|
mStageFlags = kInitialStage;
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Mesh
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2015-02-05 10:12:38 -08:00
|
|
|
|
2015-11-02 14:52:21 -08:00
|
|
|
GlopBuilder& GlopBuilder::setMeshTexturedIndexedVbo(GLuint vbo, GLsizei elementCount) {
|
|
|
|
TRIGGER_STAGE(kMeshStage);
|
|
|
|
|
|
|
|
mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->mesh.indices = {mRenderState.meshState().getQuadListIBO(), nullptr};
|
|
|
|
mOutGlop->mesh.vertices = {vbo, VertexAttribFlags::TextureCoord,
|
|
|
|
nullptr, (const void*)kMeshTextureOffset,
|
|
|
|
nullptr, kTextureVertexStride};
|
2015-11-02 14:52:21 -08:00
|
|
|
mOutGlop->mesh.elementCount = elementCount;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
GlopBuilder& GlopBuilder::setMeshUnitQuad() {
|
|
|
|
TRIGGER_STAGE(kMeshStage);
|
2015-02-05 10:12:38 -08:00
|
|
|
|
|
|
|
mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->mesh.indices = {0, nullptr};
|
|
|
|
mOutGlop->mesh.vertices = {mRenderState.meshState().getUnitQuadVBO(),
|
|
|
|
VertexAttribFlags::None,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
kTextureVertexStride};
|
2015-02-11 13:17:06 -08:00
|
|
|
mOutGlop->mesh.elementCount = 4;
|
2015-02-05 10:12:38 -08:00
|
|
|
return *this;
|
2015-02-02 18:39:33 -08:00
|
|
|
}
|
|
|
|
|
2015-02-19 09:51:53 -08:00
|
|
|
GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper) {
|
2015-02-24 13:46:29 -08:00
|
|
|
if (uvMapper) {
|
|
|
|
// can't use unit quad VBO, so build UV vertices manually
|
2015-12-10 16:28:16 -08:00
|
|
|
return setMeshTexturedUvQuad(uvMapper, Rect(1, 1));
|
2015-02-24 13:46:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TRIGGER_STAGE(kMeshStage);
|
|
|
|
|
|
|
|
mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->mesh.indices = {0, nullptr};
|
|
|
|
mOutGlop->mesh.vertices = {mRenderState.meshState().getUnitQuadVBO(),
|
|
|
|
VertexAttribFlags::TextureCoord,
|
|
|
|
nullptr,
|
|
|
|
(const void*)kMeshTextureOffset,
|
|
|
|
nullptr,
|
|
|
|
kTextureVertexStride};
|
2015-02-24 13:46:29 -08:00
|
|
|
mOutGlop->mesh.elementCount = 4;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
GlopBuilder& GlopBuilder::setMeshTexturedUvQuad(const UvMapper* uvMapper, Rect uvs) {
|
2015-02-05 10:12:38 -08:00
|
|
|
TRIGGER_STAGE(kMeshStage);
|
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
if (CC_UNLIKELY(uvMapper)) {
|
|
|
|
uvMapper->map(uvs);
|
|
|
|
}
|
2015-02-24 13:46:29 -08:00
|
|
|
setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]);
|
|
|
|
|
2015-02-25 17:16:16 -08:00
|
|
|
const TextureVertex* textureVertex = mOutGlop->mesh.mappedVertices;
|
|
|
|
mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->mesh.indices = {0, nullptr};
|
|
|
|
mOutGlop->mesh.vertices = {0,
|
|
|
|
VertexAttribFlags::TextureCoord,
|
|
|
|
&textureVertex[0].x,
|
|
|
|
&textureVertex[0].u,
|
|
|
|
nullptr,
|
|
|
|
kTextureVertexStride};
|
2015-02-11 13:17:06 -08:00
|
|
|
mOutGlop->mesh.elementCount = 4;
|
2015-02-02 18:39:33 -08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-25 17:16:16 -08:00
|
|
|
GlopBuilder& GlopBuilder::setMeshIndexedQuads(Vertex* vertexData, int quadCount) {
|
2015-02-06 15:25:51 -08:00
|
|
|
TRIGGER_STAGE(kMeshStage);
|
|
|
|
|
|
|
|
mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->mesh.indices = {mRenderState.meshState().getQuadListIBO(), nullptr};
|
2015-02-25 17:16:16 -08:00
|
|
|
mOutGlop->mesh.vertices = {
|
2017-11-03 10:12:19 -07:00
|
|
|
0, VertexAttribFlags::None, vertexData, nullptr, nullptr, kVertexStride};
|
2015-02-11 13:17:06 -08:00
|
|
|
mOutGlop->mesh.elementCount = 6 * quadCount;
|
2015-02-06 15:25:51 -08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-19 09:51:53 -08:00
|
|
|
GlopBuilder& GlopBuilder::setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount) {
|
|
|
|
TRIGGER_STAGE(kMeshStage);
|
|
|
|
|
|
|
|
mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->mesh.indices = {mRenderState.meshState().getQuadListIBO(), nullptr};
|
|
|
|
mOutGlop->mesh.vertices = {0,
|
|
|
|
VertexAttribFlags::TextureCoord,
|
|
|
|
&vertexData[0].x,
|
|
|
|
&vertexData[0].u,
|
|
|
|
nullptr,
|
|
|
|
kTextureVertexStride};
|
2015-02-25 17:16:16 -08:00
|
|
|
mOutGlop->mesh.elementCount = elementCount;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
GlopBuilder& GlopBuilder::setMeshColoredTexturedMesh(ColorTextureVertex* vertexData,
|
|
|
|
int elementCount) {
|
2015-02-25 17:16:16 -08:00
|
|
|
TRIGGER_STAGE(kMeshStage);
|
|
|
|
|
|
|
|
mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->mesh.indices = {0, nullptr};
|
|
|
|
mOutGlop->mesh.vertices = {0,
|
|
|
|
VertexAttribFlags::TextureCoord | VertexAttribFlags::Color,
|
|
|
|
&vertexData[0].x,
|
|
|
|
&vertexData[0].u,
|
|
|
|
&vertexData[0].r,
|
|
|
|
kColorTextureVertexStride};
|
2015-02-19 09:51:53 -08:00
|
|
|
mOutGlop->mesh.elementCount = elementCount;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-04-28 16:59:42 -07:00
|
|
|
GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer) {
|
2015-02-11 13:17:06 -08:00
|
|
|
TRIGGER_STAGE(kMeshStage);
|
2015-02-05 10:12:38 -08:00
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags();
|
2015-02-06 15:25:51 -08:00
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
bool alphaVertex = flags & VertexBuffer::kAlpha;
|
|
|
|
bool indices = flags & VertexBuffer::kIndices;
|
2015-02-25 17:16:16 -08:00
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->mesh.indices = {0, vertexBuffer.getIndices()};
|
|
|
|
mOutGlop->mesh.vertices = {0,
|
|
|
|
alphaVertex ? VertexAttribFlags::Alpha : VertexAttribFlags::None,
|
|
|
|
vertexBuffer.getBuffer(),
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
alphaVertex ? kAlphaVertexStride : kVertexStride};
|
|
|
|
mOutGlop->mesh.elementCount =
|
|
|
|
indices ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount();
|
|
|
|
mOutGlop->mesh.vertexCount = vertexBuffer.getVertexCount(); // used for glDrawRangeElements()
|
2015-02-06 15:25:51 -08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-03-03 09:54:14 -08:00
|
|
|
GlopBuilder& GlopBuilder::setMeshPatchQuads(const Patch& patch) {
|
|
|
|
TRIGGER_STAGE(kMeshStage);
|
|
|
|
|
|
|
|
mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->mesh.indices = {mRenderState.meshState().getQuadListIBO(), nullptr};
|
|
|
|
mOutGlop->mesh.vertices = {mCaches.patchCache.getMeshBuffer(),
|
|
|
|
VertexAttribFlags::TextureCoord,
|
|
|
|
(void*)patch.positionOffset,
|
|
|
|
(void*)patch.textureOffset,
|
|
|
|
nullptr,
|
|
|
|
kTextureVertexStride};
|
2015-03-03 09:54:14 -08:00
|
|
|
mOutGlop->mesh.elementCount = patch.indexCount;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Fill
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2015-02-06 15:25:51 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
void GlopBuilder::setFill(int color, float alphaScale, SkBlendMode mode,
|
|
|
|
Blend::ModeOrderSwap modeUsage, const SkShader* shader,
|
|
|
|
const SkColorFilter* colorFilter) {
|
2016-10-07 15:59:20 -04:00
|
|
|
if (mode != SkBlendMode::kClear) {
|
2015-02-05 10:12:38 -08:00
|
|
|
if (!shader) {
|
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 c;
|
|
|
|
c.set(color);
|
|
|
|
c.r *= alphaScale;
|
|
|
|
c.g *= alphaScale;
|
|
|
|
c.b *= alphaScale;
|
|
|
|
c.a *= alphaScale;
|
|
|
|
mOutGlop->fill.color = c;
|
2015-02-05 10:12:38 -08:00
|
|
|
} else {
|
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
|
|
|
float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.color = {1, 1, 1, alpha};
|
2015-02-02 18:39:33 -08:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.color = {0, 0, 0, 1};
|
2015-02-02 18:39:33 -08:00
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->blend = {GL_ZERO, GL_ZERO};
|
|
|
|
if (mOutGlop->fill.color.a < 1.0f ||
|
|
|
|
(mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha) ||
|
|
|
|
(mOutGlop->fill.texture.texture && mOutGlop->fill.texture.texture->blend) ||
|
|
|
|
mOutGlop->roundRectClipState || PaintUtils::isBlendedShader(shader) ||
|
|
|
|
PaintUtils::isBlendedColorFilter(colorFilter) || mode != SkBlendMode::kSrcOver) {
|
2016-10-07 15:59:20 -04:00
|
|
|
if (CC_LIKELY(mode <= SkBlendMode::kScreen)) {
|
2017-11-03 10:12:19 -07:00
|
|
|
Blend::getFactors(mode, modeUsage, &mOutGlop->blend.src, &mOutGlop->blend.dst);
|
2015-02-02 18:39:33 -08:00
|
|
|
} else {
|
|
|
|
// These blend modes are not supported by OpenGL directly and have
|
|
|
|
// to be implemented using shaders. Since the shader will perform
|
|
|
|
// the blending, don't enable GL blending off here
|
|
|
|
// If the blend mode cannot be implemented using shaders, fall
|
|
|
|
// back to the default SrcOver blend mode instead
|
2015-02-05 10:12:38 -08:00
|
|
|
if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
|
2016-10-28 17:21:45 -04:00
|
|
|
mDescription.framebufferMode = mode;
|
2015-03-09 14:17:29 -07:00
|
|
|
mDescription.swapSrcDst = (modeUsage == Blend::ModeOrderSwap::Swap);
|
2015-02-02 18:39:33 -08:00
|
|
|
// blending in shader, don't enable
|
|
|
|
} else {
|
|
|
|
// unsupported
|
2017-11-03 10:12:19 -07:00
|
|
|
Blend::getFactors(SkBlendMode::kSrcOver, modeUsage, &mOutGlop->blend.src,
|
|
|
|
&mOutGlop->blend.dst);
|
2015-02-02 18:39:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 10:12:19 -07:00
|
|
|
mShader = shader; // shader resolved in ::build()
|
2015-02-05 10:12:38 -08:00
|
|
|
|
|
|
|
if (colorFilter) {
|
|
|
|
SkColor color;
|
2016-10-28 17:21:45 -04:00
|
|
|
SkBlendMode bmode;
|
2015-02-05 10:12:38 -08:00
|
|
|
SkScalar srcColorMatrix[20];
|
2016-10-28 17:21:45 -04:00
|
|
|
if (colorFilter->asColorMode(&color, &bmode)) {
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.filterMode = mDescription.colorOp =
|
|
|
|
ProgramDescription::ColorFilterMode::Blend;
|
2016-10-28 17:21:45 -04:00
|
|
|
mDescription.colorMode = bmode;
|
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
|
|
|
mOutGlop->fill.filter.color.set(color);
|
2015-02-05 10:12:38 -08:00
|
|
|
} else if (colorFilter->asColorMatrix(srcColorMatrix)) {
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.filterMode = mDescription.colorOp =
|
|
|
|
ProgramDescription::ColorFilterMode::Matrix;
|
2015-02-05 10:12:38 -08:00
|
|
|
|
|
|
|
float* colorMatrix = mOutGlop->fill.filter.matrix.matrix;
|
|
|
|
memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
|
|
|
|
memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
|
|
|
|
memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
|
|
|
|
memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
|
|
|
|
|
|
|
|
// Skia uses the range [0..255] for the addition vector, but we need
|
|
|
|
// the [0..1] range to apply the vector in GLSL
|
|
|
|
float* colorVector = mOutGlop->fill.filter.matrix.vector;
|
2017-11-03 10:12:19 -07:00
|
|
|
colorVector[0] = EOCF(srcColorMatrix[4] / 255.0f);
|
|
|
|
colorVector[1] = EOCF(srcColorMatrix[9] / 255.0f);
|
2016-10-12 12:14:07 -07:00
|
|
|
colorVector[2] = EOCF(srcColorMatrix[14] / 255.0f);
|
2017-11-03 10:12:19 -07:00
|
|
|
colorVector[3] = srcColorMatrix[19] / 255.0f; // alpha is linear
|
2015-02-06 15:25:51 -08:00
|
|
|
} else {
|
2017-07-26 13:53:27 -04:00
|
|
|
ALOGE("unsupported ColorFilter type: %s", colorFilter->getTypeName());
|
2015-02-06 15:25:51 -08:00
|
|
|
LOG_ALWAYS_FATAL("unsupported ColorFilter");
|
2015-02-05 10:12:38 -08:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-20 15:14:06 -07:00
|
|
|
mOutGlop->fill.filterMode = ProgramDescription::ColorFilterMode::None;
|
2015-02-05 10:12:38 -08:00
|
|
|
}
|
2015-02-11 13:17:06 -08:00
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture, const int textureFillFlags,
|
|
|
|
const SkPaint* paint, float alphaScale) {
|
2015-02-11 13:17:06 -08:00
|
|
|
TRIGGER_STAGE(kFillStage);
|
2015-06-12 11:28:52 -07:00
|
|
|
REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
|
2015-02-11 13:17:06 -08:00
|
|
|
|
2015-06-01 10:35:35 -07:00
|
|
|
GLenum filter = (textureFillFlags & TextureFillFlags::ForceFilter)
|
2017-11-03 10:12:19 -07:00
|
|
|
? GL_LINEAR
|
|
|
|
: PaintUtils::getFilter(paint);
|
|
|
|
mOutGlop->fill.texture = {&texture, filter, GL_CLAMP_TO_EDGE, nullptr};
|
2015-02-11 13:17:06 -08:00
|
|
|
|
|
|
|
if (paint) {
|
|
|
|
int color = paint->getColor();
|
|
|
|
SkShader* shader = paint->getShader();
|
|
|
|
|
2015-06-01 10:35:35 -07:00
|
|
|
if (!(textureFillFlags & TextureFillFlags::IsAlphaMaskTexture)) {
|
2015-02-11 13:17:06 -08:00
|
|
|
// Texture defines color, so disable shaders, and reset all non-alpha color channels
|
|
|
|
color |= 0x00FFFFFF;
|
|
|
|
shader = nullptr;
|
|
|
|
}
|
2017-11-03 10:12:19 -07:00
|
|
|
setFill(color, alphaScale, paint->getBlendMode(), Blend::ModeOrderSwap::NoSwap, shader,
|
|
|
|
paint->getColorFilter());
|
2015-02-11 13:17:06 -08:00
|
|
|
} else {
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.color = {alphaScale, alphaScale, alphaScale, alphaScale};
|
2015-02-11 13:17:06 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
if (alphaScale < 1.0f || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha) ||
|
|
|
|
texture.blend || mOutGlop->roundRectClipState) {
|
2016-10-07 15:59:20 -04:00
|
|
|
Blend::getFactors(SkBlendMode::kSrcOver, Blend::ModeOrderSwap::NoSwap,
|
2017-11-03 10:12:19 -07:00
|
|
|
&mOutGlop->blend.src, &mOutGlop->blend.dst);
|
2015-02-11 13:17:06 -08:00
|
|
|
} else {
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->blend = {GL_ZERO, GL_ZERO};
|
2015-02-11 13:17:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 10:35:35 -07:00
|
|
|
if (textureFillFlags & TextureFillFlags::IsAlphaMaskTexture) {
|
2015-02-17 16:42:02 -08:00
|
|
|
mDescription.modulate = mOutGlop->fill.color.isNotBlack();
|
2015-02-27 17:43:02 -08:00
|
|
|
mDescription.hasAlpha8Texture = true;
|
2015-02-11 13:17:06 -08:00
|
|
|
} else {
|
|
|
|
mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-04-28 16:59:42 -07:00
|
|
|
GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale, bool shadowInterp) {
|
2015-02-11 13:17:06 -08:00
|
|
|
TRIGGER_STAGE(kFillStage);
|
2015-06-12 11:28:52 -07:00
|
|
|
REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
|
2015-02-11 13:17:06 -08:00
|
|
|
|
2016-04-28 16:59:42 -07:00
|
|
|
if (CC_LIKELY(!shadowInterp)) {
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.texture = {nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr};
|
2016-04-28 16:59:42 -07:00
|
|
|
} else {
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.texture = {mCaches.textureState().getShadowLutTexture(), GL_INVALID_ENUM,
|
|
|
|
GL_INVALID_ENUM, nullptr};
|
2016-04-28 16:59:42 -07:00
|
|
|
}
|
2015-02-11 13:17:06 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
setFill(paint.getColor(), alphaScale, paint.getBlendMode(), Blend::ModeOrderSwap::NoSwap,
|
2015-02-11 13:17:06 -08:00
|
|
|
paint.getShader(), paint.getColorFilter());
|
2016-04-28 16:59:42 -07:00
|
|
|
mDescription.useShadowAlphaInterp = shadowInterp;
|
2015-02-11 13:17:06 -08:00
|
|
|
mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture, const SkPaint& paint,
|
|
|
|
float alphaScale) {
|
2015-02-12 10:41:39 -08:00
|
|
|
TRIGGER_STAGE(kFillStage);
|
2015-06-12 11:28:52 -07:00
|
|
|
REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
|
2015-02-12 10:41:39 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
// specify invalid filter/clamp, since these are always static for PathTextures
|
|
|
|
mOutGlop->fill.texture = {&texture, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr};
|
2015-02-12 10:41:39 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
setFill(paint.getColor(), alphaScale, paint.getBlendMode(), Blend::ModeOrderSwap::NoSwap,
|
2015-02-12 10:41:39 -08:00
|
|
|
paint.getShader(), paint.getColorFilter());
|
|
|
|
|
2015-02-19 09:51:53 -08:00
|
|
|
mDescription.hasAlpha8Texture = true;
|
2015-02-17 16:42:02 -08:00
|
|
|
mDescription.modulate = mOutGlop->fill.color.isNotBlack();
|
2015-02-12 10:41:39 -08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:42:02 -08:00
|
|
|
GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
|
2017-11-03 10:12:19 -07:00
|
|
|
const SkPaint& paint, float alphaScale) {
|
2015-02-17 16:42:02 -08:00
|
|
|
TRIGGER_STAGE(kFillStage);
|
2015-06-12 11:28:52 -07:00
|
|
|
REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
|
2015-02-17 16:42:02 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
// specify invalid filter/clamp, since these are always static for ShadowTextures
|
|
|
|
mOutGlop->fill.texture = {&texture, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr};
|
2015-02-17 16:42:02 -08:00
|
|
|
|
|
|
|
const int ALPHA_BITMASK = SK_ColorBLACK;
|
|
|
|
const int COLOR_BITMASK = ~ALPHA_BITMASK;
|
|
|
|
if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) {
|
|
|
|
// shadow color is fully opaque: override its alpha with that of paint
|
|
|
|
shadowColor &= paint.getColor() | COLOR_BITMASK;
|
|
|
|
}
|
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
setFill(shadowColor, alphaScale, paint.getBlendMode(), Blend::ModeOrderSwap::NoSwap,
|
2015-02-17 16:42:02 -08:00
|
|
|
paint.getShader(), paint.getColorFilter());
|
|
|
|
|
2015-02-19 09:51:53 -08:00
|
|
|
mDescription.hasAlpha8Texture = true;
|
2015-02-17 16:42:02 -08:00
|
|
|
mDescription.modulate = mOutGlop->fill.color.isNotBlack();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
GlopBuilder& GlopBuilder::setFillBlack() {
|
|
|
|
TRIGGER_STAGE(kFillStage);
|
2015-06-12 11:28:52 -07:00
|
|
|
REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
|
2015-02-17 16:42:02 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.texture = {nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr};
|
|
|
|
setFill(SK_ColorBLACK, 1.0f, SkBlendMode::kSrcOver, Blend::ModeOrderSwap::NoSwap, nullptr,
|
|
|
|
nullptr);
|
2015-02-17 16:42:02 -08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
GlopBuilder& GlopBuilder::setFillClear() {
|
|
|
|
TRIGGER_STAGE(kFillStage);
|
2015-06-12 11:28:52 -07:00
|
|
|
REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
|
2015-02-17 16:42:02 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.texture = {nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr};
|
|
|
|
setFill(SK_ColorBLACK, 1.0f, SkBlendMode::kClear, Blend::ModeOrderSwap::NoSwap, nullptr,
|
|
|
|
nullptr);
|
2015-02-19 09:51:53 -08:00
|
|
|
return *this;
|
|
|
|
}
|
2015-02-17 16:42:02 -08:00
|
|
|
|
2015-02-19 09:51:53 -08:00
|
|
|
GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
|
2017-11-03 10:12:19 -07:00
|
|
|
float alpha, SkBlendMode mode,
|
|
|
|
Blend::ModeOrderSwap modeUsage) {
|
2015-02-19 09:51:53 -08:00
|
|
|
TRIGGER_STAGE(kFillStage);
|
2015-06-12 11:28:52 -07:00
|
|
|
REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
|
2015-02-19 09:51:53 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.texture = {&texture, GL_LINEAR, GL_CLAMP_TO_EDGE, nullptr};
|
2015-02-19 09:51:53 -08:00
|
|
|
|
2015-03-09 14:17:29 -07:00
|
|
|
setFill(SK_ColorWHITE, alpha, mode, modeUsage, nullptr, colorFilter);
|
2015-02-19 09:51:53 -08:00
|
|
|
|
|
|
|
mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
|
2015-02-17 16:42:02 -08:00
|
|
|
return *this;
|
|
|
|
}
|
2015-02-19 09:51:53 -08:00
|
|
|
|
2017-01-09 14:15:41 -05:00
|
|
|
GlopBuilder& GlopBuilder::setFillTextureLayer(GlLayer& layer, float alpha) {
|
2015-02-26 16:28:17 -08:00
|
|
|
TRIGGER_STAGE(kFillStage);
|
2015-06-12 11:28:52 -07:00
|
|
|
REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
|
2015-02-26 16:28:17 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.texture = {&(layer.getTexture()), GL_LINEAR, GL_CLAMP_TO_EDGE,
|
|
|
|
&layer.getTexTransform()};
|
2015-02-26 16:28:17 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
setFill(SK_ColorWHITE, alpha, layer.getMode(), Blend::ModeOrderSwap::NoSwap, nullptr,
|
|
|
|
layer.getColorFilter());
|
2015-02-26 16:28:17 -08:00
|
|
|
|
|
|
|
mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
|
|
|
|
mDescription.hasTextureTransform = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-06-28 11:08:07 -07:00
|
|
|
GlopBuilder& GlopBuilder::setFillExternalTexture(Texture& texture, Matrix4& textureTransform,
|
2017-11-03 10:12:19 -07:00
|
|
|
bool requiresFilter) {
|
2016-04-19 07:51:13 -07:00
|
|
|
TRIGGER_STAGE(kFillStage);
|
|
|
|
REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
|
|
|
|
|
2017-06-28 11:08:07 -07:00
|
|
|
GLenum filter = requiresFilter ? GL_LINEAR : GL_NEAREST;
|
2017-11-03 10:12:19 -07:00
|
|
|
mOutGlop->fill.texture = {&texture, filter, GL_CLAMP_TO_EDGE, &textureTransform};
|
2016-04-19 07:51:13 -07:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
setFill(SK_ColorWHITE, 1.0f, SkBlendMode::kSrc, Blend::ModeOrderSwap::NoSwap, nullptr, nullptr);
|
2016-04-19 07:51:13 -07:00
|
|
|
|
|
|
|
mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
|
2016-04-28 13:18:51 -07:00
|
|
|
mDescription.hasTextureTransform = true;
|
2016-04-19 07:51:13 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
GlopBuilder& GlopBuilder::setGammaCorrection(bool enabled) {
|
|
|
|
REQUIRE_STAGES(kFillStage);
|
|
|
|
|
|
|
|
mDescription.hasGammaCorrection = enabled;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Transform
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-10-05 13:00:52 -07:00
|
|
|
GlopBuilder& GlopBuilder::setTransform(const Matrix4& canvas, const int transformFlags) {
|
2015-02-11 13:17:06 -08:00
|
|
|
TRIGGER_STAGE(kTransformStage);
|
|
|
|
|
2015-08-19 15:10:24 -07:00
|
|
|
mOutGlop->transform.canvas = canvas;
|
2015-06-01 10:35:35 -07:00
|
|
|
mOutGlop->transform.transformFlags = transformFlags;
|
2015-10-05 13:00:52 -07:00
|
|
|
return *this;
|
2015-02-11 13:17:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// ModelView
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
|
|
|
|
TRIGGER_STAGE(kModelViewStage);
|
|
|
|
|
|
|
|
mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
|
|
|
|
mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) {
|
|
|
|
TRIGGER_STAGE(kModelViewStage);
|
|
|
|
REQUIRE_STAGES(kTransformStage | kFillStage);
|
|
|
|
|
|
|
|
float left = destination.left;
|
|
|
|
float top = destination.top;
|
|
|
|
|
2015-06-01 10:35:35 -07:00
|
|
|
const Matrix4& meshTransform = mOutGlop->transform.meshTransform();
|
|
|
|
if (CC_LIKELY(meshTransform.isPureTranslate())) {
|
2015-02-19 09:51:53 -08:00
|
|
|
// snap by adjusting the model view matrix
|
2015-06-01 10:35:35 -07:00
|
|
|
const float translateX = meshTransform.getTranslateX();
|
|
|
|
const float translateY = meshTransform.getTranslateY();
|
2015-02-11 13:17:06 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
left = (int)floorf(left + translateX + 0.5f) - translateX;
|
|
|
|
top = (int)floorf(top + translateY + 0.5f) - translateY;
|
2015-02-19 09:51:53 -08:00
|
|
|
mOutGlop->fill.texture.filter = GL_NEAREST;
|
2015-02-11 13:17:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f);
|
|
|
|
mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
|
|
|
|
TRIGGER_STAGE(kModelViewStage);
|
|
|
|
|
|
|
|
mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-02-02 18:39:33 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY,
|
|
|
|
const Rect source) {
|
2015-02-19 09:51:53 -08:00
|
|
|
TRIGGER_STAGE(kModelViewStage);
|
|
|
|
REQUIRE_STAGES(kTransformStage | kFillStage);
|
|
|
|
|
2015-06-01 10:35:35 -07:00
|
|
|
const Matrix4& meshTransform = mOutGlop->transform.meshTransform();
|
|
|
|
if (CC_LIKELY(meshTransform.isPureTranslate())) {
|
2015-02-19 09:51:53 -08:00
|
|
|
// snap by adjusting the model view matrix
|
2015-06-01 10:35:35 -07:00
|
|
|
const float translateX = meshTransform.getTranslateX();
|
|
|
|
const float translateY = meshTransform.getTranslateY();
|
2015-02-19 09:51:53 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
offsetX = (int)floorf(offsetX + translateX + source.left + 0.5f) - translateX - source.left;
|
|
|
|
offsetY = (int)floorf(offsetY + translateY + source.top + 0.5f) - translateY - source.top;
|
2015-02-19 09:51:53 -08:00
|
|
|
mOutGlop->fill.texture.filter = GL_NEAREST;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RoundRectClip
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) {
|
|
|
|
TRIGGER_STAGE(kRoundRectClipStage);
|
|
|
|
|
|
|
|
mOutGlop->roundRectClipState = roundRectClipState;
|
|
|
|
mDescription.hasRoundRectClip = roundRectClipState != nullptr;
|
2015-02-02 18:39:33 -08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Build
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-02-19 09:51:53 -08:00
|
|
|
void verify(const ProgramDescription& description, const Glop& glop) {
|
2015-03-06 17:30:11 -08:00
|
|
|
if (glop.fill.texture.texture != nullptr) {
|
2017-11-03 10:12:19 -07:00
|
|
|
LOG_ALWAYS_FATAL_IF(
|
|
|
|
((description.hasTexture && description.hasExternalTexture) ||
|
|
|
|
(!description.hasTexture && !description.hasExternalTexture &&
|
|
|
|
!description.useShadowAlphaInterp) ||
|
|
|
|
((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) == 0 &&
|
|
|
|
!description.useShadowAlphaInterp)),
|
|
|
|
"Texture %p, hT%d, hET %d, attribFlags %x", glop.fill.texture.texture,
|
2015-03-06 17:30:11 -08:00
|
|
|
description.hasTexture, description.hasExternalTexture,
|
|
|
|
glop.mesh.vertices.attribFlags);
|
|
|
|
} else {
|
2017-11-03 10:12:19 -07:00
|
|
|
LOG_ALWAYS_FATAL_IF(
|
|
|
|
(description.hasTexture || description.hasExternalTexture ||
|
|
|
|
((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) != 0)),
|
|
|
|
"No texture, hT%d, hET %d, attribFlags %x", description.hasTexture,
|
|
|
|
description.hasExternalTexture, glop.mesh.vertices.attribFlags);
|
2015-03-06 17:30:11 -08:00
|
|
|
}
|
2015-02-25 17:16:16 -08:00
|
|
|
|
2017-11-03 10:12:19 -07:00
|
|
|
if ((glop.mesh.vertices.attribFlags & VertexAttribFlags::Alpha) &&
|
|
|
|
glop.mesh.vertices.bufferObject) {
|
2015-02-25 17:16:16 -08:00
|
|
|
LOG_ALWAYS_FATAL("VBO and alpha attributes are not currently compatible");
|
|
|
|
}
|
2015-02-26 16:28:17 -08:00
|
|
|
|
|
|
|
if (description.hasTextureTransform != (glop.fill.texture.textureTransform != nullptr)) {
|
|
|
|
LOG_ALWAYS_FATAL("Texture transform incorrectly specified");
|
|
|
|
}
|
2015-02-19 09:51:53 -08:00
|
|
|
}
|
|
|
|
|
2015-02-02 18:39:33 -08:00
|
|
|
void GlopBuilder::build() {
|
2015-02-09 18:58:32 -08:00
|
|
|
REQUIRE_STAGES(kAllStages);
|
2015-06-01 10:35:35 -07:00
|
|
|
if (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) {
|
2017-04-15 21:41:22 -07:00
|
|
|
Texture* texture = mOutGlop->fill.texture.texture;
|
|
|
|
if (texture->target() == GL_TEXTURE_2D) {
|
2015-03-10 11:03:39 -07:00
|
|
|
mDescription.hasTexture = true;
|
|
|
|
} else {
|
|
|
|
mDescription.hasExternalTexture = true;
|
|
|
|
}
|
2017-03-27 00:40:21 -07:00
|
|
|
mDescription.hasLinearTexture = texture->isLinear();
|
|
|
|
mDescription.hasColorSpaceConversion = texture->hasColorSpaceConversion();
|
|
|
|
mDescription.transferFunction = texture->getTransferFunctionType();
|
|
|
|
mDescription.hasTranslucentConversion = texture->blend;
|
2015-02-26 16:28:17 -08:00
|
|
|
}
|
2015-06-01 10:35:35 -07:00
|
|
|
|
|
|
|
mDescription.hasColors = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Color;
|
|
|
|
mDescription.hasVertexAlpha = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha;
|
2015-02-25 17:16:16 -08:00
|
|
|
|
2015-04-03 09:37:49 -07:00
|
|
|
// Enable debug highlight when what we're about to draw is tested against
|
|
|
|
// the stencil buffer and if stencil highlight debugging is on
|
2017-11-03 10:12:19 -07:00
|
|
|
mDescription.hasDebugHighlight =
|
|
|
|
!Properties::debugOverdraw &&
|
|
|
|
Properties::debugStencilClip == StencilClipDebug::ShowHighlight &&
|
|
|
|
mRenderState.stencil().isTestEnabled();
|
2015-04-03 09:37:49 -07:00
|
|
|
|
2015-02-13 17:47:21 -08:00
|
|
|
// serialize shader info into ShaderData
|
2015-02-19 09:51:53 -08:00
|
|
|
GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0;
|
2015-06-01 10:35:35 -07:00
|
|
|
|
|
|
|
if (CC_LIKELY(!mShader)) {
|
|
|
|
mOutGlop->fill.skiaShaderData.skiaShaderType = kNone_SkiaShaderType;
|
|
|
|
} else {
|
|
|
|
Matrix4 shaderMatrix;
|
|
|
|
if (mOutGlop->transform.transformFlags & TransformFlags::MeshIgnoresCanvasTransform) {
|
|
|
|
// canvas level transform was built into the modelView and geometry,
|
|
|
|
// so the shader matrix must reverse this
|
|
|
|
shaderMatrix.loadInverse(mOutGlop->transform.canvas);
|
|
|
|
shaderMatrix.multiply(mOutGlop->transform.modelView);
|
|
|
|
} else {
|
2015-08-19 15:10:24 -07:00
|
|
|
shaderMatrix = mOutGlop->transform.modelView;
|
2015-06-01 10:35:35 -07:00
|
|
|
}
|
2017-11-03 10:12:19 -07:00
|
|
|
SkiaShader::store(mCaches, *mShader, shaderMatrix, &textureUnit, &mDescription,
|
|
|
|
&(mOutGlop->fill.skiaShaderData));
|
2015-06-01 10:35:35 -07:00
|
|
|
}
|
2015-02-13 17:47:21 -08:00
|
|
|
|
2015-02-11 13:17:06 -08:00
|
|
|
// duplicates ProgramCache's definition of color uniform presence
|
2017-11-03 10:12:19 -07:00
|
|
|
const bool singleColor = !mDescription.hasTexture && !mDescription.hasExternalTexture &&
|
|
|
|
!mDescription.hasGradient && !mDescription.hasBitmap;
|
2015-02-11 13:17:06 -08:00
|
|
|
mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor;
|
2015-02-19 09:51:53 -08:00
|
|
|
|
|
|
|
verify(mDescription, *mOutGlop);
|
2015-02-25 17:16:16 -08:00
|
|
|
|
|
|
|
// Final step: populate program and map bounds into render target space
|
|
|
|
mOutGlop->fill.program = mCaches.programCache.get(mDescription);
|
2015-02-02 18:39:33 -08:00
|
|
|
}
|
|
|
|
|
2015-10-05 13:00:52 -07:00
|
|
|
void GlopBuilder::dump(const Glop& glop) {
|
|
|
|
ALOGD("Glop Mesh");
|
|
|
|
const Glop::Mesh& mesh = glop.mesh;
|
|
|
|
ALOGD(" primitive mode: %d", mesh.primitiveMode);
|
2017-11-03 10:12:19 -07:00
|
|
|
ALOGD(" indices: buffer obj %x, indices %p", mesh.indices.bufferObject,
|
|
|
|
mesh.indices.indices);
|
2015-10-05 13:00:52 -07:00
|
|
|
|
|
|
|
const Glop::Mesh::Vertices& vertices = glop.mesh.vertices;
|
|
|
|
ALOGD(" vertices: buffer obj %x, flags %x, pos %p, tex %p, clr %p, stride %d",
|
2017-11-03 10:12:19 -07:00
|
|
|
vertices.bufferObject, vertices.attribFlags, vertices.position, vertices.texCoord,
|
|
|
|
vertices.color, vertices.stride);
|
2015-10-05 13:00:52 -07:00
|
|
|
ALOGD(" element count: %d", mesh.elementCount);
|
|
|
|
|
|
|
|
ALOGD("Glop Fill");
|
|
|
|
const Glop::Fill& fill = glop.fill;
|
|
|
|
ALOGD(" program %p", fill.program);
|
|
|
|
if (fill.texture.texture) {
|
2017-11-03 10:12:19 -07:00
|
|
|
ALOGD(" texture %p, target %d, filter %d, clamp %d", fill.texture.texture,
|
|
|
|
fill.texture.texture->target(), fill.texture.filter, fill.texture.clamp);
|
2015-10-05 13:00:52 -07:00
|
|
|
if (fill.texture.textureTransform) {
|
|
|
|
fill.texture.textureTransform->dump("texture transform");
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 10:12:19 -07:00
|
|
|
ALOGD_IF(fill.colorEnabled, " color (argb) %.2f %.2f %.2f %.2f", fill.color.a, fill.color.r,
|
|
|
|
fill.color.g, fill.color.b);
|
|
|
|
ALOGD_IF(fill.filterMode != ProgramDescription::ColorFilterMode::None, " filterMode %d",
|
|
|
|
(int)fill.filterMode);
|
2015-10-05 13:00:52 -07:00
|
|
|
ALOGD_IF(fill.skiaShaderData.skiaShaderType, " shader type %d",
|
2017-11-03 10:12:19 -07:00
|
|
|
fill.skiaShaderData.skiaShaderType);
|
2015-10-05 13:00:52 -07:00
|
|
|
|
|
|
|
ALOGD("Glop transform");
|
2016-03-11 18:39:01 -08:00
|
|
|
glop.transform.modelView.dump(" model view");
|
|
|
|
glop.transform.canvas.dump(" canvas");
|
|
|
|
ALOGD_IF(glop.transform.transformFlags, " transformFlags 0x%x", glop.transform.transformFlags);
|
|
|
|
|
|
|
|
ALOGD_IF(glop.roundRectClipState, "Glop RRCS %p", glop.roundRectClipState);
|
2015-10-05 13:00:52 -07:00
|
|
|
|
|
|
|
ALOGD("Glop blend %d %d", glop.blend.src, glop.blend.dst);
|
|
|
|
}
|
|
|
|
|
2015-02-02 18:39:33 -08:00
|
|
|
} /* namespace uirenderer */
|
|
|
|
} /* namespace android */
|