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