Skip to content

Commit

Permalink
fix: Screenshot quality (#939)
Browse files Browse the repository at this point in the history
* changed the resolution quality to be screen independent

* Updated CHANGELOG.md

* Format code

Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
  • Loading branch information
bitsandfoxes and getsentry-bot authored Aug 19, 2022
1 parent 89bf4c9 commit d68b107
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Fixes

- Fixed an 'Undefined symbols' issue within the Sentry Native Bridge when building for iOS ([#932](https://github.com/getsentry/sentry-unity/pull/932))
- ANR detection no longer creates an error by trying to capture a screenshot from a background thread ([#937](https://github.com/getsentry/sentry-unity/pull/937))
- Screenshots quality no longer scales off of current resolution but tries match thresholds instead ([#939](https://github.com/getsentry/sentry-unity/pull/939))

### Features

Expand All @@ -22,7 +24,6 @@
- iOS samples were missing the Objective-C plugin ([#921](https://github.com/getsentry/sentry-unity/pull/921))
- Save SampleRate to Options.asset ([#916](https://github.com/getsentry/sentry-unity/pull/916))
- Increase CLI file upload limit to 10 MiB ([#922](https://github.com/getsentry/sentry-unity/pull/922))
- ANR detection no longer creates an error by trying to capture a screenshot from a background thread ([#937](https://github.com/getsentry/sentry-unity/pull/937))

### Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ MonoBehaviour:
<EnvironmentOverride>k__BackingField:
<AttachStacktrace>k__BackingField: 0
<AttachScreenshot>k__BackingField: 1
<ScreenshotQuality>k__BackingField: 75
<ScreenshotQuality>k__BackingField: 2
<ScreenshotCompression>k__BackingField: 75
<MaxBreadcrumbs>k__BackingField: 100
<ReportAssembliesMode>k__BackingField: 1
Expand Down
6 changes: 3 additions & 3 deletions src/Sentry.Unity.Editor/ConfigurationWindow/EnrichmentTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ internal static void Display(ScriptableSentryUnityOptions options)
options.ScreenshotQuality = (ScreenshotQuality)EditorGUILayout.EnumPopup(
new GUIContent("Quality", "The resolution quality of the screenshot.\n" +
"'Full': Fully of the current resolution\n" +
"'High': Half of the current resolution\n" +
"'Medium': Third of the current resolution\n" +
"'Low': Quarter of the current resolution"),
"'High': 1080p\n" +
"'Medium': 720p\n" +
"'Low': 480p"),
options.ScreenshotQuality);

options.ScreenshotCompression = EditorGUILayout.IntSlider(
Expand Down
43 changes: 26 additions & 17 deletions src/Sentry.Unity/ScreenshotAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public ScreenshotAttachment(IAttachmentContent content)

internal class ScreenshotAttachmentContent : IAttachmentContent
{
private readonly int[] _resolutionModifiers = { 1, 2, 3, 4 }; // Full, half, third, quarter

private readonly SentryMonoBehaviour _behaviour;
private readonly SentryUnityOptions _options;

Expand All @@ -38,31 +36,42 @@ public Stream GetStream()
return new MemoryStream(CaptureScreenshot());
}

private int GetResolutionModifier(ScreenshotQuality quality)
private int GetTargetResolution(ScreenshotQuality quality)
{
var index = (int)quality;
if (index < _resolutionModifiers.Length)
return quality switch
{
return _resolutionModifiers[index];
}

return 1;
ScreenshotQuality.High => 1920, // 1080p
ScreenshotQuality.Medium => 1280, // 720p
ScreenshotQuality.Low => 854, // 480p
_ => 854 // Fallback
};
}

private byte[] CaptureScreenshot()
{
var resolutionModifier = GetResolutionModifier(_options.ScreenshotQuality);
var width = Screen.width;
var height = Screen.height;

// Make sure the screenshot size does not exceed MaxSize by scaling the image while conserving the
// Make sure the screenshot size does not exceed the target size by scaling the image while conserving the
// original ratio based on which, width or height, is the smaller
var targetWidth = Screen.width / resolutionModifier;
var targetHeight = Screen.height / resolutionModifier;
if (_options.ScreenshotQuality is not ScreenshotQuality.Full)
{
var targetResolution = GetTargetResolution(_options.ScreenshotQuality);
var ratioW = targetResolution / (float)width;
var ratioH = targetResolution / (float)height;
var ratio = Mathf.Min(ratioH, ratioW);
if (ratio is > 0.0f and < 1.0f)
{
width = Mathf.FloorToInt(width * ratio);
height = Mathf.FloorToInt(height * ratio);
}
}

// Captures the current screenshot synchronously.
var screenshot = new Texture2D(targetWidth, targetHeight, TextureFormat.RGB24, false);
var screenshot = new Texture2D(width, height, TextureFormat.RGB24, false);
var rtFull = RenderTexture.GetTemporary(Screen.width, Screen.height);
ScreenCapture.CaptureScreenshotIntoRenderTexture(rtFull);
var rtResized = RenderTexture.GetTemporary(targetWidth, targetHeight);
var rtResized = RenderTexture.GetTemporary(width, height);
// On all (currently supported) platforms except Android, the image is mirrored horizontally & vertically.
// So we must mirror it back.
if (ApplicationAdapter.Instance.Platform is (RuntimePlatform.Android or RuntimePlatform.LinuxPlayer))
Expand All @@ -80,7 +89,7 @@ private byte[] CaptureScreenshot()
try
{
// actually copy from the current render target a texture & read data from the active RenderTexture
screenshot.ReadPixels(new Rect(0, 0, targetWidth, targetHeight), 0, 0);
screenshot.ReadPixels(new Rect(0, 0, width, height), 0, 0);
screenshot.Apply();
}
finally
Expand All @@ -91,7 +100,7 @@ private byte[] CaptureScreenshot()

var bytes = screenshot.EncodeToJPG(_options.ScreenshotCompression);
_options.DiagnosticLogger?.Log(SentryLevel.Debug,
"Screenshot captured at {0}x{1}: {2} bytes", null, targetWidth, targetHeight, bytes.Length);
"Screenshot captured at {0}x{1}: {2} bytes", null, width, height, bytes.Length);
return bytes;
}
}
Expand Down

0 comments on commit d68b107

Please sign in to comment.