[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 24edbd0 | 2011-04-14 00:11:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 5 | #include "ui/gl/gl_surface.h" |
[email protected] | 24edbd0 | 2011-04-14 00:11:59 | [diff] [blame] | 6 | |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/command_line.h" |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 11 | #include "base/debug/trace_event.h" |
[email protected] | b6a9c04 | 2011-11-04 18:56:05 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
[email protected] | 1b5eee1 | 2011-10-26 21:44:12 | [diff] [blame] | 13 | #include "base/logging.h" |
[email protected] | c777de5 | 2011-09-09 23:08:56 | [diff] [blame] | 14 | #include "base/threading/thread_local.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 15 | #include "ui/gl/gl_context.h" |
| 16 | #include "ui/gl/gl_implementation.h" |
[email protected] | 4339025 | 2011-07-28 12:54:20 | [diff] [blame] | 17 | |
[email protected] | 24edbd0 | 2011-04-14 00:11:59 | [diff] [blame] | 18 | namespace gfx { |
| 19 | |
[email protected] | b6a9c04 | 2011-11-04 18:56:05 | [diff] [blame] | 20 | namespace { |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 21 | base::LazyInstance<base::ThreadLocalPointer<GLSurface> >::Leaky |
| 22 | current_surface_ = LAZY_INSTANCE_INITIALIZER; |
[email protected] | b6a9c04 | 2011-11-04 18:56:05 | [diff] [blame] | 23 | } // namespace |
[email protected] | c777de5 | 2011-09-09 23:08:56 | [diff] [blame] | 24 | |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 25 | // static |
| 26 | bool GLSurface::InitializeOneOff() { |
| 27 | static bool initialized = false; |
| 28 | if (initialized) |
| 29 | return true; |
| 30 | |
[email protected] | d13f35d | 2012-05-18 02:28:15 | [diff] [blame] | 31 | TRACE_EVENT0("gpu", "GLSurface::InitializeOneOff"); |
| 32 | |
[email protected] | 0f5e888 | 2011-11-08 22:29:38 | [diff] [blame] | 33 | std::vector<GLImplementation> allowed_impls; |
| 34 | GetAllowedGLImplementations(&allowed_impls); |
| 35 | DCHECK(!allowed_impls.empty()); |
| 36 | |
| 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; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | initialized = InitializeGLBindings(impl) && InitializeOneOffInternal(); |
| 59 | if (!initialized && fallback_to_osmesa) { |
| 60 | ClearGLBindings(); |
| 61 | initialized = InitializeGLBindings(kGLImplementationOSMesaGL) && |
| 62 | InitializeOneOffInternal(); |
| 63 | } |
| 64 | |
| 65 | if (initialized) { |
| 66 | DVLOG(1) << "Using " |
| 67 | << GetGLImplementationName(GetGLImplementation()) |
| 68 | << " GL implementation."; |
| 69 | if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 70 | switches::kEnableGPUServiceLogging)) |
| 71 | InitializeDebugGLBindings(); |
| 72 | } |
| 73 | return initialized; |
| 74 | } |
| 75 | |
[email protected] | ab9327c | 2012-05-02 02:38:08 | [diff] [blame] | 76 | GLSurface::GLSurface() {} |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 77 | |
[email protected] | 6f5fac9d1 | 2012-06-26 21:02:45 | [diff] [blame] | 78 | bool GLSurface::Initialize() { |
[email protected] | 7b1a3da2 | 2011-04-27 23:56:33 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
[email protected] | 1b5eee1 | 2011-10-26 21:44:12 | [diff] [blame] | 82 | bool GLSurface::Resize(const gfx::Size& size) { |
| 83 | NOTIMPLEMENTED(); |
| 84 | return false; |
| 85 | } |
| 86 | |
[email protected] | 85cb468 | 2013-04-20 00:54:24 | [diff] [blame] | 87 | bool GLSurface::Recreate() { |
| 88 | NOTIMPLEMENTED(); |
| 89 | return false; |
| 90 | } |
| 91 | |
[email protected] | a7266a9 | 2012-06-28 02:11:08 | [diff] [blame] | 92 | bool GLSurface::DeferDraws() { |
| 93 | return false; |
| 94 | } |
| 95 | |
[email protected] | aa25882 | 2011-11-22 13:52:25 | [diff] [blame] | 96 | std::string GLSurface::GetExtensions() { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 97 | return std::string(); |
[email protected] | 24edbd0 | 2011-04-14 00:11:59 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 6f5fac9d1 | 2012-06-26 21:02:45 | [diff] [blame] | 100 | bool GLSurface::HasExtension(const char* name) { |
| 101 | std::string extensions = GetExtensions(); |
| 102 | extensions += " "; |
| 103 | |
| 104 | std::string delimited_name(name); |
| 105 | delimited_name += " "; |
| 106 | |
| 107 | return extensions.find(delimited_name) != std::string::npos; |
| 108 | } |
| 109 | |
[email protected] | aa25882 | 2011-11-22 13:52:25 | [diff] [blame] | 110 | unsigned int GLSurface::GetBackingFrameBufferObject() { |
| 111 | return 0; |
[email protected] | 1c75a370 | 2011-11-11 14:15:28 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool GLSurface::PostSubBuffer(int x, int y, int width, int height) { |
| 115 | return false; |
| 116 | } |
| 117 | |
[email protected] | e4f9f9b | 2011-10-17 13:22:01 | [diff] [blame] | 118 | bool GLSurface::OnMakeCurrent(GLContext* context) { |
| 119 | return true; |
[email protected] | 2e7bbf2 | 2011-07-22 18:41:29 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | a9327cf | 2013-01-30 00:01:30 | [diff] [blame] | 122 | bool GLSurface::SetBackbufferAllocation(bool allocated) { |
| 123 | return true; |
[email protected] | 3ac71bf | 2012-05-16 15:55:53 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void GLSurface::SetFrontbufferAllocation(bool allocated) { |
[email protected] | 8cc980c5 | 2011-10-14 20:35:51 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 1b5eee1 | 2011-10-26 21:44:12 | [diff] [blame] | 129 | void* GLSurface::GetShareHandle() { |
| 130 | NOTIMPLEMENTED(); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | void* GLSurface::GetDisplay() { |
| 135 | NOTIMPLEMENTED(); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | void* GLSurface::GetConfig() { |
| 140 | NOTIMPLEMENTED(); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | unsigned GLSurface::GetFormat() { |
| 145 | NOTIMPLEMENTED(); |
| 146 | return 0; |
| 147 | } |
| 148 | |
[email protected] | 83f48ae | 2013-01-14 14:12:53 | [diff] [blame] | 149 | VSyncProvider* GLSurface::GetVSyncProvider() { |
| 150 | return NULL; |
[email protected] | a0c3fbb4 | 2012-11-01 15:40:36 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | c777de5 | 2011-09-09 23:08:56 | [diff] [blame] | 153 | GLSurface* GLSurface::GetCurrent() { |
[email protected] | b6a9c04 | 2011-11-04 18:56:05 | [diff] [blame] | 154 | return current_surface_.Pointer()->Get(); |
[email protected] | c777de5 | 2011-09-09 23:08:56 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | ab9327c | 2012-05-02 02:38:08 | [diff] [blame] | 157 | GLSurface::~GLSurface() { |
| 158 | if (GetCurrent() == this) |
| 159 | SetCurrent(NULL); |
| 160 | } |
| 161 | |
[email protected] | c777de5 | 2011-09-09 23:08:56 | [diff] [blame] | 162 | void GLSurface::SetCurrent(GLSurface* surface) { |
[email protected] | b6a9c04 | 2011-11-04 18:56:05 | [diff] [blame] | 163 | current_surface_.Pointer()->Set(surface); |
[email protected] | c777de5 | 2011-09-09 23:08:56 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | 706b69f | 2012-07-27 04:59:30 | [diff] [blame] | 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 += " "; |
| 172 | |
| 173 | std::string delimited_name(name); |
| 174 | delimited_name += " "; |
| 175 | |
| 176 | return extensions.find(delimited_name) != std::string::npos; |
| 177 | } |
| 178 | |
[email protected] | ab9327c | 2012-05-02 02:38:08 | [diff] [blame] | 179 | GLSurfaceAdapter::GLSurfaceAdapter(GLSurface* surface) : surface_(surface) {} |
[email protected] | 1b5eee1 | 2011-10-26 21:44:12 | [diff] [blame] | 180 | |
| 181 | bool GLSurfaceAdapter::Initialize() { |
| 182 | return surface_->Initialize(); |
| 183 | } |
| 184 | |
| 185 | void GLSurfaceAdapter::Destroy() { |
| 186 | surface_->Destroy(); |
| 187 | } |
| 188 | |
| 189 | bool GLSurfaceAdapter::Resize(const gfx::Size& size) { |
| 190 | return surface_->Resize(size); |
| 191 | } |
| 192 | |
[email protected] | 85cb468 | 2013-04-20 00:54:24 | [diff] [blame] | 193 | bool GLSurfaceAdapter::Recreate() { |
| 194 | return surface_->Recreate(); |
| 195 | } |
| 196 | |
[email protected] | a7266a9 | 2012-06-28 02:11:08 | [diff] [blame] | 197 | bool GLSurfaceAdapter::DeferDraws() { |
| 198 | return surface_->DeferDraws(); |
| 199 | } |
| 200 | |
[email protected] | 1b5eee1 | 2011-10-26 21:44:12 | [diff] [blame] | 201 | bool GLSurfaceAdapter::IsOffscreen() { |
| 202 | return surface_->IsOffscreen(); |
| 203 | } |
| 204 | |
| 205 | bool GLSurfaceAdapter::SwapBuffers() { |
| 206 | return surface_->SwapBuffers(); |
| 207 | } |
| 208 | |
[email protected] | 2368ad1 | 2011-11-23 19:49:27 | [diff] [blame] | 209 | bool GLSurfaceAdapter::PostSubBuffer(int x, int y, int width, int height) { |
| 210 | return surface_->PostSubBuffer(x, y, width, height); |
| 211 | } |
| 212 | |
| 213 | std::string GLSurfaceAdapter::GetExtensions() { |
| 214 | return surface_->GetExtensions(); |
| 215 | } |
| 216 | |
[email protected] | 1b5eee1 | 2011-10-26 21:44:12 | [diff] [blame] | 217 | gfx::Size GLSurfaceAdapter::GetSize() { |
| 218 | return surface_->GetSize(); |
| 219 | } |
| 220 | |
| 221 | void* GLSurfaceAdapter::GetHandle() { |
| 222 | return surface_->GetHandle(); |
| 223 | } |
| 224 | |
| 225 | unsigned int GLSurfaceAdapter::GetBackingFrameBufferObject() { |
| 226 | return surface_->GetBackingFrameBufferObject(); |
| 227 | } |
| 228 | |
| 229 | bool GLSurfaceAdapter::OnMakeCurrent(GLContext* context) { |
| 230 | return surface_->OnMakeCurrent(context); |
| 231 | } |
| 232 | |
[email protected] | a9327cf | 2013-01-30 00:01:30 | [diff] [blame] | 233 | bool GLSurfaceAdapter::SetBackbufferAllocation(bool allocated) { |
| 234 | return surface_->SetBackbufferAllocation(allocated); |
[email protected] | 3ac71bf | 2012-05-16 15:55:53 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void GLSurfaceAdapter::SetFrontbufferAllocation(bool allocated) { |
| 238 | surface_->SetFrontbufferAllocation(allocated); |
[email protected] | 91f30db | 2011-12-08 03:18:38 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 1b5eee1 | 2011-10-26 21:44:12 | [diff] [blame] | 241 | void* GLSurfaceAdapter::GetShareHandle() { |
| 242 | return surface_->GetShareHandle(); |
| 243 | } |
| 244 | |
| 245 | void* GLSurfaceAdapter::GetDisplay() { |
| 246 | return surface_->GetDisplay(); |
| 247 | } |
| 248 | |
| 249 | void* GLSurfaceAdapter::GetConfig() { |
| 250 | return surface_->GetConfig(); |
| 251 | } |
| 252 | |
| 253 | unsigned GLSurfaceAdapter::GetFormat() { |
| 254 | return surface_->GetFormat(); |
| 255 | } |
| 256 | |
[email protected] | 83f48ae | 2013-01-14 14:12:53 | [diff] [blame] | 257 | VSyncProvider* GLSurfaceAdapter::GetVSyncProvider() { |
| 258 | return surface_->GetVSyncProvider(); |
[email protected] | a0c3fbb4 | 2012-11-01 15:40:36 | [diff] [blame] | 259 | } |
| 260 | |
[email protected] | ab9327c | 2012-05-02 02:38:08 | [diff] [blame] | 261 | GLSurfaceAdapter::~GLSurfaceAdapter() {} |
| 262 | |
[email protected] | 24edbd0 | 2011-04-14 00:11:59 | [diff] [blame] | 263 | } // namespace gfx |