warning message on camera initialization failure added
[apps/native/sample/QrCodeRecognizer.git] / project / src / GLtools.cpp
1 //
2 // Tizen Native SDK
3 // Open Service Platform
4 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 // All rights reserved.
6 //
7 // This software contains confidential and proprietary information
8 // of Samsung Electronics Co., Ltd.
9 // The user of this software agrees not to disclose, disseminate or copy such
10 // Confidential Information and shall use the software only in accordance with
11 // the terms of the license agreement the user entered into with Samsung.
12 //
13
14 #include "GLtools.h"
15
16 using namespace Tizen::Graphics::Opengl;
17
18 EGLDisplay               GLtools::eglDisplay(EGL_DEFAULT_DISPLAY);
19 EGLSurface               GLtools::eglSurface(EGL_NO_SURFACE);
20 EGLConfig                GLtools::eglConfig(null);
21 EGLContext               GLtools::eglContext(EGL_NO_DISPLAY);
22 Tizen::Ui::Container*    GLtools::pContainer(null);
23 int                      GLtools::x(0);
24 int                      GLtools::y(0);
25 int                      GLtools::screenWidth(0);
26 int                      GLtools::screenHeight(0);
27
28
29 Tizen::Ui::Container*
30 GLtools::getCurrentGLContainer()
31 {
32     return pContainer;
33 }
34
35 bool
36 GLtools::initEGL()
37 {
38     EGLint numConfigs = 1;
39     EGLint eglConfigList[] = {
40             EGL_RED_SIZE,       5,
41             EGL_GREEN_SIZE,     6,
42             EGL_BLUE_SIZE,      5,
43             EGL_ALPHA_SIZE,     0,
44             EGL_DEPTH_SIZE, 8,
45             EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
46             EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
47             EGL_NONE
48     };
49     EGLint eglContextList[] = {
50             EGL_CONTEXT_CLIENT_VERSION, 2,
51             EGL_NONE
52     };
53
54     eglBindAPI(EGL_OPENGL_ES_API);
55
56     if (eglDisplay)
57     {
58         return false;
59     }
60
61     eglDisplay = eglGetDisplay((EGLNativeDisplayType) EGL_DEFAULT_DISPLAY);
62     if (EGL_NO_DISPLAY == eglDisplay)
63     {
64         return false;
65     }
66
67     if (EGL_FALSE == eglInitialize(eglDisplay, null, null) || EGL_SUCCESS != eglGetError())
68     {
69         return false;
70     }
71
72     if (EGL_FALSE == eglChooseConfig(eglDisplay, eglConfigList, &eglConfig, 1, &numConfigs) || EGL_SUCCESS != eglGetError())
73     {
74         return false;
75     }
76
77     if (!numConfigs)
78     {
79         return false;
80     }
81
82     eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType) pContainer, null);
83
84     if (EGL_NO_SURFACE == eglSurface || EGL_SUCCESS != eglGetError())
85     {
86         return false;
87     }
88
89     eglContext = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, eglContextList);
90     if (EGL_NO_CONTEXT == eglContext || EGL_SUCCESS != eglGetError())
91     {
92         return false;
93     }
94
95     if (EGL_FALSE == eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) || EGL_SUCCESS != eglGetError())
96     {
97         return false;
98     }
99
100     return true;
101 }
102
103 bool
104 GLtools::initGL(Tizen::Ui::Container *const container)
105 {
106     if(pContainer == container)
107         return true;
108
109     if(pContainer)
110         destroyGL();
111     pContainer = container;
112
113     if (!initEGL())
114     {
115         destroyGL();
116         pContainer        = null;
117         x            = 0;
118         y            = 0;
119         screenWidth  = 0;
120         screenHeight = 0;
121         return false;
122     }
123
124     pContainer->GetBounds(x, y, screenWidth, screenHeight);
125
126     return true;
127 }
128
129 void
130 GLtools::destroyGL()
131 {
132     if (eglDisplay)
133     {
134         eglMakeCurrent(eglDisplay, null, null, null);
135
136         if (eglContext)
137         {
138             eglDestroyContext(eglDisplay, eglContext);
139             eglContext = EGL_NO_CONTEXT;
140         }
141
142         if (eglSurface)
143         {
144             //eglDestroySurface(eglDisplay, eglSurface);
145             eglSurface = EGL_NO_SURFACE;
146         }
147
148         //eglTerminate(eglDisplay);
149         eglDisplay = EGL_NO_DISPLAY;
150     }
151
152     pContainer = null;
153     return;
154 }
155
156 bool
157 GLtools::startDrawing()
158 {
159     if (!pContainer->IsEnabled()) return false;
160
161     if (GL_FALSE == eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) || EGL_SUCCESS != eglGetError()) return false;
162
163     glViewport(x, y, screenWidth, screenHeight);
164
165     glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
166
167     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
168
169     return true;
170 }
171
172
173 void
174 GLtools::finishDrawing(void)
175 {
176     //Causing image flickering. 2.1.0_RC12_M5
177     //glFlush();
178     glFinish();
179     eglSwapBuffers(eglDisplay, eglSurface);
180 }
181
182 int
183 GLtools::getWidth(void)
184 {
185     return (screenWidth);
186 }
187
188 int
189 GLtools::getHeight(void)
190 {
191     return (screenHeight);
192 }