7dcfa0b69912c55984472cc0e25ac460eb36a29d
[android/external-libkmsxx.git] / kmscube / cube-egl.cpp
1 #include "cube-egl.h"
2 #include "cube.h"
4 #include <kms++util/kms++util.h>
6 using namespace std;
8 EglState::EglState(void *native_display)
9 {
10         EGLBoolean b;
11         EGLint major, minor, n;
13         static const EGLint context_attribs[] = {
14                 EGL_CONTEXT_CLIENT_VERSION, 2,
15                 EGL_NONE
16         };
18         static const EGLint config_attribs[] = {
19                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
20                 EGL_RED_SIZE, 8,
21                 EGL_GREEN_SIZE, 8,
22                 EGL_BLUE_SIZE, 8,
23                 EGL_ALPHA_SIZE, 0,
24                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
25                 EGL_NONE
26         };
28         m_display = eglGetDisplay((EGLNativeDisplayType)native_display);
29         FAIL_IF(!m_display, "failed to get egl display");
31         b = eglInitialize(m_display, &major, &minor);
32         FAIL_IF(!b, "failed to initialize");
34         if (s_verbose) {
35                 printf("Using display %p with EGL version %d.%d\n", m_display, major, minor);
37                 printf("EGL_VENDOR:      %s\n", eglQueryString(m_display, EGL_VENDOR));
38                 printf("EGL_VERSION:     %s\n", eglQueryString(m_display, EGL_VERSION));
39                 printf("EGL_EXTENSIONS:  %s\n", eglQueryString(m_display, EGL_EXTENSIONS));
40                 printf("EGL_CLIENT_APIS: %s\n", eglQueryString(m_display, EGL_CLIENT_APIS));
41         }
43         b = eglBindAPI(EGL_OPENGL_ES_API);
44         FAIL_IF(!b, "failed to bind api EGL_OPENGL_ES_API");
46         b = eglChooseConfig(m_display, config_attribs, &m_config, 1, &n);
47         FAIL_IF(!b || n != 1, "failed to choose config");
49         auto getconf = [this](EGLint a) { EGLint v = -1; eglGetConfigAttrib(m_display, m_config, a, &v); return v; };
51         if (s_verbose) {
52                 printf("EGL Config %d: color buf %d/%d/%d/%d = %d, depth %d, stencil %d\n",
53                        getconf(EGL_CONFIG_ID),
54                        getconf(EGL_ALPHA_SIZE),
55                        getconf(EGL_RED_SIZE),
56                        getconf(EGL_GREEN_SIZE),
57                        getconf(EGL_BLUE_SIZE),
58                        getconf(EGL_BUFFER_SIZE),
59                        getconf(EGL_DEPTH_SIZE),
60                        getconf(EGL_STENCIL_SIZE));
61         }
63         m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, context_attribs);
64         FAIL_IF(!m_context, "failed to create context");
66         EGLBoolean ok = eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_context);
67         FAIL_IF(!ok, "eglMakeCurrent() failed");
68 }
70 EglState::~EglState()
71 {
72         eglMakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
73         eglTerminate(m_display);
74 }
76 EglSurface::EglSurface(const EglState &egl, void *native_window)
77         : egl(egl)
78 {
79         esurface = eglCreateWindowSurface(egl.display(), egl.config(), (EGLNativeWindowType)native_window, NULL);
80         FAIL_IF(esurface == EGL_NO_SURFACE, "failed to create egl surface");
81 }
83 EglSurface::~EglSurface()
84 {
85         eglDestroySurface(egl.display(), esurface);
86 }
88 void EglSurface::make_current()
89 {
90         EGLBoolean ok = eglMakeCurrent(egl.display(), esurface, esurface, egl.context());
91         FAIL_IF(!ok, "eglMakeCurrent() failed");
92 }
94 void EglSurface::swap_buffers()
95 {
96         EGLBoolean ok = eglSwapBuffers(egl.display(), esurface);
97         FAIL_IF(!ok, "eglMakeCurrent() failed");
98 }