Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / gpu / gles2_conform_support / egl / egl.cc
blobe64285a02ea4f98b8ba16d281b6598989bf41d67
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.
5 #include <EGL/egl.h>
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"
13 #if REGAL_STATIC_EGL
14 extern "C" {
16 typedef EGLContext RegalSystemContext;
17 #define REGAL_DECL
18 REGAL_DECL void RegalMakeCurrent( RegalSystemContext ctx );
20 } // extern "C"
21 #endif
23 namespace {
24 void SetCurrentError(EGLint error_code) {
27 template<typename T>
28 T EglError(EGLint error_code, T return_value) {
29 SetCurrentError(error_code);
30 return return_value;
33 template<typename T>
34 T EglSuccess(T return_value) {
35 SetCurrentError(EGL_SUCCESS);
36 return return_value;
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;
47 return EGL_SUCCESS;
50 EGLint ValidateDisplayConfig(EGLDisplay dpy, EGLConfig config) {
51 EGLint error_code = ValidateDisplay(dpy);
52 if (error_code != EGL_SUCCESS)
53 return error_code;
55 egl::Display* display = static_cast<egl::Display*>(dpy);
56 if (!display->IsValidConfig(config))
57 return EGL_BAD_CONFIG;
59 return EGL_SUCCESS;
62 EGLint ValidateDisplaySurface(EGLDisplay dpy, EGLSurface surface) {
63 EGLint error_code = ValidateDisplay(dpy);
64 if (error_code != EGL_SUCCESS)
65 return error_code;
67 egl::Display* display = static_cast<egl::Display*>(dpy);
68 if (!display->IsValidSurface(surface))
69 return EGL_BAD_SURFACE;
71 return EGL_SUCCESS;
74 EGLint ValidateDisplayContext(EGLDisplay dpy, EGLContext context) {
75 EGLint error_code = ValidateDisplay(dpy);
76 if (error_code != EGL_SUCCESS)
77 return error_code;
79 egl::Display* display = static_cast<egl::Display*>(dpy);
80 if (!display->IsValidContext(context))
81 return EGL_BAD_CONTEXT;
83 return EGL_SUCCESS;
85 } // namespace
87 extern "C" {
88 EGLAPI EGLint EGLAPIENTRY eglGetError() {
89 // TODO(alokp): Fix me.
90 return EGL_SUCCESS;
93 EGLAPI EGLDisplay EGLAPIENTRY eglGetDisplay(EGLNativeDisplayType display_id) {
94 return new egl::Display(display_id);
97 EGLAPI EGLBoolean EGLAPIENTRY eglInitialize(EGLDisplay dpy,
98 EGLint* major,
99 EGLint* minor) {
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) {
110 int argc = 1;
111 const char* const argv[] = {"dummy"};
112 base::CommandLine::Init(argc, argv);
113 gfx::GLSurface::InitializeOneOff();
116 *major = 1;
117 *minor = 4;
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);
127 delete display;
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));
137 switch (name) {
138 case EGL_CLIENT_APIS:
139 return EglSuccess("OpenGL_ES");
140 case EGL_EXTENSIONS:
141 return EglSuccess("");
142 case EGL_VENDOR:
143 return EglSuccess("Google Inc.");
144 case EGL_VERSION:
145 return EglSuccess("1.4");
146 default:
147 return EglError(EGL_BAD_PARAMETER, static_cast<const char*>(NULL));
151 EGLAPI EGLBoolean EGLAPIENTRY eglChooseConfig(EGLDisplay dpy,
152 const EGLint* attrib_list,
153 EGLConfig* configs,
154 EGLint config_size,
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,
171 EGLConfig* configs,
172 EGLint config_size,
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,
189 EGLConfig config,
190 EGLint attribute,
191 EGLint* value) {
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,
205 EGLConfig config,
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,
225 EGLConfig config,
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);
232 int width = 1;
233 int height = 1;
234 if (attrib_list) {
235 for (const int32_t* attr = attrib_list; attr[0] != EGL_NONE; attr += 2) {
236 switch (attr[0]) {
237 case EGL_WIDTH:
238 width = attr[1];
239 break;
240 case EGL_HEIGHT:
241 height = attr[1];
242 break;
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,
257 EGLConfig config,
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,
275 EGLSurface surface,
276 EGLint attribute,
277 EGLint* value) {
278 return EGL_FALSE;
281 EGLAPI EGLBoolean EGLAPIENTRY eglBindAPI(EGLenum api) {
282 return EGL_FALSE;
285 EGLAPI EGLenum EGLAPIENTRY eglQueryAPI() {
286 return EGL_OPENGL_ES_API;
289 EGLAPI EGLBoolean EGLAPIENTRY eglWaitClient(void) {
290 return EGL_FALSE;
293 EGLAPI EGLBoolean EGLAPIENTRY eglReleaseThread(void) {
294 return EGL_FALSE;
297 EGLAPI EGLSurface EGLAPIENTRY
298 eglCreatePbufferFromClientBuffer(EGLDisplay dpy,
299 EGLenum buftype,
300 EGLClientBuffer buffer,
301 EGLConfig config,
302 const EGLint* attrib_list) {
303 return EGL_NO_SURFACE;
306 EGLAPI EGLBoolean EGLAPIENTRY eglSurfaceAttrib(EGLDisplay dpy,
307 EGLSurface surface,
308 EGLint attribute,
309 EGLint value) {
310 return EGL_FALSE;
313 EGLAPI EGLBoolean EGLAPIENTRY eglBindTexImage(EGLDisplay dpy,
314 EGLSurface surface,
315 EGLint buffer) {
316 return EGL_FALSE;
319 EGLAPI EGLBoolean EGLAPIENTRY eglReleaseTexImage(EGLDisplay dpy,
320 EGLSurface surface,
321 EGLint buffer) {
322 return EGL_FALSE;
325 EGLAPI EGLBoolean EGLAPIENTRY eglSwapInterval(EGLDisplay dpy, EGLint interval) {
326 return EGL_FALSE;
329 EGLAPI EGLContext EGLAPIENTRY eglCreateContext(EGLDisplay dpy,
330 EGLConfig config,
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,
353 EGLContext ctx) {
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);
360 return EGL_TRUE;
363 EGLAPI EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay dpy,
364 EGLSurface draw,
365 EGLSurface read,
366 EGLContext ctx) {
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);
383 #if REGAL_STATIC_EGL
384 RegalMakeCurrent(ctx);
385 #endif
387 return EGL_TRUE;
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,
403 EGLContext ctx,
404 EGLint attribute,
405 EGLint* value) {
406 return EGL_FALSE;
409 EGLAPI EGLBoolean EGLAPIENTRY eglWaitGL() {
410 return EGL_FALSE;
413 EGLAPI EGLBoolean EGLAPIENTRY eglWaitNative(EGLint engine) {
414 return EGL_FALSE;
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,
429 EGLSurface surface,
430 EGLNativePixmapType target) {
431 return EGL_FALSE;
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));
440 } // extern "C"