Skip to content

Commit

Permalink
chore: Replace screenshot options with internal defaults (#784)
Browse files Browse the repository at this point in the history
* removed additional screenshot options

* internal maxsize for screenshots

* Format code

* updated CHANGELOG.md

* Update CHANGELOG.md

* fixed scriptableoptions test

Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
  • Loading branch information
bitsandfoxes and getsentry-bot authored Jun 23, 2022
1 parent 50658f5 commit c67be26
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 70 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Launch a setup wizard after installation ([#780](https://github.com/getsentry/sentry-unity/pull/780))
- Reduced automated screenshot attachment controls to a simple toggle ([#784](https://github.com/getsentry/sentry-unity/pull/784))
- Disable AutoSessionTracking on unknown platforms ([#840](https://github.com/getsentry/sentry-unity/pull/840))
- Support Android apps minified with Proguard ([#844](https://github.com/getsentry/sentry-unity/pull/844))
- Bump Cocoa SDK to v7.17.0 ([#802](https://github.com/getsentry/sentry-unity/pull/802) & [#821](https://github.com/getsentry/sentry-unity/pull/821))
Expand Down
26 changes: 6 additions & 20 deletions src/Sentry.Unity.Editor/ConfigurationWindow/EnrichmentTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,12 @@ internal static void Display(ScriptableSentryUnityOptions options)
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();

{
options.AttachScreenshot = EditorGUILayout.BeginToggleGroup(
new GUIContent("Attach Screenshot", "Try to attach current screenshot on events.\n" +
"This is an early-access feature and may not work on all platforms (it is explicitly disabled on WebGL).\n" +
"Additionally, the screenshot is captured mid-frame, when an event happens, so it may be incomplete.\n" +
"A screenshot might not be able to be attached, for example when the error happens on a background thread."),
options.AttachScreenshot);
options.ScreenshotMaxWidth = EditorGUILayout.IntField(
new GUIContent("Max Width", "Maximum width of the screenshot or 0 to keep the original size. " +
"If the application window is larger, the screenshot will be resized proportionally."),
options.ScreenshotMaxWidth);
options.ScreenshotMaxHeight = EditorGUILayout.IntField(
new GUIContent("Max Height", "Maximum height of the screenshot or 0 to keep the original size. " +
"If the application window is larger, the screenshot will be resized proportionally."),
options.ScreenshotMaxHeight);
options.ScreenshotQuality = EditorGUILayout.IntSlider(
new GUIContent("JPG quality", "Quality of the JPG screenshot: 0 - 100, where 100 is the best quality and highest size."),
options.ScreenshotQuality, 0, 100);
EditorGUILayout.EndToggleGroup();
}
options.AttachScreenshot = EditorGUILayout.Toggle(
new GUIContent("Attach Screenshot", "Try to attach current screenshot on events.\n" +
"This is an early-access feature and may not work on all platforms (it is explicitly disabled on WebGL).\n" +
"Additionally, the screenshot is captured mid-frame, when an event happens, so it may be incomplete.\n" +
"A screenshot might not be able to be attached, for example when the error happens on a background thread."),
options.AttachScreenshot);
}
}
}
3 changes: 0 additions & 3 deletions src/Sentry.Unity.Editor/ScriptableSentryUnityOptionsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public override void OnInspectorGUI()
EditorGUILayout.TextField("Environment Override", options.EnvironmentOverride);
EditorGUILayout.Toggle("Attach Stacktrace", options.AttachStacktrace);
EditorGUILayout.Toggle("Attach Screenshot", options.AttachScreenshot);
EditorGUILayout.IntField("Screenshot Max Height", options.ScreenshotMaxHeight);
EditorGUILayout.IntField("Screenshot Max Width", options.ScreenshotMaxWidth);
EditorGUILayout.IntField("Screenshot Quality", options.ScreenshotQuality);
EditorGUILayout.IntField("Max Breadcrumbs", options.MaxBreadcrumbs);
EditorGUILayout.EnumPopup("Report Assemblies Mode", options.ReportAssembliesMode);
EditorGUILayout.Toggle("Send Default Pii", options.SendDefaultPii);
Expand Down
23 changes: 11 additions & 12 deletions src/Sentry.Unity/ScreenshotAttachment.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.IO;
using Sentry;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using UnityEngine;
using UnityEngine.Rendering;

namespace Sentry.Unity
{
Expand All @@ -16,6 +14,9 @@ public ScreenshotAttachment(IAttachmentContent content)

internal class ScreenshotAttachmentContent : IAttachmentContent
{
private const int Quality = 75;
private const int MaxSize = 1920;

private readonly SentryMonoBehaviour _behaviour;
private readonly SentryUnityOptions _options;

Expand All @@ -42,17 +43,17 @@ public Stream GetStream()

private byte[] CaptureScreenshot()
{
// Calculate the desired size by calculating the ratio between the desired height/width and the actual one,
// and than resizing based on the smaller of the two ratios.
// Make sure the screenshot size does not exceed MaxSize by scaling the image while conserving the
// original ratio based on which, width or height, is the smaller
var width = Screen.width;
var height = Screen.height;
var ratioW = _options.ScreenshotMaxWidth <= 0 ? 1.0f : (float)_options.ScreenshotMaxWidth / (float)width;
var ratioH = _options.ScreenshotMaxHeight <= 0 ? 1.0f : (float)_options.ScreenshotMaxHeight / (float)height;
var ratioW = width <= MaxSize ? 1.0f : MaxSize / (float)width;
var ratioH = height <= MaxSize ? 1.0f : MaxSize / (float)height;
var ratio = Mathf.Min(ratioH, ratioW);
if (ratio > 0.0f && ratio < 1.0f)
if (ratio is > 0.0f and < 1.0f)
{
width = Mathf.FloorToInt((float)width * ratio);
height = Mathf.FloorToInt((float)height * ratio);
width = Mathf.FloorToInt(width * ratio);
height = Mathf.FloorToInt(height * ratio);
}

// Captures the current screenshot synchronously.
Expand All @@ -71,11 +72,9 @@ private byte[] CaptureScreenshot()
Graphics.Blit(rtFull, rtResized, new Vector2(1, -1), new Vector2(0, 1));
}
RenderTexture.ReleaseTemporary(rtFull);

// Remember the previous render target and change it to our target texture.
var previousRT = RenderTexture.active;
RenderTexture.active = rtResized;

try
{
// actually copy from the current render target a texture & read data from the active RenderTexture
Expand All @@ -88,7 +87,7 @@ private byte[] CaptureScreenshot()
RenderTexture.active = previousRT;
}

var bytes = screenshot.EncodeToJPG(_options.ScreenshotQuality);
var bytes = screenshot.EncodeToJPG(Quality);
_options.DiagnosticLogger?.Log(SentryLevel.Debug,
"Screenshot captured at {0}x{1}: {0} bytes", null, width, height, bytes.Length);
return bytes;
Expand Down
6 changes: 0 additions & 6 deletions src/Sentry.Unity/ScriptableSentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public static string GetConfigPath(string? notDefaultConfigName = null)
[field: SerializeField] public string EnvironmentOverride { get; set; } = string.Empty;
[field: SerializeField] public bool AttachStacktrace { get; set; }
[field: SerializeField] public bool AttachScreenshot { get; set; }
[field: SerializeField] public int ScreenshotMaxWidth { get; set; }
[field: SerializeField] public int ScreenshotMaxHeight { get; set; }
[field: SerializeField] public int ScreenshotQuality { get; set; } = 75;
[field: SerializeField] public int MaxBreadcrumbs { get; set; } = Constants.DefaultMaxBreadcrumbs;

[field: SerializeField] public ReportAssembliesMode ReportAssembliesMode { get; set; } = ReportAssembliesMode.Version;
Expand Down Expand Up @@ -104,9 +101,6 @@ internal SentryUnityOptions ToSentryUnityOptions(bool isBuilding, ISentryUnityIn
AutoSessionTrackingInterval = TimeSpan.FromMilliseconds(AutoSessionTrackingInterval),
AttachStacktrace = AttachStacktrace,
AttachScreenshot = AttachScreenshot,
ScreenshotMaxWidth = ScreenshotMaxWidth,
ScreenshotMaxHeight = ScreenshotMaxHeight,
ScreenshotQuality = ScreenshotQuality,
MaxBreadcrumbs = MaxBreadcrumbs,
ReportAssembliesMode = ReportAssembliesMode,
SendDefaultPii = SendDefaultPii,
Expand Down
17 changes: 0 additions & 17 deletions src/Sentry.Unity/SentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,6 @@ public sealed class SentryUnityOptions : SentryOptions
/// </summary>
public bool AttachScreenshot { get; set; } = false;

/// <summary>
/// Maximum width of the screenshot or 0 to keep the original size.
/// If the application window is larger, the screenshot will be resized proportionally.
/// </summary>
public int ScreenshotMaxWidth { get; set; } = 0;

/// <summary>
/// Maximum height of the screenshot or 0 to keep the original size.
/// If the application window is larger, the screenshot will be resized proportionally.
/// </summary>
public int ScreenshotMaxHeight { get; set; } = 0;

/// <summary>
/// Quality of the JPG screenshot: 0 - 100, where 100 is the best quality and highest size.
/// </summary>
public int ScreenshotQuality { get; set; } = 75;

/// <summary>
/// Whether the SDK should add native support for iOS
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public void ScriptableSentryUnityOptions_Creation_AllPropertiesPresent()
StringAssert.Contains("EnvironmentOverride", optionsAsString);
StringAssert.Contains("AttachStacktrace", optionsAsString);
StringAssert.Contains("AttachScreenshot", optionsAsString);
StringAssert.Contains("ScreenshotMaxWidth", optionsAsString);
StringAssert.Contains("ScreenshotMaxHeight", optionsAsString);
StringAssert.Contains("ScreenshotQuality", optionsAsString);
StringAssert.Contains("MaxBreadcrumbs", optionsAsString);
StringAssert.Contains("ReportAssembliesMode", optionsAsString);
StringAssert.Contains("SendDefaultPii", optionsAsString);
Expand Down
9 changes: 0 additions & 9 deletions test/Sentry.Unity.Tests/ScriptableSentryUnityOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ public void ToSentryUnityOptions_ValueMapping_AreEqual(bool isBuilding, bool ena
AutoSessionTrackingInterval = TimeSpan.FromSeconds(1),
AttachStacktrace = true,
AttachScreenshot = true,
ScreenshotMaxWidth = 1,
ScreenshotMaxHeight = 1,
ScreenshotQuality = 1,
MaxBreadcrumbs = 1,
ReportAssembliesMode = ReportAssembliesMode.None,
SendDefaultPii = true,
Expand All @@ -96,9 +93,6 @@ public void ToSentryUnityOptions_ValueMapping_AreEqual(bool isBuilding, bool ena
scriptableOptions.AutoSessionTrackingInterval = (int)expectedOptions.AutoSessionTrackingInterval.TotalMilliseconds;
scriptableOptions.AttachStacktrace = expectedOptions.AttachStacktrace;
scriptableOptions.AttachScreenshot = expectedOptions.AttachScreenshot;
scriptableOptions.ScreenshotMaxWidth = expectedOptions.ScreenshotMaxWidth;
scriptableOptions.ScreenshotMaxHeight = expectedOptions.ScreenshotMaxHeight;
scriptableOptions.ScreenshotQuality = expectedOptions.ScreenshotQuality;
scriptableOptions.MaxBreadcrumbs = expectedOptions.MaxBreadcrumbs;
scriptableOptions.ReportAssembliesMode = expectedOptions.ReportAssembliesMode;
scriptableOptions.SendDefaultPii = expectedOptions.SendDefaultPii;
Expand Down Expand Up @@ -159,9 +153,6 @@ public static void AssertOptions(SentryUnityOptions expected, SentryUnityOptions
Assert.AreEqual(expected.AutoSessionTrackingInterval, actual.AutoSessionTrackingInterval);
Assert.AreEqual(expected.AttachStacktrace, actual.AttachStacktrace);
Assert.AreEqual(expected.AttachScreenshot, actual.AttachScreenshot);
Assert.AreEqual(expected.ScreenshotMaxWidth, actual.ScreenshotMaxWidth);
Assert.AreEqual(expected.ScreenshotMaxHeight, actual.ScreenshotMaxHeight);
Assert.AreEqual(expected.ScreenshotQuality, actual.ScreenshotQuality);
Assert.AreEqual(expected.MaxBreadcrumbs, actual.MaxBreadcrumbs);
Assert.AreEqual(expected.ReportAssembliesMode, actual.ReportAssembliesMode);
Assert.AreEqual(expected.SendDefaultPii, actual.SendDefaultPii);
Expand Down

0 comments on commit c67be26

Please sign in to comment.