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.
7 #include "base/command_line.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/threading/thread_local.h"
11 #include "ui/gl/gl_bindings.h"
12 #include "ui/gl/gl_context.h"
13 #include "ui/gl/gl_gl_api_implementation.h"
14 #include "ui/gl/gl_implementation.h"
15 #include "ui/gl/gl_surface.h"
16 #include "ui/gl/gl_switches.h"
21 base::LazyInstance<base::ThreadLocalPointer<GLContext> >::Leaky
22 current_context_ = LAZY_INSTANCE_INITIALIZER;
24 base::LazyInstance<base::ThreadLocalPointer<GLContext> >::Leaky
25 current_real_context_ = LAZY_INSTANCE_INITIALIZER;
28 GLContext::GLContext(GLShareGroup* share_group) : share_group_(share_group) {
29 if (!share_group_.get())
30 share_group_ = new GLShareGroup;
32 share_group_->AddContext(this);
35 GLContext::~GLContext() {
36 share_group_->RemoveContext(this);
37 if (GetCurrent() == this) {
42 bool GLContext::GetTotalGpuMemory(size_t* bytes) {
48 void GLContext::SetSafeToForceGpuSwitch() {
51 void GLContext::SetUnbindFboOnMakeCurrent() {
55 std::string GLContext::GetExtensions() {
56 DCHECK(IsCurrent(NULL));
57 const char* ext = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
58 return std::string(ext ? ext : "");
61 bool GLContext::HasExtension(const char* name) {
62 std::string extensions = GetExtensions();
65 std::string delimited_name(name);
66 delimited_name += " ";
68 return extensions.find(delimited_name) != std::string::npos;
71 GLShareGroup* GLContext::share_group() {
72 return share_group_.get();
75 bool GLContext::LosesAllContextsOnContextLost() {
76 switch (GetGLImplementation()) {
77 case kGLImplementationDesktopGL:
79 case kGLImplementationEGLGLES2:
81 case kGLImplementationOSMesaGL:
82 case kGLImplementationAppleGL:
84 case kGLImplementationMockGL:
92 GLContext* GLContext::GetCurrent() {
93 return current_context_.Pointer()->Get();
96 GLContext* GLContext::GetRealCurrent() {
97 return current_real_context_.Pointer()->Get();
100 void GLContext::SetCurrent(GLSurface* surface) {
101 current_context_.Pointer()->Set(surface ? this : NULL);
102 GLSurface::SetCurrent(surface);
105 GLStateRestorer* GLContext::GetGLStateRestorer() {
106 return state_restorer_.get();
109 void GLContext::SetGLStateRestorer(GLStateRestorer* state_restorer) {
110 state_restorer_ = make_scoped_ptr(state_restorer);
113 bool GLContext::WasAllocatedUsingRobustnessExtension() {
117 bool GLContext::InitializeExtensionBindings() {
118 DCHECK(IsCurrent(NULL));
119 static bool initialized = false;
122 initialized = InitializeGLExtensionBindings(GetGLImplementation(), this);
124 LOG(ERROR) << "Could not initialize extension bindings.";
128 void GLContext::SetupForVirtualization() {
129 if (!virtual_gl_api_) {
130 virtual_gl_api_.reset(new VirtualGLApi());
131 virtual_gl_api_->Initialize(&g_driver_gl, this);
135 bool GLContext::MakeVirtuallyCurrent(
136 GLContext* virtual_context, GLSurface* surface) {
137 DCHECK(virtual_gl_api_);
138 return virtual_gl_api_->MakeCurrent(virtual_context, surface);
141 void GLContext::OnReleaseVirtuallyCurrent(GLContext* virtual_context) {
143 virtual_gl_api_->OnReleaseVirtuallyCurrent(virtual_context);
146 void GLContext::SetRealGLApi() {
150 GLContextReal::GLContextReal(GLShareGroup* share_group)
151 : GLContext(share_group) {}
153 GLContextReal::~GLContextReal() {}
155 void GLContextReal::SetCurrent(GLSurface* surface) {
156 GLContext::SetCurrent(surface);
157 current_real_context_.Pointer()->Set(surface ? this : NULL);