From 3d8a7e4c15bb8e1340157b73831266c9e79ff12d Mon Sep 17 00:00:00 2001
From: Steven Noonan <[EMAIL REDACTED]>
Date: Fri, 9 Apr 2021 07:44:38 -0700
Subject: [PATCH] cocoa: implement GL_GetDrawableSize for OpenGL ES
---
src/video/cocoa/SDL_cocoaopengles.h | 2 ++
src/video/cocoa/SDL_cocoaopengles.m | 23 ++++++++++++++++++++++-
src/video/cocoa/SDL_cocoavideo.m | 1 +
src/video/cocoa/SDL_cocoawindow.m | 7 +++++++
4 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/src/video/cocoa/SDL_cocoaopengles.h b/src/video/cocoa/SDL_cocoaopengles.h
index 05800e3e426e..4b3b53cc66d0 100644
--- a/src/video/cocoa/SDL_cocoaopengles.h
+++ b/src/video/cocoa/SDL_cocoaopengles.h
@@ -39,6 +39,8 @@ extern int Cocoa_GLES_LoadLibrary(_THIS, const char *path);
extern SDL_GLContext Cocoa_GLES_CreateContext(_THIS, SDL_Window * window);
extern int Cocoa_GLES_SwapWindow(_THIS, SDL_Window * window);
extern int Cocoa_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
+extern void Cocoa_GLES_GetDrawableSize(_THIS, SDL_Window * window,
+ int * w, int * h);
extern void Cocoa_GLES_DeleteContext(_THIS, SDL_GLContext context);
extern int Cocoa_GLES_SetupWindow(_THIS, SDL_Window * window);
extern SDL_EGLSurface Cocoa_GLES_GetEGLSurface(_THIS, SDL_Window * window);
diff --git a/src/video/cocoa/SDL_cocoaopengles.m b/src/video/cocoa/SDL_cocoaopengles.m
index b8f17282d2c6..6df958f1db5a 100644
--- a/src/video/cocoa/SDL_cocoaopengles.m
+++ b/src/video/cocoa/SDL_cocoaopengles.m
@@ -114,8 +114,29 @@
return SDL_EGL_MakeCurrent(_this, window ? ((__bridge SDL_WindowData *) window->driverdata).egl_surface : EGL_NO_SURFACE, context);
}}
+void
+Cocoa_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
+{ @autoreleasepool
+{
+ SDL_WindowData *windata = (__bridge SDL_WindowData *)window->driverdata;
+ NSView *contentView = windata.nswindow.contentView;
+ CALayer *layer = [contentView layer];
+
+ int width = layer.bounds.size.width * layer.contentsScale;
+ int height = layer.bounds.size.height * layer.contentsScale;
+
+ if (w) {
+ *w = width;
+ }
+
+ if (h) {
+ *h = height;
+ }
+}}
+
int
Cocoa_GLES_SetupWindow(_THIS, SDL_Window * window)
+{ @autoreleasepool
{
NSView* v;
/* The current context is lost in here; save it and reset it. */
@@ -145,7 +166,7 @@
}
return Cocoa_GLES_MakeCurrent(_this, current_win, current_ctx);
-}
+}}
SDL_EGLSurface
Cocoa_GLES_GetEGLSurface(_THIS, SDL_Window * window)
diff --git a/src/video/cocoa/SDL_cocoavideo.m b/src/video/cocoa/SDL_cocoavideo.m
index a354a2451ee7..da43da732ef0 100644
--- a/src/video/cocoa/SDL_cocoavideo.m
+++ b/src/video/cocoa/SDL_cocoavideo.m
@@ -150,6 +150,7 @@ @implementation SDL_VideoData
device->GL_UnloadLibrary = Cocoa_GLES_UnloadLibrary;
device->GL_CreateContext = Cocoa_GLES_CreateContext;
device->GL_MakeCurrent = Cocoa_GLES_MakeCurrent;
+ device->GL_GetDrawableSize = Cocoa_GLES_GetDrawableSize;
device->GL_SetSwapInterval = Cocoa_GLES_SetSwapInterval;
device->GL_GetSwapInterval = Cocoa_GLES_GetSwapInterval;
device->GL_SwapWindow = Cocoa_GLES_SwapWindow;
diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index eea7f981a662..7d5ee093165a 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -1787,6 +1787,13 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
if ((window->flags & SDL_WINDOW_OPENGL) &&
_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
[contentView setWantsLayer:TRUE];
+ if (!(window->flags & SDL_WINDOW_ALLOW_HIGHDPI)) {
+ contentView.layer.contentsScale = 1;
+ } else {
+ if ([nswindow.screen respondsToSelector:@selector(backingScaleFactor)]) {
+ contentView.layer.contentsScale = nswindow.screen.backingScaleFactor;
+ }
+ }
}
#endif /* SDL_VIDEO_OPENGL_EGL */
#endif /* SDL_VIDEO_OPENGL_ES2 */