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

[Dark & Glass Modes] Improve rendering performance by using Direct2D API #264

Closed
gileli121 opened this issue Sep 30, 2022 · 17 comments
Closed
Labels
done enhancement New feature or request

Comments

@gileli121
Copy link
Member

gileli121 commented Sep 30, 2022

This task is a subtest of the main goal - to improve the performance of these effects.
The main mission is #105
In this task, I will implement the method to optimize the performance by using the Direct2D API


I found a new way how to improve the rendering performances!
Until a few days ago, I was unaware of how to invert the colors of 8,294,400 pixels (the amount in 4K resolution) in GPU without using NVIDIA CUDA API (which requires NVIDIA GPU).

And recently, I found a way to do it!
In Direct2D API, there is a GPU-accelerated method to do that, and because it is Direct2D, it is not exclusive to NVIDIA GPUs! It will work on even regular Intel GPUs. This finding means that every computer will use the GPU for this effect. Great stuff!

I made a POC that proves that it is possible to get 60+ frames per second on 4K using just intel GPU, and this is instead of 20-30 FPS.

Not only that, but this finding also comes with a new optimization advantage: because the OS API also does not send the captured frame to the CPU and because Direct2D will do its magic in GPU, now, unlike before, we will never need to move any frame data to CPU (unless the "filter images" option is enabled) so this fact also will help.
This will also reduce a lot of battery usage because everything will be done natively by the GPU!

The idea in the message above (To use DOM API) may still be worth checking after this finding. It is still relevant because it tries to optimize the performances when the "filter images" option is enabled.

Originally posted by @gileli121 in #105 (comment)

@gileli121
Copy link
Member Author

gileli121 commented Oct 9, 2022

Done.
Version for testing:
WindowTop 5.19.0-beta1 - setup.exe.zip

Dark mode renderer

  • Use Direct2D native APIs (built-in in any standard GPU) to implement the inverted color effect
  • Use more native Direct2D APIs (such as getting an image histogram) to calculate the amount of light from a window efficiently. This is needed when the "Auto disable" option is enabled (the dark mode will be disabled when the window is already dark)
  • When the option "filter images" is disabled, the whole process is implemented by the GPU, and nothing is done by the CPU (The CPU does not handle any image processing logic such as shape detection or light detection. All is done natively by making smart usage of available GPU features by Direct2D APIs)
  • When the option "filter images" is enabled, the CPU does this extra process of detecting the images. Even in this process, a small part of it uses Direct2D API for "learning" the window frame colors
  • The calculation of the image-detection algorithm is more accurate thanks to Direct2D API (Images will be detected more accurately)
  • The rendering performances improved dramatically - 20-30 FPS to 60FPS or more and no need for NVIDIA & CUDA GPU
  • The option to enable CUDA acceleration is removed because default by Direct2D does acceleration
  • The feature "Glass" removed because it was never worked stable, and no one used it anyway
  • Fixed: When closing windows in dark mode, an empty frame is left on the screen that cannot be moved or closed (When closing windows in dark mode, an empty frame is left on the screen that cannot be moved or closed. #257)
  • Fixed: Window position in the maximized state when dark mode is on (Window position in maximized state when dark mode is on #256)
  • Fixed: When enabling dark mode and set-top, the border for the always-on-top is invisible
  • Fixed: When the renderer crash, the app fails to start again.
  • Fixed: Bug in the toolbar settings that when enabling/disabling the button, the toolbar preview does not update with the new selected buttons to show
  • Changed: The toolbar settings page was a little changed.

@gileli121
Copy link
Member Author

WindowTop 5.19.0-beta3 - setup.exe.zip

  • Fixed bug in the renderer that it did not start properly after the window returned from minimized state

@gileli121
Copy link
Member Author

gileli121 commented Oct 12, 2022

This fix should resolve the bug:
Issue with dark-mode and glass-mode on dual monitor configuration when DPI scaling is not the same for each monitor

In this version, I fixed a bug when the DPI of the second monitor was not the same as the DPI of the primary monitor.
In this case, the window was rerendered to be dark, but the rerendered frame was not printed at the correct location on the screen.

Here is the fix:
WindowTop 5.19.0-beta4 - setup.exe.zip

@gileli121
Copy link
Member Author

WindowTop 5.19.0-beta5 - setup.exe.zip

This version is an extra hotfix for the DPI bugs.
This fixes a bug with Microsoft Edge / Chrome browser that when the window is maximized on a second monitor with a different DPI, the dark mode does not work.

@gileli121
Copy link
Member Author

gileli121 commented Oct 14, 2022

WindowTop 5.19.0-beta6 - setup.exe.zip

  • Optimize the speed when "Filter images" is enabled by adding logic to auto-disable the filter image algorithm. It's done by implementing an efficient algorithm that uses information about the frame returned by Direct2D API (hardware accelerated method) to evaluate if the frame may contain images. If so, it will enable the additional algorithm that prevents color inversion where the images are located with the cost of reduced performance. It means that the performances when "Filter images" are no longer reduced when no images are detected on the window/frame.
  • Because of the big optimization above, I decided that "Filter images" will be enabled by default, and I also simplified the "Set-Dark" button in the toolbar by removing from the button the small popup with the single option to enable/disable the "Filter Images" option. It is still accessible via the settings.
  • Fixed crash in the renderer that sometimes it fails to close the pipe, and as a result, you get the fatal error window
  • Don't allow using the dark-mode feature when the app is not activated/trial
  • Remove the small options popup of the dark mode button in the toolbar, and enable filter-images by default also for newer installations

@gileli121
Copy link
Member Author

gileli121 commented Oct 14, 2022

WindowTop 5.19.0-beta7 - setup.exe.zip

  • Fixed a bug in the is-image-bright algorithm that sometimes detected the frame as bright incorrectly.
  • Optimize memory usage by automatically cleaning extra buffers used by the image detection algorithm (the "Filter Images" feature) when we detect that the frame probably does not contain any images to be excluded from color inversion. It will allocate these buffers again on the fly when the algorithm detects that photos may be inside the frame/window. I tested for 4K and saw that when the frame has no images, the memory usage decreased to 10MB. And when images are detected, it is automatically increased to 40MB due to the automatic buffers allocations.
  • Optimize memory usage by requesting the system to clear an empty working set when the first captured frame arrives. Before the fix, when the renderer started, the memory usage for 4K was 177MB, and now it is 5MB-10MB on average. The algorithm optimized to request cleaning empty working set each time the buffers were reallocated, so it will ensure that the memory usage will be between 5MB-70MB for 4K

@gileli121
Copy link
Member Author

This seems very unstable in Windows 10 (unlike in Windows 11).
This will need more work

@gileli121
Copy link
Member Author

WindowTop 5.19.0-beta8 - setup.exe.zip

@gileli121
Copy link
Member Author

WindowTop 5.19.0-beta9 - setup.exe.zip

  • Stabilize a lot on Windows 10. Before that, it was extremely unstable on Windows 10.

@gileli121
Copy link
Member Author

gileli121 commented Oct 16, 2022

WindowTop 5.19.0-beta10 - setup.exe.zip

  • Delete dead code and leftover resources
  • Bring back the filter images option under the dark-mode button in the toolbar, but without the notification that it will reduce rendering speed because this time it is not true like before

@gileli121
Copy link
Member Author

WindowTop 5.19.0-beta11 - setup.exe.zip

@gileli121
Copy link
Member Author

gileli121 commented Oct 16, 2022

WindowTop 5.19.0-beta12 - setup.exe.zip

  • The image detection algorithm (the "Filter Images" option - what allows it to exclude images from color inversion). improved significantly and now works more accurately and efficiently

This is probably the final beta version of this effort. I will release it if we see that it is stable and works well enough.

@gileli121
Copy link
Member Author

WindowTop 5.19.0-beta13 - setup.exe.zip

  • More improvements to the image detection algorithm

@gileli121 gileli121 changed the title [Dark Mode] Improve rendering performance by using Direct2D API [Dark & Glass Modes] Improve rendering performance by using Direct2D API Oct 25, 2022
@gileli121
Copy link
Member Author

WindowTop 5.19.0-beta15 - setup.exe.zip

  • Bring back the "Glass" feature
  • Simplify the glass popup settings to 4 sliders
    image
  • Fixed bug with the CUDA acceleration option that, when enabled, did not work for older NVIDIA graphic cards because the code was compiled with CUDA Toolkit 11.8, which does not support older NVIDIA GPUs

@gileli121
Copy link
Member Author

gileli121 commented Oct 29, 2022

WindowTop 5.19.0-beta16 - setup.exe.zip

  • Fixed input delay bug in dark mode due to an issue that it did not use the primary display GPU. This also may also improve the rendering speed.
  • Changed default glass mode settings.
  • Implemented logic to suspend rerendering of a window that is fully covered by other windows

@gileli121
Copy link
Member Author

WindowTop 5.19.0-beta17 - setup.exe.zip

  • Fixed very high input lags on Windows 10 and a little on Windows 11 when enabling dark/glass mode for multiple windows simultaneously

@gileli121
Copy link
Member Author

@gileli121 gileli121 unpinned this issue Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
done enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant