Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / gl / gl_surface.cc
blob0feeb34ddebb0711479c12807dada960d208d39f
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"
7 #include <algorithm>
8 #include <vector>
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"
18 namespace gfx {
20 namespace {
21 base::LazyInstance<base::ThreadLocalPointer<GLSurface> >::Leaky
22 current_surface_ = LAZY_INSTANCE_INITIALIZER;
23 } // namespace
25 // static
26 bool GLSurface::InitializeOneOff() {
27 static bool initialized = false;
28 if (initialized)
29 return true;
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;
47 } else {
48 impl = GetNamedGLImplementation(requested_implementation_name);
49 if (std::find(allowed_impls.begin(),
50 allowed_impls.end(),
51 impl) == allowed_impls.end()) {
52 LOG(ERROR) << "Requested GL implementation is not available.";
53 return false;
58 initialized = InitializeGLBindings(impl) && InitializeOneOffInternal();
59 if (!initialized && fallback_to_osmesa) {
60 ClearGLBindings();
61 initialized = InitializeGLBindings(kGLImplementationOSMesaGL) &&
62 InitializeOneOffInternal();
65 if (initialized) {
66 DVLOG(1) << "Using "
67 << GetGLImplementationName(GetGLImplementation())
68 << " GL implementation.";
69 if (CommandLine::ForCurrentProcess()->HasSwitch(
70 switches::kEnableGPUServiceLogging))
71 InitializeDebugGLBindings();
73 return initialized;
76 GLSurface::GLSurface() {}
78 bool GLSurface::Initialize() {
79 return true;
82 bool GLSurface::Resize(const gfx::Size& size) {
83 NOTIMPLEMENTED();
84 return false;
87 bool GLSurface::DeferDraws() {
88 return false;
91 std::string GLSurface::GetExtensions() {
92 return std::string();
95 bool GLSurface::HasExtension(const char* name) {
96 std::string extensions = GetExtensions();
97 extensions += " ";
99 std::string delimited_name(name);
100 delimited_name += " ";
102 return extensions.find(delimited_name) != std::string::npos;
105 unsigned int GLSurface::GetBackingFrameBufferObject() {
106 return 0;
109 bool GLSurface::PostSubBuffer(int x, int y, int width, int height) {
110 return false;
113 bool GLSurface::OnMakeCurrent(GLContext* context) {
114 return true;
117 bool GLSurface::SetBackbufferAllocation(bool allocated) {
118 return true;
121 void GLSurface::SetFrontbufferAllocation(bool allocated) {
124 void* GLSurface::GetShareHandle() {
125 NOTIMPLEMENTED();
126 return NULL;
129 void* GLSurface::GetDisplay() {
130 NOTIMPLEMENTED();
131 return NULL;
134 void* GLSurface::GetConfig() {
135 NOTIMPLEMENTED();
136 return NULL;
139 unsigned GLSurface::GetFormat() {
140 NOTIMPLEMENTED();
141 return 0;
144 VSyncProvider* GLSurface::GetVSyncProvider() {
145 return NULL;
148 GLSurface* GLSurface::GetCurrent() {
149 return current_surface_.Pointer()->Get();
152 GLSurface::~GLSurface() {
153 if (GetCurrent() == this)
154 SetCurrent(NULL);
157 void GLSurface::SetCurrent(GLSurface* surface) {
158 current_surface_.Pointer()->Set(surface);
161 bool GLSurface::ExtensionsContain(const char* c_extensions, const char* name) {
162 DCHECK(name);
163 if (!c_extensions)
164 return false;
165 std::string extensions(c_extensions);
166 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() {
181 surface_->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() {}
254 } // namespace gfx