Skip to content

Commit

Permalink
feat: Android (native) context sync
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Jun 7, 2022
1 parent d05b969 commit be6c3c9
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 75 deletions.
29 changes: 24 additions & 5 deletions src/Sentry.Unity.Android/NativeContextWriter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using CWUtil = Sentry.Unity.NativeUtils.ContextWriter;

namespace Sentry.Unity.Android
{
Expand Down Expand Up @@ -62,6 +58,29 @@ protected override void WriteScope(
GpuVendorId,
GpuMultiThreadedRendering,
GpuGraphicsShaderLevel);

CWUtil.WriteGpu(
GpuId,
GpuName,
GpuVendorName,
GpuMemorySize,
GpuNpotSupport,
GpuVersion,
GpuApiType,
GpuMaxTextureSize,
GpuSupportsDrawCallInstancing,
GpuSupportsRayTracing,
GpuSupportsComputeShaders,
GpuSupportsGeometryShaders,
GpuVendorId,
GpuMultiThreadedRendering,
GpuGraphicsShaderLevel);

CWUtil.WriteUnity(
UnityInstallMode,
UnityTargetFrameRate,
UnityCopyTextureSupport,
UnityRenderingThreadingMode);
}
}
}
98 changes: 37 additions & 61 deletions src/Sentry.Unity.Native/NativeContextWriter.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using CWUtil = Sentry.Unity.NativeUtils.ContextWriter;

namespace Sentry.Unity.Native
{
/// <summary>
/// P/Invoke to `sentry-native` functions.
/// </summary>
/// <see href="https://github.com/getsentry/sentry-native"/>
internal class NativeContextWriter : ContextWriter
{
protected override void WriteScope(
Expand Down Expand Up @@ -47,62 +39,46 @@ protected override void WriteScope(
string? UnityRenderingThreadingMode
)
{
{ // App
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "app_start_time", AppStartTime);
C.setValueIfNotNull(obj, "build_type", AppBuildType);
C.sentry_set_context(Sentry.Protocol.App.Type, obj);
}
CWUtil.WriteApp(AppStartTime, AppBuildType);

{ // OperatingSystem
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "raw_description", OperatingSystemRawDescription);
C.sentry_set_context(Sentry.Protocol.OperatingSystem.Type, obj);
}
CWUtil.WriteOS(OperatingSystemRawDescription);

{ // Device
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "processor_count", DeviceProcessorCount);
C.setValueIfNotNull(obj, "cpu_description", DeviceCpuDescription);
C.setValueIfNotNull(obj, "timezone", DeviceTimezone);
C.setValueIfNotNull(obj, "supports_vibration", DeviceSupportsVibration);
C.setValueIfNotNull(obj, "name", DeviceName);
C.setValueIfNotNull(obj, "simulator", DeviceSimulator);
C.setValueIfNotNull(obj, "device_unique_identifier", DeviceDeviceUniqueIdentifier);
C.setValueIfNotNull(obj, "device_type", DeviceDeviceType);
C.setValueIfNotNull(obj, "model", DeviceModel);
C.setValueIfNotNull(obj, "memory_size", DeviceMemorySize);
C.sentry_set_context(Sentry.Protocol.Device.Type, obj);
}
CWUtil.WriteDevice(
DeviceProcessorCount,
DeviceCpuDescription,
DeviceTimezone,
DeviceSupportsVibration,
DeviceName,
DeviceSimulator,
DeviceDeviceUniqueIdentifier,
DeviceDeviceType,
DeviceModel,
DeviceMemorySize
);

{ // GPU
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "id", GpuId);
C.setValueIfNotNull(obj, "name", GpuName);
C.setValueIfNotNull(obj, "vendor_name", GpuVendorName);
C.setValueIfNotNull(obj, "memory_size", GpuMemorySize);
C.setValueIfNotNull(obj, "npot_support", GpuNpotSupport);
C.setValueIfNotNull(obj, "version", GpuVersion);
C.setValueIfNotNull(obj, "api_type", GpuApiType);
C.setValueIfNotNull(obj, "max_texture_size", GpuMaxTextureSize);
C.setValueIfNotNull(obj, "supports_draw_call_instancing", GpuSupportsDrawCallInstancing);
C.setValueIfNotNull(obj, "supports_ray_tracing", GpuSupportsRayTracing);
C.setValueIfNotNull(obj, "supports_compute_shaders", GpuSupportsComputeShaders);
C.setValueIfNotNull(obj, "supports_geometry_shaders", GpuSupportsGeometryShaders);
C.setValueIfNotNull(obj, "vendor_id", GpuVendorId);
C.setValueIfNotNull(obj, "multi_threaded_rendering", GpuMultiThreadedRendering);
C.setValueIfNotNull(obj, "graphics_shader_level", GpuGraphicsShaderLevel);
C.sentry_set_context(Sentry.Protocol.Gpu.Type, obj);
}
CWUtil.WriteGpu(
GpuId,
GpuName,
GpuVendorName,
GpuMemorySize,
GpuNpotSupport,
GpuVersion,
GpuApiType,
GpuMaxTextureSize,
GpuSupportsDrawCallInstancing,
GpuSupportsRayTracing,
GpuSupportsComputeShaders,
GpuSupportsGeometryShaders,
GpuVendorId,
GpuMultiThreadedRendering,
GpuGraphicsShaderLevel);

{ // Unity
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "install_mode", UnityInstallMode);
C.setValueIfNotNull(obj, "target_frame_rate", UnityTargetFrameRate);
C.setValueIfNotNull(obj, "copy_texture_support", UnityCopyTextureSupport);
C.setValueIfNotNull(obj, "rendering_threading_mode", UnityRenderingThreadingMode);
C.sentry_set_context(Sentry.Unity.Protocol.Unity.Type, obj);
}
CWUtil.WriteUnity(
UnityInstallMode,
UnityTargetFrameRate,
UnityCopyTextureSupport,
UnityRenderingThreadingMode);
}

}
}
1 change: 1 addition & 0 deletions src/Sentry.Unity.Native/NativeScopeObserver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using Sentry.Extensibility;
using C = Sentry.Unity.NativeUtils.C;

namespace Sentry.Unity.Native
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Sentry.Extensibility;
using Sentry.Unity.Integrations;

namespace Sentry.Unity.Native
namespace Sentry.Unity.NativeUtils
{
internal static class C
{
internal static void setValueIfNotNull(sentry_value_t obj, string key, string? value)
{
if (value is not null)
{
sentry_value_set_by_key(obj, key, sentry_value_new_string(value));
_ = sentry_value_set_by_key(obj, key, sentry_value_new_string(value));
}
}

internal static void setValueIfNotNull(sentry_value_t obj, string key, int? value)
{
if (value.HasValue)
{
sentry_value_set_by_key(obj, key, sentry_value_new_int32(value.Value));
_ = sentry_value_set_by_key(obj, key, sentry_value_new_int32(value.Value));
}
}

internal static void setValueIfNotNull(sentry_value_t obj, string key, bool? value)
{
if (value.HasValue)
{
sentry_value_set_by_key(obj, key, sentry_value_new_bool(value.Value ? 1 : 0));
_ = sentry_value_set_by_key(obj, key, sentry_value_new_bool(value.Value ? 1 : 0));
}
}

internal static void setValueIfNotNull(sentry_value_t obj, string key, double? value)
{
if (value.HasValue)
{
sentry_value_set_by_key(obj, key, sentry_value_new_double(value.Value));
_ = sentry_value_set_by_key(obj, key, sentry_value_new_double(value.Value));
}
}

Expand Down
96 changes: 96 additions & 0 deletions src/Sentry.Unity/NativeUtils/ContextWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
namespace Sentry.Unity.NativeUtils
{
internal static class ContextWriter
{
internal static void WriteApp(string? AppStartTime, string? AppBuildType)
{
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "app_start_time", AppStartTime);
C.setValueIfNotNull(obj, "build_type", AppBuildType);
C.sentry_set_context(Sentry.Protocol.App.Type, obj);
}

internal static void WriteOS(string? OperatingSystemRawDescription)
{
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "raw_description", OperatingSystemRawDescription);
C.sentry_set_context(Sentry.Protocol.OperatingSystem.Type, obj);
}

internal static void WriteDevice(
int? DeviceProcessorCount,
string? DeviceCpuDescription,
string? DeviceTimezone,
bool? DeviceSupportsVibration,
string? DeviceName,
bool? DeviceSimulator,
string? DeviceDeviceUniqueIdentifier,
string? DeviceDeviceType,
string? DeviceModel,
long? DeviceMemorySize)
{
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "processor_count", DeviceProcessorCount);
C.setValueIfNotNull(obj, "cpu_description", DeviceCpuDescription);
C.setValueIfNotNull(obj, "timezone", DeviceTimezone);
C.setValueIfNotNull(obj, "supports_vibration", DeviceSupportsVibration);
C.setValueIfNotNull(obj, "name", DeviceName);
C.setValueIfNotNull(obj, "simulator", DeviceSimulator);
C.setValueIfNotNull(obj, "device_unique_identifier", DeviceDeviceUniqueIdentifier);
C.setValueIfNotNull(obj, "device_type", DeviceDeviceType);
C.setValueIfNotNull(obj, "model", DeviceModel);
C.setValueIfNotNull(obj, "memory_size", DeviceMemorySize);
C.sentry_set_context(Sentry.Protocol.Device.Type, obj);
}

internal static void WriteGpu(
int? GpuId,
string? GpuName,
string? GpuVendorName,
int? GpuMemorySize,
string? GpuNpotSupport,
string? GpuVersion,
string? GpuApiType,
int? GpuMaxTextureSize,
bool? GpuSupportsDrawCallInstancing,
bool? GpuSupportsRayTracing,
bool? GpuSupportsComputeShaders,
bool? GpuSupportsGeometryShaders,
string? GpuVendorId,
bool? GpuMultiThreadedRendering,
string? GpuGraphicsShaderLevel)
{
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "id", GpuId);
C.setValueIfNotNull(obj, "name", GpuName);
C.setValueIfNotNull(obj, "vendor_name", GpuVendorName);
C.setValueIfNotNull(obj, "memory_size", GpuMemorySize);
C.setValueIfNotNull(obj, "npot_support", GpuNpotSupport);
C.setValueIfNotNull(obj, "version", GpuVersion);
C.setValueIfNotNull(obj, "api_type", GpuApiType);
C.setValueIfNotNull(obj, "max_texture_size", GpuMaxTextureSize);
C.setValueIfNotNull(obj, "supports_draw_call_instancing", GpuSupportsDrawCallInstancing);
C.setValueIfNotNull(obj, "supports_ray_tracing", GpuSupportsRayTracing);
C.setValueIfNotNull(obj, "supports_compute_shaders", GpuSupportsComputeShaders);
C.setValueIfNotNull(obj, "supports_geometry_shaders", GpuSupportsGeometryShaders);
C.setValueIfNotNull(obj, "vendor_id", GpuVendorId);
C.setValueIfNotNull(obj, "multi_threaded_rendering", GpuMultiThreadedRendering);
C.setValueIfNotNull(obj, "graphics_shader_level", GpuGraphicsShaderLevel);
C.sentry_set_context(Sentry.Protocol.Gpu.Type, obj);
}

internal static void WriteUnity(
string? UnityInstallMode,
string? UnityTargetFrameRate,
string? UnityCopyTextureSupport,
string? UnityRenderingThreadingMode)
{
var obj = C.sentry_value_new_object();
C.setValueIfNotNull(obj, "install_mode", UnityInstallMode);
C.setValueIfNotNull(obj, "target_frame_rate", UnityTargetFrameRate);
C.setValueIfNotNull(obj, "copy_texture_support", UnityCopyTextureSupport);
C.setValueIfNotNull(obj, "rendering_threading_mode", UnityRenderingThreadingMode);
C.sentry_set_context(Sentry.Unity.Protocol.Unity.Type, obj);
}
}
}

0 comments on commit be6c3c9

Please sign in to comment.