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

Allow inheriting LayerFilter to customize filter behavior. #390

Merged
merged 11 commits into from
Dec 20, 2024
Next Next commit
Modify the behavior of LayerFilter to allow LayerFilter to customize …
…rendering logic.
  • Loading branch information
Hparty committed Dec 18, 2024
commit 2411ae616720e8d26286c2bb53abb38dff672d3f
2 changes: 1 addition & 1 deletion include/tgfx/layers/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ class Layer {

Paint getLayerPaint(float alpha, BlendMode blendMode);

std::shared_ptr<ImageFilter> getLayerFilter(float contentScale);
std::shared_ptr<Picture> applyFilter(std::shared_ptr<Picture> source, float contentScale);

LayerContent* getRasterizedCache(const DrawArgs& args);

Expand Down
6 changes: 3 additions & 3 deletions include/tgfx/layers/filters/BlendFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

#pragma once

#include "tgfx/layers/filters/LayerFilter.h"
#include "tgfx/layers/filters/LayerImageFilter.h"

namespace tgfx {

class BlendFilter : public LayerFilter {
class BlendFilter : public LayerImageFilter {
public:
virtual ~BlendFilter() = default;
~BlendFilter() override = default;

/**
* Creates a new ColorFilter that applies blends between the constant color (src) and input color
Expand Down
6 changes: 3 additions & 3 deletions include/tgfx/layers/filters/BlurFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

#pragma once

#include "tgfx/layers/filters/LayerFilter.h"
#include "tgfx/layers/filters/LayerImageFilter.h"

namespace tgfx {

class BlurFilter : public LayerFilter {
class BlurFilter : public LayerImageFilter {
Hparty marked this conversation as resolved.
Show resolved Hide resolved
public:
virtual ~BlurFilter() = default;
~BlurFilter() override = default;

/**
* Create a filter that blurs its input by the separate X and Y blurriness. The provided tile mode
Expand Down
6 changes: 3 additions & 3 deletions include/tgfx/layers/filters/ColorMatrixFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

#pragma once

#include "tgfx/layers/filters/LayerFilter.h"
#include "tgfx/layers/filters/LayerImageFilter.h"

namespace tgfx {

class ColorMatrixFilter : public LayerFilter {
class ColorMatrixFilter : public LayerImageFilter {
public:
virtual ~ColorMatrixFilter() = default;
~ColorMatrixFilter() override = default;
Hparty marked this conversation as resolved.
Show resolved Hide resolved

/**
* Creates a new ColorMatrixFilter that transforms the color using the given 4x5 matrix. The matrix can
Expand Down
6 changes: 3 additions & 3 deletions include/tgfx/layers/filters/DropShadowFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

#pragma once

#include "tgfx/layers/filters/LayerFilter.h"
#include "tgfx/layers/filters/LayerImageFilter.h"

namespace tgfx {
class DropShadowFilter : public LayerFilter {
class DropShadowFilter : public LayerImageFilter {
public:
virtual ~DropShadowFilter() = default;
~DropShadowFilter() override = default;

/**
* Create a filter that draws a drop shadow under the input content.
Expand Down
6 changes: 3 additions & 3 deletions include/tgfx/layers/filters/InnerShadowFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

#pragma once

#include "tgfx/layers/filters/LayerFilter.h"
#include "tgfx/layers/filters/LayerImageFilter.h"

namespace tgfx {
class InnerShadowFilter : public LayerFilter {
class InnerShadowFilter : public LayerImageFilter {
public:
virtual ~InnerShadowFilter() = default;
~InnerShadowFilter() override = default;

/**
* Create a filter that draws an inner shadow over the input content.
Expand Down
33 changes: 8 additions & 25 deletions include/tgfx/layers/filters/LayerFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,18 @@ namespace tgfx {
*/
class LayerFilter : public LayerProperty {
public:
/**
* Returns the current image filter for the given scale factor. If the filter has not been
* created yet, it will be created and cached.
/*
* Applies the filter to the given picture and draws it to the canvas.
Hparty marked this conversation as resolved.
Show resolved Hide resolved
* @param scale The scale factor to apply to the filter.
* @return The current image filter.
* @return True if the filter was applied and drawn, false otherwise.
*/
std::shared_ptr<ImageFilter> getImageFilter(float scale);
virtual bool drawWithFilter(Canvas* canvas, std::shared_ptr<Picture> picture, float scale) = 0;

protected:
/**
* Creates a new image filter for the given scale factor. When it is necessary to recreate the
* ImageFilter, the onCreateImageFilter method will be called.
/*
* Return the bounds of after applying the filter to the given bounds.
Hparty marked this conversation as resolved.
Show resolved Hide resolved
* @param scale The scale factor to apply to the filter.
* @return A new image filter.
* @return The bounds of the filtered image.
*/
virtual std::shared_ptr<ImageFilter> onCreateImageFilter(float scale) = 0;

/**
* Marks the filter as dirty and invalidates the cached filter.
*/
void invalidateFilter();

private:
bool dirty = true;

float lastScale = 1.0f;

std::unique_ptr<Rect> _clipBounds = nullptr;

std::shared_ptr<ImageFilter> lastFilter;
virtual Rect filterBounds(const Rect& srcRect, float scale) = 0;
Hparty marked this conversation as resolved.
Show resolved Hide resolved
};
} // namespace tgfx
63 changes: 63 additions & 0 deletions include/tgfx/layers/filters/LayerImageFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "tgfx/layers/filters/LayerFilter.h"

namespace tgfx {

class LayerImageFilter : public LayerFilter {
public:
/**
* Applies the filter to the given picture.
* @param scale The scale factor to apply to the filter.
*/
bool drawWithFilter(Canvas* canvas, std::shared_ptr<Picture> picture, float scale) override;

/**
* Return the bounds of after applying the filter to the given bounds.
* @param scale The scale factor to apply to the filter.
* @return The bounds of the filtered image.
*/
Rect filterBounds(const Rect& srcRect, float scale) override;

protected:
/**
* Creates a new image filter for the given scale factor. When it is necessary to recreate the
* ImageFilter, the onCreateImageFilter method will be called.
* @param scale The scale factor to apply to the filter.
* @return A new image filter.
*/
virtual std::shared_ptr<ImageFilter> onCreateImageFilter(float scale) = 0;

/**
* Marks the filter as dirty and invalidates the cached filter.
*/
void invalidateFilter();

private:
std::shared_ptr<ImageFilter> getImageFilter(float scale);

bool dirty = true;

float lastScale = 1.0f;

std::shared_ptr<ImageFilter> lastFilter;
};
} // namespace tgfx
35 changes: 15 additions & 20 deletions src/layers/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,8 @@ Rect Layer::getBounds(const Layer* targetCoordinateSpace) {
bounds.join(childBounds);
}

auto imageFilter = getLayerFilter(1.0f);
if (imageFilter) {
bounds = imageFilter->filterBounds(bounds);
for (auto filter : _filters) {
bounds = filter->filterBounds(bounds, 1.0f);
}

if (targetCoordinateSpace && targetCoordinateSpace != this) {
Expand Down Expand Up @@ -535,19 +534,23 @@ Paint Layer::getLayerPaint(float alpha, BlendMode blendMode) {
return paint;
}

std::shared_ptr<ImageFilter> Layer::getLayerFilter(float contentScale) {
if (_filters.empty() || FloatNearlyZero(contentScale)) {
std::shared_ptr<Picture> Layer::applyFilter(std::shared_ptr<Picture> source, float contentScale) {
Hparty marked this conversation as resolved.
Show resolved Hide resolved
if (source == nullptr) {
return nullptr;
}
std::vector<std::shared_ptr<ImageFilter>> imageFilters;
if (_filters.empty() || FloatNearlyZero(contentScale)) {
Hparty marked this conversation as resolved.
Show resolved Hide resolved
return source;
}
auto picture = source;
for (const auto& filter : _filters) {
auto imageFilter = filter->getImageFilter(contentScale);
if (!imageFilter) {
Recorder recorder;
Hparty marked this conversation as resolved.
Show resolved Hide resolved
auto canvas = recorder.beginRecording();
if (!filter->drawWithFilter(canvas, picture, contentScale)) {
continue;
}
imageFilters.push_back(imageFilter);
picture = recorder.finishRecordingAsPicture();
}
return ImageFilter::Compose(imageFilters);
return picture;
}

LayerContent* Layer::getRasterizedCache(const DrawArgs& args) {
Expand Down Expand Up @@ -580,6 +583,7 @@ std::shared_ptr<Image> Layer::getRasterizedImage(const DrawArgs& args, float con
Matrix* drawingMatrix) {
DEBUG_ASSERT(drawingMatrix != nullptr);
auto picture = getLayerContents(args, contentScale);
picture = applyFilter(std::move(picture), contentScale);
if (!picture) {
return nullptr;
}
Expand All @@ -593,14 +597,6 @@ std::shared_ptr<Image> Layer::getRasterizedImage(const DrawArgs& args, float con
}
drawingMatrix->setScale(1.0f / contentScale, 1.0f / contentScale);
drawingMatrix->preTranslate(bounds.left, bounds.top);
if (auto filter = getLayerFilter(contentScale)) {
auto offset = Point::Zero();
image = image->makeWithFilter(std::move(filter), &offset);
if (image == nullptr) {
return nullptr;
}
drawingMatrix->preTranslate(offset.x, offset.y);
}
return image;
}

Expand Down Expand Up @@ -674,15 +670,14 @@ std::shared_ptr<MaskFilter> Layer::getMaskFilter(const DrawArgs& args, float sca
void Layer::drawOffscreen(const DrawArgs& args, Canvas* canvas, float alpha, BlendMode blendMode) {
auto contentScale = canvas->getMatrix().getMaxScale();
auto picture = getLayerContents(args, contentScale);
picture = applyFilter(std::move(picture), contentScale);
if (picture == nullptr) {
return;
}
Paint paint;
paint.setAlpha(alpha);
paint.setBlendMode(blendMode);
paint.setMaskFilter(getMaskFilter(args, contentScale));
auto filter = getLayerFilter(contentScale);
paint.setImageFilter(filter);
auto matrix = Matrix::MakeScale(1.0f / contentScale);
canvas->drawPicture(std::move(picture), &matrix, &paint);
}
Expand Down
2 changes: 1 addition & 1 deletion src/layers/filters/BlendFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ std::shared_ptr<ImageFilter> BlendFilter::onCreateImageFilter(float) {
}

BlendFilter::BlendFilter(const Color& color, BlendMode blendMode)
: LayerFilter(), _color(std::move(color)), _blendMode(blendMode) {
: _color(std::move(color)), _blendMode(blendMode) {
}

} // namespace tgfx
2 changes: 1 addition & 1 deletion src/layers/filters/BlurFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void BlurFilter::setTileMode(TileMode tileMode) {
}

BlurFilter::BlurFilter(float blurrinessX, float blurrinessY, TileMode tileMode)
: LayerFilter(), _blurrinessX(blurrinessX), _blurrinessY(blurrinessY), _tileMode(tileMode) {
: _blurrinessX(blurrinessX), _blurrinessY(blurrinessY), _tileMode(tileMode) {
}

std::shared_ptr<ImageFilter> BlurFilter::onCreateImageFilter(float scale) {
Expand Down
2 changes: 1 addition & 1 deletion src/layers/filters/ColorMatrixFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ std::shared_ptr<ImageFilter> ColorMatrixFilter::onCreateImageFilter(float) {
}

ColorMatrixFilter::ColorMatrixFilter(const std::array<float, 20>& matrix)
: LayerFilter(), _matrix(std::move(matrix)) {
: _matrix(std::move(matrix)) {
}

} // namespace tgfx
4 changes: 2 additions & 2 deletions src/layers/filters/DropShadowFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ std::shared_ptr<ImageFilter> DropShadowFilter::onCreateImageFilter(float scale)

DropShadowFilter::DropShadowFilter(float offsetX, float offsetY, float blurrinessX,
float blurrinessY, const Color& color, bool dropsShadowOnly)
: LayerFilter(), _offsetX(offsetX), _offsetY(offsetY), _blurrinessX(blurrinessX),
_blurrinessY(blurrinessY), _color(std::move(color)), _dropsShadowOnly(dropsShadowOnly) {
: _offsetX(offsetX), _offsetY(offsetY), _blurrinessX(blurrinessX), _blurrinessY(blurrinessY),
_color(std::move(color)), _dropsShadowOnly(dropsShadowOnly) {
}

} // namespace tgfx
4 changes: 2 additions & 2 deletions src/layers/filters/InnerShadowFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ std::shared_ptr<ImageFilter> InnerShadowFilter::onCreateImageFilter(float scale)

InnerShadowFilter::InnerShadowFilter(float offsetX, float offsetY, float blurrinessX,
float blurrinessY, const Color& color, bool innerShadowOnly)
: LayerFilter(), _offsetX(offsetX), _offsetY(offsetY), _blurrinessX(blurrinessX),
_blurrinessY(blurrinessY), _color(std::move(color)), _innerShadowOnly(innerShadowOnly) {
: _offsetX(offsetX), _offsetY(offsetY), _blurrinessX(blurrinessX), _blurrinessY(blurrinessY),
_color(std::move(color)), _innerShadowOnly(innerShadowOnly) {
}

} // namespace tgfx
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,42 @@
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "tgfx/layers/filters/LayerFilter.h"
#include "tgfx/layers/filters/LayerImageFilter.h"
#include "tgfx/layers/Layer.h"
namespace tgfx {

std::shared_ptr<ImageFilter> LayerFilter::getImageFilter(float filterScale) {
if (dirty || lastScale != filterScale) {
lastFilter = onCreateImageFilter(filterScale);
lastScale = filterScale;
dirty = false;
bool LayerImageFilter::drawWithFilter(Canvas* canvas, std::shared_ptr<Picture> picture,
float scale) {
auto filter = getImageFilter(scale);
if (!filter) {
return false;
}
return lastFilter;
Paint paint;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paint paint = {};

paint.setImageFilter(filter);
canvas->drawPicture(picture, nullptr, &paint);
Hparty marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

Rect LayerImageFilter::filterBounds(const Rect& srcRect, float scale) {
auto filter = getImageFilter(scale);
if (!filter) {
return srcRect;
}
return filter->filterBounds(srcRect);
}

void LayerFilter::invalidateFilter() {
void LayerImageFilter::invalidateFilter() {
dirty = true;
invalidate();
}

std::shared_ptr<ImageFilter> LayerImageFilter::getImageFilter(float scale) {
if (dirty || lastScale != scale) {
lastFilter = onCreateImageFilter(scale);
lastScale = scale;
dirty = false;
}
return lastFilter;
}

} // namespace tgfx
Loading