| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string.h> | 5 #include <string.h> |
| 6 #include <GL/glx.h> | 6 #include <GL/glx.h> |
| 7 | 7 |
| 8 #include "base/logging.h" |
| 8 #include "main.h" | 9 #include "main.h" |
| 9 #include "xlib_window.h" | 10 #include "xlib_window.h" |
| 10 | 11 |
| 11 static GLXContext glx_context = NULL; | 12 static GLXContext glx_context = NULL; |
| 12 | 13 |
| 13 #define F(fun, type) type fun = NULL; | 14 #define F(fun, type) type fun = NULL; |
| 14 LIST_PROC_FUNCTIONS(F) | 15 LIST_PROC_FUNCTIONS(F) |
| 15 #undef F | 16 #undef F |
| 16 | 17 |
| 17 bool Init() { | 18 bool Init() { |
| 18 return XlibInit(); | 19 return XlibInit(); |
| 19 } | 20 } |
| 20 | 21 |
| 21 VisualID GetVisualID() { | 22 VisualID GetVisualID() { |
| 22 int screen = DefaultScreen(xlib_display); | 23 int screen = DefaultScreen(g_xlib_display); |
| 23 int attrib[] = { | 24 int attrib[] = { |
| 24 GLX_DOUBLEBUFFER, True, | 25 GLX_DOUBLEBUFFER, True, |
| 25 GLX_RED_SIZE, 1, | 26 GLX_RED_SIZE, 1, |
| 26 GLX_GREEN_SIZE, 1, | 27 GLX_GREEN_SIZE, 1, |
| 27 GLX_BLUE_SIZE, 1, | 28 GLX_BLUE_SIZE, 1, |
| 28 GLX_DEPTH_SIZE, 1, | 29 GLX_DEPTH_SIZE, 1, |
| 29 GLX_STENCIL_SIZE, 1, | 30 GLX_STENCIL_SIZE, 1, |
| 30 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, | 31 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, |
| 31 None | 32 None |
| 32 }; | 33 }; |
| 33 int nelements; | 34 int nelements; |
| 34 GLXFBConfig *fbconfigs = glXChooseFBConfig(xlib_display, screen, | 35 GLXFBConfig *fbconfigs = glXChooseFBConfig(g_xlib_display, screen, |
| 35 attrib, &nelements); | 36 attrib, &nelements); |
| 36 CHECK(nelements >= 1); | 37 CHECK(nelements >= 1); |
| 37 int visual_id; | 38 int visual_id; |
| 38 glXGetFBConfigAttrib(xlib_display, fbconfigs[0], GLX_VISUAL_ID, &visual_id); | 39 glXGetFBConfigAttrib(g_xlib_display, fbconfigs[0], GLX_VISUAL_ID, &visual_id); |
| 39 XFree(fbconfigs); | 40 XFree(fbconfigs); |
| 40 return static_cast<VisualID>(visual_id); | 41 return static_cast<VisualID>(visual_id); |
| 41 } | 42 } |
| 42 | 43 |
| 43 bool InitContext() { | 44 bool InitContext() { |
| 44 glx_context = glXCreateContext(xlib_display, xlib_visinfo, 0, True); | 45 glx_context = glXCreateContext(g_xlib_display, g_xlib_visinfo, 0, True); |
| 45 if (!glx_context) | 46 if (!glx_context) |
| 46 return false; | 47 return false; |
| 47 | 48 |
| 48 if (!glXMakeCurrent(xlib_display, xlib_window, glx_context)) { | 49 if (!glXMakeCurrent(g_xlib_display, g_xlib_window, glx_context)) { |
| 49 glXDestroyContext(xlib_display, glx_context); | 50 glXDestroyContext(g_xlib_display, glx_context); |
| 50 return false; | 51 return false; |
| 51 } | 52 } |
| 52 | 53 |
| 53 glClearColor(1.f, 0, 0, 1.f); | 54 glClearColor(1.f, 0, 0, 1.f); |
| 54 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); | 55 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
| 55 SwapBuffers(); | 56 SwapBuffers(); |
| 56 glClearColor(0, 1.f, 0, 1.f); | 57 glClearColor(0, 1.f, 0, 1.f); |
| 57 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); | 58 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
| 58 SwapBuffers(); | 59 SwapBuffers(); |
| 59 glClearColor(0, 0, 0.f, 0.f); | 60 glClearColor(0, 0, 0.f, 0.f); |
| 60 | 61 |
| 61 const GLubyte *str = glGetString(GL_EXTENSIONS); | 62 const GLubyte *str = glGetString(GL_EXTENSIONS); |
| 62 if (!str || !strstr(reinterpret_cast<const char *>(str), | 63 if (!str || !strstr(reinterpret_cast<const char *>(str), |
| 63 "GL_ARB_vertex_buffer_object")) | 64 "GL_ARB_vertex_buffer_object")) |
| 64 return false; | 65 return false; |
| 65 | 66 |
| 66 #define F(fun, type) fun = reinterpret_cast<type>( \ | 67 #define F(fun, type) fun = reinterpret_cast<type>( \ |
| 67 glXGetProcAddress(reinterpret_cast<const GLubyte *>(#fun))); | 68 glXGetProcAddress(reinterpret_cast<const GLubyte *>(#fun))); |
| 68 LIST_PROC_FUNCTIONS(F) | 69 LIST_PROC_FUNCTIONS(F) |
| 69 #undef F | 70 #undef F |
| 70 | 71 |
| 71 return true; | 72 return true; |
| 72 } | 73 } |
| 73 | 74 |
| 74 void DestroyContext() { | 75 void DestroyContext() { |
| 75 glXMakeCurrent(xlib_display, 0, NULL); | 76 glXMakeCurrent(g_xlib_display, 0, NULL); |
| 76 glXDestroyContext(xlib_display, glx_context); | 77 glXDestroyContext(g_xlib_display, glx_context); |
| 77 } | 78 } |
| 78 | 79 |
| 79 void SwapBuffers() { | 80 void SwapBuffers() { |
| 80 glXSwapBuffers(xlib_display, xlib_window); | 81 glXSwapBuffers(g_xlib_display, g_xlib_window); |
| 81 } | 82 } |
| 82 | 83 |
| 83 bool SwapInterval(int interval) { | 84 bool SwapInterval(int interval) { |
| 84 return glXSwapIntervalSGI(interval) == 0; | 85 return glXSwapIntervalSGI(interval) == 0; |
| 85 } | 86 } |
| OLD | NEW |