all repos — mgba @ c4123a238685d644b20e11aee4fec72427e0c9f4

mGBA Game Boy Advance Emulator

SDL: Fall back to sw blit if OpenGL init fails
Vicki Pfau vi@endrift.com
Wed, 16 Dec 2020 18:04:48 -0800
commit

c4123a238685d644b20e11aee4fec72427e0c9f4

parent

7b78906772a65078c6a8098c9b63f4aa12301fea

M CHANGESCHANGES

@@ -113,6 +113,7 @@ - Qt: Add copy button to GB printer dialog

- Qt: Window title updates can be disabled (closes mgba.io/i/1912) - Qt: Redo OpenGL context thread handling (fixes mgba.io/i/1724) - Qt: Discard additional frame draws if waiting fails + - SDL: Fall back to sw blit if OpenGL init fails - Util: Reset vector size on deinit - VFS: Change semantics of VFile.sync on mapped files (fixes mgba.io/i/1730)
M src/platform/sdl/CMakeLists.txtsrc/platform/sdl/CMakeLists.txt

@@ -94,12 +94,10 @@ list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/gles2-sdl.c)

list(APPEND PLATFORM_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/gl-common.c) include_directories(${OPENGLES2_INCLUDE_DIR}) endif() - if(NOT BUILD_GL AND NOT BUILD_GLES2) - if(SDL_VERSION EQUAL "2") - list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/sw-sdl2.c) - else() - list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/sw-sdl1.c) - endif() + if(SDL_VERSION EQUAL "2") + list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/sw-sdl2.c) + else() + list(APPEND MAIN_SRC ${CMAKE_SOURCE_DIR}/src/platform/sdl/sw-sdl1.c) endif() endif()
M src/platform/sdl/gl-common.csrc/platform/sdl/gl-common.c

@@ -24,7 +24,7 @@ SDL_GL_SwapBuffers();

#endif } -void mSDLGLCommonInit(struct mSDLRenderer* renderer) { +bool mSDLGLCommonInit(struct mSDLRenderer* renderer) { #ifndef COLOR_16_BIT SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);

@@ -41,7 +41,11 @@ #endif

#if SDL_VERSION_ATLEAST(2, 0, 0) renderer->window = SDL_CreateWindow(projectName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->player.fullscreen)); - renderer->glCtx = SDL_GL_CreateContext(renderer->window); + renderer->glCtx = NULL;//SDL_GL_CreateContext(renderer->window); + if (!renderer->glCtx) { + SDL_DestroyWindow(renderer->window); + return false; + } SDL_GL_SetSwapInterval(1); SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight); renderer->player.window = renderer->window;

@@ -51,10 +55,14 @@ }

#else SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); #ifdef COLOR_16_BIT - SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL | SDL_RESIZABLE | (SDL_FULLSCREEN * renderer->player.fullscreen)); + SDL_Surface* surface = SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL | SDL_RESIZABLE | (SDL_FULLSCREEN * renderer->player.fullscreen)); #else - SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL | SDL_RESIZABLE | (SDL_FULLSCREEN * renderer->player.fullscreen)); + SDL_Surface* surface = SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL | SDL_RESIZABLE | (SDL_FULLSCREEN * renderer->player.fullscreen)); #endif + if (!surface) { + return false; + } SDL_WM_SetCaption(projectName, ""); #endif + return true; }
M src/platform/sdl/gl-common.hsrc/platform/sdl/gl-common.h

@@ -10,11 +10,11 @@ #include <mgba-util/common.h>

CXX_GUARD_START -#include "main.h" - +struct VideoBackend; +struct mSDLRenderer; void mSDLGLDoViewport(int w, int h, struct VideoBackend* v); void mSDLGLCommonSwap(struct VideoBackend* context); -void mSDLGLCommonInit(struct mSDLRenderer* renderer); +bool mSDLGLCommonInit(struct mSDLRenderer* renderer); CXX_GUARD_END
M src/platform/sdl/gl-sdl.csrc/platform/sdl/gl-sdl.c

@@ -24,8 +24,6 @@ renderer->runloop = mSDLGLRunloop;

} bool mSDLGLInit(struct mSDLRenderer* renderer) { - mSDLGLCommonInit(renderer); - size_t size = renderer->width * renderer->height * BYTES_PER_PIXEL; renderer->outputBuffer = malloc(size); memset(renderer->outputBuffer, 0, size);
M src/platform/sdl/gles2-sdl.csrc/platform/sdl/gles2-sdl.c

@@ -28,12 +28,6 @@ renderer->runloop = mSDLGLES2Runloop;

} bool mSDLGLES2Init(struct mSDLRenderer* renderer) { -#ifdef BUILD_RASPI - mRPIGLCommonInit(renderer); -#else - mSDLGLCommonInit(renderer); -#endif - size_t size = renderer->width * renderer->height * BYTES_PER_PIXEL; #ifdef _WIN32 renderer->outputBuffer = _aligned_malloc(size, 16);
M src/platform/sdl/main.csrc/platform/sdl/main.c

@@ -39,7 +39,6 @@ #include <signal.h>

#define PORT "sdl" -static bool mSDLInit(struct mSDLRenderer* renderer); static void mSDLDeinit(struct mSDLRenderer* renderer); static int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args);

@@ -84,6 +83,12 @@ freeArguments(&args);

return 0; } + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + printf("Could not initialize video: %s\n", SDL_GetError()); + freeArguments(&args); + return 1; + } + renderer.core = mCoreFind(args.fname); if (!renderer.core) { printf("Could not run game. Are you sure the file exists and is a compatible game?\n");

@@ -97,14 +102,6 @@ return 1;

} renderer.core->desiredVideoDimensions(renderer.core, &renderer.width, &renderer.height); -#ifdef BUILD_GL - mSDLGLCreate(&renderer); -#elif defined(BUILD_GLES2) || defined(USE_EPOXY) - mSDLGLES2Create(&renderer); -#else - mSDLSWCreate(&renderer); -#endif - renderer.ratio = graphicsOpts.multiplier; if (renderer.ratio == 0) { renderer.ratio = 1;

@@ -139,7 +136,25 @@ renderer.lockIntegerScaling = renderer.core->opts.lockIntegerScaling;

renderer.interframeBlending = renderer.core->opts.interframeBlending; renderer.filter = renderer.core->opts.resampleVideo; - if (!mSDLInit(&renderer)) { +#ifdef BUILD_GL + if (mSDLGLCommonInit(&renderer)) { + mSDLGLCreate(&renderer); + } else +#elif defined(BUILD_GLES2) || defined(USE_EPOXY) +#ifdef BUILD_RASPI + mRPIGLCommonInit(&renderer); +#else + if (mSDLGLCommonInit(&renderer)) +#endif + { + mSDLGLES2Create(&renderer); + } else +#endif + { + mSDLSWCreate(&renderer); + } + + if (!renderer.init(&renderer)) { freeArguments(&args); mCoreConfigDeinit(&renderer.core->config); renderer.core->deinit(renderer.core);

@@ -293,15 +308,6 @@ mScriptBridgeDestroy(bridge);

#endif return didFail; -} - -static bool mSDLInit(struct mSDLRenderer* renderer) { - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - printf("Could not initialize video: %s\n", SDL_GetError()); - return false; - } - - return renderer->init(renderer); } static void mSDLDeinit(struct mSDLRenderer* renderer) {
M src/platform/sdl/main.hsrc/platform/sdl/main.h

@@ -14,6 +14,7 @@ #include "sdl-audio.h"

#include "sdl-events.h" #ifdef BUILD_GL +#include "gl-common.h" #include "platform/opengl/gl.h" #endif

@@ -29,6 +30,7 @@ #pragma GCC diagnostic pop

#endif #if defined(BUILD_GLES2) || defined(USE_EPOXY) +#include "gl-common.h" #include "platform/opengl/gles2.h" #endif