src/platform/sdl/gles2-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
10#include <malloc.h>
11
12static bool GBASDLGLES2Init(struct SDLSoftwareRenderer* renderer);
13static void GBASDLGLES2Runloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer);
14static void GBASDLGLES2Deinit(struct SDLSoftwareRenderer* renderer);
15
16void GBASDLGLES2Create(struct SDLSoftwareRenderer* renderer) {
17 renderer->init = GBASDLGLES2Init;
18 renderer->deinit = GBASDLGLES2Deinit;
19 renderer->runloop = GBASDLGLES2Runloop;
20}
21
22bool GBASDLGLES2Init(struct SDLSoftwareRenderer* renderer) {
23#ifdef BUILD_RASPI
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#else
93 GBASDLGLCommonInit(renderer);
94#endif
95
96 renderer->d.outputBuffer = memalign(16, 256 * 256 * 4);
97 renderer->d.outputBufferStride = 256;
98
99 GBAGLES2ContextCreate(&renderer->gl);
100 renderer->gl.d.user = renderer;
101 renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
102 renderer->gl.d.filter = renderer->filter;
103 renderer->gl.d.swap = GBASDLGLCommonSwap;
104 renderer->gl.d.init(&renderer->gl.d, 0);
105 return true;
106}
107
108void GBASDLGLES2Runloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer) {
109 SDL_Event event;
110 struct VideoBackend* v = &renderer->gl.d;
111
112 while (context->state < THREAD_EXITING) {
113 while (SDL_PollEvent(&event)) {
114 GBASDLHandleEvent(context, &renderer->player, &event);
115 }
116
117 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
118 v->postFrame(v, renderer->d.outputBuffer);
119 }
120 v->drawFrame(v);
121 GBASyncWaitFrameEnd(&context->sync);
122#ifdef BUILD_RASPI
123 eglSwapBuffers(renderer->display, renderer->surface);
124#else
125 v->swap(v);
126#endif
127 }
128}
129
130void GBASDLGLES2Deinit(struct SDLSoftwareRenderer* renderer) {
131 if (renderer->gl.d.deinit) {
132 renderer->gl.d.deinit(&renderer->gl.d);
133 }
134#ifdef BUILD_RASPI
135 eglMakeCurrent(renderer->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
136 eglDestroySurface(renderer->display, renderer->surface);
137 eglDestroyContext(renderer->display, renderer->context);
138 eglTerminate(renderer->display);
139 bcm_host_deinit();
140#elif SDL_VERSION_ATLEAST(2, 0, 0)
141 SDL_GL_DeleteContext(renderer->glCtx);
142#endif
143 free(renderer->d.outputBuffer);
144}