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 mSDLGLES2Init(struct mSDLRenderer* renderer);
13static void mSDLGLES2RunloopGBA(struct mSDLRenderer* renderer, void* user);
14static void mSDLGLES2Deinit(struct mSDLRenderer* renderer);
15
16void mSDLGLES2Create(struct mSDLRenderer* renderer) {
17 renderer->init = mSDLGLES2Init;
18 renderer->deinit = mSDLGLES2Deinit;
19 renderer->runloop = mSDLGLES2RunloopGBA;
20}
21
22bool mSDLGLES2Init(struct mSDLRenderer* 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 mSDLGLCommonInit(renderer);
94#endif
95
96 renderer->d.outputBuffer = memalign(16, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
97 renderer->d.outputBufferStride = VIDEO_HORIZONTAL_PIXELS;
98
99 mGLES2ContextCreate(&renderer->gl2);
100 renderer->gl2.d.user = renderer;
101 renderer->gl2.d.lockAspectRatio = renderer->lockAspectRatio;
102 renderer->gl2.d.filter = renderer->filter;
103 renderer->gl2.d.swap = mSDLGLCommonSwap;
104 renderer->gl2.d.init(&renderer->gl2.d, 0);
105 renderer->gl2.d.setDimensions(&renderer->gl2.d, renderer->width, renderer->height);
106 return true;
107}
108
109void mSDLGLES2Runloop(struct mSDLRenderer* renderer, void* user) {
110 struct GBAThread* context = user;
111 SDL_Event event;
112 struct VideoBackend* v = &renderer->gl2.d;
113
114 while (context->state < THREAD_EXITING) {
115 while (SDL_PollEvent(&event)) {
116 mSDLHandleEventGBA(context, &renderer->player, &event);
117 }
118
119 if (mCoreSyncWaitFrameStart(&context->sync)) {
120 v->postFrame(v, renderer->d.outputBuffer);
121 }
122 mCoreSyncWaitFrameEnd(&context->sync);
123 v->drawFrame(v);
124#ifdef BUILD_RASPI
125 eglSwapBuffers(renderer->display, renderer->surface);
126#else
127 v->swap(v);
128#endif
129 }
130}
131
132void mSDLGLES2Deinit(struct mSDLRenderer* renderer) {
133 if (renderer->gl2.d.deinit) {
134 renderer->gl2.d.deinit(&renderer->gl2.d);
135 }
136#ifdef BUILD_RASPI
137 eglMakeCurrent(renderer->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138 eglDestroySurface(renderer->display, renderer->surface);
139 eglDestroyContext(renderer->display, renderer->context);
140 eglTerminate(renderer->display);
141 bcm_host_deinit();
142#elif SDL_VERSION_ATLEAST(2, 0, 0)
143 SDL_GL_DeleteContext(renderer->glCtx);
144#endif
145 free(renderer->d.outputBuffer);
146}