Search
lxdream.org :: lxdream/src/drivers/video_egl.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_egl.c
changeset 1245:01e0020adf88
next1251:b8ab59d39756
author nkeynes
date Sat Mar 03 00:17:36 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Fix GLX compile
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/video_egl.c Sat Mar 03 00:17:36 2012 +1000
1.3 @@ -0,0 +1,168 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * Window management using EGL.
1.8 + *
1.9 + * Copyright (c) 2012 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include "lxdream.h"
1.23 +#include "display.h"
1.24 +#include "video_egl.h"
1.25 +#include "video_gl.h"
1.26 +#include "pvr2/pvr2.h"
1.27 +#include "pvr2/glutil.h"
1.28 +
1.29 +static const char *getEGLErrorString( EGLint code )
1.30 +{
1.31 + switch( code ) {
1.32 + case EGL_SUCCESS: return "OK";
1.33 + case EGL_NOT_INITIALIZED: return "EGL not initialized";
1.34 + case EGL_BAD_ACCESS: return "Bad access";
1.35 + case EGL_BAD_ALLOC: return "Allocation failed";
1.36 + case EGL_BAD_ATTRIBUTE: return "Bad attribute";
1.37 + case EGL_BAD_CONTEXT: return "Bad context";
1.38 + case EGL_BAD_CONFIG: return "Bad config";
1.39 + case EGL_BAD_CURRENT_SURFACE: return "Bad current surface";
1.40 + case EGL_BAD_DISPLAY: return "Bad display";
1.41 + case EGL_BAD_MATCH: return "Bad match";
1.42 + case EGL_BAD_PARAMETER: return "Bad parameter";
1.43 + case EGL_BAD_NATIVE_PIXMAP: return "Bad native pixmap";
1.44 + case EGL_BAD_NATIVE_WINDOW: return "Bad native window";
1.45 + default: return "Unknown error";
1.46 + }
1.47 +}
1.48 +
1.49 +
1.50 +static void logEGLError(const char *msg)
1.51 +{
1.52 + EGLint error = eglGetError();
1.53 + const char *errorStr = getEGLErrorString(error);
1.54 +
1.55 + ERROR( "%s: %s (%x)", msg, errorStr, error );
1.56 +}
1.57 +
1.58 +static const EGLint RGB888_attributes[] = {
1.59 + EGL_RED_SIZE, 8,
1.60 + EGL_GREEN_SIZE, 8,
1.61 + EGL_BLUE_SIZE, 8,
1.62 + EGL_DEPTH_SIZE, 16,
1.63 + EGL_STENCIL_SIZE, 8,
1.64 + EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1.65 + EGL_RENDERABLE_TYPE,EGL_OPENGL_ES2_BIT,
1.66 + EGL_NONE, EGL_NONE };
1.67 +
1.68 +static const EGLint RGB565_attributes[] = {
1.69 + EGL_RED_SIZE, 5,
1.70 + EGL_GREEN_SIZE, 6,
1.71 + EGL_BLUE_SIZE, 5,
1.72 + EGL_DEPTH_SIZE, 16,
1.73 + EGL_STENCIL_SIZE, 8,
1.74 + EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
1.75 + EGL_RENDERABLE_TYPE,EGL_OPENGL_ES2_BIT,
1.76 + EGL_NONE, EGL_NONE };
1.77 +
1.78 +static const EGLint context_attributes[] = {
1.79 + EGL_CONTEXT_CLIENT_VERSION, 2,
1.80 + EGL_NONE, EGL_NONE };
1.81 +
1.82 +static EGLDisplay display;
1.83 +static EGLContext context = EGL_NO_CONTEXT;
1.84 +static EGLSurface surface = EGL_NO_SURFACE;
1.85 +static gboolean fbo_created = FALSE;
1.86 +
1.87 +gboolean video_egl_set_window(EGLNativeWindowType window, int width, int height, int format)
1.88 +{
1.89 + EGLConfig config;
1.90 + EGLint num_config, major = 0, minor = 0;
1.91 + const EGLint *attribute_list;
1.92 +
1.93 + display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1.94 + if( eglInitialize(display, &major, &minor) != EGL_TRUE ) {
1.95 + logEGLError( "Unable to initialise EGL display" );
1.96 + return FALSE;
1.97 + }
1.98 +
1.99 + if( format == COLFMT_RGB565 || format == COLFMT_BGRA1555 ) {
1.100 + attribute_list = RGB565_attributes;
1.101 + } else {
1.102 + attribute_list = RGB888_attributes;
1.103 + }
1.104 +
1.105 +
1.106 + eglChooseConfig(display, attribute_list, &config, 1, &num_config);
1.107 +
1.108 + context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes);
1.109 + if( context == EGL_NO_CONTEXT ) {
1.110 + logEGLError( "Unable to create EGL context" );
1.111 + video_egl_clear_window();
1.112 + return FALSE;
1.113 + }
1.114 +
1.115 + surface = eglCreateWindowSurface(display, config, window, NULL);
1.116 + if( surface == EGL_NO_SURFACE ) {
1.117 + logEGLError( "Unable to create EGL surface" );
1.118 + video_egl_clear_window();
1.119 + return FALSE;
1.120 + }
1.121 +
1.122 + if( eglMakeCurrent( display, surface, surface, context ) == EGL_FALSE ) {
1.123 + video_egl_clear_window();
1.124 + return FALSE;
1.125 + }
1.126 +
1.127 + if( gl_fbo_is_supported() ) {
1.128 + display_gl_driver.capabilities.has_gl = TRUE;
1.129 + gl_fbo_init(&display_egl_driver);
1.130 + gl_vbo_init(&display_egl_driver);
1.131 + fbo_created = TRUE;
1.132 + } else {
1.133 + ERROR( "Display does not support FBO" );
1.134 + video_egl_clear_window();
1.135 + return FALSE;
1.136 + }
1.137 + pvr2_setup_gl_context();
1.138 + INFO( "Initialised EGL %d.%d\n", major, minor );
1.139 + return TRUE;
1.140 +}
1.141 +
1.142 +void video_egl_clear_window()
1.143 +{
1.144 + if( fbo_created ) {
1.145 + gl_fbo_shutdown();
1.146 + fbo_created = FALSE;
1.147 + }
1.148 + eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1.149 + if( surface != EGL_NO_SURFACE ) {
1.150 + eglDestroySurface(display, surface);
1.151 + surface = EGL_NO_SURFACE;
1.152 + }
1.153 + if( context != EGL_NO_CONTEXT ) {
1.154 + eglDestroyContext(display, context);
1.155 + context = EGL_NO_CONTEXT;
1.156 + }
1.157 + eglTerminate(display);
1.158 +}
1.159 +
1.160 +
1.161 +
1.162 +/**
1.163 + * Minimal init and shutdown. The real work is done from set_window
1.164 + */
1.165 +struct display_driver display_egl_driver = {
1.166 + "egl", N_("OpenGLES driver"), NULL, NULL,
1.167 + NULL, NULL, NULL,
1.168 + NULL, NULL, NULL, NULL,
1.169 + gl_load_frame_buffer, gl_display_render_buffer, gl_display_blank,
1.170 + NULL, gl_read_render_buffer, NULL, NULL
1.171 +};
.