1 // Copyright (c) 2011 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 "content/gpu/gpu_info_collector.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/logging.h"
12 #include "base/string_number_conversions.h"
13 #include "base/string_piece.h"
14 #include "base/string_split.h"
15 #include "ui/gfx/gl/gl_bindings.h"
16 #include "ui/gfx/gl/gl_context.h"
17 #include "ui/gfx/gl/gl_surface.h"
21 scoped_refptr
<gfx::GLSurface
> InitializeGLSurface() {
22 scoped_refptr
<gfx::GLSurface
> surface(
23 gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1)));
25 LOG(ERROR
) << "gfx::GLContext::CreateOffscreenGLSurface failed";
32 scoped_refptr
<gfx::GLContext
> InitializeGLContext(gfx::GLSurface
* surface
) {
34 scoped_refptr
<gfx::GLContext
> context(
35 gfx::GLContext::CreateGLContext(NULL
,
37 gfx::PreferDiscreteGpu
));
39 LOG(ERROR
) << "gfx::GLContext::CreateGLContext failed";
43 if (!context
->MakeCurrent(surface
)) {
44 LOG(ERROR
) << "gfx::GLContext::MakeCurrent() failed";
51 std::string
GetGLString(unsigned int pname
) {
52 const char* gl_string
=
53 reinterpret_cast<const char*>(glGetString(pname
));
55 return std::string(gl_string
);
59 // Return a version string in the format of "major.minor".
60 std::string
GetVersionFromString(const std::string
& version_string
) {
61 size_t begin
= version_string
.find_first_of("0123456789");
62 if (begin
!= std::string::npos
) {
63 size_t end
= version_string
.find_first_not_of("01234567890.", begin
);
64 std::string sub_string
;
65 if (end
!= std::string::npos
)
66 sub_string
= version_string
.substr(begin
, end
- begin
);
68 sub_string
= version_string
.substr(begin
);
69 std::vector
<std::string
> pieces
;
70 base::SplitString(sub_string
, '.', &pieces
);
71 if (pieces
.size() >= 2)
72 return pieces
[0] + "." + pieces
[1];
77 } // namespace anonymous
79 namespace gpu_info_collector
{
81 bool CollectGraphicsInfoGL(content::GPUInfo
* gpu_info
) {
82 if (!gfx::GLSurface::InitializeOneOff()) {
83 LOG(ERROR
) << "gfx::GLSurface::InitializeOneOff() failed";
87 scoped_refptr
<gfx::GLSurface
> surface(InitializeGLSurface());
91 scoped_refptr
<gfx::GLContext
> context(InitializeGLContext(surface
.get()));
95 gpu_info
->gl_renderer
= GetGLString(GL_RENDERER
);
96 gpu_info
->gl_vendor
= GetGLString(GL_VENDOR
);
97 gpu_info
->gl_version_string
=GetGLString(GL_VERSION
);
98 gpu_info
->gl_extensions
=GetGLString(GL_EXTENSIONS
);
100 bool validGLVersionInfo
= CollectGLVersionInfo(gpu_info
);
101 bool validVideoCardInfo
= CollectVideoCardInfo(gpu_info
);
102 bool validDriverInfo
= CollectDriverInfoGL(gpu_info
);
104 return (validGLVersionInfo
&& validVideoCardInfo
&& validDriverInfo
);
107 bool CollectGLVersionInfo(content::GPUInfo
* gpu_info
) {
108 std::string gl_version_string
= gpu_info
->gl_version_string
;
109 std::string glsl_version_string
=
110 GetGLString(GL_SHADING_LANGUAGE_VERSION
);
112 gpu_info
->gl_version
= GetVersionFromString(gl_version_string
);
114 std::string glsl_version
= GetVersionFromString(glsl_version_string
);
115 gpu_info
->pixel_shader_version
= glsl_version
;
116 gpu_info
->vertex_shader_version
= glsl_version
;
121 } // namespace gpu_info_collector