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.
4
5#ifndef UI_GL_GL_BINDINGS_H_
6#define UI_GL_GL_BINDINGS_H_
7
8// Includes the platform independent and platform dependent GL headers.
9// Only include this in cc files. It pulls in system headers, including
10// the X11 headers on linux, which define all kinds of macros that are
11// liable to cause conflicts.
12
13#include <GL/gl.h>
14#include <GL/glext.h>
15#include <EGL/egl.h>
16#include <EGL/eglext.h>
17
18#include "base/logging.h"
19#include "base/threading/thread_local.h"
20#include "build/build_config.h"
21#include "ui/gl/gl_export.h"
22
23// The standard OpenGL native extension headers are also included.
24#if defined(OS_WIN)
25#include <GL/wglext.h>
26#elif defined(OS_MACOSX)
27#include <OpenGL/OpenGL.h>
28#elif defined(USE_X11)
29#include <GL/glx.h>
30#include <GL/glxext.h>
31
32// Undefine some macros defined by X headers. This is why this file should only
33// be included in .cc files.
34#undef Bool
35#undef None
36#undef Status
37#endif
38
39
40// GLES2 defines not part of Desktop GL
41// Shader Precision-Specified Types
42#define GL_LOW_FLOAT                                     0x8DF0
43#define GL_MEDIUM_FLOAT                                  0x8DF1
44#define GL_HIGH_FLOAT                                    0x8DF2
45#define GL_LOW_INT                                       0x8DF3
46#define GL_MEDIUM_INT                                    0x8DF4
47#define GL_HIGH_INT                                      0x8DF5
48#define GL_IMPLEMENTATION_COLOR_READ_TYPE                0x8B9A
49#define GL_IMPLEMENTATION_COLOR_READ_FORMAT              0x8B9B
50#define GL_MAX_FRAGMENT_UNIFORM_VECTORS                  0x8DFD
51#define GL_MAX_VERTEX_UNIFORM_VECTORS                    0x8DFB
52#define GL_MAX_VARYING_VECTORS                           0x8DFC
53#define GL_SHADER_BINARY_FORMATS                         0x8DF8
54#define GL_NUM_SHADER_BINARY_FORMATS                     0x8DF9
55#define GL_SHADER_COMPILER                               0x8DFA
56#define GL_RGB565                                        0x8D62
57#define GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES           0x8B8B
58#define GL_RGB8_OES                                      0x8051
59#define GL_RGBA8_OES                                     0x8058
60#define GL_HALF_FLOAT_OES                                0x8D61
61
62// GL_OES_EGL_image_external
63#define GL_TEXTURE_EXTERNAL_OES                          0x8D65
64#define GL_SAMPLER_EXTERNAL_OES                          0x8D66
65#define GL_TEXTURE_BINDING_EXTERNAL_OES                  0x8D67
66#define GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES              0x8D68
67
68// GL_ANGLE_translated_shader_source
69#define GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE         0x93A0
70
71// GL_CHROMIUM_flipy
72#define GL_UNPACK_FLIP_Y_CHROMIUM                        0x9240
73
74#define GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM             0x9241
75#define GL_UNPACK_UNPREMULTIPLY_ALPHA_CHROMIUM           0x9242
76#define GL_UNPACK_COLORSPACE_CONVERSION_CHROMIUM         0x9243
77#define GL_BIND_GENERATES_RESOURCE_CHROMIUM              0x9244
78
79// GL_CHROMIUM_gpu_memory_manager
80#define GL_TEXTURE_POOL_CHROMIUM                         0x6000
81#define GL_TEXTURE_POOL_MANAGED_CHROMIUM                 0x6001
82#define GL_TEXTURE_POOL_UNMANAGED_CHROMIUM               0x6002
83
84// GL_ANGLE_pack_reverse_row_order
85#define GL_PACK_REVERSE_ROW_ORDER_ANGLE                  0x93A4
86
87// GL_ANGLE_texture_usage
88#define GL_TEXTURE_USAGE_ANGLE                           0x93A2
89#define GL_FRAMEBUFFER_ATTACHMENT_ANGLE                  0x93A3
90
91// GL_EXT_texture_storage
92#define GL_TEXTURE_IMMUTABLE_FORMAT_EXT                  0x912F
93#define GL_ALPHA8_EXT                                    0x803C
94#define GL_LUMINANCE8_EXT                                0x8040
95#define GL_LUMINANCE8_ALPHA8_EXT                         0x8045
96#define GL_RGBA32F_EXT                                   0x8814
97#define GL_RGB32F_EXT                                    0x8815
98#define GL_ALPHA32F_EXT                                  0x8816
99#define GL_LUMINANCE32F_EXT                              0x8818
100#define GL_LUMINANCE_ALPHA32F_EXT                        0x8819
101#define GL_RGBA16F_EXT                                   0x881A
102#define GL_RGB16F_EXT                                    0x881B
103#define GL_ALPHA16F_EXT                                  0x881C
104#define GL_LUMINANCE16F_EXT                              0x881E
105#define GL_LUMINANCE_ALPHA16F_EXT                        0x881F
106#define GL_BGRA8_EXT                                     0x93A1
107
108// GL_ANGLE_instanced_arrays
109#define GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE             0x88FE
110
111// GL_EXT_occlusion_query_boolean
112#define GL_ANY_SAMPLES_PASSED_EXT                        0x8C2F
113#define GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT           0x8D6A
114#define GL_CURRENT_QUERY_EXT                             0x8865
115#define GL_QUERY_RESULT_EXT                              0x8866
116#define GL_QUERY_RESULT_AVAILABLE_EXT                    0x8867
117
118// GL_CHROMIUM_command_buffer_query
119#define GL_COMMANDS_ISSUED_CHROMIUM                      0x84F2
120
121/* GL_CHROMIUM_get_error_query */
122#define GL_GET_ERROR_QUERY_CHROMIUM                      0x84F3
123
124/* GL_CHROMIUM_command_buffer_latency_query */
125#define GL_LATENCY_QUERY_CHROMIUM                        0x84F4
126
127/* GL_CHROMIUM_async_pixel_transfers */
128#define GL_ASYNC_PIXEL_UNPACK_COMPLETED_CHROMIUM         0x84F5
129#define GL_ASYNC_PIXEL_PACK_COMPLETED_CHROMIUM           0x84F6
130
131// GL_CHROMIUM_sync_query
132#define GL_COMMANDS_COMPLETED_CHROMIUM                   0x84F7
133
134// GL_CHROMIUM_image
135#define GL_IMAGE_ROWBYTES_CHROMIUM                       0x78F0
136#define GL_IMAGE_MAP_CHROMIUM                            0x78F1
137#define GL_IMAGE_SCANOUT_CHROMIUM                        0x78F2
138
139// GL_OES_texure_3D
140#define GL_SAMPLER_3D_OES                                0x8B5F
141
142// GL_OES_depth24
143#define GL_DEPTH_COMPONENT24_OES                         0x81A6
144
145// GL_OES_depth32
146#define GL_DEPTH_COMPONENT32_OES                         0x81A7
147
148// GL_OES_packed_depth_stencil
149#ifndef GL_DEPTH24_STENCIL8_OES
150#define GL_DEPTH24_STENCIL8_OES                          0x88F0
151#endif
152
153#ifndef GL_DEPTH24_STENCIL8
154#define GL_DEPTH24_STENCIL8                              0x88F0
155#endif
156
157// GL_OES_compressed_ETC1_RGB8_texture
158#define GL_ETC1_RGB8_OES                                 0x8D64
159
160// GL_AMD_compressed_ATC_texture
161#define GL_ATC_RGB_AMD                                   0x8C92
162#define GL_ATC_RGBA_EXPLICIT_ALPHA_AMD                   0x8C93
163#define GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD               0x87EE
164
165// GL_IMG_texture_compression_pvrtc
166#define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG               0x8C00
167#define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG               0x8C01
168#define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG              0x8C02
169#define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG              0x8C03
170
171// GL_OES_vertex_array_object
172#define GL_VERTEX_ARRAY_BINDING_OES                      0x85B5
173
174// GL_CHROMIUM_pixel_transfer_buffer_object
175#define GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM         0x78EC
176#define GL_PIXEL_PACK_TRANSFER_BUFFER_CHROMIUM           0x78ED
177#define GL_PIXEL_PACK_TRANSFER_BUFFER_BINDING_CHROMIUM   0x78EE
178#define GL_PIXEL_UNPACK_TRANSFER_BUFFER_BINDING_CHROMIUM 0x78EF
179
180/* GL_EXT_discard_framebuffer */
181#ifndef GL_EXT_discard_framebuffer
182#define GL_COLOR_EXT                                     0x1800
183#define GL_DEPTH_EXT                                     0x1801
184#define GL_STENCIL_EXT                                   0x1802
185#endif
186
187// GL_ARB_get_program_binary
188#define PROGRAM_BINARY_RETRIEVABLE_HINT                  0x8257
189// GL_OES_get_program_binary
190#define GL_PROGRAM_BINARY_LENGTH_OES                     0x8741
191#define GL_NUM_PROGRAM_BINARY_FORMATS_OES                0x87FE
192#define GL_PROGRAM_BINARY_FORMATS_OES                    0x87FF
193
194#ifndef GL_EXT_multisampled_render_to_texture
195#define GL_RENDERBUFFER_SAMPLES_EXT                      0x8CAB
196#define GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT        0x8D56
197#define GL_MAX_SAMPLES_EXT                               0x8D57
198#define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT    0x8D6C
199#endif
200
201#ifndef GL_IMG_multisampled_render_to_texture
202#define GL_RENDERBUFFER_SAMPLES_IMG                      0x9133
203#define GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_IMG        0x9134
204#define GL_MAX_SAMPLES_IMG                               0x9135
205#define GL_TEXTURE_SAMPLES_IMG                           0x9136
206#endif
207
208#define GL_GLEXT_PROTOTYPES 1
209
210#if defined(OS_WIN)
211#define GL_BINDING_CALL WINAPI
212#else
213#define GL_BINDING_CALL
214#endif
215
216#define GL_SERVICE_LOG(args) DLOG(INFO) << args;
217#if defined(NDEBUG)
218  #define GL_SERVICE_LOG_CODE_BLOCK(code)
219#else
220  #define GL_SERVICE_LOG_CODE_BLOCK(code) code
221#endif
222
223// Forward declare OSMesa types.
224typedef struct osmesa_context *OSMesaContext;
225typedef void (*OSMESAproc)();
226
227// Forward declare EGL types.
228typedef uint64 EGLuint64CHROMIUM;
229
230#include "gl_bindings_autogen_gl.h"
231#include "gl_bindings_autogen_osmesa.h"
232
233#if defined(OS_WIN)
234#include "gl_bindings_autogen_egl.h"
235#include "gl_bindings_autogen_wgl.h"
236#elif defined(USE_X11)
237#include "gl_bindings_autogen_egl.h"
238#include "gl_bindings_autogen_glx.h"
239#elif defined(USE_OZONE)
240#include "gl_bindings_autogen_egl.h"
241#elif defined(OS_ANDROID)
242#include "gl_bindings_autogen_egl.h"
243#endif
244
245namespace gfx {
246
247struct GL_EXPORT DriverGL {
248  void InitializeStaticBindings();
249  void InitializeCustomDynamicBindings(GLContext* context);
250  void InitializeDebugBindings();
251  void InitializeNullDrawBindings();
252  // TODO(danakj): Remove this when all test suites are using null-draw.
253  bool HasInitializedNullDrawBindings();
254  bool SetNullDrawBindingsEnabled(bool enabled);
255  void ClearBindings();
256
257  ProcsGL fn;
258  ProcsGL orig_fn;
259  ProcsGL debug_fn;
260  ExtensionsGL ext;
261  bool null_draw_bindings_enabled;
262
263 private:
264  void InitializeDynamicBindings(GLContext* context);
265};
266
267struct GL_EXPORT DriverOSMESA {
268  void InitializeStaticBindings();
269  void InitializeDynamicBindings(GLContext* context);
270  void InitializeDebugBindings();
271  void ClearBindings();
272
273  ProcsOSMESA fn;
274  ProcsOSMESA debug_fn;
275  ExtensionsOSMESA ext;
276};
277
278#if defined(OS_WIN)
279struct GL_EXPORT DriverWGL {
280  void InitializeStaticBindings();
281  void InitializeDynamicBindings(GLContext* context);
282  void InitializeDebugBindings();
283  void ClearBindings();
284
285  ProcsWGL fn;
286  ProcsWGL debug_fn;
287  ExtensionsWGL ext;
288};
289#endif
290
291#if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || defined(USE_OZONE)
292struct GL_EXPORT DriverEGL {
293  void InitializeStaticBindings();
294  void InitializeDynamicBindings(GLContext* context);
295  void InitializeDebugBindings();
296  void ClearBindings();
297
298  ProcsEGL fn;
299  ProcsEGL debug_fn;
300  ExtensionsEGL ext;
301};
302#endif
303
304#if defined(USE_X11)
305struct GL_EXPORT DriverGLX {
306  void InitializeStaticBindings();
307  void InitializeDynamicBindings(GLContext* context);
308  void InitializeDebugBindings();
309  void ClearBindings();
310
311  ProcsGLX fn;
312  ProcsGLX debug_fn;
313  ExtensionsGLX ext;
314};
315#endif
316
317// This #define is here to support autogenerated code.
318#define g_current_gl_context g_current_gl_context_tls->Get()
319GL_EXPORT extern base::ThreadLocalPointer<GLApi>* g_current_gl_context_tls;
320
321GL_EXPORT extern OSMESAApi* g_current_osmesa_context;
322GL_EXPORT extern DriverGL g_driver_gl;
323GL_EXPORT extern DriverOSMESA g_driver_osmesa;
324
325#if defined(OS_WIN)
326
327GL_EXPORT extern EGLApi* g_current_egl_context;
328GL_EXPORT extern WGLApi* g_current_wgl_context;
329GL_EXPORT extern DriverEGL g_driver_egl;
330GL_EXPORT extern DriverWGL g_driver_wgl;
331
332#elif defined(USE_X11)
333
334GL_EXPORT extern EGLApi* g_current_egl_context;
335GL_EXPORT extern GLXApi* g_current_glx_context;
336GL_EXPORT extern DriverEGL g_driver_egl;
337GL_EXPORT extern DriverGLX g_driver_glx;
338
339#elif defined(USE_OZONE)
340
341GL_EXPORT extern EGLApi* g_current_egl_context;
342GL_EXPORT extern DriverEGL g_driver_egl;
343
344#elif defined(OS_ANDROID)
345
346GL_EXPORT extern EGLApi* g_current_egl_context;
347GL_EXPORT extern DriverEGL g_driver_egl;
348
349#endif
350
351}  // namespace gfx
352
353#endif  // UI_GL_GL_BINDINGS_H_
354