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"
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"
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
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
;
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
)
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
,
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 (type
== GL_FLOAT
&& gfx::g_version_info
->is_angle
&&
52 gfx::g_version_info
->is_es2
) {
53 // It's possible that the texture is using a sized internal format, and
54 // ANGLE exposing GLES2 API doesn't support those.
55 // TODO(oetuaho@nvidia.com): Remove these conversions once ANGLE has the
57 // http://code.google.com/p/angleproject/issues/detail?id=556
60 gl_internal_format
= GL_RGBA
;
63 gl_internal_format
= GL_RGB
;
70 if (gfx::g_version_info
->is_es
)
71 return gl_internal_format
;
73 if (type
== GL_FLOAT
) {
76 gl_internal_format
= GL_RGBA32F_ARB
;
79 gl_internal_format
= GL_RGB32F_ARB
;
81 case GL_LUMINANCE_ALPHA
:
82 gl_internal_format
= GL_LUMINANCE_ALPHA32F_ARB
;
85 gl_internal_format
= GL_LUMINANCE32F_ARB
;
88 gl_internal_format
= GL_ALPHA32F_ARB
;
94 } else if (type
== GL_HALF_FLOAT_OES
) {
97 gl_internal_format
= GL_RGBA16F_ARB
;
100 gl_internal_format
= GL_RGB16F_ARB
;
102 case GL_LUMINANCE_ALPHA
:
103 gl_internal_format
= GL_LUMINANCE_ALPHA16F_ARB
;
106 gl_internal_format
= GL_LUMINANCE16F_ARB
;
109 gl_internal_format
= GL_ALPHA16F_ARB
;
116 return gl_internal_format
;
119 static inline GLenum
GetTexType(GLenum type
) {
120 if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2
) {
121 if (type
== GL_HALF_FLOAT_OES
)
122 return GL_HALF_FLOAT_ARB
;
127 static void GL_BINDING_CALL
CustomTexImage2D(
128 GLenum target
, GLint level
, GLint internalformat
,
129 GLsizei width
, GLsizei height
, GLint border
, GLenum format
, GLenum type
,
130 const void* pixels
) {
131 GLenum gl_internal_format
= GetTexInternalFormat(
132 internalformat
, format
, type
);
133 GLenum gl_type
= GetTexType(type
);
134 g_driver_gl
.orig_fn
.glTexImage2DFn(
135 target
, level
, gl_internal_format
, width
, height
, border
, format
, gl_type
,
139 static void GL_BINDING_CALL
CustomTexSubImage2D(
140 GLenum target
, GLint level
, GLint xoffset
, GLint yoffset
, GLsizei width
,
141 GLsizei height
, GLenum format
, GLenum type
, const void* pixels
) {
142 GLenum gl_type
= GetTexType(type
);
143 g_driver_gl
.orig_fn
.glTexSubImage2DFn(
144 target
, level
, xoffset
, yoffset
, width
, height
, format
, gl_type
, pixels
);
147 static void GL_BINDING_CALL
CustomTexStorage2DEXT(
148 GLenum target
, GLsizei levels
, GLenum internalformat
, GLsizei width
,
150 GLenum gl_internal_format
= GetInternalFormat(internalformat
);
151 g_driver_gl
.orig_fn
.glTexStorage2DEXTFn(
152 target
, levels
, gl_internal_format
, width
, height
);
155 static void GL_BINDING_CALL
CustomRenderbufferStorageEXT(
156 GLenum target
, GLenum internalformat
, GLsizei width
, GLsizei height
) {
157 GLenum gl_internal_format
= GetInternalFormat(internalformat
);
158 g_driver_gl
.orig_fn
.glRenderbufferStorageEXTFn(
159 target
, gl_internal_format
, width
, height
);
162 // The ANGLE and IMG variants of glRenderbufferStorageMultisample currently do
163 // not support BGRA render buffers so only the EXT one is customized. If
164 // GL_CHROMIUM_renderbuffer_format_BGRA8888 support is added to ANGLE then the
165 // ANGLE version should also be customized.
166 static void GL_BINDING_CALL
CustomRenderbufferStorageMultisampleEXT(
167 GLenum target
, GLsizei samples
, GLenum internalformat
, GLsizei width
,
169 GLenum gl_internal_format
= GetInternalFormat(internalformat
);
170 g_driver_gl
.orig_fn
.glRenderbufferStorageMultisampleEXTFn(
171 target
, samples
, gl_internal_format
, width
, height
);
174 } // anonymous namespace
176 void DriverGL::InitializeCustomDynamicBindings(GLContext
* context
) {
177 InitializeDynamicBindings(context
);
179 DCHECK(orig_fn
.glTexImage2DFn
== NULL
);
180 orig_fn
.glTexImage2DFn
= fn
.glTexImage2DFn
;
182 reinterpret_cast<glTexImage2DProc
>(CustomTexImage2D
);
184 DCHECK(orig_fn
.glTexSubImage2DFn
== NULL
);
185 orig_fn
.glTexSubImage2DFn
= fn
.glTexSubImage2DFn
;
186 fn
.glTexSubImage2DFn
=
187 reinterpret_cast<glTexSubImage2DProc
>(CustomTexSubImage2D
);
189 DCHECK(orig_fn
.glTexStorage2DEXTFn
== NULL
);
190 orig_fn
.glTexStorage2DEXTFn
= fn
.glTexStorage2DEXTFn
;
191 fn
.glTexStorage2DEXTFn
=
192 reinterpret_cast<glTexStorage2DEXTProc
>(CustomTexStorage2DEXT
);
194 DCHECK(orig_fn
.glRenderbufferStorageEXTFn
== NULL
);
195 orig_fn
.glRenderbufferStorageEXTFn
= fn
.glRenderbufferStorageEXTFn
;
196 fn
.glRenderbufferStorageEXTFn
=
197 reinterpret_cast<glRenderbufferStorageEXTProc
>(
198 CustomRenderbufferStorageEXT
);
200 DCHECK(orig_fn
.glRenderbufferStorageMultisampleEXTFn
== NULL
);
201 orig_fn
.glRenderbufferStorageMultisampleEXTFn
=
202 fn
.glRenderbufferStorageMultisampleEXTFn
;
203 fn
.glRenderbufferStorageMultisampleEXTFn
=
204 reinterpret_cast<glRenderbufferStorageMultisampleEXTProc
>(
205 CustomRenderbufferStorageMultisampleEXT
);
208 static void GL_BINDING_CALL
NullDrawClearFn(GLbitfield mask
) {
209 if (!g_driver_gl
.null_draw_bindings_enabled
)
210 g_driver_gl
.orig_fn
.glClearFn(mask
);
213 static void GL_BINDING_CALL
214 NullDrawDrawArraysFn(GLenum mode
, GLint first
, GLsizei count
) {
215 if (!g_driver_gl
.null_draw_bindings_enabled
)
216 g_driver_gl
.orig_fn
.glDrawArraysFn(mode
, first
, count
);
219 static void GL_BINDING_CALL
NullDrawDrawElementsFn(GLenum mode
,
222 const void* indices
) {
223 if (!g_driver_gl
.null_draw_bindings_enabled
)
224 g_driver_gl
.orig_fn
.glDrawElementsFn(mode
, count
, type
, indices
);
227 void DriverGL::InitializeNullDrawBindings() {
228 DCHECK(orig_fn
.glClearFn
== NULL
);
229 orig_fn
.glClearFn
= fn
.glClearFn
;
230 fn
.glClearFn
= NullDrawClearFn
;
232 DCHECK(orig_fn
.glDrawArraysFn
== NULL
);
233 orig_fn
.glDrawArraysFn
= fn
.glDrawArraysFn
;
234 fn
.glDrawArraysFn
= NullDrawDrawArraysFn
;
236 DCHECK(orig_fn
.glDrawElementsFn
== NULL
);
237 orig_fn
.glDrawElementsFn
= fn
.glDrawElementsFn
;
238 fn
.glDrawElementsFn
= NullDrawDrawElementsFn
;
240 null_draw_bindings_enabled
= true;
243 bool DriverGL::HasInitializedNullDrawBindings() {
244 return orig_fn
.glClearFn
!= NULL
&& orig_fn
.glDrawArraysFn
!= NULL
&&
245 orig_fn
.glDrawElementsFn
!= NULL
;
248 bool DriverGL::SetNullDrawBindingsEnabled(bool enabled
) {
249 DCHECK(orig_fn
.glClearFn
!= NULL
);
250 DCHECK(orig_fn
.glDrawArraysFn
!= NULL
);
251 DCHECK(orig_fn
.glDrawElementsFn
!= NULL
);
253 bool before
= null_draw_bindings_enabled
;
254 null_draw_bindings_enabled
= enabled
;
258 void InitializeStaticGLBindingsGL() {
259 g_current_gl_context_tls
= new base::ThreadLocalPointer
<GLApi
>;
260 g_driver_gl
.InitializeStaticBindings();
262 g_real_gl
= new RealGLApi();
263 g_trace_gl
= new TraceGLApi(g_real_gl
);
264 g_no_context_gl
= new NoContextGLApi();
266 g_real_gl
->Initialize(&g_driver_gl
);
268 if (CommandLine::ForCurrentProcess()->HasSwitch(
269 switches::kEnableGPUServiceTracing
)) {
275 GLApi
* GetCurrentGLApi() {
276 return g_current_gl_context_tls
->Get();
279 void SetGLApi(GLApi
* api
) {
280 g_current_gl_context_tls
->Set(api
);
283 void SetGLToRealGLApi() {
287 void SetGLApiToNoContext() {
288 SetGLApi(g_no_context_gl
);
291 void InitializeDynamicGLBindingsGL(GLContext
* context
) {
292 g_driver_gl
.InitializeCustomDynamicBindings(context
);
293 DCHECK(context
&& context
->IsCurrent(NULL
) && !g_version_info
);
294 g_version_info
= new GLVersionInfo(context
->GetGLVersion().c_str(),
295 context
->GetGLRenderer().c_str());
298 void InitializeDebugGLBindingsGL() {
299 g_driver_gl
.InitializeDebugBindings();
302 void InitializeNullDrawGLBindingsGL() {
303 g_driver_gl
.InitializeNullDrawBindings();
306 bool HasInitializedNullDrawGLBindingsGL() {
307 return g_driver_gl
.HasInitializedNullDrawBindings();
310 bool SetNullDrawGLBindingsEnabledGL(bool enabled
) {
311 return g_driver_gl
.SetNullDrawBindingsEnabled(enabled
);
314 void ClearGLBindingsGL() {
323 if (g_no_context_gl
) {
324 delete g_no_context_gl
;
325 g_no_context_gl
= NULL
;
328 g_driver_gl
.ClearBindings();
329 if (g_current_gl_context_tls
) {
330 delete g_current_gl_context_tls
;
331 g_current_gl_context_tls
= NULL
;
333 if (g_version_info
) {
334 delete g_version_info
;
335 g_version_info
= NULL
;
343 if (GetCurrentGLApi() == this)
347 GLApiBase::GLApiBase()
351 GLApiBase::~GLApiBase() {
354 void GLApiBase::InitializeBase(DriverGL
* driver
) {
358 void GLApiBase::SignalFlush() {
359 DCHECK(GLContext::GetCurrent());
360 GLContext::GetCurrent()->OnFlush();
363 RealGLApi::RealGLApi() {
366 RealGLApi::~RealGLApi() {
369 void RealGLApi::Initialize(DriverGL
* driver
) {
370 InitializeBase(driver
);
373 void RealGLApi::glFlushFn() {
374 GLApiBase::glFlushFn();
375 GLApiBase::SignalFlush();
378 void RealGLApi::glFinishFn() {
379 GLApiBase::glFinishFn();
380 GLApiBase::SignalFlush();
383 TraceGLApi::~TraceGLApi() {
386 NoContextGLApi::NoContextGLApi() {
389 NoContextGLApi::~NoContextGLApi() {
392 VirtualGLApi::VirtualGLApi()
393 : real_context_(NULL
),
394 current_context_(NULL
) {
397 VirtualGLApi::~VirtualGLApi() {
400 void VirtualGLApi::Initialize(DriverGL
* driver
, GLContext
* real_context
) {
401 InitializeBase(driver
);
402 real_context_
= real_context
;
404 DCHECK(real_context
->IsCurrent(NULL
));
405 std::string
ext_string(
406 reinterpret_cast<const char*>(driver_
->fn
.glGetStringFn(GL_EXTENSIONS
)));
407 std::vector
<std::string
> ext
;
408 Tokenize(ext_string
, " ", &ext
);
410 std::vector
<std::string
>::iterator it
;
411 // We can't support GL_EXT_occlusion_query_boolean which is
412 // based on GL_ARB_occlusion_query without a lot of work virtualizing
414 it
= std::find(ext
.begin(), ext
.end(), "GL_EXT_occlusion_query_boolean");
418 extensions_
= JoinString(ext
, " ");
421 bool VirtualGLApi::MakeCurrent(GLContext
* virtual_context
, GLSurface
* surface
) {
422 bool switched_contexts
= g_current_gl_context_tls
->Get() != this;
423 GLSurface
* current_surface
= GLSurface::GetCurrent();
424 if (switched_contexts
|| surface
!= current_surface
) {
425 // MakeCurrent 'lite' path that avoids potentially expensive MakeCurrent()
426 // calls if the GLSurface uses the same underlying surface or renders to
428 if (switched_contexts
|| !current_surface
||
429 !virtual_context
->IsCurrent(surface
)) {
430 if (!real_context_
->MakeCurrent(surface
)) {
436 DCHECK_EQ(real_context_
, GLContext::GetRealCurrent());
437 DCHECK(real_context_
->IsCurrent(NULL
));
438 DCHECK(virtual_context
->IsCurrent(surface
));
440 if (switched_contexts
|| virtual_context
!= current_context_
) {
441 // There should be no errors from the previous context leaking into the
443 DCHECK_EQ(glGetErrorFn(), static_cast<GLenum
>(GL_NO_ERROR
));
445 // Set all state that is different from the real state
446 GLApi
* temp
= GetCurrentGLApi();
448 if (virtual_context
->GetGLStateRestorer()->IsInitialized()) {
449 virtual_context
->GetGLStateRestorer()->RestoreState(
450 (current_context_
&& !switched_contexts
)
451 ? current_context_
->GetGLStateRestorer()
455 current_context_
= virtual_context
;
459 virtual_context
->SetCurrent(surface
);
460 if (!surface
->OnMakeCurrent(virtual_context
)) {
461 LOG(ERROR
) << "Could not make GLSurface current.";
467 void VirtualGLApi::OnReleaseVirtuallyCurrent(GLContext
* virtual_context
) {
468 if (current_context_
== virtual_context
)
469 current_context_
= NULL
;
472 const GLubyte
* VirtualGLApi::glGetStringFn(GLenum name
) {
475 return reinterpret_cast<const GLubyte
*>(extensions_
.c_str());
477 return driver_
->fn
.glGetStringFn(name
);
481 void VirtualGLApi::glFlushFn() {
482 GLApiBase::glFlushFn();
483 GLApiBase::SignalFlush();
486 void VirtualGLApi::glFinishFn() {
487 GLApiBase::glFinishFn();
488 GLApiBase::SignalFlush();