OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gl/gl_context.h" | 5 #include "ui/gl/gl_context.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/cancelable_callback.h" | 10 #include "base/cancelable_callback.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 if (!canceled_ && GetCurrent()) { | 38 if (!canceled_ && GetCurrent()) { |
39 GetCurrent()->ReleaseCurrent(nullptr); | 39 GetCurrent()->ReleaseCurrent(nullptr); |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 void GLContext::ScopedReleaseCurrent::Cancel() { | 43 void GLContext::ScopedReleaseCurrent::Cancel() { |
44 canceled_ = true; | 44 canceled_ = true; |
45 } | 45 } |
46 | 46 |
47 GLContext::GLContext(GLShareGroup* share_group) | 47 GLContext::GLContext(GLShareGroup* share_group) |
48 : share_group_(share_group), | 48 : static_bindings_initialized_(false), |
| 49 dynamic_bindings_initialized_(false), |
| 50 share_group_(share_group), |
49 current_virtual_context_(nullptr), | 51 current_virtual_context_(nullptr), |
50 state_dirtied_externally_(false), | 52 state_dirtied_externally_(false), |
51 swap_interval_(1), | 53 swap_interval_(1), |
52 force_swap_interval_zero_(false) { | 54 force_swap_interval_zero_(false) { |
53 if (!share_group_.get()) | 55 if (!share_group_.get()) |
54 share_group_ = new gl::GLShareGroup(); | 56 share_group_ = new gl::GLShareGroup(); |
55 | 57 |
56 share_group_->AddContext(this); | 58 share_group_->AddContext(this); |
57 } | 59 } |
58 | 60 |
59 GLContext::~GLContext() { | 61 GLContext::~GLContext() { |
60 share_group_->RemoveContext(this); | 62 share_group_->RemoveContext(this); |
61 if (GetCurrent() == this) { | 63 if (GetCurrent() == this) { |
62 SetCurrent(nullptr); | 64 SetCurrent(nullptr); |
| 65 SetCurrentGL(nullptr); |
63 } | 66 } |
64 } | 67 } |
65 | 68 |
| 69 GLApi* GLContext::CreateGLApi(DriverGL* driver) { |
| 70 real_gl_api_ = new RealGLApi; |
| 71 real_gl_api_->Initialize(driver); |
| 72 return real_gl_api_; |
| 73 } |
| 74 |
66 void GLContext::SetSafeToForceGpuSwitch() { | 75 void GLContext::SetSafeToForceGpuSwitch() { |
67 } | 76 } |
68 | 77 |
69 bool GLContext::ForceGpuSwitchIfNeeded() { | 78 bool GLContext::ForceGpuSwitchIfNeeded() { |
70 return true; | 79 return true; |
71 } | 80 } |
72 | 81 |
73 void GLContext::SetUnbindFboOnMakeCurrent() { | 82 void GLContext::SetUnbindFboOnMakeCurrent() { |
74 NOTIMPLEMENTED(); | 83 NOTIMPLEMENTED(); |
75 } | 84 } |
76 | 85 |
77 std::string GLContext::GetExtensions() { | 86 std::string GLContext::GetExtensions() { |
78 DCHECK(IsCurrent(nullptr)); | 87 DCHECK(IsCurrent(nullptr)); |
79 return GetGLExtensionsFromCurrentContext(); | 88 return GetGLExtensionsFromCurrentContext(gl_api_.get()); |
80 } | 89 } |
81 | 90 |
82 std::string GLContext::GetGLVersion() { | 91 std::string GLContext::GetGLVersion() { |
83 DCHECK(IsCurrent(nullptr)); | 92 DCHECK(IsCurrent(nullptr)); |
84 const char *version = | 93 DCHECK(gl_api_ != nullptr); |
85 reinterpret_cast<const char*>(glGetString(GL_VERSION)); | 94 const char* version = |
| 95 reinterpret_cast<const char*>(gl_api_->glGetStringFn(GL_VERSION)); |
86 return std::string(version ? version : ""); | 96 return std::string(version ? version : ""); |
87 } | 97 } |
88 | 98 |
89 std::string GLContext::GetGLRenderer() { | 99 std::string GLContext::GetGLRenderer() { |
90 DCHECK(IsCurrent(nullptr)); | 100 DCHECK(IsCurrent(nullptr)); |
91 const char *renderer = | 101 DCHECK(gl_api_ != nullptr); |
92 reinterpret_cast<const char*>(glGetString(GL_RENDERER)); | 102 const char* renderer = |
| 103 reinterpret_cast<const char*>(gl_api_->glGetStringFn(GL_RENDERER)); |
93 return std::string(renderer ? renderer : ""); | 104 return std::string(renderer ? renderer : ""); |
94 } | 105 } |
95 | 106 |
96 YUVToRGBConverter* GLContext::GetYUVToRGBConverter() { | 107 YUVToRGBConverter* GLContext::GetYUVToRGBConverter() { |
97 return nullptr; | 108 return nullptr; |
98 } | 109 } |
99 | 110 |
| 111 CurrentGL* GLContext::GetCurrentGL() { |
| 112 if (!static_bindings_initialized_) { |
| 113 driver_gl_.reset(new DriverGL); |
| 114 driver_gl_->InitializeStaticBindings(); |
| 115 |
| 116 gl_api_.reset(CreateGLApi(driver_gl_.get())); |
| 117 GLApi* final_api = gl_api_.get(); |
| 118 |
| 119 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 120 switches::kEnableGPUServiceTracing)) { |
| 121 trace_gl_api_.reset(new TraceGLApi(final_api)); |
| 122 final_api = trace_gl_api_.get(); |
| 123 } |
| 124 |
| 125 if (GetDebugGLBindingsInitializedGL()) { |
| 126 debug_gl_api_.reset(new DebugGLApi(final_api)); |
| 127 final_api = debug_gl_api_.get(); |
| 128 } |
| 129 |
| 130 current_gl_.reset(new CurrentGL); |
| 131 current_gl_->Driver = driver_gl_.get(); |
| 132 current_gl_->Api = final_api; |
| 133 current_gl_->Version = version_info_.get(); |
| 134 |
| 135 static_bindings_initialized_ = true; |
| 136 } |
| 137 |
| 138 return current_gl_.get(); |
| 139 } |
| 140 |
100 bool GLContext::HasExtension(const char* name) { | 141 bool GLContext::HasExtension(const char* name) { |
101 std::string extensions = GetExtensions(); | 142 std::string extensions = GetExtensions(); |
102 extensions += " "; | 143 extensions += " "; |
103 | 144 |
104 std::string delimited_name(name); | 145 std::string delimited_name(name); |
105 delimited_name += " "; | 146 delimited_name += " "; |
106 | 147 |
107 return extensions.find(delimited_name) != std::string::npos; | 148 return extensions.find(delimited_name) != std::string::npos; |
108 } | 149 } |
109 | 150 |
110 const GLVersionInfo* GLContext::GetVersionInfo() { | 151 const GLVersionInfo* GLContext::GetVersionInfo() { |
111 if (!version_info_) { | 152 if (!version_info_) { |
112 std::string version = GetGLVersion(); | 153 version_info_ = GenerateGLVersionInfo(); |
113 std::string renderer = GetGLRenderer(); | 154 |
114 version_info_ = base::MakeUnique<GLVersionInfo>( | 155 // current_gl_ may be null for virtual contexts |
115 version.c_str(), renderer.c_str(), GetExtensions().c_str()); | 156 if (current_gl_) { |
| 157 current_gl_->Version = version_info_.get(); |
| 158 } |
116 } | 159 } |
117 return version_info_.get(); | 160 return version_info_.get(); |
118 } | 161 } |
119 | 162 |
120 GLShareGroup* GLContext::share_group() { | 163 GLShareGroup* GLContext::share_group() { |
121 return share_group_.get(); | 164 return share_group_.get(); |
122 } | 165 } |
123 | 166 |
124 bool GLContext::LosesAllContextsOnContextLost() { | 167 bool GLContext::LosesAllContextsOnContextLost() { |
125 switch (GetGLImplementation()) { | 168 switch (GetGLImplementation()) { |
126 case kGLImplementationDesktopGL: | 169 case kGLImplementationDesktopGL: |
127 return false; | 170 return false; |
128 case kGLImplementationEGLGLES2: | 171 case kGLImplementationEGLGLES2: |
129 return true; | 172 return true; |
130 case kGLImplementationOSMesaGL: | 173 case kGLImplementationOSMesaGL: |
131 case kGLImplementationAppleGL: | 174 case kGLImplementationAppleGL: |
132 return false; | 175 return false; |
133 case kGLImplementationMockGL: | 176 case kGLImplementationMockGL: |
| 177 case kGLImplementationStubGL: |
134 return false; | 178 return false; |
135 default: | 179 default: |
136 NOTREACHED(); | 180 NOTREACHED(); |
137 return true; | 181 return true; |
138 } | 182 } |
139 } | 183 } |
140 | 184 |
141 GLContext* GLContext::GetCurrent() { | 185 GLContext* GLContext::GetCurrent() { |
142 return current_context_.Pointer()->Get(); | 186 return current_context_.Pointer()->Get(); |
143 } | 187 } |
144 | 188 |
145 GLContext* GLContext::GetRealCurrent() { | 189 GLContext* GLContext::GetRealCurrent() { |
146 return current_real_context_.Pointer()->Get(); | 190 return current_real_context_.Pointer()->Get(); |
147 } | 191 } |
148 | 192 |
| 193 std::unique_ptr<gl::GLVersionInfo> GLContext::GenerateGLVersionInfo() { |
| 194 return base::MakeUnique<GLVersionInfo>( |
| 195 GetGLVersion().c_str(), GetGLRenderer().c_str(), GetExtensions().c_str()); |
| 196 } |
| 197 |
149 void GLContext::SetCurrent(GLSurface* surface) { | 198 void GLContext::SetCurrent(GLSurface* surface) { |
150 current_context_.Pointer()->Set(surface ? this : nullptr); | 199 current_context_.Pointer()->Set(surface ? this : nullptr); |
151 GLSurface::SetCurrent(surface); | 200 GLSurface::SetCurrent(surface); |
152 // Leave the real GL api current so that unit tests work correctly. | 201 // Leave the real GL api current so that unit tests work correctly. |
153 // TODO(sievers): Remove this, but needs all gpu_unittest classes | 202 // TODO(sievers): Remove this, but needs all gpu_unittest classes |
154 // to create and make current a context. | 203 // to create and make current a context. |
155 if (!surface && GetGLImplementation() != kGLImplementationMockGL) { | 204 if (!surface && GetGLImplementation() != kGLImplementationMockGL && |
156 SetGLApiToNoContext(); | 205 GetGLImplementation() != kGLImplementationStubGL) { |
| 206 SetCurrentGL(nullptr); |
157 } | 207 } |
158 } | 208 } |
159 | 209 |
160 GLStateRestorer* GLContext::GetGLStateRestorer() { | 210 GLStateRestorer* GLContext::GetGLStateRestorer() { |
161 return state_restorer_.get(); | 211 return state_restorer_.get(); |
162 } | 212 } |
163 | 213 |
164 void GLContext::SetGLStateRestorer(GLStateRestorer* state_restorer) { | 214 void GLContext::SetGLStateRestorer(GLStateRestorer* state_restorer) { |
165 state_restorer_ = base::WrapUnique(state_restorer); | 215 state_restorer_ = base::WrapUnique(state_restorer); |
166 } | 216 } |
167 | 217 |
168 void GLContext::SetSwapInterval(int interval) { | 218 void GLContext::SetSwapInterval(int interval) { |
169 swap_interval_ = interval; | 219 swap_interval_ = interval; |
170 OnSetSwapInterval(force_swap_interval_zero_ ? 0 : swap_interval_); | 220 OnSetSwapInterval(force_swap_interval_zero_ ? 0 : swap_interval_); |
171 } | 221 } |
172 | 222 |
173 void GLContext::ForceSwapIntervalZero(bool force) { | 223 void GLContext::ForceSwapIntervalZero(bool force) { |
174 force_swap_interval_zero_ = force; | 224 force_swap_interval_zero_ = force; |
175 OnSetSwapInterval(force_swap_interval_zero_ ? 0 : swap_interval_); | 225 OnSetSwapInterval(force_swap_interval_zero_ ? 0 : swap_interval_); |
176 } | 226 } |
177 | 227 |
178 bool GLContext::WasAllocatedUsingRobustnessExtension() { | 228 bool GLContext::WasAllocatedUsingRobustnessExtension() { |
179 return false; | 229 return false; |
180 } | 230 } |
181 | 231 |
182 void GLContext::InitializeDynamicBindings() { | 232 void GLContext::InitializeDynamicBindings() { |
183 DCHECK(IsCurrent(nullptr)); | 233 DCHECK(IsCurrent(nullptr)); |
184 InitializeDynamicGLBindingsGL(this); | 234 DCHECK(static_bindings_initialized_); |
| 235 if (!dynamic_bindings_initialized_) { |
| 236 if (real_gl_api_) { |
| 237 real_gl_api_->InitializeFilteredExtensions(); |
| 238 real_gl_api_->set_version(GenerateGLVersionInfo()); |
| 239 } |
| 240 |
| 241 driver_gl_->InitializeDynamicBindings(GetVersionInfo(), GetExtensions()); |
| 242 dynamic_bindings_initialized_ = true; |
| 243 } |
185 } | 244 } |
186 | 245 |
187 bool GLContext::MakeVirtuallyCurrent( | 246 bool GLContext::MakeVirtuallyCurrent( |
188 GLContext* virtual_context, GLSurface* surface) { | 247 GLContext* virtual_context, GLSurface* surface) { |
189 if (!ForceGpuSwitchIfNeeded()) | 248 if (!ForceGpuSwitchIfNeeded()) |
190 return false; | 249 return false; |
191 bool switched_real_contexts = GLContext::GetRealCurrent() != this; | 250 bool switched_real_contexts = GLContext::GetRealCurrent() != this; |
192 GLSurface* current_surface = GLSurface::GetCurrent(); | 251 GLSurface* current_surface = GLSurface::GetCurrent(); |
193 if (switched_real_contexts || surface != current_surface) { | 252 if (switched_real_contexts || surface != current_surface) { |
194 // MakeCurrent 'lite' path that avoids potentially expensive MakeCurrent() | 253 // MakeCurrent 'lite' path that avoids potentially expensive MakeCurrent() |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 return false; | 299 return false; |
241 } | 300 } |
242 return true; | 301 return true; |
243 } | 302 } |
244 | 303 |
245 void GLContext::OnReleaseVirtuallyCurrent(GLContext* virtual_context) { | 304 void GLContext::OnReleaseVirtuallyCurrent(GLContext* virtual_context) { |
246 if (current_virtual_context_ == virtual_context) | 305 if (current_virtual_context_ == virtual_context) |
247 current_virtual_context_ = nullptr; | 306 current_virtual_context_ = nullptr; |
248 } | 307 } |
249 | 308 |
250 void GLContext::SetRealGLApi() { | 309 void GLContext::BindGLApi() { |
251 SetGLToRealGLApi(); | 310 SetCurrentGL(GetCurrentGL()); |
252 } | 311 } |
253 | 312 |
254 GLContextReal::GLContextReal(GLShareGroup* share_group) | 313 GLContextReal::GLContextReal(GLShareGroup* share_group) |
255 : GLContext(share_group) {} | 314 : GLContext(share_group) {} |
256 | 315 |
257 scoped_refptr<GPUTimingClient> GLContextReal::CreateGPUTimingClient() { | 316 scoped_refptr<GPUTimingClient> GLContextReal::CreateGPUTimingClient() { |
258 if (!gpu_timing_) { | 317 if (!gpu_timing_) { |
259 gpu_timing_.reset(GPUTiming::CreateGPUTiming(this)); | 318 gpu_timing_.reset(GPUTiming::CreateGPUTiming(this)); |
260 } | 319 } |
261 return gpu_timing_->CreateGPUTimingClient(); | 320 return gpu_timing_->CreateGPUTimingClient(); |
(...skipping 11 matching lines...) Expand all Loading... |
273 | 332 |
274 scoped_refptr<GLContext> InitializeGLContext(scoped_refptr<GLContext> context, | 333 scoped_refptr<GLContext> InitializeGLContext(scoped_refptr<GLContext> context, |
275 GLSurface* compatible_surface, | 334 GLSurface* compatible_surface, |
276 const GLContextAttribs& attribs) { | 335 const GLContextAttribs& attribs) { |
277 if (!context->Initialize(compatible_surface, attribs)) | 336 if (!context->Initialize(compatible_surface, attribs)) |
278 return nullptr; | 337 return nullptr; |
279 return context; | 338 return context; |
280 } | 339 } |
281 | 340 |
282 } // namespace gl | 341 } // namespace gl |
OLD | NEW |