[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] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 5 | #include "gpu/config/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] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 10 | #include "base/debug/trace_event.h" |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | f439096 | 2013-06-11 07:29:22 | [diff] [blame^] | 13 | #include "base/strings/string_number_conversions.h" |
[email protected] | b9e7c479f | 2013-04-12 04:33:24 | [diff] [blame] | 14 | #include "base/strings/string_piece.h" |
[email protected] | 27c0573 | 2013-02-15 21:55:49 | [diff] [blame] | 15 | #include "base/strings/string_split.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 16 | #include "ui/gl/gl_bindings.h" |
| 17 | #include "ui/gl/gl_context.h" |
[email protected] | 4589503 | 2013-05-30 17:06:43 | [diff] [blame] | 18 | #include "ui/gl/gl_implementation.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 19 | #include "ui/gl/gl_surface.h" |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 23 | scoped_refptr<gfx::GLSurface> InitializeGLSurface() { |
| 24 | scoped_refptr<gfx::GLSurface> surface( |
[email protected] | f81f595 | 2011-07-21 18:52:47 | [diff] [blame] | 25 | gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1))); |
[email protected] | 7cd76fd | 2013-06-02 21:11:11 | [diff] [blame] | 26 | if (!surface.get()) { |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 27 | LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLSurface failed"; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 28 | return NULL; |
| 29 | } |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 30 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 31 | return surface; |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 34 | scoped_refptr<gfx::GLContext> InitializeGLContext(gfx::GLSurface* surface) { |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 35 | |
[email protected] | 7196e01 | 2011-06-16 20:54:53 | [diff] [blame] | 36 | scoped_refptr<gfx::GLContext> context( |
[email protected] | 276f8906 | 2011-10-13 22:55:50 | [diff] [blame] | 37 | gfx::GLContext::CreateGLContext(NULL, |
| 38 | surface, |
[email protected] | 56403c2 | 2012-10-04 18:27:04 | [diff] [blame] | 39 | gfx::PreferIntegratedGpu)); |
[email protected] | 7cd76fd | 2013-06-02 21:11:11 | [diff] [blame] | 40 | if (!context.get()) { |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 41 | LOG(ERROR) << "gfx::GLContext::CreateGLContext failed"; |
| 42 | return NULL; |
| 43 | } |
| 44 | |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 45 | if (!context->MakeCurrent(surface)) { |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 46 | LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed"; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 47 | return NULL; |
| 48 | } |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 49 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 50 | return context; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | std::string GetGLString(unsigned int pname) { |
| 54 | const char* gl_string = |
| 55 | reinterpret_cast<const char*>(glGetString(pname)); |
| 56 | if (gl_string) |
| 57 | return std::string(gl_string); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 58 | return std::string(); |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 61 | // Return a version string in the format of "major.minor". |
| 62 | std::string GetVersionFromString(const std::string& version_string) { |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 63 | size_t begin = version_string.find_first_of("0123456789"); |
| 64 | if (begin != std::string::npos) { |
| 65 | size_t end = version_string.find_first_not_of("01234567890.", begin); |
| 66 | std::string sub_string; |
| 67 | if (end != std::string::npos) |
| 68 | sub_string = version_string.substr(begin, end - begin); |
| 69 | else |
| 70 | sub_string = version_string.substr(begin); |
| 71 | std::vector<std::string> pieces; |
| 72 | base::SplitString(sub_string, '.', &pieces); |
[email protected] | 6cc8ece6 | 2011-03-14 20:05:47 | [diff] [blame] | 73 | if (pieces.size() >= 2) |
| 74 | return pieces[0] + "." + pieces[1]; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 75 | } |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 76 | return std::string(); |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } // namespace anonymous |
| 80 | |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 81 | namespace gpu { |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 82 | |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 83 | bool CollectGraphicsInfoGL(GPUInfo* gpu_info) { |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 84 | TRACE_EVENT0("startup", "gpu_info_collector::CollectGraphicsInfoGL"); |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 85 | if (!gfx::GLSurface::InitializeOneOff()) { |
[email protected] | 51dd1b48 | 2011-10-18 19:35:19 | [diff] [blame] | 86 | LOG(ERROR) << "gfx::GLSurface::InitializeOneOff() failed"; |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 90 | scoped_refptr<gfx::GLSurface> surface(InitializeGLSurface()); |
[email protected] | 7cd76fd | 2013-06-02 21:11:11 | [diff] [blame] | 91 | if (!surface.get()) |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 92 | return false; |
| 93 | |
[email protected] | fbe2037 | 2011-06-01 01:46:38 | [diff] [blame] | 94 | scoped_refptr<gfx::GLContext> context(InitializeGLContext(surface.get())); |
[email protected] | 7cd76fd | 2013-06-02 21:11:11 | [diff] [blame] | 95 | if (!context.get()) |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 96 | return false; |
| 97 | |
[email protected] | a61508e5 | 2011-03-08 17:59:42 | [diff] [blame] | 98 | gpu_info->gl_renderer = GetGLString(GL_RENDERER); |
| 99 | gpu_info->gl_vendor = GetGLString(GL_VENDOR); |
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 100 | gpu_info->gl_extensions = GetGLString(GL_EXTENSIONS); |
| 101 | gpu_info->gl_version_string = GetGLString(GL_VERSION); |
| 102 | std::string glsl_version_string = GetGLString(GL_SHADING_LANGUAGE_VERSION); |
[email protected] | 4589503 | 2013-05-30 17:06:43 | [diff] [blame] | 103 | |
| 104 | gfx::GLWindowSystemBindingInfo window_system_binding_info; |
| 105 | if (GetGLWindowSystemBindingInfo(&window_system_binding_info)) { |
| 106 | gpu_info->gl_ws_vendor = window_system_binding_info.vendor; |
| 107 | gpu_info->gl_ws_version = window_system_binding_info.version; |
| 108 | gpu_info->gl_ws_extensions = window_system_binding_info.extensions; |
| 109 | } |
| 110 | |
[email protected] | 2436a6b | 2012-04-13 21:08:51 | [diff] [blame] | 111 | // TODO(kbr): remove once the destruction of a current context automatically |
| 112 | // clears the current context. |
| 113 | context->ReleaseCurrent(surface.get()); |
| 114 | |
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 115 | gpu_info->gl_version = GetVersionFromString(gpu_info->gl_version_string); |
[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 | |
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 120 | return CollectDriverInfoGL(gpu_info); |
| 121 | } |
| 122 | |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 123 | void MergeGPUInfoGL(GPUInfo* basic_gpu_info, |
| 124 | const GPUInfo& context_gpu_info) { |
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 125 | DCHECK(basic_gpu_info); |
| 126 | basic_gpu_info->gl_renderer = context_gpu_info.gl_renderer; |
| 127 | basic_gpu_info->gl_vendor = context_gpu_info.gl_vendor; |
| 128 | basic_gpu_info->gl_version_string = context_gpu_info.gl_version_string; |
| 129 | basic_gpu_info->gl_extensions = context_gpu_info.gl_extensions; |
| 130 | basic_gpu_info->gl_version = context_gpu_info.gl_version; |
| 131 | basic_gpu_info->pixel_shader_version = |
| 132 | context_gpu_info.pixel_shader_version; |
| 133 | basic_gpu_info->vertex_shader_version = |
| 134 | context_gpu_info.vertex_shader_version; |
[email protected] | 4589503 | 2013-05-30 17:06:43 | [diff] [blame] | 135 | basic_gpu_info->gl_ws_vendor = context_gpu_info.gl_ws_vendor; |
| 136 | basic_gpu_info->gl_ws_version = context_gpu_info.gl_ws_version; |
| 137 | basic_gpu_info->gl_ws_extensions = context_gpu_info.gl_ws_extensions; |
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 138 | |
| 139 | if (!context_gpu_info.driver_vendor.empty()) |
| 140 | basic_gpu_info->driver_vendor = context_gpu_info.driver_vendor; |
| 141 | if (!context_gpu_info.driver_version.empty()) |
| 142 | basic_gpu_info->driver_version = context_gpu_info.driver_version; |
| 143 | |
| 144 | basic_gpu_info->can_lose_context = context_gpu_info.can_lose_context; |
| 145 | basic_gpu_info->sandboxed = context_gpu_info.sandboxed; |
[email protected] | 97e231f | 2013-02-27 19:45:21 | [diff] [blame] | 146 | basic_gpu_info->gpu_accessible = context_gpu_info.gpu_accessible; |
[email protected] | 4df0884f | 2012-12-17 21:10:09 | [diff] [blame] | 147 | basic_gpu_info->finalized = context_gpu_info.finalized; |
| 148 | basic_gpu_info->initialization_time = context_gpu_info.initialization_time; |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 149 | } |
| 150 | |
[email protected] | d7b5cc7 | 2013-05-23 20:05:00 | [diff] [blame] | 151 | } // namespace gpu |
[email protected] | 7004d7eb | 2011-01-21 00:27:53 | [diff] [blame] | 152 | |