- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_context.cc
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.
4
5 #include <string>
6
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"
17
18 namespace gfx {
19
20 namespace {
21 base::LazyInstance<base::ThreadLocalPointer<GLContext> >::Leaky
22     current_context_ = LAZY_INSTANCE_INITIALIZER;
23
24 base::LazyInstance<base::ThreadLocalPointer<GLContext> >::Leaky
25     current_real_context_ = LAZY_INSTANCE_INITIALIZER;
26 }  // namespace
27
28 GLContext::GLContext(GLShareGroup* share_group) : share_group_(share_group) {
29   if (!share_group_.get())
30     share_group_ = new GLShareGroup;
31
32   share_group_->AddContext(this);
33 }
34
35 GLContext::~GLContext() {
36   share_group_->RemoveContext(this);
37   if (GetCurrent() == this) {
38     SetCurrent(NULL);
39   }
40 }
41
42 bool GLContext::GetTotalGpuMemory(size_t* bytes) {
43   DCHECK(bytes);
44   *bytes = 0;
45   return false;
46 }
47
48 void GLContext::SetSafeToForceGpuSwitch() {
49 }
50
51 void GLContext::SetUnbindFboOnMakeCurrent() {
52   NOTIMPLEMENTED();
53 }
54
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 : "");
59 }
60
61 bool GLContext::HasExtension(const char* name) {
62   std::string extensions = GetExtensions();
63   extensions += " ";
64
65   std::string delimited_name(name);
66   delimited_name += " ";
67
68   return extensions.find(delimited_name) != std::string::npos;
69 }
70
71 GLShareGroup* GLContext::share_group() {
72   return share_group_.get();
73 }
74
75 bool GLContext::LosesAllContextsOnContextLost() {
76   switch (GetGLImplementation()) {
77     case kGLImplementationDesktopGL:
78       return false;
79     case kGLImplementationEGLGLES2:
80       return true;
81     case kGLImplementationOSMesaGL:
82     case kGLImplementationAppleGL:
83       return false;
84     case kGLImplementationMockGL:
85       return false;
86     default:
87       NOTREACHED();
88       return true;
89   }
90 }
91
92 GLContext* GLContext::GetCurrent() {
93   return current_context_.Pointer()->Get();
94 }
95
96 GLContext* GLContext::GetRealCurrent() {
97   return current_real_context_.Pointer()->Get();
98 }
99
100 void GLContext::SetCurrent(GLSurface* surface) {
101   current_context_.Pointer()->Set(surface ? this : NULL);
102   GLSurface::SetCurrent(surface);
103 }
104
105 GLStateRestorer* GLContext::GetGLStateRestorer() {
106   return state_restorer_.get();
107 }
108
109 void GLContext::SetGLStateRestorer(GLStateRestorer* state_restorer) {
110   state_restorer_ = make_scoped_ptr(state_restorer);
111 }
112
113 bool GLContext::WasAllocatedUsingRobustnessExtension() {
114   return false;
115 }
116
117 bool GLContext::InitializeExtensionBindings() {
118   DCHECK(IsCurrent(NULL));
119   static bool initialized = false;
120   if (initialized)
121     return initialized;
122   initialized = InitializeGLExtensionBindings(GetGLImplementation(), this);
123   if (!initialized)
124     LOG(ERROR) << "Could not initialize extension bindings.";
125   return initialized;
126 }
127
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);
132   }
133 }
134
135 bool GLContext::MakeVirtuallyCurrent(
136     GLContext* virtual_context, GLSurface* surface) {
137   DCHECK(virtual_gl_api_);
138   return virtual_gl_api_->MakeCurrent(virtual_context, surface);
139 }
140
141 void GLContext::OnReleaseVirtuallyCurrent(GLContext* virtual_context) {
142   if (virtual_gl_api_)
143     virtual_gl_api_->OnReleaseVirtuallyCurrent(virtual_context);
144 }
145
146 void GLContext::SetRealGLApi() {
147   SetGLToRealGLApi();
148 }
149
150 GLContextReal::GLContextReal(GLShareGroup* share_group)
151     : GLContext(share_group) {}
152
153 GLContextReal::~GLContextReal() {}
154
155 void GLContextReal::SetCurrent(GLSurface* surface) {
156   GLContext::SetCurrent(surface);
157   current_real_context_.Pointer()->Set(surface ? this : NULL);
158 }
159
160 }  // namespace gfx