Skip to content

Commit

Permalink
Recalculate the texture size during the blur process based on clipBou…
Browse files Browse the repository at this point in the history
…nds. (#431)
  • Loading branch information
Hparty authored Jan 14, 2025
1 parent 9a4035d commit bcbac5d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
34 changes: 22 additions & 12 deletions src/core/filters/BlurImageFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,25 @@ std::shared_ptr<TextureProxy> BlurImageFilter::lockTextureProxy(std::shared_ptr<
auto drawRect = Rect::MakeWH(lastRenderTarget->width(), lastRenderTarget->height());
FPArgs fpArgs(args.context, args.renderFlags, drawRect, Matrix::I());

// calculate the bounds of filter after scaling. Bounds means the max texture size of the filter.
auto bounds = filterBounds(Rect::MakeWH(source->width(), source->height()));
auto filterOffset = Point::Make(bounds.left, bounds.top);
bounds.scale(scaleFactor, scaleFactor);
// Calculate the bounds of the filter after scaling.
// `boundsWillSample` will determine the size of the texture after the first downsample.
// By calculating the clip bound after filter, we can determine the bounds that will affect the
// result. However, the bounds may be larger than the origin filter bounds, so we need to
// intersect the bounds.
auto boundsWillSample = filterBounds(clipBounds);
auto filterOriginBounds = filterBounds(Rect::MakeWH(source->width(), source->height()));
boundsWillSample.intersect(filterOriginBounds);

// sampleOffset means the offset between the source bounds and the sample bounds.
auto sampleOffset = Point::Make(boundsWillSample.left, boundsWillSample.top);
boundsWillSample.scale(scaleFactor, scaleFactor);

std::vector<std::shared_ptr<RenderTargetProxy>> renderTargets = {};
// calculate the size of the source image of the first downsample
auto textureSize = Size::Make(bounds.width(), bounds.height());
// calculate the uv matrix of the first downsample
auto blurUVMatrix = Matrix::MakeTrans(filterOffset.x * scaleFactor * downScaling,
filterOffset.y * scaleFactor * downScaling);
auto textureSize = Size::Make(boundsWillSample.width(), boundsWillSample.height());
// calculate the uv matrix of the first downsample. Add sampleOffset to get the sample texture.
auto blurUVMatrix = Matrix::MakeTrans(sampleOffset.x * scaleFactor * downScaling,
sampleOffset.y * scaleFactor * downScaling);

// scale the source image to smaller size
auto sourceUVMatrix = Matrix::MakeScale(1 / (downScaling * scaleFactor));
Expand Down Expand Up @@ -170,12 +178,14 @@ std::shared_ptr<TextureProxy> BlurImageFilter::lockTextureProxy(std::shared_ptr<
for (size_t i = renderTargets.size(); i > 0; --i) {
const auto& renderTarget = renderTargets[i - 1];
if (i == 1) {
// at the last iteration, we need to calculate the clip bounds of the filter
// At the last iteration, we need to calculate the clip bounds of the filter.
// Subtract sampleOffset means to convert sample bounds to origin bounds.
// Add offset of clipBounds to apply clipBounds.
blurUVMatrix =
Matrix::MakeTrans(clipBounds.left - filterOffset.x, clipBounds.top - filterOffset.y);
Matrix::MakeTrans(clipBounds.left - sampleOffset.x, clipBounds.top - sampleOffset.y);
blurUVMatrix.postScale(scaleFactor, scaleFactor);
sourceUVMatrix = Matrix::MakeScale(textureSize.width / bounds.width(),
textureSize.height / bounds.height());
sourceUVMatrix = Matrix::MakeScale(textureSize.width / boundsWillSample.width(),
textureSize.height / boundsWillSample.height());
} else {
// at other iterations, we upscale only
sourceUVMatrix =
Expand Down
10 changes: 5 additions & 5 deletions test/baseline/version.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
"drawColor": "b7aefeb",
"drawImage": "6ab1e4f",
"drawPaint": "b7aefeb",
"drawPaint_shadow": "270695a",
"drawPaint_shadow": "4d5c3b0",
"drawShape": "e031f25",
"filter_mode_linear": "d010fb8",
"filter_mode_nearest": "d010fb8",
"hardware_render_target_blend": "d010fb8",
"inversePath_rect": "6fd4617",
"inversePath_text": "270695a",
"inversePath_text": "4d5c3b0",
"merge_draw_call_rect": "d010fb8",
"merge_draw_call_rrect": "d010fb8",
"merge_draw_clear_op": "d010fb8",
Expand Down Expand Up @@ -73,13 +73,13 @@
"AlphaThreshold_empty": "5012d73",
"ComposeColorFilter": "f710e29",
"ComposeImageFilter": "2028a1b",
"ComposeImageFilter2": "270695a",
"ComposeImageFilter2": "4d5c3b0",
"EmptyShadowTest": "b9a42bf",
"ImageFilterShader": "2028a1b",
"InnerShadowBadCase": "270695a",
"InnerShadowBadCase": "4d5c3b0",
"ModeColorFilter": "c2b0b18",
"RuntimeEffect": "d93c573",
"blur": "270695a",
"blur": "4d5c3b0",
"blur-large-pixel": "270695a",
"dropShadow": "a30de8b",
"greyColorMatrix": "a30de8b",
Expand Down

0 comments on commit bcbac5d

Please sign in to comment.