From 395a61b6e06542c93847f0e581360a7123a9ef88 Mon Sep 17 00:00:00 2001 From: Koji Nakamaru Date: Wed, 17 Apr 2024 15:58:03 +0900 Subject: [PATCH] introduced devicePixelRatio for osx. --- plugins/Mac/Sources/WebView.mm | 28 +++++++++++++++++--------- plugins/WebViewObject.cs | 5 +++-- sample/Assets/Scripts/SampleWebView.cs | 1 + 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/plugins/Mac/Sources/WebView.mm b/plugins/Mac/Sources/WebView.mm index 41afe7fc..0520f059 100644 --- a/plugins/Mac/Sources/WebView.mm +++ b/plugins/Mac/Sources/WebView.mm @@ -89,6 +89,7 @@ @interface CWebViewPlugin : NSObject 2) + devicePixelRatio = 2; // [webView cacheDisplayInRect:webView.frame toBitmapImageRep:bitmap]; // bitmap = [webView bitmapImageRepForCachingDisplayInRect:webView.frame]; NSRect rect = webView.frame; - if (bitmaps[0] == nil || bitmaps[1] == nil) { + if (bitmaps[0] == nil || bitmaps[1] == nil || devicePixelRatio0 != devicePixelRatio) { + webView.pageZoom = devicePixelRatio; + devicePixelRatio0 = devicePixelRatio; for (int i = 0; i < 2; i++) { bitmaps[i] = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil - pixelsWide:rect.size.width - pixelsHigh:rect.size.height + pixelsWide:(rect.size.width * devicePixelRatio) + pixelsHigh:(rect.size.height * devicePixelRatio) bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bitmapFormat:0 - bytesPerRow:(4 * rect.size.width) + bytesPerRow:(4 * rect.size.width * devicePixelRatio) bitsPerPixel:32]; } bitmap = bitmaps[0]; @@ -844,7 +851,10 @@ - (void)update:(BOOL)refreshBitmap NSBitmapImageRep *bitmap1 = (bitmap == bitmaps[0]) ? bitmaps[1] : bitmaps[0]; { [self runBlock:^{ - [self->webView takeSnapshotWithConfiguration:[WKSnapshotConfiguration new] + WKSnapshotConfiguration *config = [WKSnapshotConfiguration new]; + config.rect = rect; + config.snapshotWidth = @(rect.size.width * devicePixelRatio); + [self->webView takeSnapshotWithConfiguration:config completionHandler:^(NSImage *nsImg, NSError *err) { if (err == nil) { NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap1]; @@ -981,7 +991,7 @@ - (const char *)getCustomRequestHeaderValue:(const char *)headerKey void _CWebViewPlugin_Reload(void *instance); void _CWebViewPlugin_SendMouseEvent(void *instance, int x, int y, float deltaY, int mouseState); void _CWebViewPlugin_SendKeyEvent(void *instance, int x, int y, char *keyChars, unsigned short keyCode, int keyState); - void _CWebViewPlugin_Update(void *instance, BOOL refreshBitmap); + void _CWebViewPlugin_Update(void *instance, BOOL refreshBitmap, int devicePixelRatio); int _CWebViewPlugin_BitmapWidth(void *instance); int _CWebViewPlugin_BitmapHeight(void *instance); void _CWebViewPlugin_Render(void *instance, void *textureBuffer); @@ -1111,10 +1121,10 @@ void _CWebViewPlugin_SendKeyEvent(void *instance, int x, int y, char *keyChars, [webViewPlugin sendKeyEvent:x y:y keyChars:keyChars keyCode:keyCode keyState:keyState]; } -void _CWebViewPlugin_Update(void *instance, BOOL refreshBitmap) +void _CWebViewPlugin_Update(void *instance, BOOL refreshBitmap, int devicePixelRatio) { CWebViewPlugin *webViewPlugin = (__bridge CWebViewPlugin *)instance; - [webViewPlugin update:refreshBitmap]; + [webViewPlugin update:refreshBitmap with:devicePixelRatio]; } int _CWebViewPlugin_BitmapWidth(void *instance) diff --git a/plugins/WebViewObject.cs b/plugins/WebViewObject.cs index d4758cfd..26c61f03 100644 --- a/plugins/WebViewObject.cs +++ b/plugins/WebViewObject.cs @@ -480,7 +480,7 @@ private static extern void _CWebViewPlugin_Reload( [DllImport("WebView")] private static extern void _CWebViewPlugin_SendKeyEvent(IntPtr instance, int x, int y, string keyChars, ushort keyCode, int keyState); [DllImport("WebView")] - private static extern void _CWebViewPlugin_Update(IntPtr instance, bool refreshBitmap); + private static extern void _CWebViewPlugin_Update(IntPtr instance, bool refreshBitmap, int devicePixelRatio); [DllImport("WebView")] private static extern int _CWebViewPlugin_BitmapWidth(IntPtr instance); [DllImport("WebView")] @@ -1546,7 +1546,7 @@ void Update() if (webView == IntPtr.Zero || !visibility) return; bool refreshBitmap = (Time.frameCount % bitmapRefreshCycle == 0); - _CWebViewPlugin_Update(webView, refreshBitmap); + _CWebViewPlugin_Update(webView, refreshBitmap, devicePixelRatio); if (refreshBitmap) { { var w = _CWebViewPlugin_BitmapWidth(webView); @@ -1582,6 +1582,7 @@ void UpdateBGTransform() } public int bitmapRefreshCycle = 1; + public int devicePixelRatio = 1; void OnGUI() { diff --git a/sample/Assets/Scripts/SampleWebView.cs b/sample/Assets/Scripts/SampleWebView.cs index faabee0d..f2d2515b 100644 --- a/sample/Assets/Scripts/SampleWebView.cs +++ b/sample/Assets/Scripts/SampleWebView.cs @@ -130,6 +130,7 @@ IEnumerator Start() ); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webViewObject.bitmapRefreshCycle = 1; + webViewObject.devicePixelRatio = 1; // 1 or 2 #endif // cf. https://github.com/gree/unity-webview/pull/512 // Added alertDialogEnabled flag to enable/disable alert/confirm/prompt dialogs. by KojiNakamaru · Pull Request #512 · gree/unity-webview