1 /**************************************************************************
2 *
3 * Copyright 2010, 2011 BMW Car IT GmbH
4 * Copyright (C) 2011 DENSO CORPORATION and Robert Bosch Car Multimedia Gmbh
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ****************************************************************************/
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #include "WLEGLSurface.h"
25
26 //////////////////////////////////////////////////////////////////////////////
27 #define CHECK_EGL_ERROR(FUNC, STAT) \
28 STAT = eglGetError(); \
29 if (STAT != EGL_SUCCESS){ \
30 fprintf(stderr, "[ERROR] %s failed: %d\n", FUNC, STAT); \
31 return false; \
32 }
33
34 //////////////////////////////////////////////////////////////////////////////
35
WLEGLSurface(WLContext * wlContext)36 WLEGLSurface::WLEGLSurface(WLContext* wlContext)
37 : WLSurface(wlContext)
38 , m_wlEglWindow(NULL)
39 , m_eglDisplay(EGL_NO_DISPLAY)
40 , m_eglConfig(0)
41 , m_eglSurface(EGL_NO_SURFACE)
42 , m_eglContext(EGL_NO_CONTEXT)
43 {
44 }
45
~WLEGLSurface()46 WLEGLSurface::~WLEGLSurface()
47 {
48 if (m_wlEglWindow)
49 wl_egl_window_destroy(m_wlEglWindow);
50 eglMakeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
51 eglTerminate(m_eglDisplay);
52 }
53
54 bool
CreatePlatformSurface()55 WLEGLSurface::CreatePlatformSurface()
56 {
57 EGLint eglStat = EGL_SUCCESS;
58 EGLint major, minor;
59 int nConfig;
60 EGLint configAttribs[] = {
61 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
62 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
63 EGL_RED_SIZE, 8,
64 EGL_GREEN_SIZE, 8,
65 EGL_BLUE_SIZE, 8,
66 EGL_ALPHA_SIZE, 8,
67 //EGL_SAMPLE_BUFFERS, 1,
68 //EGL_SAMPLES, 2,
69 EGL_NONE,
70 };
71 EGLint contextAttribs[] = {
72 EGL_CONTEXT_CLIENT_VERSION, 2,
73 EGL_NONE,
74 };
75
76 m_wlEglWindow = wl_egl_window_create(m_wlSurface, m_width, m_height);
77
78 m_eglDisplay = eglGetDisplay((EGLNativeDisplayType)m_wlContext->GetWLDisplay());
79 CHECK_EGL_ERROR("eglGetDisplay", eglStat);
80
81 if (!eglInitialize(m_eglDisplay, &major, &minor)){
82 CHECK_EGL_ERROR("eglInitialize", eglStat);
83 }
84
85 eglBindAPI(EGL_OPENGL_ES_API);
86 CHECK_EGL_ERROR("eglBindAPI", eglStat);
87
88 if (!eglChooseConfig(m_eglDisplay, configAttribs, &m_eglConfig, 1, &nConfig)
89 || (nConfig != 1)){
90 CHECK_EGL_ERROR("eglChooseConfig", eglStat);
91 }
92
93 m_eglSurface = eglCreateWindowSurface(m_eglDisplay, m_eglConfig,
94 (EGLNativeWindowType)m_wlEglWindow, NULL);
95 CHECK_EGL_ERROR("eglCreateWindowSurface", eglStat);
96
97 m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, NULL, contextAttribs);
98 CHECK_EGL_ERROR("eglCreateContext", eglStat);
99
100 eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext);
101 CHECK_EGL_ERROR("eglMakeCurrent", eglStat);
102
103 //eglSwapInterval(m_eglDisplay, 1);
104 //CHECK_EGL_ERROR("eglSwapInterval", eglStat);
105
106 return true;
107 }
108