Revert 233039 "Blink roll 161254:161319"
[chromium-blink-merge.git] / ui / gl / gl_surface.cc
blobf09aa35924daf21f1ab19a5eb57ba463eb8e011c
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::Recreate() {
88 NOTIMPLEMENTED();
89 return false;
92 bool GLSurface::DeferDraws() {
93 return false;
96 std::string GLSurface::GetExtensions() {
97 return std::string();
100 bool GLSurface::HasExtension(const char* name) {
101 std::string extensions = GetExtensions();
102 extensions += " ";
104 std::string delimited_name(name);
105 delimited_name += " ";
107 return extensions.find(delimited_name) != std::string::npos;
110 unsigned int GLSurface::GetBackingFrameBufferObject() {
111 return 0;
114 bool GLSurface::PostSubBuffer(int x, int y, int width, int height) {
115 return false;
118 bool GLSurface::OnMakeCurrent(GLContext* context) {
119 return true;
122 bool GLSurface::SetBackbufferAllocation(bool allocated) {
123 return true;
126 void GLSurface::SetFrontbufferAllocation(bool allocated) {
129 void* GLSurface::GetShareHandle() {
130 NOTIMPLEMENTED();
131 return NULL;
134 void* GLSurface::GetDisplay() {
135 NOTIMPLEMENTED();
136 return NULL;
139 void* GLSurface::GetConfig() {
140 NOTIMPLEMENTED();
141 return NULL;
144 unsigned GLSurface::GetFormat() {
145 NOTIMPLEMENTED();
146 return 0;
149 VSyncProvider* GLSurface::GetVSyncProvider() {
150 return NULL;
153 GLSurface* GLSurface::GetCurrent() {
154 return current_surface_.Pointer()->Get();
157 GLSurface::~GLSurface() {
158 if (GetCurrent() == this)
159 SetCurrent(NULL);
162 void GLSurface::SetCurrent(GLSurface* surface) {
163 current_surface_.Pointer()->Set(surface);
166 bool GLSurface::ExtensionsContain(const char* c_extensions, const char* name) {
167 DCHECK(name);
168 if (!c_extensions)
169 return false;
170 std::string extensions(c_extensions);
171 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() {
186 surface_->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() {}
263 } // namespace gfx