[email protected] | 623c0bd | 2011-03-12 01:00:41 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 623c0bd | 2011-03-12 01:00:41 | [diff] [blame] | 5 | #include "content/gpu/gpu_info_collector.h" |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 6 | |
7 | #include <string> | ||||
8 | #include <vector> | ||||
9 | |||||
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 11 | #include "base/logging.h" |
12 | #include "base/string_number_conversions.h" | ||||
13 | #include "base/string_piece.h" | ||||
14 | #include "base/string_split.h" | ||||
[email protected] | 5ae0b28 | 2011-03-28 19:24:49 | [diff] [blame] | 15 | #include "ui/gfx/gl/gl_bindings.h" |
16 | #include "ui/gfx/gl/gl_context.h" | ||||
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 17 | #include "ui/gfx/gl/gl_surface.h" |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 18 | |
19 | namespace { | ||||
20 | |||||
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 21 | scoped_refptr<gfx::GLSurface> InitializeGLSurface() { |
22 | scoped_refptr<gfx::GLSurface> surface( | ||||
[email protected] | f81f595 | 2011-07-21 18:52:47 | [diff] [blame^] | 23 | gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1))); |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 24 | if (!surface.get()) { |
25 | LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLSurface failed"; | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 26 | return NULL; |
27 | } | ||||
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 28 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 29 | return surface; |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 30 | } |
31 | |||||
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 32 | scoped_refptr<gfx::GLContext> InitializeGLContext(gfx::GLSurface* surface) { |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 33 | |
[email protected] | 7196e01 | 2011-06-16 20:54:53 | [diff] [blame] | 34 | scoped_refptr<gfx::GLContext> context( |
35 | gfx::GLContext::CreateGLContext(NULL, surface)); | ||||
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 36 | if (!context.get()) { |
37 | LOG(ERROR) << "gfx::GLContext::CreateGLContext failed"; | ||||
38 | return NULL; | ||||
39 | } | ||||
40 | |||||
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 41 | if (!context->MakeCurrent(surface)) { |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 42 | LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed"; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 43 | return NULL; |
44 | } | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 45 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 46 | return context; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 47 | } |
48 | |||||
49 | std::string GetGLString(unsigned int pname) { | ||||
50 | const char* gl_string = | ||||
51 | reinterpret_cast<const char*>(glGetString(pname)); | ||||
52 | if (gl_string) | ||||
53 | return std::string(gl_string); | ||||
54 | return ""; | ||||
55 | } | ||||
56 | |||||
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 57 | // Return a version string in the format of "major.minor". |
58 | std::string GetVersionFromString(const std::string& version_string) { | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 59 | size_t begin = version_string.find_first_of("0123456789"); |
60 | if (begin != std::string::npos) { | ||||
61 | size_t end = version_string.find_first_not_of("01234567890.", begin); | ||||
62 | std::string sub_string; | ||||
63 | if (end != std::string::npos) | ||||
64 | sub_string = version_string.substr(begin, end - begin); | ||||
65 | else | ||||
66 | sub_string = version_string.substr(begin); | ||||
67 | std::vector<std::string> pieces; | ||||
68 | base::SplitString(sub_string, '.', &pieces); | ||||
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 69 | if (pieces.size() >= 2) |
70 | return pieces[0] + "." + pieces[1]; | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 71 | } |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 72 | return ""; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 73 | } |
74 | |||||
75 | } // namespace anonymous | ||||
76 | |||||
77 | namespace gpu_info_collector { | ||||
78 | |||||
79 | bool CollectGraphicsInfoGL(GPUInfo* gpu_info) { | ||||
80 | DCHECK(gpu_info); | ||||
81 | |||||
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 82 | if (!gfx::GLSurface::InitializeOneOff()) { |
83 | LOG(ERROR) << "gfx::GLContext::InitializeOneOff() failed"; | ||||
84 | return false; | ||||
85 | } | ||||
86 | |||||
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 87 | scoped_refptr<gfx::GLSurface> surface(InitializeGLSurface()); |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 88 | if (!surface.get()) |
89 | return false; | ||||
90 | |||||
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 91 | scoped_refptr<gfx::GLContext> context(InitializeGLContext(surface.get())); |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 92 | if (!context.get()) |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 93 | return false; |
94 | |||||
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 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); | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 99 | |
100 | bool validGLVersionInfo = CollectGLVersionInfo(gpu_info); | ||||
101 | bool validVideoCardInfo = CollectVideoCardInfo(gpu_info); | ||||
[email protected] | 76665309 | 2011-03-01 16:45:23 | [diff] [blame] | 102 | bool validDriverInfo = CollectDriverInfoGL(gpu_info); |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 103 | |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 104 | return (validGLVersionInfo && validVideoCardInfo && validDriverInfo); |
105 | } | ||||
106 | |||||
107 | bool CollectGLVersionInfo(GPUInfo* gpu_info) { | ||||
108 | DCHECK(gpu_info); | ||||
109 | |||||
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 110 | std::string gl_version_string = gpu_info->gl_version_string; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 111 | std::string glsl_version_string = |
112 | GetGLString(GL_SHADING_LANGUAGE_VERSION); | ||||
113 | |||||
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 114 | gpu_info->gl_version = GetVersionFromString(gl_version_string); |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 115 | |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 116 | std::string glsl_version = GetVersionFromString(glsl_version_string); |
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 117 | gpu_info->pixel_shader_version = glsl_version; |
118 | gpu_info->vertex_shader_version = glsl_version; | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 119 | |
120 | return true; | ||||
121 | } | ||||
122 | |||||
123 | } // namespace gpu_info_collector | ||||
124 |