Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: ui/gl/gl_surface.cc

Issue 135213003: Ensure GL initialization only happens once, and provide common init path (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: initgl: rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #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
79 GLSurface::GLSurface() {} 147 GLSurface::GLSurface() {}
80 148
81 bool GLSurface::Initialize() { 149 bool GLSurface::Initialize() {
82 return true; 150 return true;
83 } 151 }
84 152
85 bool GLSurface::Resize(const gfx::Size& size) { 153 bool GLSurface::Resize(const gfx::Size& size) {
86 NOTIMPLEMENTED(); 154 NOTIMPLEMENTED();
87 return false; 155 return false;
88 } 156 }
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 return surface_->GetFormat(); 325 return surface_->GetFormat();
258 } 326 }
259 327
260 VSyncProvider* GLSurfaceAdapter::GetVSyncProvider() { 328 VSyncProvider* GLSurfaceAdapter::GetVSyncProvider() {
261 return surface_->GetVSyncProvider(); 329 return surface_->GetVSyncProvider();
262 } 330 }
263 331
264 GLSurfaceAdapter::~GLSurfaceAdapter() {} 332 GLSurfaceAdapter::~GLSurfaceAdapter() {}
265 333
266 } // namespace gfx 334 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698