Don't recreate the window when creating a Metal renderer on an OpenGL window.

It turns out that we can safely create a Metal view on an existing window, and that avoids issues with the window being recreated with the wrong orientation in iOS 16.

Fixes https://github.com/libsdl-org/SDL/issues/6289
This commit is contained in:
Sam Lantinga
2022-11-15 10:18:41 -08:00
parent a71ad40ac3
commit 70656b133c
5 changed files with 11 additions and 65 deletions

View File

@@ -55,9 +55,6 @@
/* Apple Metal renderer implementation */
/* Used to re-create the window with Metal capability */
extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
/* macOS requires constants in a buffer to have a 256 byte alignment. */
/* Use native type alignments from https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf */
#if defined(__MACOSX__) || TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
@@ -1635,13 +1632,11 @@ METAL_CreateRenderer(SDL_Window * window, Uint32 flags)
SDL_MetalView view = NULL;
CAMetalLayer *layer = nil;
SDL_SysWMinfo syswm;
SDL_bool changed_window = SDL_FALSE;
NSError *err = nil;
dispatch_data_t mtllibdata;
char *constantdata;
int maxtexsize, quadcount = UINT16_MAX / 4;
UInt16 *indexdata;
Uint32 window_flags;
size_t indicessize = sizeof(UInt16) * quadcount * 6;
MTLSamplerDescriptor *samplerdesc;
id<MTLCommandQueue> mtlcmdqueue;
@@ -1697,20 +1692,9 @@ METAL_CreateRenderer(SDL_Window * window, Uint32 flags)
return NULL;
}
window_flags = SDL_GetWindowFlags(window);
if (!(window_flags & SDL_WINDOW_METAL)) {
changed_window = SDL_TRUE;
if (SDL_RecreateWindow(window, (window_flags & ~(SDL_WINDOW_VULKAN | SDL_WINDOW_OPENGL)) | SDL_WINDOW_METAL) < 0) {
return NULL;
}
}
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
if (changed_window) {
SDL_RecreateWindow(window, window_flags);
}
return NULL;
}
@@ -1720,9 +1704,6 @@ METAL_CreateRenderer(SDL_Window * window, Uint32 flags)
if (mtldevice == nil) {
SDL_free(renderer);
SDL_SetError("Failed to obtain Metal device");
if (changed_window) {
SDL_RecreateWindow(window, window_flags);
}
return NULL;
}
@@ -1733,9 +1714,6 @@ METAL_CreateRenderer(SDL_Window * window, Uint32 flags)
if (view == NULL) {
SDL_free(renderer);
if (changed_window) {
SDL_RecreateWindow(window, window_flags);
}
return NULL;
}
@@ -1749,9 +1727,6 @@ METAL_CreateRenderer(SDL_Window * window, Uint32 flags)
/* SDL_Metal_DestroyView(view); */
CFBridgingRelease(view);
SDL_free(renderer);
if (changed_window) {
SDL_RecreateWindow(window, window_flags);
}
return NULL;
}