Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Color with Color4f in tgfx. #106

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions include/pag/pag.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,15 @@ class PAG_API PAGLayer : public Content {
Matrix getTotalMatrix();

/**
* Set the opacity of the layer. it will be concatenated to current animation opacity for
* displaying.
* Returns the current alpha of the layer if previously set.
*/
Opacity opacity() const;
float alpha() const;

void setOpacity(const Opacity& value);
/**
* Set the alpha of the layer, which will be concatenated to the current animation opacity for
* displaying.
*/
void setAlpha(float value);

/**
* Whether or not the layer is visible.
Expand Down Expand Up @@ -414,7 +417,7 @@ class PAG_API PAGLayer : public Content {
PAGFile* rootFile = nullptr;
std::weak_ptr<PAGLayer> weakThis;
Matrix layerMatrix = {};
Opacity layerOpacity = Opaque;
float layerAlpha = 1.0f;
PAGLayer* trackMatteOwner = nullptr;

const Layer* getLayer() const;
Expand All @@ -426,7 +429,6 @@ class PAG_API PAGLayer : public Content {
void measureBounds(Rect* bounds) override;
Matrix getTotalMatrixInternal();
virtual void setMatrixInternal(const Matrix& matrix);
void setOpacityInternal(const Opacity& opacity);
virtual float frameRateInternal() const;
double getProgressInternal();
void setProgressInternal(double percent);
Expand Down
3 changes: 2 additions & 1 deletion src/rendering/caches/SolidContentCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "SolidContentCache.h"
#include "rendering/graphics/Shape.h"
#include "rendering/utils/TGFXTypes.h"

namespace pag {
SolidContentCache::SolidContentCache(SolidLayer* layer) : ContentCache(layer) {
Expand All @@ -27,7 +28,7 @@ GraphicContent* SolidContentCache::createContent(Frame) const {
auto solidLayer = static_cast<SolidLayer*>(layer);
Path path = {};
path.addRect(0, 0, solidLayer->width, solidLayer->height);
auto graphic = Shape::MakeFrom(path, solidLayer->solidColor);
auto graphic = Shape::MakeFrom(path, ToTGFXColor(solidLayer->solidColor));
return new GraphicContent(graphic);
}
} // namespace pag
9 changes: 5 additions & 4 deletions src/rendering/filters/dropshadow/DropShadowFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "base/utils/MathExtra.h"
#include "gpu/opengl/GLUtil.h"
#include "rendering/filters/utils/FilterHelper.h"
#include "rendering/utils/TGFXTypes.h"

namespace pag {
DropShadowFilter::DropShadowFilter(DropShadowStyle* layerStyle) : layerStyle(layerStyle) {
Expand Down Expand Up @@ -56,8 +57,8 @@ void DropShadowFilter::update(Frame frame, const Rect& contentBounds, const Rect
const Point& filterScale) {
LayerFilter::update(frame, contentBounds, transformedBounds, filterScale);

color = layerStyle->color->getValueAt(layerFrame);
opacity = layerStyle->opacity->getValueAt(layerFrame);
color = ToTGFXColor(layerStyle->color->getValueAt(layerFrame));
alpha = ToAlpha(layerStyle->opacity->getValueAt(layerFrame));
spread = layerStyle->spread->getValueAt(layerFrame);
auto size = layerStyle->size->getValueAt(layerFrame);
spread *= (spread == 1.0) ? 1.0 : 0.8;
Expand Down Expand Up @@ -188,7 +189,7 @@ void DropShadowFilter::onDrawModeNotSpread(Context* context, const FilterSource*

auto sourceH = blurFilterBuffer->toFilterSource(source->scale);

blurFilterH->updateParams(blurSize, opacity / 255.f, false, BlurMode::Shadow);
blurFilterH->updateParams(blurSize, alpha, false, BlurMode::Shadow);
Matrix revertMatrix =
Matrix::MakeTrans((filterBounds.left - contentBounds.left) * source->scale.x,
(filterBounds.top - contentBounds.top) * source->scale.y);
Expand Down Expand Up @@ -248,7 +249,7 @@ void DropShadowFilter::onDrawModeNotFullSpread(Context* context, const FilterSou
(filterBounds.top - contentBounds.top) * source->scale.y);
auto targetH = *target;
PreConcatMatrix(&targetH, revertMatrix);
blurFilterH->updateParams(blurSize, opacity / 255.f, false, BlurMode::Shadow);
blurFilterH->updateParams(blurSize, alpha, false, BlurMode::Shadow);
blurFilterH->draw(context, sourceH.get(), &targetH);
}

Expand Down
4 changes: 2 additions & 2 deletions src/rendering/filters/dropshadow/DropShadowFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class DropShadowFilter : public LayerFilter {
DropShadowSpreadFilter* spreadFilter = nullptr;
DropShadowSpreadFilter* spreadThickFilter = nullptr;

Color color = Black;
float opacity = 0.0f;
Color4f color = Color4f::Black();
float alpha = 0.0f;
float spread = 0.0f;
float spreadSize = 0.0f;
float blurSize = 0.0f;
Expand Down
19 changes: 10 additions & 9 deletions src/rendering/filters/dropshadow/DropShadowSpreadFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
#include "DropShadowSpreadFilter.h"
#include "gpu/opengl/GLUtil.h"
#include "rendering/filters/utils/BlurTypes.h"
#include "rendering/utils/TGFXTypes.h"

namespace pag {
static const char DROPSHADOW_SPREAD_FRAGMENT_SHADER[] = R"(
#version 100
precision highp float;
uniform sampler2D uTextureInput;
uniform vec3 uColor;
uniform float uOpacity;
uniform float uAlpha;
uniform vec2 uSize;

varying vec2 vertexColor;
Expand All @@ -44,7 +45,7 @@ static const char DROPSHADOW_SPREAD_FRAGMENT_SHADER[] = R"(
alphaSum += texture2D(uTextureInput, vertexColor + vec2(measureX, -measureY)).a;
}

gl_FragColor = (alphaSum > 0.0) ? vec4(uColor, uOpacity) : vec4(0.0);
gl_FragColor = (alphaSum > 0.0) ? vec4(uColor, uAlpha) : vec4(0.0);
}
)";

Expand All @@ -53,7 +54,7 @@ static const char DROPSHADOW_SPREAD_THICK_FRAGMENT_SHADER[] = R"(
precision highp float;
uniform sampler2D uTextureInput;
uniform vec3 uColor;
uniform float uOpacity;
uniform float uAlpha;
uniform vec2 uSize;

varying vec2 vertexColor;
Expand All @@ -73,7 +74,7 @@ static const char DROPSHADOW_SPREAD_THICK_FRAGMENT_SHADER[] = R"(
alphaSum += texture2D(uTextureInput, vertexColor + vec2(measureX / 2.0, -measureY / 2.0)).a;
}

gl_FragColor = (alphaSum > 0.0) ? vec4(uColor, uOpacity) : vec4(0.0);
gl_FragColor = (alphaSum > 0.0) ? vec4(uColor, uAlpha) : vec4(0.0);
}
)";

Expand All @@ -90,14 +91,14 @@ std::string DropShadowSpreadFilter::onBuildFragmentShader() {

void DropShadowSpreadFilter::onPrepareProgram(const GLInterface* gl, unsigned program) {
spreadColorHandle = gl->getUniformLocation(program, "uColor");
spreadOpacityHandle = gl->getUniformLocation(program, "uOpacity");
spreadAlphaHandle = gl->getUniformLocation(program, "uAlpha");
spreadSizeHandle = gl->getUniformLocation(program, "uSize");
}

void DropShadowSpreadFilter::onUpdateParams(const GLInterface* gl, const Rect& contentBounds,
const Point& filterScale) {
auto color = layerStyle->color->getValueAt(layerFrame);
auto opacity = layerStyle->opacity->getValueAt(layerFrame);
auto color = ToTGFXColor(layerStyle->color->getValueAt(layerFrame));
auto alpha = ToAlpha(layerStyle->opacity->getValueAt(layerFrame));
auto spread = layerStyle->spread->getValueAt(layerFrame);
auto size = layerStyle->size->getValueAt(layerFrame);
spread *= (spread == 1.0) ? 1.0 : 0.8;
Expand All @@ -107,8 +108,8 @@ void DropShadowSpreadFilter::onUpdateParams(const GLInterface* gl, const Rect& c
spreadSizeX = std::min(spreadSizeX, DROPSHADOW_MAX_SPREAD_SIZE);
spreadSizeY = std::min(spreadSizeY, DROPSHADOW_MAX_SPREAD_SIZE);

gl->uniform3f(spreadColorHandle, color.red / 255.f, color.green / 255.f, color.blue / 255.f);
gl->uniform1f(spreadOpacityHandle, opacity / 255.f);
gl->uniform3f(spreadColorHandle, color.red, color.green, color.blue);
gl->uniform1f(spreadAlphaHandle, alpha);
gl->uniform2f(spreadSizeHandle, spreadSizeX / contentBounds.width(),
spreadSizeY / contentBounds.height());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DropShadowSpreadFilter : public LayerFilter {
DropShadowStyleMode styleMode;

int spreadColorHandle = -1;
int spreadOpacityHandle = -1;
int spreadAlphaHandle = -1;
int spreadSizeHandle = -1;
};
} // namespace pag
18 changes: 9 additions & 9 deletions src/rendering/filters/gaussblur/SinglePassBlurFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static const char BLUR_FRAGMENT_SHADER[] = R"(

uniform vec3 uColor;
uniform float uColorValid;
uniform float uOpacity;
uniform float uAlpha;

varying vec2 vertexColor;

Expand Down Expand Up @@ -73,7 +73,7 @@ static const char BLUR_FRAGMENT_SHADER[] = R"(
}

color = sum / divisor;
gl_FragColor = vec4(mix(color.rgb, uColor * color.a, uColorValid), color.a) * uOpacity;
gl_FragColor = vec4(mix(color.rgb, uColor * color.a, uColorValid), color.a) * uAlpha;
}
)";

Expand All @@ -90,13 +90,13 @@ void SinglePassBlurFilter::onPrepareProgram(const GLInterface* gl, unsigned int
repeatEdgeHandle = gl->getUniformLocation(program, "uRepeatEdge");
colorHandle = gl->getUniformLocation(program, "uColor");
colorValidHandle = gl->getUniformLocation(program, "uColorValid");
opacityHandle = gl->getUniformLocation(program, "uOpacity");
alphaHandle = gl->getUniformLocation(program, "uAlpha");
}

void SinglePassBlurFilter::updateParams(float blurrinessValue, float blurOpacityValue,
void SinglePassBlurFilter::updateParams(float blurrinessValue, float blurAlphaValue,
bool repeatEdgeValue, BlurMode mode) {
blurriness = blurrinessValue;
opacity = blurOpacityValue;
alpha = blurAlphaValue;
repeatEdge = repeatEdgeValue;
switch (mode) {
case BlurMode::Picture:
Expand All @@ -112,14 +112,14 @@ void SinglePassBlurFilter::updateParams(float blurrinessValue, float blurOpacity
}
}

void SinglePassBlurFilter::enableBlurColor(Color blurColor) {
void SinglePassBlurFilter::enableBlurColor(Color4f blurColor) {
isColorValid = true;
color = blurColor;
}

void SinglePassBlurFilter::disableBlurColor() {
isColorValid = false;
color = Black;
color = Color4f::Black();
}

void SinglePassBlurFilter::onUpdateParams(const GLInterface* gl, const Rect& contentBounds,
Expand All @@ -137,9 +137,9 @@ void SinglePassBlurFilter::onUpdateParams(const GLInterface* gl, const Rect& con
blurLevel / static_cast<float>(contentBounds.height()) *
(direction == BlurDirection::Vertical));
gl->uniform1f(repeatEdgeHandle, repeatEdge);
gl->uniform3f(colorHandle, color.red / 255.f, color.green / 255.f, color.blue / 255.f);
gl->uniform3f(colorHandle, color.red, color.green, color.blue);
gl->uniform1f(colorValidHandle, isColorValid);
gl->uniform1f(opacityHandle, opacity);
gl->uniform1f(alphaHandle, alpha);
}

std::vector<Point> SinglePassBlurFilter::computeVertices(const Rect& inputBounds,
Expand Down
10 changes: 5 additions & 5 deletions src/rendering/filters/gaussblur/SinglePassBlurFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class SinglePassBlurFilter : public LayerFilter {
explicit SinglePassBlurFilter(BlurDirection blurDirection);
~SinglePassBlurFilter() override = default;

void updateParams(float blurriness, float opacity, bool repeatEdge, BlurMode mode);
void updateParams(float blurriness, float alpha, bool repeatEdge, BlurMode mode);

void enableBlurColor(Color blurColor);
void enableBlurColor(Color4f blurColor);
void disableBlurColor();

protected:
Expand All @@ -49,13 +49,13 @@ class SinglePassBlurFilter : public LayerFilter {
int repeatEdgeHandle = -1;
int colorHandle = -1;
int colorValidHandle = -1;
int opacityHandle = -1;
int alphaHandle = -1;

BlurDirection direction;
Color color = Black;
Color4f color = Color4f::Black();
bool isColorValid = false;
float blurriness = 0.0;
float opacity = 1.0f;
float alpha = 1.0f;
bool repeatEdge = true;
float maxRadius = 3.0f;
float maxLevel = 13.0f;
Expand Down
5 changes: 3 additions & 2 deletions src/rendering/graphics/Glyph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ void Glyph::computeStyleKey(BytesKey* styleKey) const {
styleKey->write(m.getSkewX());
styleKey->write(m.getSkewY());
styleKey->write(m.getScaleY());
uint8_t fillValues[] = {fillColor.red, fillColor.green, fillColor.blue, alpha};
uint8_t fillValues[] = {fillColor.red, fillColor.green, fillColor.blue,
static_cast<uint8_t>(alpha * 255)};
styleKey->write(fillValues);
uint8_t strokeValues[] = {strokeColor.red, strokeColor.green, strokeColor.blue,
static_cast<uint8_t>(textStyle)};
Expand All @@ -115,7 +116,7 @@ void Glyph::computeStyleKey(BytesKey* styleKey) const {
}

bool Glyph::isVisible() const {
return matrix.invertible() && alpha != Transparent && !bounds.isEmpty();
return matrix.invertible() && alpha != 0.0f && !bounds.isEmpty();
}

Matrix Glyph::getTotalMatrix() const {
Expand Down
8 changes: 4 additions & 4 deletions src/rendering/graphics/Glyph.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ class Glyph {
/**
* Retrieves alpha from the color used when stroking and filling.
*/
Opacity getAlpha() const {
float getAlpha() const {
return alpha;
}

/**
* Replaces alpha of the color used when stroking and filling, leaving RGB unchanged.
*/
void setAlpha(Opacity opacity) {
alpha = opacity;
void setAlpha(float newAlpha) {
alpha = newAlpha;
}

/**
Expand Down Expand Up @@ -239,7 +239,7 @@ class Glyph {
// writable attributes:
Matrix matrix = Matrix::I();
TextStyle textStyle = TextStyle::Fill;
Opacity alpha = Opaque;
float alpha = 1.0f;
Color fillColor = Black;
Color strokeColor = Black;
float strokeWidth = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/rendering/graphics/Graphic.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ struct GradientPaint {
Enum gradientType;
Point startPoint;
Point endPoint;
std::vector<Color> colors;
std::vector<Opacity> alphas;
std::vector<Color4f> colors;
std::vector<float> positions;
};

Expand Down
Loading