-
Notifications
You must be signed in to change notification settings - Fork 464
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
LayerStyle supports deferred rendering. #2607
Changes from 14 commits
0c2fc0b
52c58a5
34e27c9
63c5b0f
2925cbf
3f8c1d5
cd18289
bc0a9e5
c4014ba
79061ce
0a3f485
35b1dc5
c5a6813
7619ec9
2ce21e5
6be2d0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "LayerStylesFilter.h" | ||
#include <tgfx/core/Surface.h> | ||
#include "layerstyle/LayerStyleFilter.h" | ||
#include "rendering/caches/RenderCache.h" | ||
#include "rendering/renderers/FilterRenderer.h" | ||
|
||
|
@@ -31,34 +33,39 @@ void LayerStylesFilter::TransformBounds(tgfx::Rect* bounds, const FilterList* fi | |
} | ||
} | ||
|
||
LayerStylesFilter::LayerStylesFilter(RenderCache* renderCache) : renderCache(renderCache) { | ||
drawFilter = new LayerFilter(); | ||
bool LayerStylesFilter::initialize(tgfx::Context*) { | ||
return true; | ||
} | ||
|
||
LayerStylesFilter::~LayerStylesFilter() { | ||
delete drawFilter; | ||
} | ||
|
||
bool LayerStylesFilter::initialize(tgfx::Context* context) { | ||
return drawFilter->initialize(context); | ||
} | ||
|
||
void LayerStylesFilter::update(const FilterList* list, const tgfx::Rect& inputBounds, | ||
const tgfx::Rect& outputBounds, const tgfx::Point& extraScale) { | ||
void LayerStylesFilter::update(const FilterList* list, const tgfx::Point& extraScale) { | ||
filterList = list; | ||
contentBounds = inputBounds; | ||
transformedBounds = outputBounds; | ||
filterScale = extraScale; | ||
} | ||
|
||
void LayerStylesFilter::draw(tgfx::Context* context, const FilterSource* source, | ||
const FilterTarget* target) { | ||
tgfx::BackendTexture backendTexture = {source->sampler, source->width, source->height}; | ||
auto sourceImage = tgfx::Image::MakeFrom(context, backendTexture, source->origin); | ||
if (sourceImage == nullptr) { | ||
return; | ||
} | ||
|
||
tgfx::BackendRenderTarget renderTarget = {target->frameBuffer, target->width, target->height}; | ||
auto surface = tgfx::Surface::MakeFrom(context, renderTarget, tgfx::ImageOrigin::TopLeft); | ||
if (surface == nullptr) { | ||
return; | ||
} | ||
auto canvas = surface->getCanvas(); | ||
canvas->concat(ToMatrix(target)); | ||
auto totalScale = filterScale; | ||
totalScale.x *= source->scale.x; | ||
totalScale.y *= source->scale.y; | ||
for (auto& layerStyle : filterList->layerStyles) { | ||
if (layerStyle->drawPosition() == LayerStylePosition::Blow) { | ||
auto filter = renderCache->getFilterCache(layerStyle); | ||
auto filter = LayerStyleFilter::Make(layerStyle); | ||
if (filter) { | ||
filter->update(filterList->layerFrame, contentBounds, transformedBounds, filterScale); | ||
filter->draw(context, source, target); | ||
filter->update(filterList->layerFrame, totalScale); | ||
filter->draw(canvas, sourceImage); | ||
} | ||
} | ||
} | ||
|
@@ -68,18 +75,20 @@ void LayerStylesFilter::draw(tgfx::Context* context, const FilterSource* source, | |
bool drawSource = true; | ||
for (auto& layerStyle : filterList->layerStyles) { | ||
if (layerStyle->drawPosition() == LayerStylePosition::Above) { | ||
auto filter = renderCache->getFilterCache(layerStyle); | ||
auto filter = LayerStyleFilter::Make(layerStyle); | ||
if (filter) { | ||
filter->update(filterList->layerFrame, contentBounds, transformedBounds, filterScale); | ||
filter->draw(context, source, target); | ||
// GradientOverlayFilter is scale-invariant for the source image, so we can use the filter | ||
// scale directly. | ||
filter->update(filterList->layerFrame, filterScale); | ||
filter->draw(canvas, sourceImage); | ||
drawSource = false; | ||
} | ||
} | ||
} | ||
|
||
if (drawSource) { | ||
drawFilter->update(filterList->layerFrame, contentBounds, contentBounds, filterScale); | ||
drawFilter->draw(context, source, target); | ||
canvas->drawImage(sourceImage); | ||
} | ||
context->flush(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里删掉 |
||
} | ||
} // namespace pag |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Tencent is pleased to support the open source community by making libpag available. | ||
// | ||
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// 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. | ||
// | ||
///////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#pragma once | ||
|
||
#include "FilterEffect.h" | ||
#include "rendering/filters/LayerFilter.h" | ||
|
||
namespace pag { | ||
|
||
class AlphaEdgeDetectEffectUniforms : public Uniforms { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 都改名为Filter,避免和PAG的Effect概念冲突。 |
||
public: | ||
AlphaEdgeDetectEffectUniforms(tgfx::Context* context, unsigned program) | ||
: Uniforms(context, program) { | ||
auto gl = tgfx::GLFunctions::Get(context); | ||
horizontalStepHandle = gl->getUniformLocation(program, "mHorizontalStep"); | ||
verticalStepHandle = gl->getUniformLocation(program, "mVerticalStep"); | ||
} | ||
int horizontalStepHandle = -1; | ||
int verticalStepHandle = -1; | ||
}; | ||
|
||
class AlphaEdgeDetectLayerEffect : public FilterEffect { | ||
public: | ||
DEFINE_RUNTIME_EFFECT_TYPE; | ||
explicit AlphaEdgeDetectLayerEffect() : FilterEffect(Type()) { | ||
} | ||
|
||
std::string onBuildFragmentShader() const override; | ||
|
||
std::unique_ptr<Uniforms> onPrepareProgram(tgfx::Context* context, | ||
unsigned program) const override; | ||
|
||
void onUpdateParams(tgfx::Context* context, const EffectProgram* program, | ||
const std::vector<tgfx::BackendTexture>& sources) const override; | ||
}; | ||
|
||
} // namespace pag |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不用尖括号