SDL: Refactor use of VideoBackend to slim down GL backends

This commit is contained in:
Vicki Pfau 2023-02-28 22:27:11 -08:00
parent 08f360af90
commit 48c9261b05
5 changed files with 49 additions and 78 deletions

View File

@ -5,6 +5,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "main.h"
#include <mgba/core/core.h>
#include <mgba/core/thread.h>
#include <mgba/core/version.h>
void mSDLGLDoViewport(int w, int h, struct VideoBackend* v) {
@ -66,3 +68,43 @@ bool mSDLGLCommonInit(struct mSDLRenderer* renderer) {
#endif
return true;
}
void mSDLGLCommonRunloop(struct mSDLRenderer* renderer, void* user) {
struct mCoreThread* context = user;
SDL_Event event;
struct VideoBackend* v = renderer->backend;
while (mCoreThreadIsActive(context)) {
while (SDL_PollEvent(&event)) {
mSDLHandleEvent(context, &renderer->player, &event);
// Event handling can change the size of the screen
if (renderer->player.windowUpdated) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
#else
renderer->viewportWidth = renderer->player.newWidth;
renderer->viewportHeight = renderer->player.newHeight;
mSDLGLCommonInit(renderer);
#endif
mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, v);
renderer->player.windowUpdated = 0;
}
}
renderer->core->currentVideoSize(renderer->core, &renderer->width, &renderer->height);
struct Rectangle dims;
v->layerDimensions(v, VIDEO_LAYER_IMAGE, &dims);
if (renderer->width != dims.width || renderer->height != dims.height) {
renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
dims.width = renderer->width;
dims.height = renderer->height;
v->setLayerDimensions(v, VIDEO_LAYER_IMAGE, &dims);
}
if (mCoreSyncWaitFrameStart(&context->impl->sync)) {
v->setImage(v, VIDEO_LAYER_IMAGE, renderer->outputBuffer);
}
mCoreSyncWaitFrameEnd(&context->impl->sync);
v->drawFrame(v);
v->swap(v);
}
}

View File

@ -15,6 +15,7 @@ struct mSDLRenderer;
void mSDLGLDoViewport(int w, int h, struct VideoBackend* v);
void mSDLGLCommonSwap(struct VideoBackend* context);
bool mSDLGLCommonInit(struct mSDLRenderer* renderer);
void mSDLGLCommonRunloop(struct mSDLRenderer* renderer, void* user);
CXX_GUARD_END

View File

@ -8,19 +8,17 @@
#include "gl-common.h"
#include <mgba/core/core.h>
#include <mgba/core/thread.h>
#include <mgba-util/math.h>
#include "platform/opengl/gl.h"
static bool mSDLGLInit(struct mSDLRenderer* renderer);
static void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user);
static void mSDLGLDeinit(struct mSDLRenderer* renderer);
void mSDLGLCreate(struct mSDLRenderer* renderer) {
renderer->init = mSDLGLInit;
renderer->deinit = mSDLGLDeinit;
renderer->runloop = mSDLGLRunloop;
renderer->runloop = mSDLGLCommonRunloop;
renderer->backend = &renderer->gl.d;
}
bool mSDLGLInit(struct mSDLRenderer* renderer) {
@ -49,46 +47,6 @@ bool mSDLGLInit(struct mSDLRenderer* renderer) {
return true;
}
void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user) {
struct mCoreThread* context = user;
SDL_Event event;
struct VideoBackend* v = &renderer->gl.d;
while (mCoreThreadIsActive(context)) {
while (SDL_PollEvent(&event)) {
mSDLHandleEvent(context, &renderer->player, &event);
// Event handling can change the size of the screen
if (renderer->player.windowUpdated) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
#else
renderer->viewportWidth = renderer->player.newWidth;
renderer->viewportHeight = renderer->player.newHeight;
mSDLGLCommonInit(renderer);
#endif
mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, v);
renderer->player.windowUpdated = 0;
}
}
renderer->core->currentVideoSize(renderer->core, &renderer->width, &renderer->height);
struct Rectangle dims;
v->layerDimensions(v, VIDEO_LAYER_IMAGE, &dims);
if (renderer->width != dims.width || renderer->height != dims.height) {
renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
dims.width = renderer->width;
dims.height = renderer->height;
v->setLayerDimensions(v, VIDEO_LAYER_IMAGE, &dims);
}
if (mCoreSyncWaitFrameStart(&context->impl->sync)) {
v->setImage(v, VIDEO_LAYER_IMAGE, renderer->outputBuffer);
}
mCoreSyncWaitFrameEnd(&context->impl->sync);
v->drawFrame(v);
v->swap(v);
}
}
void mSDLGLDeinit(struct mSDLRenderer* renderer) {
if (renderer->gl.d.deinit) {
renderer->gl.d.deinit(&renderer->gl.d);

View File

@ -11,20 +11,19 @@
#endif
#include <mgba/core/core.h>
#include <mgba/core/thread.h>
#ifdef __linux__
#include <malloc.h>
#endif
static bool mSDLGLES2Init(struct mSDLRenderer* renderer);
static void mSDLGLES2Runloop(struct mSDLRenderer* renderer, void* user);
static void mSDLGLES2Deinit(struct mSDLRenderer* renderer);
void mSDLGLES2Create(struct mSDLRenderer* renderer) {
renderer->init = mSDLGLES2Init;
renderer->deinit = mSDLGLES2Deinit;
renderer->runloop = mSDLGLES2Runloop;
renderer->runloop = mSDLGLCommonRunloop;
renderer->backend = &renderer->gl2.d;
}
bool mSDLGLES2Init(struct mSDLRenderer* renderer) {
@ -63,37 +62,6 @@ bool mSDLGLES2Init(struct mSDLRenderer* renderer) {
return true;
}
void mSDLGLES2Runloop(struct mSDLRenderer* renderer, void* user) {
struct mCoreThread* context = user;
SDL_Event event;
struct VideoBackend* v = &renderer->gl2.d;
while (mCoreThreadIsActive(context)) {
while (SDL_PollEvent(&event)) {
mSDLHandleEvent(context, &renderer->player, &event);
// Event handling can change the size of the screen
if (renderer->player.windowUpdated) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
#else
renderer->viewportWidth = renderer->player.newWidth;
renderer->viewportHeight = renderer->player.newHeight;
mSDLGLCommonInit(renderer);
#endif
mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, v);
renderer->player.windowUpdated = 0;
}
}
if (mCoreSyncWaitFrameStart(&context->impl->sync)) {
v->setImage(v, VIDEO_LAYER_IMAGE, renderer->outputBuffer);
}
mCoreSyncWaitFrameEnd(&context->impl->sync);
v->drawFrame(v);
v->swap(v);
}
}
void mSDLGLES2Deinit(struct mSDLRenderer* renderer) {
if (renderer->gl2.d.deinit) {
renderer->gl2.d.deinit(&renderer->gl2.d);

View File

@ -76,6 +76,8 @@ struct mSDLRenderer {
struct mGLES2Context gl2;
#endif
struct VideoBackend* backend;
#ifdef USE_PIXMAN
pixman_image_t* pix;
pixman_image_t* screenpix;