12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424 |
- #if UNITY_ANDROID
-
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- using static System.Threading.Thread;
- using UnityEngine.Scripting;
- using UnityEngine.AdaptivePerformance.Provider;
-
- [assembly: AlwaysLinkAssembly]
- namespace UnityEngine.AdaptivePerformance.Samsung.Android
- {
- internal static class GameSDKLog
- {
- static SamsungAndroidProviderSettings settings = SamsungAndroidProviderSettings.GetSettings();
-
- [Conditional("DEVELOPMENT_BUILD")]
- public static void Debug(string format, params object[] args)
- {
- if (settings != null && settings.samsungProviderLogging)
- UnityEngine.Debug.Log(System.String.Format("[Samsung GameSDK] " + format, args));
- }
- }
-
- internal class AsyncUpdater : IDisposable
- {
- private Thread m_Thread;
- private bool m_Disposed = false;
- private bool m_Quit = false;
-
- private List<Action> m_UpdateAction = new List<Action>();
- private int[] m_UpdateRequests = null;
- private bool[] m_RequestComplete = null;
- private int m_UpdateRequestReadIndex = 0;
- private int m_UpdateRequestWriteIndex = 0;
-
- private object m_Mutex = new object();
- private Semaphore m_Semaphore = null;
-
- public int Register(Action action)
- {
- if (m_Thread.IsAlive)
- return -1;
-
- int handle = m_UpdateAction.Count;
- m_UpdateAction.Add(action);
-
- return handle;
- }
-
- public void Start()
- {
- int maxRequests = m_UpdateAction.Count;
- if (maxRequests <= 0)
- return;
-
- m_Semaphore = new Semaphore(0, maxRequests);
- m_UpdateRequests = new int[maxRequests];
- m_RequestComplete = new bool[maxRequests];
-
- m_Thread.Start();
- }
-
- public bool RequestUpdate(int handle)
- {
- lock (m_Mutex)
- {
- int newWriteIndex = (m_UpdateRequestWriteIndex + 1) % m_UpdateRequests.Length;
- if (newWriteIndex == m_UpdateRequestReadIndex)
- {
- return false;
- }
- m_UpdateRequests[m_UpdateRequestWriteIndex] = handle;
- m_RequestComplete[handle] = false;
- m_UpdateRequestWriteIndex = newWriteIndex;
- }
-
- m_Semaphore.Release();
-
- return true;
- }
-
- public bool IsRequestComplete(int handle)
- {
- lock (m_Mutex)
- {
- return m_RequestComplete[handle];
- }
- }
-
- public AsyncUpdater()
- {
- m_Thread = new Thread(new ThreadStart(ThreadProc));
- m_Thread.Name = "SamsungGameSDK";
- }
-
- private void ThreadProc()
- {
- AndroidJNI.AttachCurrentThread();
-
- while (true)
- {
- try
- {
- m_Semaphore.WaitOne();
- }
- catch (Exception)
- {
- break;
- }
-
- int handle = -1;
-
- lock (m_Mutex)
- {
- if (m_Quit)
- break;
-
- if (m_UpdateRequestReadIndex != m_UpdateRequestWriteIndex)
- {
- handle = m_UpdateRequests[m_UpdateRequestReadIndex];
- m_UpdateRequestReadIndex = (m_UpdateRequestReadIndex + 1) % m_UpdateRequests.Length;
- }
- }
-
- if (handle >= 0)
- {
- try
- {
- m_UpdateAction[handle].Invoke();
- }
- catch (Exception)
- {
- }
-
- lock (m_Mutex)
- {
- m_RequestComplete[handle] = true;
- }
- }
- }
-
- AndroidJNI.DetachCurrentThread();
- }
-
- private void Dispose(bool disposing)
- {
- if (m_Disposed)
- return;
-
- if (disposing)
- {
- if (m_Thread.IsAlive)
- {
- lock (m_Mutex)
- {
- m_Quit = true;
- }
-
- m_Semaphore.Release();
- m_Thread.Join();
- }
- }
- m_Disposed = true;
- }
-
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- }
-
- internal class AsyncValue<T>
- {
- private AsyncUpdater updater = null;
- private int updateHandle = -1;
- private bool pendingUpdate = false;
- private Func<T> updateFunc = null;
- private T newValue;
-
- private float updateTimeDeltaSeconds;
- private float updateTimestamp;
-
- public AsyncValue(AsyncUpdater updater, T value, float updateTimeDeltaSeconds, Func<T> updateFunc)
- {
- this.updater = updater;
- this.updateTimeDeltaSeconds = updateTimeDeltaSeconds;
- this.updateFunc = updateFunc;
- this.value = value;
- this.updateHandle = updater.Register(() => newValue = updateFunc());
- }
-
- public bool Update(float timestamp)
- {
- bool changed = false;
-
- if (pendingUpdate && updater.IsRequestComplete(updateHandle))
- {
- changed = !value.Equals(newValue);
- if (changed)
- changeTimestamp = timestamp;
-
- value = newValue;
- updateTimestamp = timestamp;
- pendingUpdate = false;
- }
-
- if (!pendingUpdate)
- {
- if (timestamp - updateTimestamp > updateTimeDeltaSeconds)
- {
- pendingUpdate = updater.RequestUpdate(updateHandle);
- }
- }
- return changed;
- }
-
- public void SyncUpdate(float timestamp)
- {
- var oldValue = value;
- updateTimestamp = timestamp;
- value = updateFunc();
- if (!value.Equals(oldValue))
- changeTimestamp = timestamp;
- }
-
- public T value { get; private set; }
- public float changeTimestamp { get; private set; }
- }
-
- [Preserve]
- public class SamsungGameSDKAdaptivePerformanceSubsystem : AdaptivePerformanceSubsystem, IApplicationLifecycle, IDevicePerformanceLevelControl
- {
- private NativeApi m_Api = null;
-
- private AsyncUpdater m_AsyncUpdater;
-
- private PerformanceDataRecord m_Data = new PerformanceDataRecord();
- private object m_DataLock = new object();
-
- private AsyncValue<double> m_SkinTemp = null;
- private AsyncValue<double> m_GPUTime = null;
-
- private Version m_Version = null;
-
- private float m_MinTempLevel = 0.0f;
- private float m_MaxTempLevel = 10.0f;
- bool m_PerformanceLevelControlSystemChange = false;
- bool m_AllowPerformanceLevelControlChanges = true;
-
- private AutoVariableRefreshRate m_AutoVariableRefreshRate;
-
- public override IApplicationLifecycle ApplicationLifecycle { get { return this; } }
- public override IDevicePerformanceLevelControl PerformanceLevelControl { get { return this; } }
-
- public int MaxCpuPerformanceLevel { get; set; }
- public int MaxGpuPerformanceLevel { get; set; }
-
- static SamsungAndroidProviderSettings settings = SamsungAndroidProviderSettings.GetSettings();
-
- /// <summary>
- /// InvalidOperation is the return value of an SDK API call when the feature is not available.
- /// </summary>
- /// <value>-999</value>
- const int k_InvalidOperation = -999;
-
- public SamsungGameSDKAdaptivePerformanceSubsystem()
- {
- MaxCpuPerformanceLevel = 3;
- MaxGpuPerformanceLevel = 3;
-
- m_Api = new NativeApi(OnPerformanceWarning, OnPerformanceLevelTimeout, () => (VariableRefreshRate.Instance as VRRManager)?.OnRefreshRateChanged(), OnCpuPerformanceBoostModeTimeout, OnGpuPerformanceBoostModeTimeout);
- m_AsyncUpdater = new AsyncUpdater();
- m_SkinTemp = new AsyncValue<double>(m_AsyncUpdater, -1.0, 2.7f, () => GetHighPrecisionSkinTempLevel());
- m_GPUTime = new AsyncValue<double>(m_AsyncUpdater, -1.0, 0.0f, () => m_Api.GetGpuFrameTime());
-
- Capabilities = Feature.CpuPerformanceLevel | Feature.GpuPerformanceLevel | Feature.PerformanceLevelControl | Feature.TemperatureLevel | Feature.WarningLevel | Feature.GpuFrameTime;
-
- m_AsyncUpdater.Start();
- }
-
- private void OnPerformanceWarning(WarningLevel warningLevel)
- {
- lock (m_DataLock)
- {
- m_Data.ChangeFlags |= Feature.WarningLevel;
- m_Data.ChangeFlags |= Feature.PerformanceLevelControl;
- m_Data.WarningLevel = warningLevel;
- }
- }
-
- private void OnPerformanceLevelTimeout()
- {
- lock (m_DataLock)
- {
- m_Data.ChangeFlags |= Feature.CpuPerformanceLevel;
- m_Data.ChangeFlags |= Feature.GpuPerformanceLevel;
- m_Data.CpuPerformanceLevel = Constants.UnknownPerformanceLevel;
- m_Data.GpuPerformanceLevel = Constants.UnknownPerformanceLevel;
- }
- }
-
- private void OnCpuPerformanceBoostModeTimeout()
- {
- lock (m_DataLock)
- {
- m_Data.ChangeFlags |= Feature.CpuPerformanceBoost;
- m_Data.CpuPerformanceBoost = false;
- }
- }
-
- private void OnGpuPerformanceBoostModeTimeout()
- {
- lock (m_DataLock)
- {
- m_Data.ChangeFlags |= Feature.GpuPerformanceBoost;
- m_Data.GpuPerformanceBoost = false;
- }
- }
-
- private float GetHighPrecisionSkinTempLevel()
- {
- return (float)m_Api.GetHighPrecisionSkinTempLevel();
- }
-
- private int GetClusterInfo()
- {
- return m_Api.GetClusterInfo();
- }
-
- private void ImmediateUpdateTemperature()
- {
- var timestamp = Time.time;
- m_SkinTemp.SyncUpdate(timestamp);
-
- lock (m_DataLock)
- {
- m_Data.ChangeFlags |= Feature.TemperatureLevel;
- m_Data.TemperatureLevel = GetTemperatureLevel();
- }
- }
-
- private static bool TryParseVersion(string versionString, out Version version)
- {
- try
- {
- version = new Version(versionString);
- }
- catch (Exception)
- {
- version = null;
- return false;
- }
- return true;
- }
-
- internal bool Initialize()
- {
- if (initialized)
- {
- return true;
- }
-
- if (!m_Api.Initialize())
- {
- return false;
- }
-
- if (TryParseVersion(m_Api.GetVersion(), out m_Version))
- {
- if (m_Version >= new Version(3, 5))
- {
- initialized = true;
- MaxCpuPerformanceLevel = m_Api.GetMaxCpuPerformanceLevel();
- MaxGpuPerformanceLevel = m_Api.GetMaxGpuPerformanceLevel();
- Capabilities |= Feature.CpuPerformanceBoost | Feature.GpuPerformanceBoost;
- }
- else if (m_Version >= new Version(3, 4))
- {
- initialized = true;
- MaxCpuPerformanceLevel = m_Api.GetMaxCpuPerformanceLevel();
- MaxGpuPerformanceLevel = m_Api.GetMaxGpuPerformanceLevel();
- }
- else if (m_Version >= new Version(3, 2))
- {
- initialized = true;
- MaxCpuPerformanceLevel = m_Api.GetMaxCpuPerformanceLevel();
- MaxGpuPerformanceLevel = m_Api.GetMaxGpuPerformanceLevel();
- }
- else
- {
- m_Api.Terminate();
- initialized = false;
- }
- }
-
- if (MaxCpuPerformanceLevel == k_InvalidOperation)
- {
- MaxCpuPerformanceLevel = Constants.UnknownPerformanceLevel;
- Capabilities &= ~Feature.CpuPerformanceLevel;
-
- m_AllowPerformanceLevelControlChanges = false;
- }
-
- if (MaxGpuPerformanceLevel == k_InvalidOperation)
- {
- MaxGpuPerformanceLevel = Constants.UnknownPerformanceLevel;
- Capabilities &= ~Feature.GpuPerformanceLevel;
-
- m_AllowPerformanceLevelControlChanges = false;
- }
-
- m_Data.PerformanceLevelControlAvailable = m_AllowPerformanceLevelControlChanges;
-
- return initialized;
- }
-
- public override void Start()
- {
- if (!initialized)
- {
- return;
- }
-
- ImmediateUpdateTemperature();
-
- Thread t = new Thread(CheckInitialTemperatureAndSendWarnings);
- t.Start();
-
- CheckAndInitializeVRR();
- }
-
- void CheckAndInitializeVRR()
- {
- if (m_Api.IsVariableRefreshRateSupported())
- {
- if (VariableRefreshRate.Instance == null)
- {
- VariableRefreshRate.Instance = new VRRManager(m_Api);
- m_AutoVariableRefreshRate = new AutoVariableRefreshRate(VariableRefreshRate.Instance);
- }
- }
- else
- {
- VariableRefreshRate.Instance = null;
- m_AutoVariableRefreshRate = null;
- }
- }
-
- void CheckInitialTemperatureAndSendWarnings()
- {
- // If the device is already warm upon startup and past the throttling imminent warning level
- // the warning callback is not called as it's not available yet. We need to set it manually based on temperature as workaround.
- // On startup the temperature reading is always 0. After a couple of seconds a true value is returned. Therefore we wait for 2 seconds before we make the reading.
- Sleep(TimeSpan.FromSeconds(2));
- float currentTempLevel = GetHighPrecisionSkinTempLevel();
-
- if (m_Version >= new Version(3, 2))
- {
- if (currentTempLevel >= 7)
- OnPerformanceWarning(WarningLevel.Throttling);
- else if (currentTempLevel >= 5)
- OnPerformanceWarning(WarningLevel.ThrottlingImminent);
- }
-
- if (m_Version >= new Version(3, 5))
- {
- // Cluster info is not available in the same frame as game sdk init so we need to wait a bit.
- int clusterInfo = m_Api.GetClusterInfo();
- if (clusterInfo != -999)
- {
- var aClusterInfo = new ClusterInfo();
- aClusterInfo.BigCore = clusterInfo / 100;
- aClusterInfo.MediumCore = (clusterInfo % 100) / 10;
- aClusterInfo.LittleCore = (clusterInfo % 100) % 10;
- lock (m_DataLock)
- {
- m_Data.ClusterInfo = aClusterInfo;
- m_Data.ChangeFlags |= Feature.ClusterInfo;
- }
- Capabilities |= Feature.ClusterInfo;
- }
- }
- }
-
- public override void Stop()
- {
- }
-
- protected override void OnDestroy()
- {
- VariableRefreshRate.Instance = null;
- m_AutoVariableRefreshRate = null;
-
- if (initialized)
- {
- m_Api.Terminate();
- initialized = false;
- }
-
- m_AsyncUpdater.Dispose();
- }
-
- public override string Stats => $"SkinTemp={m_SkinTemp?.value ?? -1} GPUTime={m_GPUTime?.value ?? -1}";
-
- public override PerformanceDataRecord Update()
- {
- // GameSDK API is very slow (~4ms per call), so update those numbers once per frame from another thread
-
- float timeSinceStartup = Time.time;
-
- m_GPUTime.Update(timeSinceStartup);
-
- bool tempChanged = m_SkinTemp.Update(timeSinceStartup);
-
- (VariableRefreshRate.Instance as VRRManager)?.Update();
-
- if ((VariableRefreshRate.Instance as VRRManager) != null && settings.automaticVRR)
- if (QualitySettings.vSyncCount == 0)
- m_AutoVariableRefreshRate.UpdateAutoVRR();
-
- if (m_PerformanceLevelControlSystemChange)
- {
- var temperatureLevel = (float)m_SkinTemp.value;
- if (temperatureLevel < 5)
- {
- lock (m_DataLock)
- {
- DisableSystemControl();
- }
- }
- }
-
- lock (m_DataLock)
- {
- if (tempChanged)
- {
- m_Data.ChangeFlags |= Feature.TemperatureLevel;
- m_Data.TemperatureLevel = GetTemperatureLevel();
- }
-
- m_Data.GpuFrameTime = LatestGpuFrameTime();
- m_Data.ChangeFlags |= Feature.GpuFrameTime;
-
- PerformanceDataRecord result = m_Data;
- m_Data.ChangeFlags = Feature.None;
-
- return result;
- }
- }
-
- public override Version Version
- {
- get
- {
- return m_Version;
- }
- }
-
- private static float NormalizeTemperatureLevel(float currentTempLevel, float minValue, float maxValue)
- {
- float tempLevel = -1.0f;
- if (currentTempLevel >= minValue && currentTempLevel <= maxValue)
- {
- tempLevel = currentTempLevel / maxValue;
- tempLevel = Math.Min(Math.Max(tempLevel, Constants.MinTemperatureLevel), maxValue);
- }
- return tempLevel;
- }
-
- private float NormalizeTemperatureLevel(float currentTempLevel)
- {
- return NormalizeTemperatureLevel(currentTempLevel, m_MinTempLevel, m_MaxTempLevel);
- }
-
- private float GetTemperatureLevel()
- {
- return NormalizeTemperatureLevel((float)m_SkinTemp.value);
- }
-
- private float LatestGpuFrameTime()
- {
- return (float)(m_GPUTime.value / 1000.0);
- }
-
- public bool SetPerformanceLevel(ref int cpuLevel, ref int gpuLevel)
- {
- if ((Capabilities & Feature.CpuPerformanceLevel) != Feature.CpuPerformanceLevel ||
- (Capabilities & Feature.GpuPerformanceLevel) != Feature.GpuPerformanceLevel)
- return false;
-
- if (cpuLevel < 0)
- cpuLevel = 0;
- else if (cpuLevel > MaxCpuPerformanceLevel)
- cpuLevel = MaxCpuPerformanceLevel;
-
- if (gpuLevel < 0)
- gpuLevel = 0;
- else if (gpuLevel > MaxGpuPerformanceLevel)
- gpuLevel = MaxGpuPerformanceLevel;
-
- if (m_Version == new Version(3, 2) && cpuLevel == 0)
- cpuLevel = 1;
-
- bool success = false;
-
- int result = m_Api.SetFreqLevels(cpuLevel, gpuLevel);
- success = result == 1;
-
- lock (m_DataLock)
- {
- var oldCpuLevel = m_Data.CpuPerformanceLevel;
- var oldGpuLevel = m_Data.GpuPerformanceLevel;
-
- m_Data.CpuPerformanceLevel = success ? cpuLevel : Constants.UnknownPerformanceLevel;
- m_Data.GpuPerformanceLevel = success ? gpuLevel : Constants.UnknownPerformanceLevel;
-
- if (success)
- {
- if (m_Data.CpuPerformanceLevel != oldCpuLevel)
- m_Data.ChangeFlags |= Feature.CpuPerformanceLevel;
- if (m_Data.GpuPerformanceLevel != oldGpuLevel)
- m_Data.ChangeFlags |= Feature.GpuPerformanceLevel;
- }
-
- if (result > 1)
- {
- if (result == 2)
- {
- GameSDKLog.Debug($"Thermal Mitigation Logic is working and CPU({cpuLevel})/GPU({gpuLevel}) level change request was not approved.");
- }
- else if (result == 3)
- {
- GameSDKLog.Debug($"CPU or GPU Boost mode is active and CPU({cpuLevel})/GPU({gpuLevel}) level change request was not approved.");
- }
-
- EnableSystemControl();
- }
- }
- return success;
- }
-
- public bool EnableCpuBoost()
- {
- var result = m_Api.EnableCpuBoost();
-
- lock (m_DataLock)
- {
- var oldPerformanceBoost = m_Data.CpuPerformanceBoost;
- m_Data.CpuPerformanceBoost = result;
- if (m_Data.CpuPerformanceBoost != oldPerformanceBoost)
- m_Data.ChangeFlags |= Feature.CpuPerformanceBoost;
-
- if (result)
- {
- EnableSystemControl();
- }
- }
- return result;
- }
-
- public bool EnableGpuBoost()
- {
- var result = m_Api.EnableGpuBoost();
-
- lock (m_DataLock)
- {
- var oldPerformanceBoost = m_Data.GpuPerformanceBoost;
- m_Data.GpuPerformanceBoost = result;
- if (m_Data.GpuPerformanceBoost != oldPerformanceBoost)
- m_Data.ChangeFlags |= Feature.GpuPerformanceBoost;
-
- if (result)
- {
- EnableSystemControl();
- }
- }
- return result;
- }
-
- public void ApplicationPause() {}
-
- public void ApplicationResume()
- {
- //We need to re-initialize because some Android onForegroundchange() APIs do not detect the change (e.g. bixby)
- if (!m_Api.Initialize())
- GameSDKLog.Debug("Resume: reinitialization failed!");
-
- if ((Capabilities & Feature.CpuPerformanceLevel) == Feature.CpuPerformanceLevel)
- {
- lock (m_DataLock)
- {
- m_Data.CpuPerformanceLevel = Constants.UnknownPerformanceLevel;
- m_Data.ChangeFlags |= Feature.CpuPerformanceLevel;
- }
- }
-
- if ((Capabilities & Feature.GpuPerformanceLevel) == Feature.GpuPerformanceLevel)
- {
- lock (m_DataLock)
- {
- m_Data.GpuPerformanceLevel = Constants.UnknownPerformanceLevel;
- m_Data.ChangeFlags |= Feature.GpuPerformanceLevel;
- }
- }
-
- ImmediateUpdateTemperature();
-
- CheckAndInitializeVRR();
-
- (VariableRefreshRate.Instance as VRRManager)?.Resume();
- }
-
- void EnableSystemControl()
- {
- if (!m_AllowPerformanceLevelControlChanges)
- return;
-
- m_Data.PerformanceLevelControlAvailable = false;
- m_Data.ChangeFlags |= Feature.PerformanceLevelControl;
- m_PerformanceLevelControlSystemChange = true;
- }
-
- void DisableSystemControl()
- {
- if (!m_AllowPerformanceLevelControlChanges)
- return;
-
- m_Data.PerformanceLevelControlAvailable = true;
- m_Data.ChangeFlags |= Feature.PerformanceLevelControl;
- m_PerformanceLevelControlSystemChange = false;
- }
-
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
- static void RegisterDescriptor()
- {
- if (!SystemInfo.deviceModel.StartsWith("samsung", StringComparison.OrdinalIgnoreCase))
- {
- GameSDKLog.Debug($"The device {SystemInfo.deviceModel} is not a supported Samsung phone. This provider will not run. Aborting registering the Adaptive Performance provider descriptor.");
- return;
- }
-
- if (!NativeApi.IsAvailable())
- {
- GameSDKLog.Debug($"The native API for this provider is not available. Aborting registering the Adaptive Performance provider descriptor.");
- return;
- }
-
- AdaptivePerformanceSubsystemDescriptor.RegisterDescriptor(new AdaptivePerformanceSubsystemDescriptor.Cinfo
- {
- id = "SamsungGameSDK",
- subsystemImplementationType = typeof(SamsungGameSDKAdaptivePerformanceSubsystem)
- });
- }
-
- internal class NativeApi : AndroidJavaProxy
- {
- static private AndroidJavaObject s_GameSDK = null;
- static private IntPtr s_GameSDKRawObjectID;
- static private IntPtr s_GetGpuFrameTimeID;
- static private IntPtr s_GetHighPrecisionSkinTempLevelID;
- static private IntPtr s_GetClusterInfolID;
-
- static private bool s_isAvailable = false;
- static private jvalue[] s_NoArgs = new jvalue[0];
-
- private Action<WarningLevel> PerformanceWarningEvent;
- private Action PerformanceLevelTimeoutEvent;
- private Action CpuPerformanceBoostReleasedByTimeoutEvent;
- private Action GpuPerformanceBoostReleasedByTimeoutEvent;
- private Action RefreshRateChangedEvent;
-
- public NativeApi(Action<WarningLevel> sustainedPerformanceWarning, Action sustainedPerformanceTimeout, Action refreshRateChanged, Action cpuPerformanceBoostReleasedByTimeout, Action gpuPerformanceBoostReleasedByTimeout)
- : base("com.samsung.android.gamesdk.GameSDKManager$Listener")
- {
- PerformanceWarningEvent = sustainedPerformanceWarning;
- PerformanceLevelTimeoutEvent = sustainedPerformanceTimeout;
- RefreshRateChangedEvent = refreshRateChanged;
- CpuPerformanceBoostReleasedByTimeoutEvent = cpuPerformanceBoostReleasedByTimeout;
- GpuPerformanceBoostReleasedByTimeoutEvent = gpuPerformanceBoostReleasedByTimeout;
- StaticInit();
- }
-
- [Preserve]
- void onHighTempWarning(int warningLevel)
- {
- GameSDKLog.Debug("Listener: onHighTempWarning(warningLevel={0})", warningLevel);
- if (warningLevel == 0)
- PerformanceWarningEvent(WarningLevel.NoWarning);
- else if (warningLevel == 1)
- PerformanceWarningEvent(WarningLevel.ThrottlingImminent);
- else if (warningLevel == 2)
- PerformanceWarningEvent(WarningLevel.Throttling);
- }
-
- [Preserve]
- void onReleasedByTimeout()
- {
- GameSDKLog.Debug("Listener: onReleasedByTimeout()");
- PerformanceLevelTimeoutEvent();
- }
-
- [Preserve]
- void onReleasedCpuBoost()
- {
- GameSDKLog.Debug("Listener: onReleasedCpuBoost()");
- CpuPerformanceBoostReleasedByTimeoutEvent();
- }
-
- [Preserve]
- void onReleasedGpuBoost()
- {
- GameSDKLog.Debug("Listener: onReleasedGPUBoost()");
- GpuPerformanceBoostReleasedByTimeoutEvent();
- }
-
- [Preserve]
- void onRefreshRateChanged()
- {
- GameSDKLog.Debug("Listener: onRefreshRateChanged()");
- RefreshRateChangedEvent();
- }
-
- static IntPtr GetJavaMethodID(IntPtr classId, string name, string sig)
- {
- AndroidJNI.ExceptionClear();
- var mid = AndroidJNI.GetMethodID(classId, name, sig);
-
- IntPtr ex = AndroidJNI.ExceptionOccurred();
- if (ex != (IntPtr)0)
- {
- AndroidJNI.ExceptionDescribe();
- AndroidJNI.ExceptionClear();
- return (IntPtr)0;
- }
- else
- {
- return mid;
- }
- }
-
- static private void StaticInit()
- {
- if (s_GameSDK == null)
- {
- try
- {
- s_GameSDK = new AndroidJavaObject("com.samsung.android.gamesdk.GameSDKManager");
- if (s_GameSDK != null)
- s_isAvailable = s_GameSDK.CallStatic<bool>("isAvailable");
- }
- catch (Exception ex)
- {
- GameSDKLog.Debug($"GameSDK is not available due to {ex} Aborting Adaptive Performance initialization.");
- s_isAvailable = false;
- s_GameSDK = null;
- }
-
- if (s_isAvailable)
- {
- if (!IsAPVersionSupported())
- {
- GameSDKLog.Debug($"GameSDK is the wrong version. Aborting Adaptive Performance Samsung Android initialization.");
- s_isAvailable = false;
- s_GameSDK = null;
- return;
- }
-
- s_GameSDKRawObjectID = s_GameSDK.GetRawObject();
- var classID = s_GameSDK.GetRawClass();
-
- s_GetGpuFrameTimeID = GetJavaMethodID(classID, "getGpuFrameTime", "()D");
- s_GetHighPrecisionSkinTempLevelID = GetJavaMethodID(classID, "getHighPrecisionSkinTempLevel", "()D");
- s_GetClusterInfolID = GetJavaMethodID(classID, "getClusterInfo", "()I");
-
- if (s_GetGpuFrameTimeID == (IntPtr)0 || s_GetHighPrecisionSkinTempLevelID == (IntPtr)0)
- s_isAvailable = false;
- }
- }
- }
-
- static public bool IsAvailable()
- {
- StaticInit();
- return s_isAvailable;
- }
-
- public bool RegisterListener()
- {
- bool success = false;
- try
- {
- success = s_GameSDK.Call<bool>("setListener", this);
- }
- catch (Exception)
- {
- success = false;
- }
-
- if (!success)
- GameSDKLog.Debug("failed to register listener");
-
- return success;
- }
-
- public void UnregisterListener()
- {
- bool success = true;
- try
- {
- GameSDKLog.Debug("setListener(null)");
- success = s_GameSDK.Call<bool>("setListener", (Object)null);
- }
- catch (Exception)
- {
- success = false;
- }
-
- if (!success)
- GameSDKLog.Debug("setListener(null) failed!");
- }
-
- static public bool IsAPVersionSupported()
- {
- try
- {
- Version initVersion;
- if (TryParseVersion(s_GameSDK.Call<string>("getVersion"), out initVersion))
- return initVersion >= new Version(3, 2);
- else
- return false;
- }
- catch (Exception)
- {
- GameSDKLog.Debug("[Exception] IsAPVersionSupported() failed!");
- }
- return false;
- }
-
- public bool Initialize()
- {
- bool isInitialized = false;
- try
- {
- Version initVersion;
- if (TryParseVersion(GetVersion(), out initVersion))
- {
- if (initVersion >= new Version(3, 2))
- {
- isInitialized = s_GameSDK.Call<bool>("initialize", initVersion.ToString());
- }
- else
- {
- GameSDKLog.Debug("GameSDK {0} is not supported and will not be initialized, Adaptive Performance will not be used.", initVersion);
- }
-
- if (isInitialized)
- {
- isInitialized = RegisterListener();
- }
- else
- {
- GameSDKLog.Debug("GameSDK.initialize() failed!");
- }
- }
- }
- catch (Exception)
- {
- GameSDKLog.Debug("[Exception] GameSDK.initialize() failed!");
- }
-
- return isInitialized;
- }
-
- public void Terminate()
- {
- UnregisterListener();
-
- try
- {
- var packageName = Application.identifier;
- GameSDKLog.Debug("GameSDK.finalize({0})", packageName);
- s_GameSDK.Call<bool>("finalize", packageName);
- }
- catch (Exception)
- {
- GameSDKLog.Debug("GameSDK.finalize() failed!");
- }
- }
-
- public string GetVersion()
- {
- string sdkVersion = "";
- try
- {
- sdkVersion = s_GameSDK.Call<string>("getVersion");
- }
- catch (Exception)
- {
- GameSDKLog.Debug("[Exception] GameSDK.getVersion() failed!");
- }
- return sdkVersion;
- }
-
- public double GetHighPrecisionSkinTempLevel()
- {
- double currentTempLevel = -1.0;
- try
- {
- currentTempLevel = AndroidJNI.CallDoubleMethod(s_GameSDKRawObjectID, s_GetHighPrecisionSkinTempLevelID, s_NoArgs);
- if (AndroidJNI.ExceptionOccurred() != IntPtr.Zero)
- {
- AndroidJNI.ExceptionDescribe();
- AndroidJNI.ExceptionClear();
- }
- }
- catch (Exception)
- {
- GameSDKLog.Debug("[Exception] GameSDK.getHighPrecisionSkinTempLevel() failed!");
- }
- return currentTempLevel;
- }
-
- public double GetGpuFrameTime()
- {
- double gpuFrameTime = -1.0;
- try
- {
- gpuFrameTime = AndroidJNI.CallDoubleMethod(s_GameSDKRawObjectID, s_GetGpuFrameTimeID, s_NoArgs);
- if (AndroidJNI.ExceptionOccurred() != IntPtr.Zero)
- {
- AndroidJNI.ExceptionDescribe();
- AndroidJNI.ExceptionClear();
- }
- }
- catch (Exception)
- {
- GameSDKLog.Debug("[Exception] GameSDK.getGpuFrameTime() failed!");
- }
-
- return gpuFrameTime;
- }
-
- public int SetFreqLevels(int cpu, int gpu)
- {
- int result = 0;
- try
- {
- result = s_GameSDK.Call<int>("setFreqLevels", cpu, gpu);
- GameSDKLog.Debug("setFreqLevels({0}, {1}) -> {2}", cpu, gpu, result);
- }
- catch (Exception x)
- {
- GameSDKLog.Debug("[Exception] GameSDK.setFreqLevels({0}, {1}) failed: {2}", cpu, gpu, x);
- }
- return result;
- }
-
- public bool EnableCpuBoost()
- {
- bool result = false;
- try
- {
- result = s_GameSDK.Call<bool>("setCpuBoostMode", 1);
- GameSDKLog.Debug("setCpuBoostMode(1) -> {0}", result);
- }
- catch (Exception x)
- {
- GameSDKLog.Debug("[Exception] GameSDK.setCpuBoostMode(1) failed: {0}", x);
- }
- return result;
- }
-
- public bool EnableGpuBoost()
- {
- bool result = false;
- try
- {
- result = s_GameSDK.Call<bool>("setGpuBoostMode", 1);
- GameSDKLog.Debug("setGpuBoostMode(1) -> {0}", result);
- }
- catch (Exception x)
- {
- GameSDKLog.Debug("[Exception] GameSDK.setGpuBoostMode(1) failed: {0}", x);
- }
- return result;
- }
-
- public int GetClusterInfo()
- {
- int result = -999;
- try
- {
- result = AndroidJNI.CallIntMethod(s_GameSDKRawObjectID, s_GetClusterInfolID, s_NoArgs);
- if (AndroidJNI.ExceptionOccurred() != IntPtr.Zero)
- {
- AndroidJNI.ExceptionDescribe();
- AndroidJNI.ExceptionClear();
- }
- GameSDKLog.Debug("getClusterInfo() -> {0}", result);
- }
- catch (Exception x)
- {
- GameSDKLog.Debug("[Exception] GameSDK.getClusterInfo() failed: {0}", x);
- }
- return result;
- }
-
- public int GetMaxCpuPerformanceLevel()
- {
- int maxCpuPerformanceLevel = Constants.UnknownPerformanceLevel;
- try
- {
- maxCpuPerformanceLevel = s_GameSDK.Call<int>("getCPULevelMax");
- }
- catch (Exception)
- {
- GameSDKLog.Debug("[Exception] GameSDK.getCPULevelMax() failed!");
- }
- return maxCpuPerformanceLevel;
- }
-
- public int GetMaxGpuPerformanceLevel()
- {
- int maxGpuPerformanceLevel = Constants.UnknownPerformanceLevel;
- try
- {
- maxGpuPerformanceLevel = s_GameSDK.Call<int>("getGPULevelMax");
- }
- catch (Exception)
- {
- GameSDKLog.Debug("[Exception] GameSDK.getGPULevelMax() failed!");
- }
- return maxGpuPerformanceLevel;
- }
-
- public bool IsVariableRefreshRateSupported()
- {
- bool vrrSupported = false;
- try
- {
- vrrSupported = s_GameSDK.Call<bool>("isGameSDKVariableRefreshRateSupported");
- GameSDKLog.Debug("isGameSDKVariableRefreshRateSupported->{0}", vrrSupported);
- }
- catch (Exception x)
- {
- GameSDKLog.Debug("[Exception] GameSDK.isGameSDKVariableRefreshRateSupported() failed: " + x.Message);
- }
-
- return vrrSupported;
- }
-
- public int[] GetSupportedRefreshRates()
- {
- int[] result = null;
- try
- {
- result = s_GameSDK.Call<int[]>("getSupportedRefreshRates");
- }
- catch (Exception x)
- {
- GameSDKLog.Debug("[Exception] GameSDK.getSupportedRefreshRates() failed: " + x.Message);
- }
-
- return result != null ? result : new int[0];
- }
-
- public bool SetRefreshRate(int targetRefreshRate)
- {
- try
- {
- s_GameSDK.Call("setRefreshRate", targetRefreshRate);
- }
- catch (Exception x)
- {
- GameSDKLog.Debug("[Exception] GameSDK.setRefreshRate() failed: " + x.Message);
- return false;
- }
- return true;
- }
-
- public bool ResetRefreshRate()
- {
- try
- {
- s_GameSDK.Call("resetRefreshRate");
- }
- catch (Exception x)
- {
- GameSDKLog.Debug("[Exception] GameSDK.resetRefreshRate() failed: " + x.Message);
- return false;
- }
- return true;
- }
-
- public int GetCurrentRefreshRate()
- {
- int result = -1;
- try
- {
- result = s_GameSDK.Call<int>("getCurrentRefreshRate");
- }
- catch (Exception x)
- {
- GameSDKLog.Debug("[Exception] GameSDK.getCurrentRefreshRate() failed: " + x.Message);
- }
- return result;
- }
- }
-
- [Preserve]
- internal class VRRManager : IVariableRefreshRate
- {
- NativeApi m_Api;
- object m_RefreshRateChangedLock = new object();
- bool m_RefreshRateChanged;
- int[] m_SupportedRefreshRates = new int[0];
- int m_CurrentRefreshRate = -1;
- int m_LastSetRefreshRate = -1;
-
- private void UpdateRefreshRateInfo()
- {
- var supportedRefreshRates = m_Api.GetSupportedRefreshRates();
- if (settings.highSpeedVRR)
- {
- m_SupportedRefreshRates = supportedRefreshRates;
- }
- else
- {
- List<int> shrunkSupportedRefreshRates = new List<int>();
- for (var i = 0; i < supportedRefreshRates.Length; ++i)
- {
- if (supportedRefreshRates[i] <= 60)
- shrunkSupportedRefreshRates.Add(supportedRefreshRates[i]);
- }
- m_SupportedRefreshRates = shrunkSupportedRefreshRates.ToArray();
- }
-
- m_CurrentRefreshRate = m_Api.GetCurrentRefreshRate();
- }
-
- public VRRManager(NativeApi api)
- {
- m_Api = api;
- SetDefaultVRR();
- UpdateRefreshRateInfo();
- }
-
- // If HighSpeedVRR is not enabled we should not set over 60hz by default
- private void SetDefaultVRR()
- {
- if (settings.highSpeedVRR)
- return;
-
- var index = Array.IndexOf(m_SupportedRefreshRates, 60);
-
- if (index != -1)
- {
- SetRefreshRateByIndexInternal(index);
- }
- }
-
- public void Resume()
- {
- bool changed = false;
-
- var oldSupportedRefreshRates = m_SupportedRefreshRates;
- var oldRefreshRate = m_LastSetRefreshRate;
-
- UpdateRefreshRateInfo();
-
- if (m_CurrentRefreshRate != oldRefreshRate)
- changed = true;
- else if (oldSupportedRefreshRates != m_SupportedRefreshRates)
- changed = true;
-
- if (changed)
- {
- lock (m_RefreshRateChangedLock)
- {
- m_RefreshRateChanged = true;
- }
- }
- }
-
- public void Update()
- {
- bool refreshRateChanged = false;
- lock (m_RefreshRateChangedLock)
- {
- refreshRateChanged = m_RefreshRateChanged;
- m_RefreshRateChanged = false;
- }
-
- if (refreshRateChanged)
- {
- UpdateRefreshRateInfo();
-
- var index = Array.IndexOf(m_SupportedRefreshRates, m_LastSetRefreshRate);
-
- if (index != -1)
- {
- SetRefreshRateByIndexInternal(index);
- }
- else if (index == -1 && m_LastSetRefreshRate != -1)
- {
- // Previous set refresh rate is not in available in the refreshrate list.
- // Need to set 60Hz or lowest refresh rate possible.
- // User sets 48Hz, but 48Hz is not on list anymore, because user changed Setting App - Display - Smooth option.
- index = Array.IndexOf(m_SupportedRefreshRates, 60);
-
- if (index != -1)
- SetRefreshRateByIndexInternal(index);
- }
- RefreshRateChanged?.Invoke();
- }
- }
-
- public int[] SupportedRefreshRates { get { return m_SupportedRefreshRates; } }
- public int CurrentRefreshRate { get { return m_CurrentRefreshRate; } }
-
- public bool SetRefreshRateByIndex(int index)
- {
- // Refreshrate potentially set by user
- settings.automaticVRR = false;
- return SetRefreshRateByIndexInternal(index);
- }
-
- private bool SetRefreshRateByIndexInternal(int index)
- {
- if (index >= 0 && index < SupportedRefreshRates.Length)
- {
- var refreshRateFromIndex = SupportedRefreshRates[index];
- if (Application.targetFrameRate > 0 && index > 0 && SupportedRefreshRates[--index] > Application.targetFrameRate)
- {
- GameSDKLog.Debug("SetRefreshRateByIndex tries to set the refreshRateTarget {0} way higher than the targetFrameRate {1} which is not recommended due to temperature increase and unused performance.", refreshRateFromIndex, Application.targetFrameRate);
- }
- if (!settings.highSpeedVRR)
- {
- if (refreshRateFromIndex > 60)
- {
- GameSDKLog.Debug("High-Speed VRR is not enabled in the settings. Setting a refreshrate ({0}Hz) over 60Hz is not permitted due to temperature reasons.", refreshRateFromIndex);
- return false;
- }
- }
- if (m_Api.SetRefreshRate(refreshRateFromIndex))
- {
- m_CurrentRefreshRate = refreshRateFromIndex;
- m_LastSetRefreshRate = refreshRateFromIndex;
- return true;
- }
- }
- return false;
- }
-
- public event VariableRefreshRateEventHandler RefreshRateChanged;
-
- public void OnRefreshRateChanged()
- {
- lock (m_RefreshRateChangedLock)
- {
- m_RefreshRateChanged = true;
- }
- }
- }
- internal class AutoVariableRefreshRate
- {
- SamsungAndroidProviderSettings settings = SamsungAndroidProviderSettings.GetSettings();
- IVariableRefreshRate vrrManager;
-
- public AutoVariableRefreshRate(IVariableRefreshRate vrrManagerInstance)
- {
- vrrManager = vrrManagerInstance;
- }
-
- // Temperature checks of hardware are around 5sec and we don't need to check that often.
- float VrrUpdateTime = 1;
- int lastRefreshRateIndex = -1;
-
- public void UpdateAutoVRR()
- {
- VrrUpdateTime -= Time.unscaledDeltaTime;
-
- if (VrrUpdateTime <= 0)
- {
- VrrUpdateTime = 1;
-
- // targetFPS = 70 (in 48/60/96/120)-> vrr 96 never 120
- // targetFPS = 40 (in 48/60/96/120)-> vrr 60 never 96
- // targetFPS = 48/60/96/120 (in 48/60/96/120) -> vrr 48/60/96/12 never higher
- // targetFPS = 70 (in 48/60)-> 60
- var refreshRateIndex = vrrManager.SupportedRefreshRates.Length - 1;
- // we look if a targetFrameRate is set, even in vsync mode were target framerate is ignored. Otherwise we use maximum framerate
- if (Application.targetFrameRate > 0)
- {
- for (int i = 0; i < vrrManager.SupportedRefreshRates.Length; ++i)
- {
- if (Application.targetFrameRate > vrrManager.SupportedRefreshRates[i])
- {
- continue;
- }
- else
- {
- refreshRateIndex = i;
- break;
- }
- }
- }
-
- if (lastRefreshRateIndex != refreshRateIndex)
- {
- lastRefreshRateIndex = refreshRateIndex;
- vrrManager.SetRefreshRateByIndex(refreshRateIndex);
- // automatic VRR gets disabled in SetRefreshRateByIndex and we want to ensure we still get updated.
- settings.automaticVRR = true;
- }
- }
- }
- }
- }
- }
- #endif
|