Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / util / EGLWindow.cpp
1 //
2 // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #include <cassert>
8
9 #include "EGLWindow.h"
10 #include "OSWindow.h"
11
12 #ifdef _WIN32
13 #include "win32/Win32Timer.h"
14 #include "win32/Win32Window.h"
15 #else
16 #error unsupported OS.
17 #endif
18
19 EGLWindow::EGLWindow(size_t width, size_t height,
20                      EGLint glesMajorVersion, EGLint requestedRenderer)
21     : mSurface(EGL_NO_SURFACE),
22       mContext(EGL_NO_CONTEXT),
23       mDisplay(EGL_NO_DISPLAY),
24       mClientVersion(glesMajorVersion),
25       mRequestedRenderer(requestedRenderer),
26       mWidth(width),
27       mHeight(height),
28       mRedBits(-1),
29       mGreenBits(-1),
30       mBlueBits(-1),
31       mAlphaBits(-1),
32       mDepthBits(-1),
33       mStencilBits(-1),
34       mMultisample(false),
35       mSwapInterval(-1)
36 {
37 }
38
39 EGLWindow::~EGLWindow()
40 {
41     destroyGL();
42 }
43
44 void EGLWindow::swap()
45 {
46     eglSwapBuffers(mDisplay, mSurface);
47 }
48
49 EGLConfig EGLWindow::getConfig() const
50 {
51     return mConfig;
52 }
53
54 EGLDisplay EGLWindow::getDisplay() const
55 {
56     return mDisplay;
57 }
58
59 EGLSurface EGLWindow::getSurface() const
60 {
61     return mSurface;
62 }
63
64 EGLContext EGLWindow::getContext() const
65 {
66     return mContext;
67 }
68
69 bool EGLWindow::initializeGL(OSWindow *osWindow)
70 {
71     PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT"));
72     if (!eglGetPlatformDisplayEXT)
73     {
74         return false;
75     }
76
77     const EGLint displayAttributes[] =
78     {
79         EGL_PLATFORM_ANGLE_TYPE_ANGLE, mRequestedRenderer,
80         EGL_NONE,
81     };
82
83     mDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, osWindow->getNativeDisplay(), displayAttributes);
84     if (mDisplay == EGL_NO_DISPLAY)
85     {
86         destroyGL();
87         return false;
88     }
89
90     EGLint majorVersion, minorVersion;
91     if (!eglInitialize(mDisplay, &majorVersion, &minorVersion))
92     {
93         destroyGL();
94         return false;
95     }
96
97     eglBindAPI(EGL_OPENGL_ES_API);
98     if (eglGetError() != EGL_SUCCESS)
99     {
100         destroyGL();
101         return false;
102     }
103
104     const EGLint configAttributes[] =
105     {
106         EGL_RED_SIZE,       (mRedBits >= 0)     ? mRedBits     : EGL_DONT_CARE,
107         EGL_GREEN_SIZE,     (mGreenBits >= 0)   ? mGreenBits   : EGL_DONT_CARE,
108         EGL_BLUE_SIZE,      (mBlueBits >= 0)    ? mBlueBits    : EGL_DONT_CARE,
109         EGL_ALPHA_SIZE,     (mAlphaBits >= 0)   ? mAlphaBits   : EGL_DONT_CARE,
110         EGL_DEPTH_SIZE,     (mDepthBits >= 0)   ? mDepthBits   : EGL_DONT_CARE,
111         EGL_STENCIL_SIZE,   (mStencilBits >= 0) ? mStencilBits : EGL_DONT_CARE,
112         EGL_SAMPLE_BUFFERS, mMultisample ? 1 : 0,
113         EGL_NONE
114     };
115
116     EGLint configCount;
117     if (!eglChooseConfig(mDisplay, configAttributes, &mConfig, 1, &configCount) || (configCount != 1))
118     {
119         destroyGL();
120         return false;
121     }
122
123     eglGetConfigAttrib(mDisplay, mConfig, EGL_RED_SIZE, &mRedBits);
124     eglGetConfigAttrib(mDisplay, mConfig, EGL_GREEN_SIZE, &mGreenBits);
125     eglGetConfigAttrib(mDisplay, mConfig, EGL_BLUE_SIZE, &mBlueBits);
126     eglGetConfigAttrib(mDisplay, mConfig, EGL_ALPHA_SIZE, &mBlueBits);
127     eglGetConfigAttrib(mDisplay, mConfig, EGL_DEPTH_SIZE, &mDepthBits);
128     eglGetConfigAttrib(mDisplay, mConfig, EGL_STENCIL_SIZE, &mStencilBits);
129
130     const EGLint surfaceAttributes[] =
131     {
132         EGL_POST_SUB_BUFFER_SUPPORTED_NV, EGL_TRUE,
133         EGL_NONE, EGL_NONE,
134     };
135
136     mSurface = eglCreateWindowSurface(mDisplay, mConfig, osWindow->getNativeWindow(), surfaceAttributes);
137     if (mSurface == EGL_NO_SURFACE)
138     {
139         eglGetError(); // Clear error and try again
140         mSurface = eglCreateWindowSurface(mDisplay, mConfig, NULL, NULL);
141     }
142
143     if (eglGetError() != EGL_SUCCESS)
144     {
145         destroyGL();
146         return false;
147     }
148
149     EGLint contextAttibutes[] =
150     {
151         EGL_CONTEXT_CLIENT_VERSION, mClientVersion,
152         EGL_NONE
153     };
154
155     mContext = eglCreateContext(mDisplay, mConfig, NULL, contextAttibutes);
156     if (eglGetError() != EGL_SUCCESS)
157     {
158         destroyGL();
159         return false;
160     }
161
162     eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
163     if (eglGetError() != EGL_SUCCESS)
164     {
165         destroyGL();
166         return false;
167     }
168
169     if (mSwapInterval != -1)
170     {
171         eglSwapInterval(mDisplay, mSwapInterval);
172     }
173
174     return true;
175 }
176
177 void EGLWindow::destroyGL()
178 {
179     if (mSurface != EGL_NO_SURFACE)
180     {
181         assert(mDisplay != EGL_NO_DISPLAY);
182         eglDestroySurface(mDisplay, mSurface);
183         mSurface = EGL_NO_SURFACE;
184     }
185
186     if (mContext != EGL_NO_CONTEXT)
187     {
188         assert(mDisplay != EGL_NO_DISPLAY);
189         eglDestroyContext(mDisplay, mContext);
190         mContext = EGL_NO_CONTEXT;
191     }
192
193     if (mDisplay != EGL_NO_DISPLAY)
194     {
195         eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
196         eglTerminate(mDisplay);
197         mDisplay = EGL_NO_DISPLAY;
198     }
199 }