[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] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 10 | #include "base/logging.h" |
11 | #include "base/string_number_conversions.h" | ||||
12 | #include "base/string_piece.h" | ||||
13 | #include "base/string_split.h" | ||||
[email protected] | 5ae0b28 | 2011-03-28 19:24:49 | [diff] [blame^] | 14 | #include "ui/gfx/gl/gl_bindings.h" |
15 | #include "ui/gfx/gl/gl_context.h" | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 16 | |
17 | namespace { | ||||
18 | |||||
19 | // This creates an offscreen GL context for gl queries. Returned GLContext | ||||
20 | // should be deleted in FinalizeGLContext. | ||||
21 | gfx::GLContext* InitializeGLContext() { | ||||
22 | if (!gfx::GLContext::InitializeOneOff()) { | ||||
23 | LOG(ERROR) << "gfx::GLContext::InitializeOneOff() failed"; | ||||
24 | return NULL; | ||||
25 | } | ||||
26 | gfx::GLContext* context = gfx::GLContext::CreateOffscreenGLContext(NULL); | ||||
27 | if (context == NULL) { | ||||
28 | LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLContext(NULL) failed"; | ||||
29 | return NULL; | ||||
30 | } | ||||
31 | if (!context->MakeCurrent()) { | ||||
32 | LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed"; | ||||
33 | context->Destroy(); | ||||
34 | delete context; | ||||
35 | return NULL; | ||||
36 | } | ||||
37 | return context; | ||||
38 | } | ||||
39 | |||||
40 | // This destroy and delete the GL context. | ||||
41 | void FinalizeGLContext(gfx::GLContext** context) { | ||||
42 | DCHECK(context); | ||||
43 | if (*context) { | ||||
44 | (*context)->Destroy(); | ||||
45 | delete *context; | ||||
46 | *context = NULL; | ||||
47 | } | ||||
48 | } | ||||
49 | |||||
50 | std::string GetGLString(unsigned int pname) { | ||||
51 | const char* gl_string = | ||||
52 | reinterpret_cast<const char*>(glGetString(pname)); | ||||
53 | if (gl_string) | ||||
54 | return std::string(gl_string); | ||||
55 | return ""; | ||||
56 | } | ||||
57 | |||||
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 58 | // Return a version string in the format of "major.minor". |
59 | std::string GetVersionFromString(const std::string& version_string) { | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 60 | size_t begin = version_string.find_first_of("0123456789"); |
61 | if (begin != std::string::npos) { | ||||
62 | size_t end = version_string.find_first_not_of("01234567890.", begin); | ||||
63 | std::string sub_string; | ||||
64 | if (end != std::string::npos) | ||||
65 | sub_string = version_string.substr(begin, end - begin); | ||||
66 | else | ||||
67 | sub_string = version_string.substr(begin); | ||||
68 | std::vector<std::string> pieces; | ||||
69 | base::SplitString(sub_string, '.', &pieces); | ||||
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 70 | if (pieces.size() >= 2) |
71 | return pieces[0] + "." + pieces[1]; | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 72 | } |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 73 | return ""; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 74 | } |
75 | |||||
76 | } // namespace anonymous | ||||
77 | |||||
78 | namespace gpu_info_collector { | ||||
79 | |||||
80 | bool CollectGraphicsInfoGL(GPUInfo* gpu_info) { | ||||
81 | DCHECK(gpu_info); | ||||
82 | |||||
83 | gfx::GLContext* context = InitializeGLContext(); | ||||
84 | if (context == NULL) | ||||
85 | return false; | ||||
86 | |||||
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 87 | gpu_info->gl_renderer = GetGLString(GL_RENDERER); |
88 | gpu_info->gl_vendor = GetGLString(GL_VENDOR); | ||||
89 | gpu_info->gl_version_string =GetGLString(GL_VERSION); | ||||
90 | gpu_info->gl_extensions =GetGLString(GL_EXTENSIONS); | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 91 | |
92 | bool validGLVersionInfo = CollectGLVersionInfo(gpu_info); | ||||
93 | bool validVideoCardInfo = CollectVideoCardInfo(gpu_info); | ||||
[email protected] | 76665309 | 2011-03-01 16:45:23 | [diff] [blame] | 94 | bool validDriverInfo = CollectDriverInfoGL(gpu_info); |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 95 | |
96 | FinalizeGLContext(&context); | ||||
97 | |||||
98 | return (validGLVersionInfo && validVideoCardInfo && validDriverInfo); | ||||
99 | } | ||||
100 | |||||
101 | bool CollectGLVersionInfo(GPUInfo* gpu_info) { | ||||
102 | DCHECK(gpu_info); | ||||
103 | |||||
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 104 | std::string gl_version_string = gpu_info->gl_version_string; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 105 | std::string glsl_version_string = |
106 | GetGLString(GL_SHADING_LANGUAGE_VERSION); | ||||
107 | |||||
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 108 | gpu_info->gl_version = GetVersionFromString(gl_version_string); |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 109 | |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 110 | std::string glsl_version = GetVersionFromString(glsl_version_string); |
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 111 | gpu_info->pixel_shader_version = glsl_version; |
112 | gpu_info->vertex_shader_version = glsl_version; | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 113 | |
114 | return true; | ||||
115 | } | ||||
116 | |||||
117 | } // namespace gpu_info_collector | ||||
118 |