Also ran 'git cl format' on this file since I was touching a lot of it anyway.
[chromium-blink-merge.git] / ui / gl / gl_gl_api_implementation.cc
blob81a1b72657ba0d841f9fd8780f68f57260160609
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 "ui/gl/gl_gl_api_implementation.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/command_line.h"
11 #include "base/strings/string_util.h"
12 #include "ui/gl/gl_context.h"
13 #include "ui/gl/gl_implementation.h"
14 #include "ui/gl/gl_state_restorer.h"
15 #include "ui/gl/gl_surface.h"
16 #include "ui/gl/gl_switches.h"
17 #include "ui/gl/gl_version_info.h"
19 namespace gfx {
21 // The GL Api being used. This could be g_real_gl or gl_trace_gl
22 static GLApi* g_gl = NULL;
23 // A GL Api that calls directly into the driver.
24 static RealGLApi* g_real_gl = NULL;
25 // A GL Api that does nothing but warn about illegal GL calls without a context
26 // current.
27 static NoContextGLApi* g_no_context_gl = NULL;
28 // A GL Api that calls TRACE and then calls another GL api.
29 static TraceGLApi* g_trace_gl = NULL;
30 // GL version used when initializing dynamic bindings.
31 static GLVersionInfo* g_version_info = NULL;
33 namespace {
35 static inline GLenum GetInternalFormat(GLenum internal_format) {
36 if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2) {
37 if (internal_format == GL_BGRA_EXT || internal_format == GL_BGRA8_EXT)
38 return GL_RGBA8;
40 return internal_format;
43 // TODO(epenner): Could the above function be merged into this and removed?
44 static inline GLenum GetTexInternalFormat(GLenum internal_format,
45 GLenum format,
46 GLenum type) {
47 GLenum gl_internal_format = GetInternalFormat(internal_format);
49 // g_version_info must be initialized when this function is bound.
50 DCHECK(gfx::g_version_info);
51 if (gfx::g_version_info->is_es3) {
52 if (format == GL_RED_EXT) {
53 switch (type) {
54 case GL_UNSIGNED_BYTE:
55 gl_internal_format = GL_R8_EXT;
56 break;
57 case GL_HALF_FLOAT_OES:
58 gl_internal_format = GL_R16F_EXT;
59 break;
60 case GL_FLOAT:
61 gl_internal_format = GL_R32F_EXT;
62 break;
63 default:
64 NOTREACHED();
65 break;
67 return gl_internal_format;
68 } else if (format == GL_RG_EXT) {
69 switch (type) {
70 case GL_UNSIGNED_BYTE:
71 gl_internal_format = GL_RG8_EXT;
72 break;
73 case GL_HALF_FLOAT_OES:
74 gl_internal_format = GL_RG16F_EXT;
75 break;
76 case GL_FLOAT:
77 gl_internal_format = GL_RG32F_EXT;
78 break;
79 default:
80 NOTREACHED();
81 break;
83 return gl_internal_format;
87 if (type == GL_FLOAT && gfx::g_version_info->is_angle &&
88 gfx::g_version_info->is_es && gfx::g_version_info->major_version == 2) {
89 // It's possible that the texture is using a sized internal format, and
90 // ANGLE exposing GLES2 API doesn't support those.
91 // TODO(oetuaho@nvidia.com): Remove these conversions once ANGLE has the
92 // support.
93 // http://code.google.com/p/angleproject/issues/detail?id=556
94 switch (format) {
95 case GL_RGBA:
96 gl_internal_format = GL_RGBA;
97 break;
98 case GL_RGB:
99 gl_internal_format = GL_RGB;
100 break;
101 default:
102 break;
106 if (gfx::g_version_info->is_es)
107 return gl_internal_format;
109 if (type == GL_FLOAT) {
110 switch (format) {
111 case GL_RGBA:
112 gl_internal_format = GL_RGBA32F_ARB;
113 break;
114 case GL_RGB:
115 gl_internal_format = GL_RGB32F_ARB;
116 break;
117 case GL_LUMINANCE_ALPHA:
118 gl_internal_format = GL_LUMINANCE_ALPHA32F_ARB;
119 break;
120 case GL_LUMINANCE:
121 gl_internal_format = GL_LUMINANCE32F_ARB;
122 break;
123 case GL_ALPHA:
124 gl_internal_format = GL_ALPHA32F_ARB;
125 break;
126 default:
127 NOTREACHED();
128 break;
130 } else if (type == GL_HALF_FLOAT_OES) {
131 switch (format) {
132 case GL_RGBA:
133 gl_internal_format = GL_RGBA16F_ARB;
134 break;
135 case GL_RGB:
136 gl_internal_format = GL_RGB16F_ARB;
137 break;
138 case GL_LUMINANCE_ALPHA:
139 gl_internal_format = GL_LUMINANCE_ALPHA16F_ARB;
140 break;
141 case GL_LUMINANCE:
142 gl_internal_format = GL_LUMINANCE16F_ARB;
143 break;
144 case GL_ALPHA:
145 gl_internal_format = GL_ALPHA16F_ARB;
146 break;
147 default:
148 NOTREACHED();
149 break;
152 return gl_internal_format;
155 static inline GLenum GetTexType(GLenum type) {
156 if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2) {
157 if (type == GL_HALF_FLOAT_OES)
158 return GL_HALF_FLOAT_ARB;
160 return type;
163 static void GL_BINDING_CALL CustomTexImage2D(
164 GLenum target, GLint level, GLint internalformat,
165 GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type,
166 const void* pixels) {
167 GLenum gl_internal_format = GetTexInternalFormat(
168 internalformat, format, type);
169 GLenum gl_type = GetTexType(type);
170 g_driver_gl.orig_fn.glTexImage2DFn(
171 target, level, gl_internal_format, width, height, border, format, gl_type,
172 pixels);
175 static void GL_BINDING_CALL CustomTexSubImage2D(
176 GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width,
177 GLsizei height, GLenum format, GLenum type, const void* pixels) {
178 GLenum gl_type = GetTexType(type);
179 g_driver_gl.orig_fn.glTexSubImage2DFn(
180 target, level, xoffset, yoffset, width, height, format, gl_type, pixels);
183 static void GL_BINDING_CALL CustomTexStorage2DEXT(
184 GLenum target, GLsizei levels, GLenum internalformat, GLsizei width,
185 GLsizei height) {
186 GLenum gl_internal_format = GetInternalFormat(internalformat);
187 g_driver_gl.orig_fn.glTexStorage2DEXTFn(
188 target, levels, gl_internal_format, width, height);
191 static void GL_BINDING_CALL CustomRenderbufferStorageEXT(
192 GLenum target, GLenum internalformat, GLsizei width, GLsizei height) {
193 GLenum gl_internal_format = GetInternalFormat(internalformat);
194 g_driver_gl.orig_fn.glRenderbufferStorageEXTFn(
195 target, gl_internal_format, width, height);
198 // The ANGLE and IMG variants of glRenderbufferStorageMultisample currently do
199 // not support BGRA render buffers so only the EXT one is customized. If
200 // GL_CHROMIUM_renderbuffer_format_BGRA8888 support is added to ANGLE then the
201 // ANGLE version should also be customized.
202 static void GL_BINDING_CALL CustomRenderbufferStorageMultisampleEXT(
203 GLenum target, GLsizei samples, GLenum internalformat, GLsizei width,
204 GLsizei height) {
205 GLenum gl_internal_format = GetInternalFormat(internalformat);
206 g_driver_gl.orig_fn.glRenderbufferStorageMultisampleEXTFn(
207 target, samples, gl_internal_format, width, height);
210 } // anonymous namespace
212 void DriverGL::InitializeCustomDynamicBindings(GLContext* context) {
213 InitializeDynamicBindings(context);
215 DCHECK(orig_fn.glTexImage2DFn == NULL);
216 orig_fn.glTexImage2DFn = fn.glTexImage2DFn;
217 fn.glTexImage2DFn =
218 reinterpret_cast<glTexImage2DProc>(CustomTexImage2D);
220 DCHECK(orig_fn.glTexSubImage2DFn == NULL);
221 orig_fn.glTexSubImage2DFn = fn.glTexSubImage2DFn;
222 fn.glTexSubImage2DFn =
223 reinterpret_cast<glTexSubImage2DProc>(CustomTexSubImage2D);
225 DCHECK(orig_fn.glTexStorage2DEXTFn == NULL);
226 orig_fn.glTexStorage2DEXTFn = fn.glTexStorage2DEXTFn;
227 fn.glTexStorage2DEXTFn =
228 reinterpret_cast<glTexStorage2DEXTProc>(CustomTexStorage2DEXT);
230 DCHECK(orig_fn.glRenderbufferStorageEXTFn == NULL);
231 orig_fn.glRenderbufferStorageEXTFn = fn.glRenderbufferStorageEXTFn;
232 fn.glRenderbufferStorageEXTFn =
233 reinterpret_cast<glRenderbufferStorageEXTProc>(
234 CustomRenderbufferStorageEXT);
236 DCHECK(orig_fn.glRenderbufferStorageMultisampleEXTFn == NULL);
237 orig_fn.glRenderbufferStorageMultisampleEXTFn =
238 fn.glRenderbufferStorageMultisampleEXTFn;
239 fn.glRenderbufferStorageMultisampleEXTFn =
240 reinterpret_cast<glRenderbufferStorageMultisampleEXTProc>(
241 CustomRenderbufferStorageMultisampleEXT);
244 static void GL_BINDING_CALL NullDrawClearFn(GLbitfield mask) {
245 if (!g_driver_gl.null_draw_bindings_enabled)
246 g_driver_gl.orig_fn.glClearFn(mask);
249 static void GL_BINDING_CALL
250 NullDrawDrawArraysFn(GLenum mode, GLint first, GLsizei count) {
251 if (!g_driver_gl.null_draw_bindings_enabled)
252 g_driver_gl.orig_fn.glDrawArraysFn(mode, first, count);
255 static void GL_BINDING_CALL NullDrawDrawElementsFn(GLenum mode,
256 GLsizei count,
257 GLenum type,
258 const void* indices) {
259 if (!g_driver_gl.null_draw_bindings_enabled)
260 g_driver_gl.orig_fn.glDrawElementsFn(mode, count, type, indices);
263 void DriverGL::InitializeNullDrawBindings() {
264 DCHECK(orig_fn.glClearFn == NULL);
265 orig_fn.glClearFn = fn.glClearFn;
266 fn.glClearFn = NullDrawClearFn;
268 DCHECK(orig_fn.glDrawArraysFn == NULL);
269 orig_fn.glDrawArraysFn = fn.glDrawArraysFn;
270 fn.glDrawArraysFn = NullDrawDrawArraysFn;
272 DCHECK(orig_fn.glDrawElementsFn == NULL);
273 orig_fn.glDrawElementsFn = fn.glDrawElementsFn;
274 fn.glDrawElementsFn = NullDrawDrawElementsFn;
276 null_draw_bindings_enabled = true;
279 bool DriverGL::HasInitializedNullDrawBindings() {
280 return orig_fn.glClearFn != NULL && orig_fn.glDrawArraysFn != NULL &&
281 orig_fn.glDrawElementsFn != NULL;
284 bool DriverGL::SetNullDrawBindingsEnabled(bool enabled) {
285 DCHECK(orig_fn.glClearFn != NULL);
286 DCHECK(orig_fn.glDrawArraysFn != NULL);
287 DCHECK(orig_fn.glDrawElementsFn != NULL);
289 bool before = null_draw_bindings_enabled;
290 null_draw_bindings_enabled = enabled;
291 return before;
294 void InitializeStaticGLBindingsGL() {
295 g_current_gl_context_tls = new base::ThreadLocalPointer<GLApi>;
296 g_driver_gl.InitializeStaticBindings();
297 if (!g_real_gl) {
298 g_real_gl = new RealGLApi();
299 g_trace_gl = new TraceGLApi(g_real_gl);
300 g_no_context_gl = new NoContextGLApi();
302 g_real_gl->Initialize(&g_driver_gl);
303 g_gl = g_real_gl;
304 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
305 switches::kEnableGPUServiceTracing)) {
306 g_gl = g_trace_gl;
308 SetGLToRealGLApi();
311 GLApi* GetCurrentGLApi() {
312 return g_current_gl_context_tls->Get();
315 void SetGLApi(GLApi* api) {
316 g_current_gl_context_tls->Set(api);
319 void SetGLToRealGLApi() {
320 SetGLApi(g_gl);
323 void SetGLApiToNoContext() {
324 SetGLApi(g_no_context_gl);
327 const GLVersionInfo* GetGLVersionInfo() {
328 return g_version_info;
331 void InitializeDynamicGLBindingsGL(GLContext* context) {
332 g_driver_gl.InitializeCustomDynamicBindings(context);
333 DCHECK(context && context->IsCurrent(NULL) && !g_version_info);
334 g_version_info = new GLVersionInfo(context->GetGLVersion().c_str(),
335 context->GetGLRenderer().c_str());
338 void InitializeDebugGLBindingsGL() {
339 g_driver_gl.InitializeDebugBindings();
342 void InitializeNullDrawGLBindingsGL() {
343 g_driver_gl.InitializeNullDrawBindings();
346 bool HasInitializedNullDrawGLBindingsGL() {
347 return g_driver_gl.HasInitializedNullDrawBindings();
350 bool SetNullDrawGLBindingsEnabledGL(bool enabled) {
351 return g_driver_gl.SetNullDrawBindingsEnabled(enabled);
354 void ClearGLBindingsGL() {
355 if (g_real_gl) {
356 delete g_real_gl;
357 g_real_gl = NULL;
359 if (g_trace_gl) {
360 delete g_trace_gl;
361 g_trace_gl = NULL;
363 if (g_no_context_gl) {
364 delete g_no_context_gl;
365 g_no_context_gl = NULL;
367 g_gl = NULL;
368 g_driver_gl.ClearBindings();
369 if (g_current_gl_context_tls) {
370 delete g_current_gl_context_tls;
371 g_current_gl_context_tls = NULL;
373 if (g_version_info) {
374 delete g_version_info;
375 g_version_info = NULL;
379 GLApi::GLApi() {
382 GLApi::~GLApi() {
383 if (GetCurrentGLApi() == this)
384 SetGLApi(NULL);
387 GLApiBase::GLApiBase()
388 : driver_(NULL) {
391 GLApiBase::~GLApiBase() {
394 void GLApiBase::InitializeBase(DriverGL* driver) {
395 driver_ = driver;
398 RealGLApi::RealGLApi() {
401 RealGLApi::~RealGLApi() {
404 void RealGLApi::Initialize(DriverGL* driver) {
405 InitializeBase(driver);
408 void RealGLApi::glFlushFn() {
409 GLApiBase::glFlushFn();
412 void RealGLApi::glFinishFn() {
413 GLApiBase::glFinishFn();
416 TraceGLApi::~TraceGLApi() {
419 NoContextGLApi::NoContextGLApi() {
422 NoContextGLApi::~NoContextGLApi() {
425 VirtualGLApi::VirtualGLApi()
426 : real_context_(NULL),
427 current_context_(NULL) {
430 VirtualGLApi::~VirtualGLApi() {
433 void VirtualGLApi::Initialize(DriverGL* driver, GLContext* real_context) {
434 InitializeBase(driver);
435 real_context_ = real_context;
437 DCHECK(real_context->IsCurrent(NULL));
438 std::string ext_string(
439 reinterpret_cast<const char*>(driver_->fn.glGetStringFn(GL_EXTENSIONS)));
440 std::vector<std::string> ext;
441 Tokenize(ext_string, " ", &ext);
443 std::vector<std::string>::iterator it;
444 // We can't support GL_EXT_occlusion_query_boolean which is
445 // based on GL_ARB_occlusion_query without a lot of work virtualizing
446 // queries.
447 it = std::find(ext.begin(), ext.end(), "GL_EXT_occlusion_query_boolean");
448 if (it != ext.end())
449 ext.erase(it);
451 extensions_ = JoinString(ext, " ");
454 bool VirtualGLApi::MakeCurrent(GLContext* virtual_context, GLSurface* surface) {
455 bool switched_contexts = g_current_gl_context_tls->Get() != this;
456 GLSurface* current_surface = GLSurface::GetCurrent();
457 if (switched_contexts || surface != current_surface) {
458 // MakeCurrent 'lite' path that avoids potentially expensive MakeCurrent()
459 // calls if the GLSurface uses the same underlying surface or renders to
460 // an FBO.
461 if (switched_contexts || !current_surface ||
462 !virtual_context->IsCurrent(surface)) {
463 if (!real_context_->MakeCurrent(surface)) {
464 return false;
469 DCHECK_EQ(real_context_, GLContext::GetRealCurrent());
470 DCHECK(real_context_->IsCurrent(NULL));
471 DCHECK(virtual_context->IsCurrent(surface));
473 if (switched_contexts || virtual_context != current_context_) {
474 #if DCHECK_IS_ON()
475 GLenum error = glGetErrorFn();
476 // Accepting a context loss error here enables using debug mode to work on
477 // context loss handling in virtual context mode.
478 // There should be no other errors from the previous context leaking into
479 // the new context.
480 DCHECK(error == GL_NO_ERROR || error == GL_CONTEXT_LOST_KHR);
481 #endif
483 // Set all state that is different from the real state
484 GLApi* temp = GetCurrentGLApi();
485 SetGLToRealGLApi();
486 if (virtual_context->GetGLStateRestorer()->IsInitialized()) {
487 virtual_context->GetGLStateRestorer()->RestoreState(
488 (current_context_ && !switched_contexts)
489 ? current_context_->GetGLStateRestorer()
490 : NULL);
492 SetGLApi(temp);
493 current_context_ = virtual_context;
495 SetGLApi(this);
497 virtual_context->SetCurrent(surface);
498 if (!surface->OnMakeCurrent(virtual_context)) {
499 LOG(ERROR) << "Could not make GLSurface current.";
500 return false;
502 return true;
505 void VirtualGLApi::OnReleaseVirtuallyCurrent(GLContext* virtual_context) {
506 if (current_context_ == virtual_context)
507 current_context_ = NULL;
510 const GLubyte* VirtualGLApi::glGetStringFn(GLenum name) {
511 switch (name) {
512 case GL_EXTENSIONS:
513 return reinterpret_cast<const GLubyte*>(extensions_.c_str());
514 default:
515 return driver_->fn.glGetStringFn(name);
519 void VirtualGLApi::glFlushFn() {
520 GLApiBase::glFlushFn();
523 void VirtualGLApi::glFinishFn() {
524 GLApiBase::glFinishFn();
527 ScopedSetGLToRealGLApi::ScopedSetGLToRealGLApi()
528 : old_gl_api_(GetCurrentGLApi()) {
529 SetGLToRealGLApi();
532 ScopedSetGLToRealGLApi::~ScopedSetGLToRealGLApi() {
533 SetGLApi(old_gl_api_);
536 } // namespace gfx