all repos — mgba @ 59f101eb496401f64933d27e365346d03ac8c0a8

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2015 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
  8static void _sdlSwap(struct VideoBackend* context) {
  9	UNUSED(context);
 10	SDL_GL_SwapBuffers();
 11}
 12
 13static bool GBASDLGLES2Init(struct SDLSoftwareRenderer* renderer);
 14static void GBASDLGLES2Runloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer);
 15static void GBASDLGLES2Deinit(struct SDLSoftwareRenderer* renderer);
 16
 17void GBASDLGLES2Create(struct SDLSoftwareRenderer* renderer) {
 18	renderer->init = GBASDLGLES2Init;
 19	renderer->deinit = GBASDLGLES2Deinit;
 20	renderer->runloop = GBASDLGLES2Runloop;
 21}
 22
 23bool GBASDLGLES2Init(struct SDLSoftwareRenderer* renderer) {
 24	bcm_host_init();
 25	renderer->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
 26	int major, minor;
 27	if (EGL_FALSE == eglInitialize(renderer->display, &major, &minor)) {
 28		printf("Failed to initialize EGL");
 29		return false;
 30	}
 31
 32	if (EGL_FALSE == eglBindAPI(EGL_OPENGL_ES_API)) {
 33		printf("Failed to get GLES API");
 34		return false;
 35	}
 36
 37	const EGLint requestConfig[] = {
 38		EGL_RED_SIZE, 5,
 39		EGL_GREEN_SIZE, 5,
 40		EGL_BLUE_SIZE, 5,
 41		EGL_ALPHA_SIZE, 1,
 42		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
 43		EGL_NONE
 44	};
 45
 46	EGLConfig config;
 47	EGLint numConfigs;
 48
 49	if (EGL_FALSE == eglChooseConfig(renderer->display, requestConfig, &config, 1, &numConfigs)) {
 50		printf("Failed to choose EGL config\n");
 51		return false;
 52	}
 53
 54	const EGLint contextAttributes[] = {
 55		EGL_CONTEXT_CLIENT_VERSION, 2,
 56		EGL_NONE
 57	};
 58
 59	int dispWidth = 240, dispHeight = 160, adjWidth;
 60	renderer->context = eglCreateContext(renderer->display, config, EGL_NO_CONTEXT, contextAttributes);
 61	graphics_get_display_size(0, &dispWidth, &dispHeight);
 62	adjWidth = dispHeight / 2 * 3;
 63
 64	DISPMANX_DISPLAY_HANDLE_T display = vc_dispmanx_display_open(0);
 65	DISPMANX_UPDATE_HANDLE_T update = vc_dispmanx_update_start(0);
 66
 67	VC_RECT_T destRect = {
 68		.x = (dispWidth - adjWidth) / 2,
 69		.y = 0,
 70		.width = adjWidth,
 71		.height = dispHeight
 72	};
 73
 74	VC_RECT_T srcRect = {
 75		.x = 0,
 76		.y = 0,
 77		.width = 240 << 16,
 78		.height = 160 << 16
 79	};
 80
 81	DISPMANX_ELEMENT_HANDLE_T element = vc_dispmanx_element_add(update, display, 0, &destRect, 0, &srcRect, DISPMANX_PROTECTION_NONE, 0, 0, 0);
 82	vc_dispmanx_update_submit_sync(update);
 83
 84	renderer->window.element = element;
 85	renderer->window.width = dispWidth;
 86	renderer->window.height = dispHeight;
 87
 88	renderer->surface = eglCreateWindowSurface(renderer->display, config, &renderer->window, 0);
 89	if (EGL_FALSE == eglMakeCurrent(renderer->display, renderer->surface, renderer->surface, renderer->context)) {
 90		return false;
 91	}
 92
 93	renderer->d.outputBuffer = memalign(16, 256 * 256 * 4);
 94	renderer->d.outputBufferStride = 256;
 95
 96	GBAGLES2ContextCreate(&renderer->gl);
 97	renderer->gl.d.user = renderer;
 98	renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
 99	renderer->gl.d.filter = renderer->filter;
100	renderer->gl.d.swap = _sdlSwap;
101	renderer->gl.d.init(&renderer->gl.d, 0);
102	return true;
103}
104
105void GBASDLGLES2Runloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer) {
106	SDL_Event event;
107	struct VideoBackend* v = &renderer->gl.d;
108
109	while (context->state < THREAD_EXITING) {
110		while (SDL_PollEvent(&event)) {
111			GBASDLHandleEvent(context, &renderer->player, &event);
112		}
113
114		if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
115			v->postFrame(v, renderer->d.outputBuffer);
116		}
117		v->drawFrame(v);
118		GBASyncWaitFrameEnd(&context->sync);
119		eglSwapBuffers(renderer->display, renderer->surface);
120	}
121}
122
123void GBASDLGLES2Deinit(struct SDLSoftwareRenderer* renderer) {
124	if (renderer->gl.d.deinit) {
125		renderer->gl.d.deinit(&renderer->gl.d);
126	}
127	eglMakeCurrent(renderer->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
128	eglDestroySurface(renderer->display, renderer->surface);
129	eglDestroyContext(renderer->display, renderer->context);
130	eglTerminate(renderer->display);
131	free(renderer->d.outputBuffer);
132	bcm_host_deinit();
133}