Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduced devicePixelRatio for osx. #1069

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions plugins/Mac/Sources/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ @interface CWebViewPlugin : NSObject<WKUIDelegate, WKNavigationDelegate, WKScrip
NSRegularExpression *denyRegex;
NSRegularExpression *hookRegex;
BOOL inRendering;
int devicePixelRatio0;
}
@end

Expand Down Expand Up @@ -811,7 +812,7 @@ - (void)sendKeyEvent:(int)x y:(int)y keyChars:(char *)keyChars keyCode:(unsigned
}];
}

- (void)update:(BOOL)refreshBitmap
- (void)update:(BOOL)refreshBitmap with:(int)devicePixelRatio
{
if (webView == nil)
return;
Expand All @@ -821,30 +822,39 @@ - (void)update:(BOOL)refreshBitmap
return;
inRendering = YES;
}
if (devicePixelRatio < 1)
devicePixelRatio = 1;
else if (devicePixelRatio > 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];
}
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];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions plugins/WebViewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1582,6 +1582,7 @@ void UpdateBGTransform()
}

public int bitmapRefreshCycle = 1;
public int devicePixelRatio = 1;

void OnGUI()
{
Expand Down
1 change: 1 addition & 0 deletions sample/Assets/Scripts/SampleWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down