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::Recreate() {
92 bool GLSurface::DeferDraws() {
96 std::string
GLSurface::GetExtensions() {
100 bool GLSurface::HasExtension(const char* name
) {
101 std::string extensions
= GetExtensions();
104 std::string
delimited_name(name
);
105 delimited_name
+= " ";
107 return extensions
.find(delimited_name
) != std::string::npos
;
110 unsigned int GLSurface::GetBackingFrameBufferObject() {
114 bool GLSurface::PostSubBuffer(int x
, int y
, int width
, int height
) {
118 bool GLSurface::OnMakeCurrent(GLContext
* context
) {
122 bool GLSurface::SetBackbufferAllocation(bool allocated
) {
126 void GLSurface::SetFrontbufferAllocation(bool allocated
) {
129 void* GLSurface::GetShareHandle() {
134 void* GLSurface::GetDisplay() {
139 void* GLSurface::GetConfig() {
144 unsigned GLSurface::GetFormat() {
149 VSyncProvider
* GLSurface::GetVSyncProvider() {
153 GLSurface
* GLSurface::GetCurrent() {
154 return current_surface_
.Pointer()->Get();
157 GLSurface::~GLSurface() {
158 if (GetCurrent() == this)
162 void GLSurface::SetCurrent(GLSurface
* surface
) {
163 current_surface_
.Pointer()->Set(surface
);
166 bool GLSurface::ExtensionsContain(const char* c_extensions
, const char* name
) {
170 std::string
extensions(c_extensions
);
173 std::string
delimited_name(name
);
174 delimited_name
+= " ";
176 return extensions
.find(delimited_name
) != std::string::npos
;
179 GLSurfaceAdapter::GLSurfaceAdapter(GLSurface
* surface
) : surface_(surface
) {}
181 bool GLSurfaceAdapter::Initialize() {
182 return surface_
->Initialize();
185 void GLSurfaceAdapter::Destroy() {
189 bool GLSurfaceAdapter::Resize(const gfx::Size
& size
) {
190 return surface_
->Resize(size
);
193 bool GLSurfaceAdapter::Recreate() {
194 return surface_
->Recreate();
197 bool GLSurfaceAdapter::DeferDraws() {
198 return surface_
->DeferDraws();
201 bool GLSurfaceAdapter::IsOffscreen() {
202 return surface_
->IsOffscreen();
205 bool GLSurfaceAdapter::SwapBuffers() {
206 return surface_
->SwapBuffers();
209 bool GLSurfaceAdapter::PostSubBuffer(int x
, int y
, int width
, int height
) {
210 return surface_
->PostSubBuffer(x
, y
, width
, height
);
213 std::string
GLSurfaceAdapter::GetExtensions() {
214 return surface_
->GetExtensions();
217 gfx::Size
GLSurfaceAdapter::GetSize() {
218 return surface_
->GetSize();
221 void* GLSurfaceAdapter::GetHandle() {
222 return surface_
->GetHandle();
225 unsigned int GLSurfaceAdapter::GetBackingFrameBufferObject() {
226 return surface_
->GetBackingFrameBufferObject();
229 bool GLSurfaceAdapter::OnMakeCurrent(GLContext
* context
) {
230 return surface_
->OnMakeCurrent(context
);
233 bool GLSurfaceAdapter::SetBackbufferAllocation(bool allocated
) {
234 return surface_
->SetBackbufferAllocation(allocated
);
237 void GLSurfaceAdapter::SetFrontbufferAllocation(bool allocated
) {
238 surface_
->SetFrontbufferAllocation(allocated
);
241 void* GLSurfaceAdapter::GetShareHandle() {
242 return surface_
->GetShareHandle();
245 void* GLSurfaceAdapter::GetDisplay() {
246 return surface_
->GetDisplay();
249 void* GLSurfaceAdapter::GetConfig() {
250 return surface_
->GetConfig();
253 unsigned GLSurfaceAdapter::GetFormat() {
254 return surface_
->GetFormat();
257 VSyncProvider
* GLSurfaceAdapter::GetVSyncProvider() {
258 return surface_
->GetVSyncProvider();
261 GLSurfaceAdapter::~GLSurfaceAdapter() {}