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_surface.h"
10 #include "base/command_line.h"
11 #include "base/debug/trace_event.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/threading/thread_local.h"
15 #include "ui/gl/gl_context.h"
16 #include "ui/gl/gl_implementation.h"
21 base::LazyInstance
<base::ThreadLocalPointer
<GLSurface
> >::Leaky
22 current_surface_
= LAZY_INSTANCE_INITIALIZER
;
26 bool GLSurface::InitializeOneOff() {
27 static bool initialized
= false;
31 TRACE_EVENT0("gpu", "GLSurface::InitializeOneOff");
33 std::vector
<GLImplementation
> allowed_impls
;
34 GetAllowedGLImplementations(&allowed_impls
);
35 DCHECK(!allowed_impls
.empty());
37 // The default implementation is always the first one in list.
38 GLImplementation impl
= allowed_impls
[0];
39 bool fallback_to_osmesa
= false;
40 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseGL
)) {
41 std::string requested_implementation_name
=
42 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switches::kUseGL
);
43 if (requested_implementation_name
== "any") {
44 fallback_to_osmesa
= true;
45 } else if (requested_implementation_name
== "swiftshader") {
46 impl
= kGLImplementationEGLGLES2
;
48 impl
= GetNamedGLImplementation(requested_implementation_name
);
49 if (std::find(allowed_impls
.begin(),
51 impl
) == allowed_impls
.end()) {
52 LOG(ERROR
) << "Requested GL implementation is not available.";
58 initialized
= InitializeGLBindings(impl
) && InitializeOneOffInternal();
59 if (!initialized
&& fallback_to_osmesa
) {
61 initialized
= InitializeGLBindings(kGLImplementationOSMesaGL
) &&
62 InitializeOneOffInternal();
67 << GetGLImplementationName(GetGLImplementation())
68 << " GL implementation.";
69 if (CommandLine::ForCurrentProcess()->HasSwitch(
70 switches::kEnableGPUServiceLogging
))
71 InitializeDebugGLBindings();
76 GLSurface::GLSurface() {}
78 bool GLSurface::Initialize() {
82 bool GLSurface::Resize(const gfx::Size
& size
) {
87 bool GLSurface::DeferDraws() {
91 std::string
GLSurface::GetExtensions() {
95 bool GLSurface::HasExtension(const char* name
) {
96 std::string extensions
= GetExtensions();
99 std::string
delimited_name(name
);
100 delimited_name
+= " ";
102 return extensions
.find(delimited_name
) != std::string::npos
;
105 unsigned int GLSurface::GetBackingFrameBufferObject() {
109 bool GLSurface::PostSubBuffer(int x
, int y
, int width
, int height
) {
113 bool GLSurface::OnMakeCurrent(GLContext
* context
) {
117 bool GLSurface::SetBackbufferAllocation(bool allocated
) {
121 void GLSurface::SetFrontbufferAllocation(bool allocated
) {
124 void* GLSurface::GetShareHandle() {
129 void* GLSurface::GetDisplay() {
134 void* GLSurface::GetConfig() {
139 unsigned GLSurface::GetFormat() {
144 VSyncProvider
* GLSurface::GetVSyncProvider() {
148 GLSurface
* GLSurface::GetCurrent() {
149 return current_surface_
.Pointer()->Get();
152 GLSurface::~GLSurface() {
153 if (GetCurrent() == this)
157 void GLSurface::SetCurrent(GLSurface
* surface
) {
158 current_surface_
.Pointer()->Set(surface
);
161 bool GLSurface::ExtensionsContain(const char* c_extensions
, const char* name
) {
165 std::string
extensions(c_extensions
);
168 std::string
delimited_name(name
);
169 delimited_name
+= " ";
171 return extensions
.find(delimited_name
) != std::string::npos
;
174 GLSurfaceAdapter::GLSurfaceAdapter(GLSurface
* surface
) : surface_(surface
) {}
176 bool GLSurfaceAdapter::Initialize() {
177 return surface_
->Initialize();
180 void GLSurfaceAdapter::Destroy() {
184 bool GLSurfaceAdapter::Resize(const gfx::Size
& size
) {
185 return surface_
->Resize(size
);
188 bool GLSurfaceAdapter::DeferDraws() {
189 return surface_
->DeferDraws();
192 bool GLSurfaceAdapter::IsOffscreen() {
193 return surface_
->IsOffscreen();
196 bool GLSurfaceAdapter::SwapBuffers() {
197 return surface_
->SwapBuffers();
200 bool GLSurfaceAdapter::PostSubBuffer(int x
, int y
, int width
, int height
) {
201 return surface_
->PostSubBuffer(x
, y
, width
, height
);
204 std::string
GLSurfaceAdapter::GetExtensions() {
205 return surface_
->GetExtensions();
208 gfx::Size
GLSurfaceAdapter::GetSize() {
209 return surface_
->GetSize();
212 void* GLSurfaceAdapter::GetHandle() {
213 return surface_
->GetHandle();
216 unsigned int GLSurfaceAdapter::GetBackingFrameBufferObject() {
217 return surface_
->GetBackingFrameBufferObject();
220 bool GLSurfaceAdapter::OnMakeCurrent(GLContext
* context
) {
221 return surface_
->OnMakeCurrent(context
);
224 bool GLSurfaceAdapter::SetBackbufferAllocation(bool allocated
) {
225 return surface_
->SetBackbufferAllocation(allocated
);
228 void GLSurfaceAdapter::SetFrontbufferAllocation(bool allocated
) {
229 surface_
->SetFrontbufferAllocation(allocated
);
232 void* GLSurfaceAdapter::GetShareHandle() {
233 return surface_
->GetShareHandle();
236 void* GLSurfaceAdapter::GetDisplay() {
237 return surface_
->GetDisplay();
240 void* GLSurfaceAdapter::GetConfig() {
241 return surface_
->GetConfig();
244 unsigned GLSurfaceAdapter::GetFormat() {
245 return surface_
->GetFormat();
248 VSyncProvider
* GLSurfaceAdapter::GetVSyncProvider() {
249 return surface_
->GetVSyncProvider();
252 GLSurfaceAdapter::~GLSurfaceAdapter() {}