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
3 // found in the LICENSE file.
5 #include "gpu/config/gpu_info_collector.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_split.h"
15 #include "base/trace_event/trace_event.h"
16 #include "ui/gl/gl_bindings.h"
17 #include "ui/gl/gl_context.h"
18 #include "ui/gl/gl_implementation.h"
19 #include "ui/gl/gl_surface.h"
23 scoped_refptr
<gfx::GLSurface
> InitializeGLSurface() {
24 scoped_refptr
<gfx::GLSurface
> surface(
25 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
27 LOG(ERROR
) << "gfx::GLContext::CreateOffscreenGLSurface failed";
34 scoped_refptr
<gfx::GLContext
> InitializeGLContext(gfx::GLSurface
* surface
) {
36 scoped_refptr
<gfx::GLContext
> context(
37 gfx::GLContext::CreateGLContext(NULL
,
39 gfx::PreferIntegratedGpu
));
41 LOG(ERROR
) << "gfx::GLContext::CreateGLContext failed";
45 if (!context
->MakeCurrent(surface
)) {
46 LOG(ERROR
) << "gfx::GLContext::MakeCurrent() failed";
53 std::string
GetGLString(unsigned int pname
) {
54 const char* gl_string
=
55 reinterpret_cast<const char*>(glGetString(pname
));
57 return std::string(gl_string
);
61 // Return a version string in the format of "major.minor".
62 std::string
GetVersionFromString(const std::string
& version_string
) {
63 size_t begin
= version_string
.find_first_of("0123456789");
64 if (begin
!= std::string::npos
) {
65 size_t end
= version_string
.find_first_not_of("01234567890.", begin
);
66 std::string sub_string
;
67 if (end
!= std::string::npos
)
68 sub_string
= version_string
.substr(begin
, end
- begin
);
70 sub_string
= version_string
.substr(begin
);
71 std::vector
<std::string
> pieces
;
72 base::SplitString(sub_string
, '.', &pieces
);
73 if (pieces
.size() >= 2)
74 return pieces
[0] + "." + pieces
[1];
79 } // namespace anonymous
83 CollectInfoResult
CollectGraphicsInfoGL(GPUInfo
* gpu_info
) {
84 TRACE_EVENT0("startup", "gpu_info_collector::CollectGraphicsInfoGL");
85 DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone
);
87 scoped_refptr
<gfx::GLSurface
> surface(InitializeGLSurface());
89 LOG(ERROR
) << "Could not create surface for info collection.";
90 return kCollectInfoFatalFailure
;
93 scoped_refptr
<gfx::GLContext
> context(InitializeGLContext(surface
.get()));
95 LOG(ERROR
) << "Could not create context for info collection.";
96 return kCollectInfoFatalFailure
;
99 gpu_info
->gl_renderer
= GetGLString(GL_RENDERER
);
100 gpu_info
->gl_vendor
= GetGLString(GL_VENDOR
);
101 gpu_info
->gl_extensions
= GetGLString(GL_EXTENSIONS
);
102 gpu_info
->gl_version
= GetGLString(GL_VERSION
);
103 std::string glsl_version_string
= GetGLString(GL_SHADING_LANGUAGE_VERSION
);
105 gfx::GLWindowSystemBindingInfo window_system_binding_info
;
106 if (GetGLWindowSystemBindingInfo(&window_system_binding_info
)) {
107 gpu_info
->gl_ws_vendor
= window_system_binding_info
.vendor
;
108 gpu_info
->gl_ws_version
= window_system_binding_info
.version
;
109 gpu_info
->gl_ws_extensions
= window_system_binding_info
.extensions
;
110 gpu_info
->direct_rendering
= window_system_binding_info
.direct_rendering
;
113 bool supports_robustness
=
114 gpu_info
->gl_extensions
.find("GL_EXT_robustness") != std::string::npos
||
115 gpu_info
->gl_extensions
.find("GL_KHR_robustness") != std::string::npos
||
116 gpu_info
->gl_extensions
.find("GL_ARB_robustness") != std::string::npos
;
117 if (supports_robustness
) {
118 glGetIntegerv(GL_RESET_NOTIFICATION_STRATEGY_ARB
,
119 reinterpret_cast<GLint
*>(&gpu_info
->gl_reset_notification_strategy
));
122 // TODO(kbr): remove once the destruction of a current context automatically
123 // clears the current context.
124 context
->ReleaseCurrent(surface
.get());
126 std::string glsl_version
= GetVersionFromString(glsl_version_string
);
127 gpu_info
->pixel_shader_version
= glsl_version
;
128 gpu_info
->vertex_shader_version
= glsl_version
;
130 return CollectDriverInfoGL(gpu_info
);
133 void MergeGPUInfoGL(GPUInfo
* basic_gpu_info
,
134 const GPUInfo
& context_gpu_info
) {
135 DCHECK(basic_gpu_info
);
136 basic_gpu_info
->gl_renderer
= context_gpu_info
.gl_renderer
;
137 basic_gpu_info
->gl_vendor
= context_gpu_info
.gl_vendor
;
138 basic_gpu_info
->gl_version
= context_gpu_info
.gl_version
;
139 basic_gpu_info
->gl_extensions
= context_gpu_info
.gl_extensions
;
140 basic_gpu_info
->pixel_shader_version
=
141 context_gpu_info
.pixel_shader_version
;
142 basic_gpu_info
->vertex_shader_version
=
143 context_gpu_info
.vertex_shader_version
;
144 basic_gpu_info
->gl_ws_vendor
= context_gpu_info
.gl_ws_vendor
;
145 basic_gpu_info
->gl_ws_version
= context_gpu_info
.gl_ws_version
;
146 basic_gpu_info
->gl_ws_extensions
= context_gpu_info
.gl_ws_extensions
;
147 basic_gpu_info
->gl_reset_notification_strategy
=
148 context_gpu_info
.gl_reset_notification_strategy
;
150 if (!context_gpu_info
.driver_vendor
.empty())
151 basic_gpu_info
->driver_vendor
= context_gpu_info
.driver_vendor
;
152 if (!context_gpu_info
.driver_version
.empty())
153 basic_gpu_info
->driver_version
= context_gpu_info
.driver_version
;
155 basic_gpu_info
->can_lose_context
= context_gpu_info
.can_lose_context
;
156 basic_gpu_info
->sandboxed
= context_gpu_info
.sandboxed
;
157 basic_gpu_info
->direct_rendering
= context_gpu_info
.direct_rendering
;
158 basic_gpu_info
->context_info_state
= context_gpu_info
.context_info_state
;
159 basic_gpu_info
->initialization_time
= context_gpu_info
.initialization_time
;
160 basic_gpu_info
->video_encode_accelerator_supported_profiles
=
161 context_gpu_info
.video_encode_accelerator_supported_profiles
;