Skip to content

Commit 882647b

Browse files
committed
fix: mac os slow webview loading with preloading, gpu args, 2d mode
1 parent e6f31d1 commit 882647b

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

src/Packages/Passport/Runtime/Scripts/Private/UI/WebViews/MacOSPassportWebView.cs

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,41 @@ private async UniTaskVoid InitializeAsync(PassportWebViewConfig config)
6565
try
6666
{
6767
PassportLogger.Info($"{TAG} Starting Vuplex CanvasWebViewPrefab instantiation...");
68+
69+
// Apply aggressive performance optimizations for macOS
70+
try
71+
{
72+
StandaloneWebView.SetCommandLineArguments(
73+
"--disable-gpu " +
74+
"--disable-gpu-compositing " +
75+
"--disable-software-rasterizer " +
76+
"--disable-background-timer-throttling " +
77+
"--disable-renderer-backgrounding " +
78+
"--disable-features=TranslateUI " +
79+
"--no-sandbox"
80+
);
81+
PassportLogger.Info($"{TAG} Applied comprehensive performance optimizations for macOS");
82+
}
83+
catch (System.Exception ex)
84+
{
85+
PassportLogger.Warn($"{TAG} Could not apply performance optimizations: {ex.Message}");
86+
}
87+
6888
// Create WebView prefab and parent to Canvas
6989
_webViewPrefab = CanvasWebViewPrefab.Instantiate();
7090
PassportLogger.Info($"{TAG} CanvasWebViewPrefab created successfully");
71-
_webViewPrefab.Native2DModeEnabled = true; // Use Native2DMode for better performance on desktop
72-
73-
// Set lower resolution for faster loading and rendering
74-
_webViewPrefab.Resolution = 0.75f; // Balanced resolution for desktop
91+
92+
// Enable Native2DMode and additional performance settings
93+
_webViewPrefab.Native2DModeEnabled = true; // Direct native rendering - fastest on desktop
94+
_webViewPrefab.Resolution = 0.5f; // Balanced resolution for desktop
95+
96+
// Additional 2D mode optimizations
97+
if (_webViewPrefab.Native2DModeEnabled)
98+
{
99+
PassportLogger.Info($"{TAG} Native2DMode confirmed enabled - using direct native rendering");
100+
// In Native2D mode, reduce pixel density for better performance
101+
_webViewPrefab.PixelDensity = 1.0f; // Standard density, no high-DPI overhead
102+
}
75103

76104
// Must be child of Canvas for Vuplex to work
77105
_webViewPrefab.transform.SetParent(_canvasReference.canvas.transform, false);
@@ -89,9 +117,31 @@ private async UniTaskVoid InitializeAsync(PassportWebViewConfig config)
89117

90118
PassportLogger.Info($"{TAG} Using WebView dimensions: {width}x{height}");
91119

92-
// Wait for WebView initialization
120+
// Wait for WebView initialization with timing
121+
var startTime = System.DateTime.Now;
93122
await _webViewPrefab.WaitUntilInitialized();
94-
PassportLogger.Info($"{TAG} Vuplex WebView initialization completed");
123+
var initTime = (System.DateTime.Now - startTime).TotalSeconds;
124+
PassportLogger.Info($"{TAG} Vuplex WebView initialization completed in {initTime:F2}s");
125+
126+
// Pre-load the login page for instant display
127+
try
128+
{
129+
if (!string.IsNullOrEmpty(config.InitialUrl) && config.InitialUrl != "about:blank")
130+
{
131+
_webViewPrefab.WebView.LoadUrl(config.InitialUrl);
132+
PassportLogger.Info($"{TAG} Pre-loaded login page: {config.InitialUrl}");
133+
}
134+
else
135+
{
136+
// Load minimal blank page if no URL provided (rare edge case)
137+
_webViewPrefab.WebView.LoadHtml("<html><body style='margin:0;padding:20px;font-family:system-ui;color:#666;text-align:center;'>Initializing...</body></html>");
138+
PassportLogger.Info($"{TAG} Loaded minimal blank page (no InitialUrl provided)");
139+
}
140+
}
141+
catch (System.Exception ex)
142+
{
143+
PassportLogger.Warn($"{TAG} Could not pre-load content: {ex.Message}");
144+
}
95145

96146
// Setup event handlers
97147
_webViewPrefab.WebView.LoadProgressChanged += (sender, progressArgs) =>
@@ -157,6 +207,19 @@ public void LoadUrl(string url)
157207
return;
158208
}
159209

210+
// Check if the requested URL is already loaded (performance optimization)
211+
var currentUrl = _webViewPrefab.WebView.Url;
212+
if (currentUrl == url)
213+
{
214+
PassportLogger.Info($"{TAG} URL already loaded, showing instantly: {url}");
215+
// No need to reload - just show the WebView if hidden
216+
if (!_webViewPrefab.Visible)
217+
{
218+
_webViewPrefab.Visible = true;
219+
}
220+
return;
221+
}
222+
160223
PassportLogger.Info($"{TAG} Loading URL: {url}");
161224
_webViewPrefab.WebView.LoadUrl(url);
162225
}

src/Packages/Passport/Runtime/Scripts/Private/UI/WebViews/WindowsPassportWebView.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,19 @@ private void ConfigureUWBSettings()
520520
webBrowserClient.headless = false;
521521
webBrowserClient.popupAction = PopupAction.OpenExternalWindow;
522522

523-
// Set initial URL
524-
webBrowserClient.initialUrl = config.InitialUrl;
525-
PassportLogger.Info($"{TAG} Set initial URL to: {config.InitialUrl}");
523+
// Set initial URL (use about:blank for fast startup, pre-load login page separately)
524+
if (!string.IsNullOrEmpty(config.InitialUrl) && config.InitialUrl != "about:blank")
525+
{
526+
// Store login URL for pre-loading after initialization
527+
queuedUrl = config.InitialUrl;
528+
webBrowserClient.initialUrl = "about:blank"; // Fast startup
529+
PassportLogger.Info($"{TAG} Queued login URL for pre-loading: {config.InitialUrl}");
530+
}
531+
else
532+
{
533+
webBrowserClient.initialUrl = config.InitialUrl;
534+
PassportLogger.Info($"{TAG} Set initial URL to: {config.InitialUrl}");
535+
}
526536

527537
// Configure isolated instance
528538
ConfigureIsolatedInstance();

src/Packages/Passport/Runtime/Scripts/Public/PassportUI.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,13 @@ private void CreateWebView()
375375
throw new NotSupportedException("WebView not supported on this platform");
376376
}
377377

378-
// Configure WebView
378+
// Configure WebView with login page pre-loading for better performance
379379
var config = new PassportWebViewConfig
380380
{
381381
EnableRemoteDebugging = enableRemoteDebugging,
382382
RemoteDebuggingPort = remoteDebuggingPort,
383383
ClearCacheOnInit = clearCacheOnLogin,
384-
InitialUrl = "about:blank",
384+
InitialUrl = WebViewUrl, // Pre-load the login page during initialization
385385
Width = webViewWidth > 0 ? webViewWidth : (int)rawImage.rectTransform.rect.width,
386386
Height = webViewHeight > 0 ? webViewHeight : (int)rawImage.rectTransform.rect.height
387387
};
@@ -730,8 +730,9 @@ public async UniTask<bool> ShowLoginUI()
730730
webView.Show();
731731
PassportLogger.Info($"{TAG} WebView shown");
732732

733+
// Load URL (WebView will optimize if already loaded)
733734
webView.LoadUrl(WebViewUrl);
734-
PassportLogger.Info($"{TAG} Navigated to configured URL: {WebViewUrl}");
735+
PassportLogger.Info($"{TAG} Login page ready: {WebViewUrl}");
735736

736737
// Return true since we successfully started the OAuth flow
737738
// The actual authentication completion is handled by the deep link system

0 commit comments

Comments
 (0)