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

Use PixelCopy API for capturing screenshots on API level 24+ #3008

Merged
merged 11 commits into from
Oct 31, 2023

Conversation

markushi
Copy link
Member

@markushi markushi commented Oct 23, 2023

📜 Description

Use PixelCopy on newer devices, as it allows us to capture screenshots on background threads as well.

Right now we don't seem to have any tests for ScreenshotUtils.java, if you have an idea on how we could test this, I'm happy to add tests!

💡 Motivation and Context

Fixes getsentry/sentry-react-native#3314
Closes #2916

💚 How did you test it?

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

@markushi markushi changed the title Use PixelCopy API for capturing screenshots on newer API levels Use PixelCopy API for capturing screenshots on API level 24+ Oct 23, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Oct 23, 2023

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 413237c

@github-actions
Copy link
Contributor

github-actions bot commented Oct 23, 2023

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 390.47 ms 514.08 ms 123.61 ms
Size 1.72 MiB 2.29 MiB 577.52 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
7ca9895 364.31 ms 460.46 ms 96.15 ms
93a76ca 377.96 ms 447.52 ms 69.56 ms
86f0096 368.63 ms 446.92 ms 78.29 ms
bc4be3b 360.40 ms 435.04 ms 74.64 ms
93a76ca 391.54 ms 475.65 ms 84.11 ms
c7e2fbc 372.00 ms 461.71 ms 89.71 ms
99a51e2 405.11 ms 479.65 ms 74.54 ms
c7e2fbc 393.98 ms 478.24 ms 84.27 ms
93a76ca 377.41 ms 448.22 ms 70.81 ms
86f0096 381.33 ms 476.58 ms 95.25 ms

App size

Revision Plain With Sentry Diff
7ca9895 1.72 MiB 2.29 MiB 576.51 KiB
93a76ca 1.72 MiB 2.29 MiB 576.75 KiB
86f0096 1.72 MiB 2.29 MiB 576.50 KiB
bc4be3b 1.72 MiB 2.29 MiB 576.53 KiB
93a76ca 1.72 MiB 2.29 MiB 576.75 KiB
c7e2fbc 1.72 MiB 2.29 MiB 576.40 KiB
99a51e2 1.72 MiB 2.29 MiB 576.34 KiB
c7e2fbc 1.72 MiB 2.29 MiB 576.40 KiB
93a76ca 1.72 MiB 2.29 MiB 576.75 KiB
86f0096 1.72 MiB 2.29 MiB 576.50 KiB

Previous results on branch: feat/pixel-copy-api-for-screenshots

Startup times

Revision Plain With Sentry Diff
74e9aa1 409.14 ms 494.29 ms 85.15 ms
30edd98 371.16 ms 489.10 ms 117.94 ms
65c0ceb 383.39 ms 442.74 ms 59.35 ms

App size

Revision Plain With Sentry Diff
74e9aa1 1.72 MiB 2.29 MiB 577.53 KiB
30edd98 1.72 MiB 2.29 MiB 576.85 KiB
65c0ceb 1.72 MiB 2.29 MiB 576.81 KiB

@codecov
Copy link

codecov bot commented Oct 24, 2023

Codecov Report

Attention: 8 lines in your changes are missing coverage. Please review.

Files Coverage Δ
...ry/android/core/internal/util/ScreenshotUtils.java 71.21% <80.48%> (+18.71%) ⬆️

📢 Thoughts on this report? Let us know!.

CHANGELOG.md Show resolved Hide resolved
@@ -38,13 +43,14 @@ public class ScreenshotUtils {

if (!isActivityValid(activity, buildInfoProvider)
|| activity.getWindow() == null
|| activity.getWindow().getDecorView() == null
|| activity.getWindow().getDecorView().getRootView() == null) {
|| activity.getWindow().peekDecorView() == null
Copy link
Member

Choose a reason for hiding this comment

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

m: I'm wondering if we should rather memoize this into a variable, because the next time we call it, it might've already changed? especially if we are not on the main thread

// Use Pixel Copy API on new devices, fallback to canvas rendering on older ones
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

final HandlerThread thread = new HandlerThread("screenshot");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
final HandlerThread thread = new HandlerThread("screenshot");
final HandlerThread thread = new HandlerThread("SentryScreenshot");

to make it more specific to our sdk


success =
latch.await(CAPTURE_TIMEOUT_MS, TimeUnit.MILLISECONDS) && copyResultSuccess.get();
} catch (InterruptedException e) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} catch (InterruptedException e) {
} catch (Throwable e) {

I'd catch any throwable just to make sure

success =
latch.await(CAPTURE_TIMEOUT_MS, TimeUnit.MILLISECONDS) && copyResultSuccess.get();
} catch (InterruptedException e) {
// ignored
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// ignored
logger.log(SentryLevel.ERROR, "Taking screenshot failed (view.draw).", e);

final @NotNull CountDownLatch latch = new CountDownLatch(1);

// Use Pixel Copy API on new devices, fallback to canvas rendering on older ones
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Copy link
Member

Choose a reason for hiding this comment

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

l: We could use BuildInfoProvider here and test it. You could take a look at SentryGestureListenerClickTest where we completely mock windows and decor views, and there's also ShadowPixelCopy of robolectric that would be helpful

Copy link
Member Author

Choose a reason for hiding this comment

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

Completely forget about that, I've added some test using ShadowPixelCopy

Copy link
Member

@romtsn romtsn left a comment

Choose a reason for hiding this comment

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

LGTM, just a few minor comments + potential way to test this

@markushi markushi merged commit 0404ea3 into main Oct 31, 2023
21 checks passed
@markushi markushi deleted the feat/pixel-copy-api-for-screenshots branch October 31, 2023 19:58
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.

attachScreenshot option can result in a blank UI on Android Use PixelCopy to create Screenshots
3 participants