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

feat: change screenshot image format from Jpeg to WebP #273

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

ioannisj
Copy link
Contributor

@ioannisj ioannisj commented Dec 12, 2024

💡 Motivation and Context

Motivation is to reduce the size of our replay snapshots by switching to a more compressed image format.
From empirical testing on our sample apps, WebP is around ~40% more compressed than JPEG.

Why WebP?

  • WebP is natively supported on Android
  • WebP is supported by all modern browsers. ~97% of browsers support WebP
  • We want to maintain the same image format across all SDKs so we can better monitor the impact of the switch.

Alternative formats

  • AVIF was also a good alternative since it's the successor of WebP.
    • Browser support for it is good, but less than WebP.
    • We may consider adding support for it in the future but for now we'll stick with WebP for reasons outlined above.

WebP on iOS:

Unfortunately, there's no native support for encoding WebP images in iOS. (iOS added support for decoding in iOS 14).

To add support we vendored in libwebp which is the standard lib for WebP encoding.

We had to change our package definitions both for Cocopods and SPM, which differ a bit because:

  • SPM does not support mix-language packages,
    • We had to create a separate private module (libwebp) and import it in our main target.
    • SPM manages all bridging code between Swift and C
    • SPM builds use a conditional import libwebp
  • Pods, on the other hand, don't support nested frameworks (use_frameworks! is standard), or at least I couldn't get it to work.
    • So for Cocopods we had to package as a mix-language framework.
    • This means that we needed to add some module maps and umbrella headers to get C-interop to work

Note: There are some changes in project files as well since I needed to add some bridging code.

Note: this is a big PR mainly due to vendored code. Once I remove decoding/animated webp bits it could be more manageable but still a bit of a pain to review. 🙏

Checklist:

  • Make sure SPM and Cocopods packages are functional
    • swift package describe
    • pod spec lint PostHog.podspec --allow-warnings
  • Make sure that all sample apps compile
  • Make sure we maintain permissive licensing info from libwebp
  • Cleanup decoding related vendored code
  • Cleanup animated webp related vendored code

💚 How did you test it?

@marandaneto I'll need your help testing this out, especially verifying that we didn't break package builds for people.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

@ioannisj ioannisj requested a review from a team December 12, 2024 06:56
@ioannisj
Copy link
Contributor Author

Removed some of the vendored code that is not called when encoding an image to webp (e.g decoding parts, dealing with animated webp etc).

Test coverage is better now but there are still some files with close to 0% coverage which suggests there's still some code that can be removed. However, I don't feel comfortable stripping this further tbh. I'll try to check bundle size delta next and if we are satisfied I'll leave this as is for now

@ioannisj ioannisj marked this pull request as ready for review December 12, 2024 13:36
@ioannisj
Copy link
Contributor Author

@marandaneto can you try make buildExamples on your machine? Compiles okay on mine but fails in CI for some reason

@marandaneto
Copy link
Member

@marandaneto can you try make buildExamples on your machine? Compiles okay on mine but fails in CI for some reason

works for me but I see this error in CI:

❌ /Users/runner/work/posthog-ios/posthog-ios/vendor/libwebp/include/types.h:20:10: include of non-modular header inside framework module 'PostHog.types': '/Applications/Xcode_15.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/include/inttypes.h' [-Werror,-Wnon-modular-include-in-framework-module]

#include <inttypes.h>

Check the CI runs-on: macos-13 and xcode-version: latest-stable
Maybe its running on an older/newer versions where inttypes.h isn't available? (clang version etc), we need to be sure that webp works on iOS >= 13 btw

Copy link
Member

@marandaneto marandaneto left a comment

Choose a reason for hiding this comment

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

once we make CI happy, we can merge this

@ioannisj
Copy link
Contributor Author

ioannisj commented Dec 17, 2024

@marandaneto can you try make buildExamples on your machine? Compiles okay on mine but fails in CI for some reason

works for me but I see this error in CI:

❌ /Users/runner/work/posthog-ios/posthog-ios/vendor/libwebp/include/types.h:20:10: include of non-modular header inside framework module 'PostHog.types': '/Applications/Xcode_15.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/include/inttypes.h' [-Werror,-Wnon-modular-include-in-framework-module]

#include <inttypes.h>

Check the CI runs-on: macos-13 and xcode-version: latest-stable Maybe its running on an older/newer versions where inttypes.h isn't available? (clang version etc), we need to be sure that webp works on iOS >= 13 btw

Yeah I think you may be right! I'd downloading some older runtimes and I'll check. We should be safe since I see here that libwebp has iOS >= 6 https://github.com/webmproject/libwebp/blob/e4f7a9f0c7c9fbfae1568bc7fa5c94b989b50872/iosbuild.sh#L17C10-L17C25

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.

2 participants