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