Browse Source

* performance.now interface

Michaël Van Canneyt 2 months ago
parent
commit
163f97b84e

+ 4 - 0
packages/wasm-utils/src/wasm.timer.api.pas

@@ -28,6 +28,8 @@ uses
 
 Type
   TWasmTimerTickEvent = Procedure (aTimerID : TWasmTimerID; userdata : pointer; var aContinue : Boolean);
+  TDomHighResolutionTimeStamp = Double;
+  PDomHighResolutionTimeStamp = ^TDomHighResolutionTimeStamp;
 
 function __wasm_timer_allocate(ainterval : longint; userdata: pointer) : TWasmTimerID; external TimerExportName name TimerFN_allocate;
 
@@ -35,6 +37,8 @@ procedure __wasm_timer_deallocate(timerid: TWasmTimerID); external TimerExportNa
 
 function __wasm_timer_tick(timerid: TWasmTimerID; userdata : pointer) : boolean;
 
+function __wasm_timer_performance_now(aNow : PDomHighResolutionTimeStamp) : Longint; external TimerExportName name TimerFN_Performance_Now;
+
 procedure __wasmtimer_log(level : TWasmLogLevel; const Msg : String);
 procedure __wasmtimer_log(level : TWasmLogLevel; const Fmt : String; Args : Array of const);
 

+ 10 - 0
packages/wasm-utils/src/wasm.timer.objects.pas

@@ -45,6 +45,7 @@ Type
     property OnTimer : TNotifyEvent Read FOnTimer;
     Property ID : TWasmTimerID Read FID;
     class procedure HandleWasmTimer(aTimerID: TWasmTimerID; userdata: pointer; var aContinue: Boolean); static;
+    class function getPerformanceNow : Double;
   end;
 
   TTimer = Class(TComponent)
@@ -112,6 +113,15 @@ begin
     Obj.Execute;
 end;
 
+class function TWasmTimer.getPerformanceNow: Double;
+begin
+  if __wasm_timer_performance_now(@Result)<>ETIMER_SUCCESS then
+    begin
+    __wasmtimer_log(wllError, 'No performance timer available');
+    Raise EWasmTimer.Create('No performance timer available');
+    end;
+end;
+
 { TTimer }
 
 procedure TTimer.SetEnabled(AValue: Boolean);

+ 4 - 1
packages/wasm-utils/src/wasm.timer.shared.pas

@@ -22,10 +22,13 @@ Type
   TWasmTimerID = Longint;
 
 const
+  ETIMER_SUCCESS       = 0;
+  ETIMER_NOPERFORMANCE = -1;
+
   TimerExportName  = 'timer';
   TimerFN_Allocate = 'allocate_timer';
   TimerFN_DeAllocate = 'deallocate_timer';
-
+  TimerFN_Performance_Now = 'timer_performance_now';
 
 implementation