OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2006-2010 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. | |
4 | |
5 #include "chrome/gpu/gpu_info_collector.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "app/gfx/gl/gl_bindings.h" | |
11 #include "app/gfx/gl/gl_context.h" | |
12 #include "base/logging.h" | |
13 #include "base/string_number_conversions.h" | |
14 #include "base/string_piece.h" | |
15 #include "base/string_split.h" | |
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) { | |
Ken Russell (switch to Gerrit)
2011/01/19 22:11:43
Mutable references aren't allowed in the Google C+
Zhenyao Mo
2011/01/19 23:36:22
Changed to gfx::GLContext**.
On 2011/01/19 22:11:
| |
42 if (context) { | |
43 context->Destroy(); | |
44 delete context; | |
45 context = NULL; | |
46 } | |
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 | |
57 uint32 GetVersionNumberFromString(const std::string& version_string) { | |
58 int major = 0, minor = 0; | |
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); | |
69 if (pieces.size() >= 2) { | |
70 base::StringToInt(pieces[0], &major); | |
71 base::StringToInt(pieces[1], &minor); | |
72 } | |
73 } | |
74 return ((major << 8) + minor); | |
75 } | |
76 | |
77 } // namespace anonymous | |
78 | |
79 namespace gpu_info_collector { | |
80 | |
81 bool CollectGraphicsInfoGL(GPUInfo* gpu_info) { | |
82 DCHECK(gpu_info); | |
83 | |
84 gfx::GLContext* context = InitializeGLContext(); | |
85 if (context == NULL) | |
86 return false; | |
87 | |
88 gpu_info->SetGLRenderer(GetGLString(GL_RENDERER)); | |
89 gpu_info->SetGLVendor(GetGLString(GL_VENDOR)); | |
90 gpu_info->SetGLVersionString(GetGLString(GL_VERSION)); | |
91 | |
92 bool validGLVersionInfo = CollectGLVersionInfo(gpu_info); | |
93 bool validVideoCardInfo = CollectVideoCardInfo(gpu_info); | |
94 bool validDriverInfo = CollectDriverInfo(gpu_info); | |
95 | |
96 FinalizeGLContext(context); | |
97 | |
98 return (validGLVersionInfo && validVideoCardInfo && validDriverInfo); | |
99 } | |
100 | |
101 bool CollectGLVersionInfo(GPUInfo* gpu_info) { | |
102 DCHECK(gpu_info); | |
103 | |
104 std::string gl_version_string = gpu_info->gl_version_string(); | |
105 std::string glsl_version_string = | |
106 GetGLString(GL_SHADING_LANGUAGE_VERSION); | |
107 | |
108 uint32 gl_version = GetVersionNumberFromString(gl_version_string); | |
109 gpu_info->SetGLVersion(gl_version); | |
110 | |
111 uint32 glsl_version = GetVersionNumberFromString(glsl_version_string); | |
112 gpu_info->SetShaderVersion(glsl_version, glsl_version); | |
113 | |
114 return true; | |
115 } | |
116 | |
117 } // namespace gpu_info_collector | |
118 | |
OLD | NEW |