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