| 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_surface.h" | 5 #include "ui/gl/gl_surface.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/threading/thread_local.h" | 14 #include "base/threading/thread_local.h" |
| 15 #include "ui/gl/gl_context.h" | 15 #include "ui/gl/gl_context.h" |
| 16 #include "ui/gl/gl_implementation.h" | 16 #include "ui/gl/gl_implementation.h" |
| 17 #include "ui/gl/gl_switches.h" | |
| 18 | 17 |
| 19 namespace gfx { | 18 namespace gfx { |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 base::LazyInstance<base::ThreadLocalPointer<GLSurface> >::Leaky | 21 base::LazyInstance<base::ThreadLocalPointer<GLSurface> >::Leaky |
| 23 current_surface_ = LAZY_INSTANCE_INITIALIZER; | 22 current_surface_ = LAZY_INSTANCE_INITIALIZER; |
| 24 } // namespace | 23 } // namespace |
| 25 | 24 |
| 26 // static | 25 // static |
| 27 bool GLSurface::InitializeOneOff() { | 26 bool GLSurface::InitializeOneOff() { |
| 28 DCHECK_EQ(kGLImplementationNone, GetGLImplementation()); | 27 static bool initialized = false; |
| 28 if (initialized) |
| 29 return true; |
| 29 | 30 |
| 30 TRACE_EVENT0("gpu", "GLSurface::InitializeOneOff"); | 31 TRACE_EVENT0("gpu", "GLSurface::InitializeOneOff"); |
| 31 | 32 |
| 32 std::vector<GLImplementation> allowed_impls; | 33 std::vector<GLImplementation> allowed_impls; |
| 33 GetAllowedGLImplementations(&allowed_impls); | 34 GetAllowedGLImplementations(&allowed_impls); |
| 34 DCHECK(!allowed_impls.empty()); | 35 DCHECK(!allowed_impls.empty()); |
| 35 | 36 |
| 36 CommandLine* cmd = CommandLine::ForCurrentProcess(); | |
| 37 | |
| 38 // The default implementation is always the first one in list. | 37 // The default implementation is always the first one in list. |
| 39 GLImplementation impl = allowed_impls[0]; | 38 GLImplementation impl = allowed_impls[0]; |
| 40 bool fallback_to_osmesa = false; | 39 bool fallback_to_osmesa = false; |
| 41 if (cmd->HasSwitch(switches::kUseGL)) { | 40 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseGL)) { |
| 42 std::string requested_implementation_name = | 41 std::string requested_implementation_name = |
| 43 cmd->GetSwitchValueASCII(switches::kUseGL); | 42 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switches::kUseGL); |
| 44 if (requested_implementation_name == "any") { | 43 if (requested_implementation_name == "any") { |
| 45 fallback_to_osmesa = true; | 44 fallback_to_osmesa = true; |
| 46 } else if (requested_implementation_name == "swiftshader") { | 45 } else if (requested_implementation_name == "swiftshader") { |
| 47 impl = kGLImplementationEGLGLES2; | 46 impl = kGLImplementationEGLGLES2; |
| 48 } else { | 47 } else { |
| 49 impl = GetNamedGLImplementation(requested_implementation_name); | 48 impl = GetNamedGLImplementation(requested_implementation_name); |
| 50 if (std::find(allowed_impls.begin(), | 49 if (std::find(allowed_impls.begin(), |
| 51 allowed_impls.end(), | 50 allowed_impls.end(), |
| 52 impl) == allowed_impls.end()) { | 51 impl) == allowed_impls.end()) { |
| 53 LOG(ERROR) << "Requested GL implementation is not available."; | 52 LOG(ERROR) << "Requested GL implementation is not available."; |
| 54 return false; | 53 return false; |
| 55 } | 54 } |
| 56 } | 55 } |
| 57 } | 56 } |
| 58 | 57 |
| 59 bool gpu_service_logging = cmd->HasSwitch(switches::kEnableGPUServiceLogging); | 58 initialized = InitializeStaticGLBindings(impl) && InitializeOneOffInternal(); |
| 60 bool disable_gl_drawing = cmd->HasSwitch(switches::kDisableGLDrawingForTests); | |
| 61 | |
| 62 return InitializeOneOffImplementation( | |
| 63 impl, fallback_to_osmesa, gpu_service_logging, disable_gl_drawing); | |
| 64 } | |
| 65 | |
| 66 // static | |
| 67 bool GLSurface::InitializeOneOffImplementation(GLImplementation impl, | |
| 68 bool fallback_to_osmesa, | |
| 69 bool gpu_service_logging, | |
| 70 bool disable_gl_drawing) { | |
| 71 bool initialized = | |
| 72 InitializeStaticGLBindings(impl) && InitializeOneOffInternal(); | |
| 73 if (!initialized && fallback_to_osmesa) { | 59 if (!initialized && fallback_to_osmesa) { |
| 74 ClearGLBindings(); | 60 ClearGLBindings(); |
| 75 initialized = InitializeStaticGLBindings(kGLImplementationOSMesaGL) && | 61 initialized = InitializeStaticGLBindings(kGLImplementationOSMesaGL) && |
| 76 InitializeOneOffInternal(); | 62 InitializeOneOffInternal(); |
| 77 } | 63 } |
| 78 if (!initialized) | |
| 79 ClearGLBindings(); | |
| 80 | 64 |
| 81 if (initialized) { | 65 if (initialized) { |
| 82 DVLOG(1) << "Using " | 66 DVLOG(1) << "Using " |
| 83 << GetGLImplementationName(GetGLImplementation()) | 67 << GetGLImplementationName(GetGLImplementation()) |
| 84 << " GL implementation."; | 68 << " GL implementation."; |
| 85 if (gpu_service_logging) | 69 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 70 switches::kEnableGPUServiceLogging)) |
| 86 InitializeDebugGLBindings(); | 71 InitializeDebugGLBindings(); |
| 87 if (disable_gl_drawing) | 72 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 73 switches::kDisableGLDrawingForTests)) |
| 88 InitializeNullDrawGLBindings(); | 74 InitializeNullDrawGLBindings(); |
| 89 } | 75 } |
| 90 return initialized; | 76 return initialized; |
| 91 } | 77 } |
| 92 | 78 |
| 93 // static | |
| 94 void GLSurface::InitializeOneOffForTests() { | |
| 95 bool use_osmesa = true; | |
| 96 | |
| 97 #if defined(OS_ANDROID) | |
| 98 // On Android we always use hardware GL. | |
| 99 use_osmesa = false; | |
| 100 #endif | |
| 101 | |
| 102 std::vector<GLImplementation> allowed_impls; | |
| 103 GetAllowedGLImplementations(&allowed_impls); | |
| 104 DCHECK(!allowed_impls.empty()); | |
| 105 | |
| 106 GLImplementation impl = allowed_impls[0]; | |
| 107 if (use_osmesa) | |
| 108 impl = kGLImplementationOSMesaGL; | |
| 109 | |
| 110 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseGL)) | |
| 111 << "kUseGL has not effect in tests"; | |
| 112 | |
| 113 bool fallback_to_osmesa = false; | |
| 114 bool gpu_service_logging = false; | |
| 115 bool disable_gl_drawing = false; | |
| 116 // TODO(danakj): Unit tests do not produce pixel output by default. | |
| 117 // bool disable_gl_drawing = true; | |
| 118 | |
| 119 CHECK(InitializeOneOffImplementation( | |
| 120 impl, fallback_to_osmesa, gpu_service_logging, disable_gl_drawing)); | |
| 121 } | |
| 122 | |
| 123 // static | |
| 124 void GLSurface::InitializeOneOffWithMockBindingsForTests() { | |
| 125 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseGL)) | |
| 126 << "kUseGL has not effect in tests"; | |
| 127 | |
| 128 // This method may be called multiple times in the same process to set up | |
| 129 // mock bindings in different ways. | |
| 130 ClearGLBindings(); | |
| 131 | |
| 132 bool fallback_to_osmesa = false; | |
| 133 bool gpu_service_logging = false; | |
| 134 bool disable_gl_drawing = false; | |
| 135 | |
| 136 CHECK(InitializeOneOffImplementation(kGLImplementationMockGL, | |
| 137 fallback_to_osmesa, | |
| 138 gpu_service_logging, | |
| 139 disable_gl_drawing)); | |
| 140 } | |
| 141 | |
| 142 // static | |
| 143 void GLSurface::InitializeDynamicMockBindingsForTests(GLContext* context) { | |
| 144 CHECK(InitializeDynamicGLBindings(kGLImplementationMockGL, context)); | |
| 145 } | |
| 146 | |
| 147 GLSurface::GLSurface() {} | 79 GLSurface::GLSurface() {} |
| 148 | 80 |
| 149 bool GLSurface::Initialize() { | 81 bool GLSurface::Initialize() { |
| 150 return true; | 82 return true; |
| 151 } | 83 } |
| 152 | 84 |
| 153 bool GLSurface::Resize(const gfx::Size& size) { | 85 bool GLSurface::Resize(const gfx::Size& size) { |
| 154 NOTIMPLEMENTED(); | 86 NOTIMPLEMENTED(); |
| 155 return false; | 87 return false; |
| 156 } | 88 } |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 return surface_->GetFormat(); | 257 return surface_->GetFormat(); |
| 326 } | 258 } |
| 327 | 259 |
| 328 VSyncProvider* GLSurfaceAdapter::GetVSyncProvider() { | 260 VSyncProvider* GLSurfaceAdapter::GetVSyncProvider() { |
| 329 return surface_->GetVSyncProvider(); | 261 return surface_->GetVSyncProvider(); |
| 330 } | 262 } |
| 331 | 263 |
| 332 GLSurfaceAdapter::~GLSurfaceAdapter() {} | 264 GLSurfaceAdapter::~GLSurfaceAdapter() {} |
| 333 | 265 |
| 334 } // namespace gfx | 266 } // namespace gfx |
| OLD | NEW |