1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/command_line.h"
8 #include "gpu/command_buffer/client/gles2_lib.h"
9 #include "gpu/gles2_conform_support/egl/display.h"
10 #include "ui/gl/gl_context.h"
11 #include "ui/gl/gl_surface.h"
16 typedef EGLContext RegalSystemContext
;
18 REGAL_DECL
void RegalMakeCurrent( RegalSystemContext ctx
);
24 void SetCurrentError(EGLint error_code
) {
28 T
EglError(EGLint error_code
, T return_value
) {
29 SetCurrentError(error_code
);
34 T
EglSuccess(T return_value
) {
35 SetCurrentError(EGL_SUCCESS
);
39 EGLint
ValidateDisplay(EGLDisplay dpy
) {
40 if (dpy
== EGL_NO_DISPLAY
)
41 return EGL_BAD_DISPLAY
;
43 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
44 if (!display
->is_initialized())
45 return EGL_NOT_INITIALIZED
;
50 EGLint
ValidateDisplayConfig(EGLDisplay dpy
, EGLConfig config
) {
51 EGLint error_code
= ValidateDisplay(dpy
);
52 if (error_code
!= EGL_SUCCESS
)
55 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
56 if (!display
->IsValidConfig(config
))
57 return EGL_BAD_CONFIG
;
62 EGLint
ValidateDisplaySurface(EGLDisplay dpy
, EGLSurface surface
) {
63 EGLint error_code
= ValidateDisplay(dpy
);
64 if (error_code
!= EGL_SUCCESS
)
67 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
68 if (!display
->IsValidSurface(surface
))
69 return EGL_BAD_SURFACE
;
74 EGLint
ValidateDisplayContext(EGLDisplay dpy
, EGLContext context
) {
75 EGLint error_code
= ValidateDisplay(dpy
);
76 if (error_code
!= EGL_SUCCESS
)
79 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
80 if (!display
->IsValidContext(context
))
81 return EGL_BAD_CONTEXT
;
88 EGLAPI EGLint EGLAPIENTRY
eglGetError() {
89 // TODO(alokp): Fix me.
93 EGLAPI EGLDisplay EGLAPIENTRY
eglGetDisplay(EGLNativeDisplayType display_id
) {
94 return new egl::Display(display_id
);
97 EGLAPI EGLBoolean EGLAPIENTRY
eglInitialize(EGLDisplay dpy
,
100 if (dpy
== EGL_NO_DISPLAY
)
101 return EglError(EGL_BAD_DISPLAY
, EGL_FALSE
);
103 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
104 if (!display
->Initialize())
105 return EglError(EGL_NOT_INITIALIZED
, EGL_FALSE
);
107 // eglInitialize can be called multiple times, prevent InitializeOneOff from
108 // being called multiple times.
109 if (gfx::GetGLImplementation() == gfx::kGLImplementationNone
) {
111 const char* const argv
[] = {"dummy"};
112 base::CommandLine::Init(argc
, argv
);
113 gfx::GLSurface::InitializeOneOff();
118 return EglSuccess(EGL_TRUE
);
121 EGLAPI EGLBoolean EGLAPIENTRY
eglTerminate(EGLDisplay dpy
) {
122 EGLint error_code
= ValidateDisplay(dpy
);
123 if (error_code
!= EGL_SUCCESS
)
124 return EglError(error_code
, EGL_FALSE
);
126 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
129 return EglSuccess(EGL_TRUE
);
132 EGLAPI
const char* EGLAPIENTRY
eglQueryString(EGLDisplay dpy
, EGLint name
) {
133 EGLint error_code
= ValidateDisplay(dpy
);
134 if (error_code
!= EGL_SUCCESS
)
135 return EglError(error_code
, static_cast<const char*>(NULL
));
138 case EGL_CLIENT_APIS
:
139 return EglSuccess("OpenGL_ES");
141 return EglSuccess("");
143 return EglSuccess("Google Inc.");
145 return EglSuccess("1.4");
147 return EglError(EGL_BAD_PARAMETER
, static_cast<const char*>(NULL
));
151 EGLAPI EGLBoolean EGLAPIENTRY
eglChooseConfig(EGLDisplay dpy
,
152 const EGLint
* attrib_list
,
155 EGLint
* num_config
) {
156 EGLint error_code
= ValidateDisplay(dpy
);
157 if (error_code
!= EGL_SUCCESS
)
158 return EglError(error_code
, EGL_FALSE
);
160 if (num_config
== NULL
)
161 return EglError(EGL_BAD_PARAMETER
, EGL_FALSE
);
163 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
164 if (!display
->ChooseConfigs(configs
, config_size
, num_config
))
165 return EglError(EGL_BAD_ATTRIBUTE
, EGL_FALSE
);
167 return EglSuccess(EGL_TRUE
);
170 EGLAPI EGLBoolean EGLAPIENTRY
eglGetConfigs(EGLDisplay dpy
,
173 EGLint
* num_config
) {
174 EGLint error_code
= ValidateDisplay(dpy
);
175 if (error_code
!= EGL_SUCCESS
)
176 return EglError(error_code
, EGL_FALSE
);
178 if (num_config
== NULL
)
179 return EglError(EGL_BAD_PARAMETER
, EGL_FALSE
);
181 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
182 if (!display
->GetConfigs(configs
, config_size
, num_config
))
183 return EglError(EGL_BAD_ATTRIBUTE
, EGL_FALSE
);
185 return EglSuccess(EGL_TRUE
);
188 EGLAPI EGLBoolean EGLAPIENTRY
eglGetConfigAttrib(EGLDisplay dpy
,
192 EGLint error_code
= ValidateDisplayConfig(dpy
, config
);
193 if (error_code
!= EGL_SUCCESS
)
194 return EglError(error_code
, EGL_FALSE
);
196 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
197 if (!display
->GetConfigAttrib(config
, attribute
, value
))
198 return EglError(EGL_BAD_ATTRIBUTE
, EGL_FALSE
);
200 return EglSuccess(EGL_TRUE
);
203 EGLAPI EGLSurface EGLAPIENTRY
204 eglCreateWindowSurface(EGLDisplay dpy
,
206 EGLNativeWindowType win
,
207 const EGLint
* attrib_list
) {
208 EGLint error_code
= ValidateDisplayConfig(dpy
, config
);
209 if (error_code
!= EGL_SUCCESS
)
210 return EglError(error_code
, EGL_NO_SURFACE
);
212 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
213 if (!display
->IsValidNativeWindow(win
))
214 return EglError(EGL_BAD_NATIVE_WINDOW
, EGL_NO_SURFACE
);
216 EGLSurface surface
= display
->CreateWindowSurface(config
, win
, attrib_list
);
217 if (surface
== EGL_NO_SURFACE
)
218 return EglError(EGL_BAD_ALLOC
, EGL_NO_SURFACE
);
220 return EglSuccess(surface
);
223 EGLAPI EGLSurface EGLAPIENTRY
224 eglCreatePbufferSurface(EGLDisplay dpy
,
226 const EGLint
* attrib_list
) {
227 EGLint error_code
= ValidateDisplayConfig(dpy
, config
);
228 if (error_code
!= EGL_SUCCESS
)
229 return EglError(error_code
, EGL_NO_SURFACE
);
231 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
235 for (const int32_t* attr
= attrib_list
; attr
[0] != EGL_NONE
; attr
+= 2) {
246 display
->SetCreateOffscreen(width
, height
);
248 EGLSurface surface
= display
->CreateWindowSurface(config
, 0, attrib_list
);
249 if (surface
== EGL_NO_SURFACE
)
250 return EglError(EGL_BAD_ALLOC
, EGL_NO_SURFACE
);
252 return EglSuccess(surface
);
255 EGLAPI EGLSurface EGLAPIENTRY
256 eglCreatePixmapSurface(EGLDisplay dpy
,
258 EGLNativePixmapType pixmap
,
259 const EGLint
* attrib_list
) {
260 return EGL_NO_SURFACE
;
263 EGLAPI EGLBoolean EGLAPIENTRY
eglDestroySurface(EGLDisplay dpy
,
264 EGLSurface surface
) {
265 EGLint error_code
= ValidateDisplaySurface(dpy
, surface
);
266 if (error_code
!= EGL_SUCCESS
)
267 return EglError(error_code
, EGL_FALSE
);
269 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
270 display
->DestroySurface(surface
);
271 return EglSuccess(EGL_TRUE
);
274 EGLAPI EGLBoolean EGLAPIENTRY
eglQuerySurface(EGLDisplay dpy
,
281 EGLAPI EGLBoolean EGLAPIENTRY
eglBindAPI(EGLenum api
) {
285 EGLAPI EGLenum EGLAPIENTRY
eglQueryAPI() {
286 return EGL_OPENGL_ES_API
;
289 EGLAPI EGLBoolean EGLAPIENTRY
eglWaitClient(void) {
293 EGLAPI EGLBoolean EGLAPIENTRY
eglReleaseThread(void) {
297 EGLAPI EGLSurface EGLAPIENTRY
298 eglCreatePbufferFromClientBuffer(EGLDisplay dpy
,
300 EGLClientBuffer buffer
,
302 const EGLint
* attrib_list
) {
303 return EGL_NO_SURFACE
;
306 EGLAPI EGLBoolean EGLAPIENTRY
eglSurfaceAttrib(EGLDisplay dpy
,
313 EGLAPI EGLBoolean EGLAPIENTRY
eglBindTexImage(EGLDisplay dpy
,
319 EGLAPI EGLBoolean EGLAPIENTRY
eglReleaseTexImage(EGLDisplay dpy
,
325 EGLAPI EGLBoolean EGLAPIENTRY
eglSwapInterval(EGLDisplay dpy
, EGLint interval
) {
329 EGLAPI EGLContext EGLAPIENTRY
eglCreateContext(EGLDisplay dpy
,
331 EGLContext share_context
,
332 const EGLint
* attrib_list
) {
333 EGLint error_code
= ValidateDisplayConfig(dpy
, config
);
334 if (error_code
!= EGL_SUCCESS
)
335 return EglError(error_code
, EGL_NO_CONTEXT
);
337 if (share_context
!= EGL_NO_CONTEXT
) {
338 error_code
= ValidateDisplayContext(dpy
, share_context
);
339 if (error_code
!= EGL_SUCCESS
)
340 return EglError(error_code
, EGL_NO_CONTEXT
);
343 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
344 EGLContext context
= display
->CreateContext(
345 config
, share_context
, attrib_list
);
346 if (context
== EGL_NO_CONTEXT
)
347 return EglError(EGL_BAD_ALLOC
, EGL_NO_CONTEXT
);
349 return EglSuccess(context
);
352 EGLAPI EGLBoolean EGLAPIENTRY
eglDestroyContext(EGLDisplay dpy
,
354 EGLint error_code
= ValidateDisplayContext(dpy
, ctx
);
355 if (error_code
!= EGL_SUCCESS
)
356 return EglError(error_code
, EGL_FALSE
);
358 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
359 display
->DestroyContext(ctx
);
363 EGLAPI EGLBoolean EGLAPIENTRY
eglMakeCurrent(EGLDisplay dpy
,
367 if (ctx
!= EGL_NO_CONTEXT
) {
368 EGLint error_code
= ValidateDisplaySurface(dpy
, draw
);
369 if (error_code
!= EGL_SUCCESS
)
370 return EglError(error_code
, EGL_FALSE
);
371 error_code
= ValidateDisplaySurface(dpy
, read
);
372 if (error_code
!= EGL_SUCCESS
)
373 return EglError(error_code
, EGL_FALSE
);
374 error_code
= ValidateDisplayContext(dpy
, ctx
);
375 if (error_code
!= EGL_SUCCESS
)
376 return EglError(error_code
, EGL_FALSE
);
379 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
380 if (!display
->MakeCurrent(draw
, read
, ctx
))
381 return EglError(EGL_CONTEXT_LOST
, EGL_FALSE
);
384 RegalMakeCurrent(ctx
);
390 EGLAPI EGLContext EGLAPIENTRY
eglGetCurrentContext() {
391 return EGL_NO_CONTEXT
;
394 EGLAPI EGLSurface EGLAPIENTRY
eglGetCurrentSurface(EGLint readdraw
) {
395 return EGL_NO_SURFACE
;
398 EGLAPI EGLDisplay EGLAPIENTRY
eglGetCurrentDisplay() {
399 return EGL_NO_DISPLAY
;
402 EGLAPI EGLBoolean EGLAPIENTRY
eglQueryContext(EGLDisplay dpy
,
409 EGLAPI EGLBoolean EGLAPIENTRY
eglWaitGL() {
413 EGLAPI EGLBoolean EGLAPIENTRY
eglWaitNative(EGLint engine
) {
417 EGLAPI EGLBoolean EGLAPIENTRY
eglSwapBuffers(EGLDisplay dpy
,
418 EGLSurface surface
) {
419 EGLint error_code
= ValidateDisplaySurface(dpy
, surface
);
420 if (error_code
!= EGL_SUCCESS
)
421 return EglError(error_code
, EGL_FALSE
);
423 egl::Display
* display
= static_cast<egl::Display
*>(dpy
);
424 display
->SwapBuffers(surface
);
425 return EglSuccess(EGL_TRUE
);
428 EGLAPI EGLBoolean EGLAPIENTRY
eglCopyBuffers(EGLDisplay dpy
,
430 EGLNativePixmapType target
) {
434 /* Now, define eglGetProcAddress using the generic function ptr. type */
435 EGLAPI __eglMustCastToProperFunctionPointerType EGLAPIENTRY
436 eglGetProcAddress(const char* procname
) {
437 return reinterpret_cast<__eglMustCastToProperFunctionPointerType
>(
438 gles2::GetGLFunctionPointer(procname
));