[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 1 | // Copyright (c) 2012 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" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 12 | #include "base/string_number_conversions.h" |
13 | #include "base/string_piece.h" | ||||
[email protected] | 27c0573 | 2013-02-15 21:55:49 | [diff] [blame] | 14 | #include "base/strings/string_split.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 15 | #include "ui/gl/gl_bindings.h" |
16 | #include "ui/gl/gl_context.h" | ||||
17 | #include "ui/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( |
[email protected] | 276f8906 | 2011-10-13 22:55:50 | [diff] [blame] | 35 | gfx::GLContext::CreateGLContext(NULL, |
36 | surface, | ||||
[email protected] | 56403c2 | 2012-10-04 18:27:04 | [diff] [blame] | 37 | gfx::PreferIntegratedGpu)); |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 38 | if (!context.get()) { |
39 | LOG(ERROR) << "gfx::GLContext::CreateGLContext failed"; | ||||
40 | return NULL; | ||||
41 | } | ||||
42 | |||||
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 43 | if (!context->MakeCurrent(surface)) { |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 44 | LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed"; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 45 | return NULL; |
46 | } | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 47 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 48 | return context; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 49 | } |
50 | |||||
51 | std::string GetGLString(unsigned int pname) { | ||||
52 | const char* gl_string = | ||||
53 | reinterpret_cast<const char*>(glGetString(pname)); | ||||
54 | if (gl_string) | ||||
55 | return std::string(gl_string); | ||||
[email protected] | 69d7f89 | 2013-04-09 06:41:12 | [diff] [blame] | 56 | return ""; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 57 | } |
58 | |||||
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 59 | // Return a version string in the format of "major.minor". |
60 | std::string GetVersionFromString(const std::string& version_string) { | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 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); | ||||
67 | else | ||||
68 | sub_string = version_string.substr(begin); | ||||
69 | std::vector<std::string> pieces; | ||||
70 | base::SplitString(sub_string, '.', &pieces); | ||||
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 71 | if (pieces.size() >= 2) |
72 | return pieces[0] + "." + pieces[1]; | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 73 | } |
[email protected] | 69d7f89 | 2013-04-09 06:41:12 | [diff] [blame] | 74 | return ""; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 75 | } |
76 | |||||
77 | } // namespace anonymous | ||||
78 | |||||
79 | namespace gpu_info_collector { | ||||
80 | |||||
[email protected] | a80f5ece | 2011-10-20 23:56:55 | [diff] [blame] | 81 | bool CollectGraphicsInfoGL(content::GPUInfo* gpu_info) { |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 82 | if (!gfx::GLSurface::InitializeOneOff()) { |
[email protected] | 51dd1b48 | 2011-10-18 19:35:19 | [diff] [blame] | 83 | LOG(ERROR) << "gfx::GLSurface::InitializeOneOff() failed"; |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 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); | ||||
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 97 | gpu_info->gl_extensions = GetGLString(GL_EXTENSIONS); |
98 | gpu_info->gl_version_string = GetGLString(GL_VERSION); | ||||
99 | std::string glsl_version_string = GetGLString(GL_SHADING_LANGUAGE_VERSION); | ||||
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 100 | // TODO(kbr): remove once the destruction of a current context automatically |
101 | // clears the current context. | ||||
102 | context->ReleaseCurrent(surface.get()); | ||||
103 | |||||
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 104 | gpu_info->gl_version = GetVersionFromString(gpu_info->gl_version_string); |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 105 | std::string glsl_version = GetVersionFromString(glsl_version_string); |
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 106 | gpu_info->pixel_shader_version = glsl_version; |
107 | gpu_info->vertex_shader_version = glsl_version; | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 108 | |
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 109 | return CollectDriverInfoGL(gpu_info); |
110 | } | ||||
111 | |||||
112 | void MergeGPUInfoGL(content::GPUInfo* basic_gpu_info, | ||||
113 | const content::GPUInfo& context_gpu_info) { | ||||
114 | DCHECK(basic_gpu_info); | ||||
115 | basic_gpu_info->gl_renderer = context_gpu_info.gl_renderer; | ||||
116 | basic_gpu_info->gl_vendor = context_gpu_info.gl_vendor; | ||||
117 | basic_gpu_info->gl_version_string = context_gpu_info.gl_version_string; | ||||
118 | basic_gpu_info->gl_extensions = context_gpu_info.gl_extensions; | ||||
119 | basic_gpu_info->gl_version = context_gpu_info.gl_version; | ||||
120 | basic_gpu_info->pixel_shader_version = | ||||
121 | context_gpu_info.pixel_shader_version; | ||||
122 | basic_gpu_info->vertex_shader_version = | ||||
123 | context_gpu_info.vertex_shader_version; | ||||
124 | |||||
125 | if (!context_gpu_info.driver_vendor.empty()) | ||||
126 | basic_gpu_info->driver_vendor = context_gpu_info.driver_vendor; | ||||
127 | if (!context_gpu_info.driver_version.empty()) | ||||
128 | basic_gpu_info->driver_version = context_gpu_info.driver_version; | ||||
129 | |||||
130 | basic_gpu_info->can_lose_context = context_gpu_info.can_lose_context; | ||||
131 | basic_gpu_info->sandboxed = context_gpu_info.sandboxed; | ||||
[email protected] | 97e231f | 2013-02-27 19:45:21 | [diff] [blame] | 132 | basic_gpu_info->gpu_accessible = context_gpu_info.gpu_accessible; |
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 133 | basic_gpu_info->finalized = context_gpu_info.finalized; |
134 | basic_gpu_info->initialization_time = context_gpu_info.initialization_time; | ||||
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 135 | } |
136 | |||||
137 | } // namespace gpu_info_collector | ||||
138 |