OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/gpu/gpu_info_collector.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/debug/trace_event.h" | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/string_number_conversions.h" | |
14 #include "base/strings/string_piece.h" | |
15 #include "base/strings/string_split.h" | |
16 #include "ui/gl/gl_bindings.h" | |
17 #include "ui/gl/gl_context.h" | |
18 #include "ui/gl/gl_surface.h" | |
19 | |
20 namespace { | |
21 | |
22 scoped_refptr<gfx::GLSurface> InitializeGLSurface() { | |
23 scoped_refptr<gfx::GLSurface> surface( | |
24 gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1))); | |
25 if (!surface) { | |
26 LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLSurface failed"; | |
27 return NULL; | |
28 } | |
29 | |
30 return surface; | |
31 } | |
32 | |
33 scoped_refptr<gfx::GLContext> InitializeGLContext(gfx::GLSurface* surface) { | |
34 | |
35 scoped_refptr<gfx::GLContext> context( | |
36 gfx::GLContext::CreateGLContext(NULL, | |
37 surface, | |
38 gfx::PreferIntegratedGpu)); | |
39 if (!context) { | |
40 LOG(ERROR) << "gfx::GLContext::CreateGLContext failed"; | |
41 return NULL; | |
42 } | |
43 | |
44 if (!context->MakeCurrent(surface)) { | |
45 LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed"; | |
46 return NULL; | |
47 } | |
48 | |
49 return context; | |
50 } | |
51 | |
52 std::string GetGLString(unsigned int pname) { | |
53 const char* gl_string = | |
54 reinterpret_cast<const char*>(glGetString(pname)); | |
55 if (gl_string) | |
56 return std::string(gl_string); | |
57 return std::string(); | |
58 } | |
59 | |
60 // Return a version string in the format of "major.minor". | |
61 std::string GetVersionFromString(const std::string& version_string) { | |
62 size_t begin = version_string.find_first_of("0123456789"); | |
63 if (begin != std::string::npos) { | |
64 size_t end = version_string.find_first_not_of("01234567890.", begin); | |
65 std::string sub_string; | |
66 if (end != std::string::npos) | |
67 sub_string = version_string.substr(begin, end - begin); | |
68 else | |
69 sub_string = version_string.substr(begin); | |
70 std::vector<std::string> pieces; | |
71 base::SplitString(sub_string, '.', &pieces); | |
72 if (pieces.size() >= 2) | |
73 return pieces[0] + "." + pieces[1]; | |
74 } | |
75 return std::string(); | |
76 } | |
77 | |
78 } // namespace anonymous | |
79 | |
80 namespace gpu_info_collector { | |
81 | |
82 bool CollectGraphicsInfoGL(content::GPUInfo* gpu_info) { | |
83 TRACE_EVENT0("startup", "gpu_info_collector::CollectGraphicsInfoGL"); | |
84 if (!gfx::GLSurface::InitializeOneOff()) { | |
85 LOG(ERROR) << "gfx::GLSurface::InitializeOneOff() failed"; | |
86 return false; | |
87 } | |
88 | |
89 scoped_refptr<gfx::GLSurface> surface(InitializeGLSurface()); | |
90 if (!surface) | |
91 return false; | |
92 | |
93 scoped_refptr<gfx::GLContext> context(InitializeGLContext(surface.get())); | |
94 if (!context) | |
95 return false; | |
96 | |
97 gpu_info->gl_renderer = GetGLString(GL_RENDERER); | |
98 gpu_info->gl_vendor = GetGLString(GL_VENDOR); | |
99 gpu_info->gl_extensions = GetGLString(GL_EXTENSIONS); | |
100 gpu_info->gl_version_string = GetGLString(GL_VERSION); | |
101 std::string glsl_version_string = GetGLString(GL_SHADING_LANGUAGE_VERSION); | |
102 // TODO(kbr): remove once the destruction of a current context automatically | |
103 // clears the current context. | |
104 context->ReleaseCurrent(surface.get()); | |
105 | |
106 gpu_info->gl_version = GetVersionFromString(gpu_info->gl_version_string); | |
107 std::string glsl_version = GetVersionFromString(glsl_version_string); | |
108 gpu_info->pixel_shader_version = glsl_version; | |
109 gpu_info->vertex_shader_version = glsl_version; | |
110 | |
111 return CollectDriverInfoGL(gpu_info); | |
112 } | |
113 | |
114 void MergeGPUInfoGL(content::GPUInfo* basic_gpu_info, | |
115 const content::GPUInfo& context_gpu_info) { | |
116 DCHECK(basic_gpu_info); | |
117 basic_gpu_info->gl_renderer = context_gpu_info.gl_renderer; | |
118 basic_gpu_info->gl_vendor = context_gpu_info.gl_vendor; | |
119 basic_gpu_info->gl_version_string = context_gpu_info.gl_version_string; | |
120 basic_gpu_info->gl_extensions = context_gpu_info.gl_extensions; | |
121 basic_gpu_info->gl_version = context_gpu_info.gl_version; | |
122 basic_gpu_info->pixel_shader_version = | |
123 context_gpu_info.pixel_shader_version; | |
124 basic_gpu_info->vertex_shader_version = | |
125 context_gpu_info.vertex_shader_version; | |
126 | |
127 if (!context_gpu_info.driver_vendor.empty()) | |
128 basic_gpu_info->driver_vendor = context_gpu_info.driver_vendor; | |
129 if (!context_gpu_info.driver_version.empty()) | |
130 basic_gpu_info->driver_version = context_gpu_info.driver_version; | |
131 | |
132 basic_gpu_info->can_lose_context = context_gpu_info.can_lose_context; | |
133 basic_gpu_info->sandboxed = context_gpu_info.sandboxed; | |
134 basic_gpu_info->gpu_accessible = context_gpu_info.gpu_accessible; | |
135 basic_gpu_info->finalized = context_gpu_info.finalized; | |
136 basic_gpu_info->initialization_time = context_gpu_info.initialization_time; | |
137 } | |
138 | |
139 } // namespace gpu_info_collector | |
140 | |
OLD | NEW |