Skip to content

Perf: Optimize background opacity calculation for high-res images#6276

Draft
KSSJW wants to merge 3 commits into
HMCL-dev:mainfrom
KSSJW-Contribution:opacity-calculation-for-high-res-background
Draft

Perf: Optimize background opacity calculation for high-res images#6276
KSSJW wants to merge 3 commits into
HMCL-dev:mainfrom
KSSJW-Contribution:opacity-calculation-for-high-res-background

Conversation

@KSSJW

@KSSJW KSSJW commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

优化了高分辨率背景图的透明度计算

测试材料

Default

实况

修改前

1.mp4

修改后

2.mp4

@KSSJW

KSSJW commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request modifies the background image opacity rendering in Themes.java by replacing the pixel-by-pixel loop with a Canvas and Canvas.snapshot() approach. However, the review feedback highlights critical issues with this change: calling Canvas.snapshot() on a background thread will throw an IllegalStateException, and hardcoding the canvas size to 800x480 degrades image quality. The reviewer suggests using batch pixel operations via PixelReader.getPixels() and PixelWriter.setPixels() to achieve thread safety, high performance, and full resolution. Additionally, suggestions are provided to update the imports accordingly and to check for a null PixelReader to prevent potential NullPointerExceptions.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread HMCL/src/main/java/org/jackhuang/hmcl/theme/Themes.java Outdated
Comment thread HMCL/src/main/java/org/jackhuang/hmcl/theme/Themes.java Outdated
Comment thread HMCL/src/main/java/org/jackhuang/hmcl/theme/Themes.java Outdated
@Wulian233

Copy link
Copy Markdown
Contributor

现在流畅非常多

@KSSJW

KSSJW commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

感觉 PixelFormatPixelReaderPixelWriter 没有 SnapshotParametersCanvasGraphicsContext 那么快(

@KSSJW

KSSJW commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes the background opacity calculation in Themes.java by scaling the image to a fixed 800x480 resolution using AWT/Swing utilities and performing bulk pixel operations. The reviewer pointed out that this approach introduces unnecessary AWT dependencies and causes image blurriness on high DPI screens. They recommended performing bulk pixel operations directly on the original JavaFX image to maintain image quality and performance without AWT overhead, providing a code suggestion to achieve this.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread HMCL/src/main/java/org/jackhuang/hmcl/theme/Themes.java
@Glavo

Glavo commented Jun 30, 2026

Copy link
Copy Markdown
Member

之后我打算重构壁纸相关功能,用 StackPane 在主界面后面叠一层 ImageView 来实现背景,这样调整不透明度就不需要重写图片了。

@KSSJW

KSSJW commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

之后我打算重构壁纸相关功能,用 StackPane 在主界面后面叠一层 ImageView 来实现背景,这样调整不透明度就不需要重写图片了。

确实,不然方法怎么优化本质上还是在重新绘制图像(
当前背景不能直接设置透明度,应该是ReadOnlyObjectWrapper<Background>类型的问题(?)

private static final ReadOnlyObjectWrapper<Background> background = new ReadOnlyObjectWrapper<>(
            new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));

private static void refreshBackgroundOpacity() {
    ...
    LoadedBackground refreshed;
    refreshed = new LoadedBackground.Image(...)
    ...
    loadedBackground = refreshed;
    background.set(refreshed.background());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants