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

Refactory the logic to handle force decode logic to avoid CA copy frame buffer, introduce SDImageForceDecodePolicy detailed control #3559

Conversation

dreampiggy
Copy link
Contributor

@dreampiggy dreampiggy commented Jul 9, 2023

New Pull Request Checklist

  • I have read and understood the CONTRIBUTING guide

  • I have read the Documentation

  • I have searched for a similar pull request in the project and found none

  • I have updated this branch with the latest master to avoid conflicts (via merge from master or rebase)

  • I have added the required tests to prove the fix/feature I am adding

  • I have updated the documentation (if necessary)

  • I have run the tests and they pass

  • I have run the lint and it passes (pod lib lint)

This merge request fixes / refers to the following issues: ...

Pull Request Description

This close #3417

Reason

This commit: ff6b3b9 cause to not force decode on Non-ImageIO created CGImage. In theory it's correct because like libwebp-created CGImage has already bitmap buffer in RAM.

However, this change has bad effect on final RAM usage. Because of https://github.com/path/FastImageCache#byte-alignment

When some bad coder (The SDWebImageWebPCoder in that example), does not handle the byte alignment correctly, CoreAnimation will try to copy the bitmap buffer when rendering, cause 2 * bitmap buffer usage, instead of 1.

You can use Xcode's Color Copied Image to find this behavior.
See:

image

Solution

  1. SD itself provide the helper function to easily handle the bitmap info and byte alignment, using runtime detection (instead of hardcode). For example, previous hardcode BRRA8888/BGRX8888 is not correct.
  2. WebPCoder need update its codebase to byte alignment the CGImage
  3. (new) Users can control whether always force-decode, never, or use the automatic smart check provided by us

dreampiggy added a commit to SDWebImage/SDWebImageWebPCoder that referenced this pull request Jul 9, 2023
@dreampiggy dreampiggy force-pushed the performance/default_bitmap_and_force_decode branch from 71c17a3 to 6938175 Compare July 10, 2023 15:27
@dreampiggy dreampiggy changed the title Refactory the logic to handle non-ImageIO CGImage's force decode logic to avoid CA copy frame buffer Refactory the logic to handle force decode logic to avoid CA copy frame buffer, introduce SDImageForceDecodePolicy detailed control Jul 12, 2023
@dreampiggy
Copy link
Contributor Author

The SDWebImageAvoidDecodeImage is deprecated, using the new policy to control the behavior, which has 3 values instead of 2 values BOOL

@dreampiggy
Copy link
Contributor Author

dreampiggy commented Jul 12, 2023

Added new SDImageForceDecodePolicy, which seems match user's feedback about details control, like only some of URLs, like webp URL, always force-decode, other use the automatic checking

@@ -14,5 +14,6 @@

+ (NSUInteger)totalMemory;
+ (NSUInteger)freeMemory;
+ (size_t)cacheLineSize;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems useless.

@dreampiggy dreampiggy force-pushed the performance/default_bitmap_and_force_decode branch 2 times, most recently from 42979e6 to 99873fc Compare July 12, 2023 17:32
…ck to handle the decode output bitmap info

Include: colorspace, byte alignment, bitmap info

Remove the exists hardcode on BGRA8888
Now it's will mark as false for not byte-aligned CGImage
The sd_isDecoded default value only check once
…orce-decode using automatic check, always or never

Deprecate the `SDWebImageAvoidDecodeImage`

This can help for some user who don't use ImageIO and need to ensure the memory footprint
Should use device RGB, like Color LCD
@dreampiggy dreampiggy force-pushed the performance/default_bitmap_and_force_decode branch from c771362 to ed1a44b Compare July 13, 2023 14:58
@dreampiggy dreampiggy added this to the 5.17.0 milestone Jul 13, 2023
…e and cause copy

Fix the NSColor should still convert to sRGB for print...
@dreampiggy dreampiggy force-pushed the performance/default_bitmap_and_force_decode branch 2 times, most recently from e3b6900 to 1fc9d4b Compare July 13, 2023 15:35
@dreampiggy dreampiggy force-pushed the performance/default_bitmap_and_force_decode branch from 1fc9d4b to 808cedc Compare July 13, 2023 15:41
@dreampiggy dreampiggy merged commit 8f16a63 into SDWebImage:master Jul 13, 2023
3 of 4 checks passed
@dreampiggy dreampiggy deleted the performance/default_bitmap_and_force_decode branch July 13, 2023 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory usage increases when webp is used after 5.13.0
1 participant