Added a hint to mark a foreign window as usable with OpenGL

Fixes https://github.com/libsdl-org/SDL/issues/2942
This commit is contained in:
Sam Lantinga 2022-03-17 14:44:34 -07:00
parent 4e49b78a11
commit e5f45455c9
3 changed files with 47 additions and 11 deletions

View File

@ -1453,6 +1453,17 @@ extern "C" {
*/ */
#define SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT "SDL_VIDEO_WINDOW_SHARE_PIXEL_FORMAT" #define SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT "SDL_VIDEO_WINDOW_SHARE_PIXEL_FORMAT"
/**
* \brief When calling SDL_CreateWindowFrom(), make the window compatible with OpenGL.
*
* This variable can be set to the following values:
* "0" - Don't add any graphics flags to the SDL_WindowFlags
* "1" - Add SDL_WINDOW_OPENGL to the SDL_WindowFlags
*
* By default SDL will not make the foreign window compatible with OpenGL.
*/
#define SDL_HINT_VIDEO_FOREIGN_WINDOW_OPENGL "SDL_VIDEO_FOREIGN_WINDOW_OPENGL"
/** /**
* \brief When calling SDL_CreateWindowFrom(), make the window compatible with Vulkan. * \brief When calling SDL_CreateWindowFrom(), make the window compatible with Vulkan.
* *

View File

@ -1676,6 +1676,7 @@ SDL_Window *
SDL_CreateWindowFrom(const void *data) SDL_CreateWindowFrom(const void *data)
{ {
SDL_Window *window; SDL_Window *window;
Uint32 flags = SDL_WINDOW_FOREIGN;
if (!_this) { if (!_this) {
SDL_UninitializedVideo(); SDL_UninitializedVideo();
@ -1685,6 +1686,37 @@ SDL_CreateWindowFrom(const void *data)
SDL_Unsupported(); SDL_Unsupported();
return NULL; return NULL;
} }
if (SDL_GetHintBoolean(SDL_HINT_VIDEO_FOREIGN_WINDOW_OPENGL, SDL_FALSE)) {
if (!_this->GL_CreateContext) {
SDL_SetError("OpenGL support is either not configured in SDL "
"or not available in current SDL video driver "
"(%s) or platform", _this->name);
return NULL;
}
if (SDL_GL_LoadLibrary(NULL) < 0) {
return NULL;
}
flags |= SDL_WINDOW_OPENGL;
}
if (SDL_GetHintBoolean(SDL_HINT_VIDEO_FOREIGN_WINDOW_VULKAN, SDL_FALSE)) {
if (!_this->Vulkan_CreateSurface) {
SDL_SetError("Vulkan support is either not configured in SDL "
"or not available in current SDL video driver "
"(%s) or platform", _this->name);
return NULL;
}
if (flags & SDL_WINDOW_OPENGL) {
SDL_SetError("Vulkan and OpenGL not supported on same window");
return NULL;
}
if (SDL_Vulkan_LoadLibrary(NULL) < 0) {
return NULL;
}
flags |= SDL_WINDOW_VULKAN;
}
window = (SDL_Window *)SDL_calloc(1, sizeof(*window)); window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
if (!window) { if (!window) {
SDL_OutOfMemory(); SDL_OutOfMemory();
@ -1692,7 +1724,7 @@ SDL_CreateWindowFrom(const void *data)
} }
window->magic = &_this->window_magic; window->magic = &_this->window_magic;
window->id = _this->next_object_id++; window->id = _this->next_object_id++;
window->flags = SDL_WINDOW_FOREIGN; window->flags = flags;
window->last_fullscreen_flags = window->flags; window->last_fullscreen_flags = window->flags;
window->is_destroying = SDL_FALSE; window->is_destroying = SDL_FALSE;
window->opacity = 1.0f; window->opacity = 1.0f;
@ -1703,16 +1735,6 @@ SDL_CreateWindowFrom(const void *data)
} }
_this->windows = window; _this->windows = window;
#if SDL_VIDEO_VULKAN
if (SDL_GetHintBoolean(SDL_HINT_VIDEO_FOREIGN_WINDOW_VULKAN, SDL_FALSE)) {
window->flags |= SDL_WINDOW_VULKAN;
if (SDL_Vulkan_LoadLibrary(NULL) < 0) {
SDL_DestroyWindow(window);
return NULL;
}
}
#endif
if (_this->CreateSDLWindowFrom(_this, window, data) < 0) { if (_this->CreateSDLWindowFrom(_this, window, data) < 0) {
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
return NULL; return NULL;

View File

@ -422,6 +422,9 @@ WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
} }
} }
} }
} else if (window->flags & SDL_WINDOW_OPENGL) {
/* Try to set up the pixel format, if it hasn't been set by the application */
WIN_GL_SetupWindow(_this, window);
} }
} }
#endif #endif