Skip to content

Commit

Permalink
Force OpenGL Core Profile context on macOS
Browse files Browse the repository at this point in the history
By default, SDL creates an OpenGL 2.1 context on macOS for an OpenGL
renderer. As a consequence, mipmapping is not supported.

Force to use a core profile context, to get a higher version.

Before:

    INFO: Renderer: opengl
    INFO: OpenGL version: 2.1 NVIDIA-14.0.32 355.11.11.10.10.143
    WARN: Trilinear filtering disabled (OpenGL 3.0+ or ES 2.0+ required)

After:

    INFO: Renderer: opengl
    DEBUG: Creating OpenGL Core Profile context
    INFO: OpenGL version: 4.1 NVIDIA-14.0.32 355.11.11.10.10.143
    INFO: Trilinear filtering enabled

when running with:

    scrcpy --verbosity=debug --render-driver=opengl

Note: Since SDL_CreateRenderer() causes a fallback to OpenGL 2.1, the
profile and version attributes have to be set and the context created
_after_.

PR #3895 <#3895>

Signed-off-by: Romain Vimont <rom@rom1v.com>
  • Loading branch information
Yan authored and rom1v committed Apr 12, 2023
1 parent 9eb6591 commit c083a7c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ sc_display_init(struct sc_display *display, SDL_Window *window, bool mipmaps) {
// starts with "opengl"
bool use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6);
if (use_opengl) {

#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
// Persuade macOS to give us something better than OpenGL 2.1.
// If we create a Core Profile context, we get the best OpenGL version.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE);

LOGD("Creating OpenGL Core Profile context");
display->gl_context = SDL_GL_CreateContext(window);
if (!display->gl_context) {
LOGE("Could not create OpenGL context: %s", SDL_GetError());
SDL_DestroyRenderer(display->renderer);
return false;
}
#endif

struct sc_opengl *gl = &display->gl;
sc_opengl_init(gl);

Expand Down Expand Up @@ -51,6 +67,9 @@ sc_display_init(struct sc_display *display, SDL_Window *window, bool mipmaps) {

void
sc_display_destroy(struct sc_display *display) {
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
SDL_GL_DeleteContext(display->gl_context);
#endif
if (display->texture) {
SDL_DestroyTexture(display->texture);
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
#include "coords.h"
#include "opengl.h"

#ifdef __APPLE__
# define SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
#endif

struct sc_display {
SDL_Renderer *renderer;
SDL_Texture *texture;

struct sc_opengl gl;
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
SDL_GLContext *gl_context;
#endif

bool mipmaps;
};

Expand Down

0 comments on commit c083a7c

Please sign in to comment.