all repos — mgba @ a714774a2598c043c3c4183ed2825fe8cbc1b748

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
 10#include <mgba/core/core.h>
 11#include <mgba/core/thread.h>
 12
 13#ifndef __APPLE__
 14#include <malloc.h>
 15#endif
 16
 17static bool mSDLGLES2Init(struct mSDLRenderer* renderer);
 18static void mSDLGLES2Runloop(struct mSDLRenderer* renderer, void* user);
 19static void mSDLGLES2Deinit(struct mSDLRenderer* renderer);
 20
 21void mSDLGLES2Create(struct mSDLRenderer* renderer) {
 22	renderer->init = mSDLGLES2Init;
 23	renderer->deinit = mSDLGLES2Deinit;
 24	renderer->runloop = mSDLGLES2Runloop;
 25}
 26
 27bool mSDLGLES2Init(struct mSDLRenderer* renderer) {
 28#ifdef BUILD_RASPI
 29	bcm_host_init();
 30	renderer->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
 31	int major, minor;
 32	if (EGL_FALSE == eglInitialize(renderer->display, &major, &minor)) {
 33		printf("Failed to initialize EGL");
 34		return false;
 35	}
 36
 37	if (EGL_FALSE == eglBindAPI(EGL_OPENGL_ES_API)) {
 38		printf("Failed to get GLES API");
 39		return false;
 40	}
 41
 42	const EGLint requestConfig[] = {
 43		EGL_RED_SIZE, 5,
 44		EGL_GREEN_SIZE, 5,
 45		EGL_BLUE_SIZE, 5,
 46		EGL_ALPHA_SIZE, 1,
 47		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
 48		EGL_NONE
 49	};
 50
 51	EGLConfig config;
 52	EGLint numConfigs;
 53
 54	if (EGL_FALSE == eglChooseConfig(renderer->display, requestConfig, &config, 1, &numConfigs)) {
 55		printf("Failed to choose EGL config\n");
 56		return false;
 57	}
 58
 59	const EGLint contextAttributes[] = {
 60		EGL_CONTEXT_CLIENT_VERSION, 2,
 61		EGL_NONE
 62	};
 63
 64	int dispWidth = 240, dispHeight = 160, adjWidth;
 65	renderer->context = eglCreateContext(renderer->display, config, EGL_NO_CONTEXT, contextAttributes);
 66	graphics_get_display_size(0, &dispWidth, &dispHeight);
 67	adjWidth = dispHeight / 2 * 3;
 68
 69	DISPMANX_DISPLAY_HANDLE_T display = vc_dispmanx_display_open(0);
 70	DISPMANX_UPDATE_HANDLE_T update = vc_dispmanx_update_start(0);
 71
 72	VC_RECT_T destRect = {
 73		.x = (dispWidth - adjWidth) / 2,
 74		.y = 0,
 75		.width = adjWidth,
 76		.height = dispHeight
 77	};
 78
 79	VC_RECT_T srcRect = {
 80		.x = 0,
 81		.y = 0,
 82		.width = 240 << 16,
 83		.height = 160 << 16
 84	};
 85
 86	DISPMANX_ELEMENT_HANDLE_T element = vc_dispmanx_element_add(update, display, 0, &destRect, 0, &srcRect, DISPMANX_PROTECTION_NONE, 0, 0, 0);
 87	vc_dispmanx_update_submit_sync(update);
 88
 89	renderer->window.element = element;
 90	renderer->window.width = dispWidth;
 91	renderer->window.height = dispHeight;
 92
 93	renderer->surface = eglCreateWindowSurface(renderer->display, config, &renderer->window, 0);
 94	if (EGL_FALSE == eglMakeCurrent(renderer->display, renderer->surface, renderer->surface, renderer->context)) {
 95		return false;
 96	}
 97#else
 98	mSDLGLCommonInit(renderer);
 99#endif
100
101	size_t size = toPow2(renderer->width) * toPow2(renderer->height) * BYTES_PER_PIXEL;
102#ifndef __APPLE__
103	renderer->outputBuffer = memalign(16, size);
104#else
105	posix_memalign((void**) &renderer->outputBuffer, 16, size);
106#endif
107	memset(renderer->outputBuffer, 0, size);
108	renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, toPow2(renderer->width));
109
110	mGLES2ContextCreate(&renderer->gl2);
111	renderer->gl2.d.user = renderer;
112	renderer->gl2.d.lockAspectRatio = renderer->lockAspectRatio;
113	renderer->gl2.d.lockIntegerScaling = renderer->lockIntegerScaling;
114	renderer->gl2.d.filter = renderer->filter;
115	renderer->gl2.d.swap = mSDLGLCommonSwap;
116	renderer->gl2.d.init(&renderer->gl2.d, 0);
117	renderer->gl2.d.setDimensions(&renderer->gl2.d, renderer->width, renderer->height);
118	return true;
119}
120
121void mSDLGLES2Runloop(struct mSDLRenderer* renderer, void* user) {
122	struct mCoreThread* context = user;
123	SDL_Event event;
124	struct VideoBackend* v = &renderer->gl2.d;
125
126	while (context->state < THREAD_EXITING) {
127		while (SDL_PollEvent(&event)) {
128			mSDLHandleEvent(context, &renderer->player, &event);
129		}
130
131		if (mCoreSyncWaitFrameStart(&context->sync)) {
132			v->postFrame(v, renderer->outputBuffer);
133		}
134		mCoreSyncWaitFrameEnd(&context->sync);
135		v->drawFrame(v);
136#ifdef BUILD_RASPI
137		eglSwapBuffers(renderer->display, renderer->surface);
138#else
139		v->swap(v);
140#endif
141	}
142}
143
144void mSDLGLES2Deinit(struct mSDLRenderer* renderer) {
145	if (renderer->gl2.d.deinit) {
146		renderer->gl2.d.deinit(&renderer->gl2.d);
147	}
148#ifdef BUILD_RASPI
149	eglMakeCurrent(renderer->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
150	eglDestroySurface(renderer->display, renderer->surface);
151	eglDestroyContext(renderer->display, renderer->context);
152	eglTerminate(renderer->display);
153	bcm_host_deinit();
154#elif SDL_VERSION_ATLEAST(2, 0, 0)
155	SDL_GL_DeleteContext(renderer->glCtx);
156#endif
157	free(renderer->outputBuffer);
158}