all repos — mgba @ af35438aaab9307f0451f358a678644d4d9336ba

mGBA Game Boy Advance Emulator

src/platform/sdl/gles2-sdl.c (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "main.h"
  7
  8#include "gl-common.h"
  9#ifdef BUILD_RASPI
 10#include "rpi-common.h"
 11#endif
 12
 13#include <mgba/core/core.h>
 14#include <mgba/core/thread.h>
 15
 16#ifndef __APPLE__
 17#include <malloc.h>
 18#endif
 19
 20static bool mSDLGLES2Init(struct mSDLRenderer* renderer);
 21static void mSDLGLES2Runloop(struct mSDLRenderer* renderer, void* user);
 22static void mSDLGLES2Deinit(struct mSDLRenderer* renderer);
 23
 24void mSDLGLES2Create(struct mSDLRenderer* renderer) {
 25	renderer->init = mSDLGLES2Init;
 26	renderer->deinit = mSDLGLES2Deinit;
 27	renderer->runloop = mSDLGLES2Runloop;
 28}
 29
 30bool mSDLGLES2Init(struct mSDLRenderer* renderer) {
 31#ifdef BUILD_RASPI
 32	mRPIGLCommonInit(renderer);
 33#else
 34	mSDLGLCommonInit(renderer);
 35#endif
 36
 37	size_t size = renderer->width * renderer->height * BYTES_PER_PIXEL;
 38#ifndef __APPLE__
 39	renderer->outputBuffer = memalign(16, size);
 40#else
 41	posix_memalign((void**) &renderer->outputBuffer, 16, size);
 42#endif
 43	memset(renderer->outputBuffer, 0, size);
 44	renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
 45
 46	mGLES2ContextCreate(&renderer->gl2);
 47	renderer->gl2.d.user = renderer;
 48	renderer->gl2.d.lockAspectRatio = renderer->lockAspectRatio;
 49	renderer->gl2.d.lockIntegerScaling = renderer->lockIntegerScaling;
 50	renderer->gl2.d.filter = renderer->filter;
 51#ifdef BUILD_RASPI
 52	renderer->gl2.d.swap = mRPIGLCommonSwap;
 53#else
 54	renderer->gl2.d.swap = mSDLGLCommonSwap;
 55#endif
 56	renderer->gl2.d.init(&renderer->gl2.d, 0);
 57	renderer->gl2.d.setDimensions(&renderer->gl2.d, renderer->width, renderer->height);
 58
 59	mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl2.d);
 60	return true;
 61}
 62
 63void mSDLGLES2Runloop(struct mSDLRenderer* renderer, void* user) {
 64	struct mCoreThread* context = user;
 65	SDL_Event event;
 66	struct VideoBackend* v = &renderer->gl2.d;
 67
 68	while (mCoreThreadIsActive(context)) {
 69		while (SDL_PollEvent(&event)) {
 70			mSDLHandleEvent(context, &renderer->player, &event);
 71			// Event handling can change the size of the screen
 72			if (renderer->player.windowUpdated) {
 73#if SDL_VERSION_ATLEAST(2, 0, 0)
 74				SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
 75#else
 76				renderer->viewportWidth = renderer->player.newWidth;
 77				renderer->viewportHeight = renderer->player.newHeight;
 78				mSDLGLCommonInit(renderer);
 79#endif
 80				mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, v);
 81				renderer->player.windowUpdated = 0;
 82			}
 83		}
 84
 85		if (mCoreSyncWaitFrameStart(&context->impl->sync)) {
 86			v->postFrame(v, renderer->outputBuffer);
 87		}
 88		mCoreSyncWaitFrameEnd(&context->impl->sync);
 89		v->drawFrame(v);
 90		v->swap(v);
 91	}
 92}
 93
 94void mSDLGLES2Deinit(struct mSDLRenderer* renderer) {
 95	if (renderer->gl2.d.deinit) {
 96		renderer->gl2.d.deinit(&renderer->gl2.d);
 97	}
 98#ifdef BUILD_RASPI
 99	eglMakeCurrent(renderer->eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
100	eglDestroySurface(renderer->eglDisplay, renderer->eglSurface);
101	eglDestroyContext(renderer->eglDisplay, renderer->eglContext);
102	eglTerminate(renderer->eglDisplay);
103	bcm_host_deinit();
104#elif SDL_VERSION_ATLEAST(2, 0, 0)
105	SDL_GL_DeleteContext(renderer->glCtx);
106#endif
107	free(renderer->outputBuffer);
108}