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"
20 // The GL Api being used. This could be g_real_gl or gl_trace_gl
22 // A GL Api that calls directly into the driver.
23 static RealGLApi* g_real_gl;
24 // A GL Api that calls TRACE and then calls another GL api.
25 static TraceGLApi* g_trace_gl;
29 static inline GLenum GetInternalFormat(GLenum internal_format) {
30 if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2) {
31 if (internal_format == GL_BGRA_EXT || internal_format == GL_BGRA8_EXT)
34 return internal_format;
37 // TODO(epenner): Could the above function be merged into this and removed?
38 static inline GLenum GetTexInternalFormat(GLenum internal_format,
41 GLenum gl_internal_format = GetInternalFormat(internal_format);
43 if (gfx::GetGLImplementation() == gfx::kGLImplementationEGLGLES2)
44 return gl_internal_format;
46 if (type == GL_FLOAT) {
49 gl_internal_format = GL_RGBA32F_ARB;
52 gl_internal_format = GL_RGB32F_ARB;
54 case GL_LUMINANCE_ALPHA:
55 gl_internal_format = GL_LUMINANCE_ALPHA32F_ARB;
58 gl_internal_format = GL_LUMINANCE32F_ARB;
61 gl_internal_format = GL_ALPHA32F_ARB;
67 } else if (type == GL_HALF_FLOAT_OES) {
70 gl_internal_format = GL_RGBA16F_ARB;
73 gl_internal_format = GL_RGB16F_ARB;
75 case GL_LUMINANCE_ALPHA:
76 gl_internal_format = GL_LUMINANCE_ALPHA16F_ARB;
79 gl_internal_format = GL_LUMINANCE16F_ARB;
82 gl_internal_format = GL_ALPHA16F_ARB;
89 return gl_internal_format;
92 static inline GLenum GetTexType(GLenum type) {
93 if (gfx::GetGLImplementation() != gfx::kGLImplementationEGLGLES2) {
94 if (type == GL_HALF_FLOAT_OES)
95 return GL_HALF_FLOAT_ARB;
100 static void GL_BINDING_CALL CustomTexImage2D(
101 GLenum target, GLint level, GLint internalformat,
102 GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type,
103 const void* pixels) {
104 GLenum gl_internal_format = GetTexInternalFormat(
105 internalformat, format, type);
106 GLenum gl_type = GetTexType(type);
107 return g_driver_gl.orig_fn.glTexImage2DFn(
108 target, level, gl_internal_format, width, height, border, format, gl_type,
112 static void GL_BINDING_CALL CustomTexSubImage2D(
113 GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width,
114 GLsizei height, GLenum format, GLenum type, const void* pixels) {
115 GLenum gl_type = GetTexType(type);
116 return g_driver_gl.orig_fn.glTexSubImage2DFn(
117 target, level, xoffset, yoffset, width, height, format, gl_type, pixels);
120 static void GL_BINDING_CALL CustomTexStorage2DEXT(
121 GLenum target, GLsizei levels, GLenum internalformat, GLsizei width,
123 GLenum gl_internal_format = GetInternalFormat(internalformat);
124 return g_driver_gl.orig_fn.glTexStorage2DEXTFn(
125 target, levels, gl_internal_format, width, height);
128 static void GL_BINDING_CALL CustomRenderbufferStorageEXT(
129 GLenum target, GLenum internalformat, GLsizei width, GLsizei height) {
130 GLenum gl_internal_format = GetInternalFormat(internalformat);
131 return g_driver_gl.orig_fn.glRenderbufferStorageEXTFn(
132 target, gl_internal_format, width, height);
135 // The ANGLE and IMG variants of glRenderbufferStorageMultisample currently do
136 // not support BGRA render buffers so only the EXT one is customized. If
137 // GL_CHROMIUM_renderbuffer_format_BGRA8888 support is added to ANGLE then the
138 // ANGLE version should also be customized.
139 static void GL_BINDING_CALL CustomRenderbufferStorageMultisampleEXT(
140 GLenum target, GLsizei samples, GLenum internalformat, GLsizei width,
142 GLenum gl_internal_format = GetInternalFormat(internalformat);
143 return g_driver_gl.orig_fn.glRenderbufferStorageMultisampleEXTFn(
144 target, samples, gl_internal_format, width, height);
147 } // anonymous namespace
149 void DriverGL::Initialize() {
150 InitializeBindings();
153 void DriverGL::InitializeExtensions(GLContext* context) {
154 InitializeExtensionBindings(context);
157 reinterpret_cast<glTexImage2DProc>(CustomTexImage2D);
158 fn.glTexSubImage2DFn =
159 reinterpret_cast<glTexSubImage2DProc>(CustomTexSubImage2D);
160 fn.glTexStorage2DEXTFn =
161 reinterpret_cast<glTexStorage2DEXTProc>(CustomTexStorage2DEXT);
162 fn.glRenderbufferStorageEXTFn =
163 reinterpret_cast<glRenderbufferStorageEXTProc>(
164 CustomRenderbufferStorageEXT);
165 fn.glRenderbufferStorageMultisampleEXTFn =
166 reinterpret_cast<glRenderbufferStorageMultisampleEXTProc>(
167 CustomRenderbufferStorageMultisampleEXT);
170 void InitializeGLBindingsGL() {
171 g_current_gl_context_tls = new base::ThreadLocalPointer<GLApi>;
172 g_driver_gl.Initialize();
174 g_real_gl = new RealGLApi();
175 g_trace_gl = new TraceGLApi(g_real_gl);
177 g_real_gl->Initialize(&g_driver_gl);
179 if (CommandLine::ForCurrentProcess()->HasSwitch(
180 switches::kEnableGPUServiceTracing)) {
186 GLApi* GetCurrentGLApi() {
187 return g_current_gl_context_tls->Get();
190 void SetGLApi(GLApi* api) {
191 g_current_gl_context_tls->Set(api);
194 void SetGLToRealGLApi() {
198 void InitializeGLExtensionBindingsGL(GLContext* context) {
199 g_driver_gl.InitializeExtensions(context);
202 void InitializeDebugGLBindingsGL() {
203 g_driver_gl.InitializeDebugBindings();
206 void ClearGLBindingsGL() {
216 g_driver_gl.ClearBindings();
217 if (g_current_gl_context_tls) {
218 delete g_current_gl_context_tls;
219 g_current_gl_context_tls = NULL;
227 if (GetCurrentGLApi() == this)
231 GLApiBase::GLApiBase()
235 GLApiBase::~GLApiBase() {
238 void GLApiBase::InitializeBase(DriverGL* driver) {
242 RealGLApi::RealGLApi() {
245 RealGLApi::~RealGLApi() {
248 void RealGLApi::Initialize(DriverGL* driver) {
249 InitializeBase(driver);
252 TraceGLApi::~TraceGLApi() {
255 VirtualGLApi::VirtualGLApi()
256 : real_context_(NULL),
257 current_context_(NULL) {
260 VirtualGLApi::~VirtualGLApi() {
263 void VirtualGLApi::Initialize(DriverGL* driver, GLContext* real_context) {
264 InitializeBase(driver);
265 real_context_ = real_context;
267 DCHECK(real_context->IsCurrent(NULL));
268 std::string ext_string(
269 reinterpret_cast<const char*>(driver_->fn.glGetStringFn(GL_EXTENSIONS)));
270 std::vector<std::string> ext;
271 Tokenize(ext_string, " ", &ext);
273 std::vector<std::string>::iterator it;
274 // We can't support GL_EXT_occlusion_query_boolean which is
275 // based on GL_ARB_occlusion_query without a lot of work virtualizing
277 it = std::find(ext.begin(), ext.end(), "GL_EXT_occlusion_query_boolean");
281 extensions_ = JoinString(ext, " ");
284 bool VirtualGLApi::MakeCurrent(GLContext* virtual_context, GLSurface* surface) {
285 bool switched_contexts = g_current_gl_context_tls->Get() != this;
286 GLSurface* current_surface = GLSurface::GetCurrent();
287 if (switched_contexts || surface != current_surface) {
288 // MakeCurrent 'lite' path that avoids potentially expensive MakeCurrent()
289 // calls if the GLSurface uses the same underlying surface or renders to
291 if (switched_contexts || !current_surface ||
292 !virtual_context->IsCurrent(surface)) {
293 if (!real_context_->MakeCurrent(surface)) {
299 DCHECK_EQ(real_context_, GLContext::GetRealCurrent());
300 DCHECK(real_context_->IsCurrent(NULL));
301 DCHECK(virtual_context->IsCurrent(surface));
303 if (switched_contexts || virtual_context != current_context_) {
304 // There should be no errors from the previous context leaking into the
306 DCHECK_EQ(glGetErrorFn(), static_cast<GLenum>(GL_NO_ERROR));
308 current_context_ = virtual_context;
309 // Set all state that is different from the real state
310 // NOTE: !!! This is a temporary implementation that just restores all
311 // state to let us test that it works.
312 // TODO: ASAP, change this to something that only restores the state
313 // needed for individual GL calls.
314 GLApi* temp = GetCurrentGLApi();
316 if (virtual_context->GetGLStateRestorer()->IsInitialized())
317 virtual_context->GetGLStateRestorer()->RestoreState();
322 virtual_context->SetCurrent(surface);
323 if (!surface->OnMakeCurrent(virtual_context)) {
324 LOG(ERROR) << "Could not make GLSurface current.";
330 void VirtualGLApi::OnReleaseVirtuallyCurrent(GLContext* virtual_context) {
331 if (current_context_ == virtual_context)
332 current_context_ = NULL;
335 const GLubyte* VirtualGLApi::glGetStringFn(GLenum name) {
338 return reinterpret_cast<const GLubyte*>(extensions_.c_str());
340 return driver_->fn.glGetStringFn(name);