blob: f66833c9cf12f199e89c4aa8929b31c69e9ad9a6 [file] [log] [blame]
[email protected]c9e2cbbb2012-05-12 21:17:271// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]7004d7eb2011-01-21 00:27:532// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]d7b5cc72013-05-23 20:05:005#include "gpu/config/gpu_info_collector.h"
[email protected]7004d7eb2011-01-21 00:27:536
7#include <string>
8#include <vector>
9
[email protected]aa328c72013-05-03 10:55:4310#include "base/debug/trace_event.h"
[email protected]7004d7eb2011-01-21 00:27:5311#include "base/logging.h"
[email protected]c9e2cbbb2012-05-12 21:17:2712#include "base/memory/scoped_ptr.h"
[email protected]f4390962013-06-11 07:29:2213#include "base/strings/string_number_conversions.h"
[email protected]b9e7c479f2013-04-12 04:33:2414#include "base/strings/string_piece.h"
[email protected]27c05732013-02-15 21:55:4915#include "base/strings/string_split.h"
[email protected]c9e2cbbb2012-05-12 21:17:2716#include "ui/gl/gl_bindings.h"
17#include "ui/gl/gl_context.h"
[email protected]45895032013-05-30 17:06:4318#include "ui/gl/gl_implementation.h"
[email protected]c9e2cbbb2012-05-12 21:17:2719#include "ui/gl/gl_surface.h"
[email protected]7004d7eb2011-01-21 00:27:5320
21namespace {
22
[email protected]fbe20372011-06-01 01:46:3823scoped_refptr<gfx::GLSurface> InitializeGLSurface() {
24 scoped_refptr<gfx::GLSurface> surface(
[email protected]f81f5952011-07-21 18:52:4725 gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1)));
[email protected]7cd76fd2013-06-02 21:11:1126 if (!surface.get()) {
[email protected]ffae4022011-05-12 22:54:2927 LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLSurface failed";
[email protected]7004d7eb2011-01-21 00:27:5328 return NULL;
29 }
[email protected]ffae4022011-05-12 22:54:2930
[email protected]fbe20372011-06-01 01:46:3831 return surface;
[email protected]f62a5ab2011-05-23 20:34:1532}
33
[email protected]fbe20372011-06-01 01:46:3834scoped_refptr<gfx::GLContext> InitializeGLContext(gfx::GLSurface* surface) {
[email protected]f62a5ab2011-05-23 20:34:1535
[email protected]7196e012011-06-16 20:54:5336 scoped_refptr<gfx::GLContext> context(
[email protected]276f89062011-10-13 22:55:5037 gfx::GLContext::CreateGLContext(NULL,
38 surface,
[email protected]56403c22012-10-04 18:27:0439 gfx::PreferIntegratedGpu));
[email protected]7cd76fd2013-06-02 21:11:1140 if (!context.get()) {
[email protected]ffae4022011-05-12 22:54:2941 LOG(ERROR) << "gfx::GLContext::CreateGLContext failed";
42 return NULL;
43 }
44
[email protected]f62a5ab2011-05-23 20:34:1545 if (!context->MakeCurrent(surface)) {
[email protected]7004d7eb2011-01-21 00:27:5346 LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed";
[email protected]7004d7eb2011-01-21 00:27:5347 return NULL;
48 }
[email protected]7004d7eb2011-01-21 00:27:5349
[email protected]fbe20372011-06-01 01:46:3850 return context;
[email protected]7004d7eb2011-01-21 00:27:5351}
52
53std::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]007b3f82013-04-09 08:46:4558 return std::string();
[email protected]7004d7eb2011-01-21 00:27:5359}
60
[email protected]6cc8ece62011-03-14 20:05:4761// Return a version string in the format of "major.minor".
62std::string GetVersionFromString(const std::string& version_string) {
[email protected]7004d7eb2011-01-21 00:27:5363 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]6cc8ece62011-03-14 20:05:4773 if (pieces.size() >= 2)
74 return pieces[0] + "." + pieces[1];
[email protected]7004d7eb2011-01-21 00:27:5375 }
[email protected]007b3f82013-04-09 08:46:4576 return std::string();
[email protected]7004d7eb2011-01-21 00:27:5377}
78
79} // namespace anonymous
80
[email protected]d7b5cc72013-05-23 20:05:0081namespace gpu {
[email protected]7004d7eb2011-01-21 00:27:5382
[email protected]d7b5cc72013-05-23 20:05:0083bool CollectGraphicsInfoGL(GPUInfo* gpu_info) {
[email protected]aa328c72013-05-03 10:55:4384 TRACE_EVENT0("startup", "gpu_info_collector::CollectGraphicsInfoGL");
[email protected]f62a5ab2011-05-23 20:34:1585 if (!gfx::GLSurface::InitializeOneOff()) {
[email protected]51dd1b482011-10-18 19:35:1986 LOG(ERROR) << "gfx::GLSurface::InitializeOneOff() failed";
[email protected]f62a5ab2011-05-23 20:34:1587 return false;
88 }
89
[email protected]fbe20372011-06-01 01:46:3890 scoped_refptr<gfx::GLSurface> surface(InitializeGLSurface());
[email protected]7cd76fd2013-06-02 21:11:1191 if (!surface.get())
[email protected]f62a5ab2011-05-23 20:34:1592 return false;
93
[email protected]fbe20372011-06-01 01:46:3894 scoped_refptr<gfx::GLContext> context(InitializeGLContext(surface.get()));
[email protected]7cd76fd2013-06-02 21:11:1195 if (!context.get())
[email protected]7004d7eb2011-01-21 00:27:5396 return false;
97
[email protected]a61508e52011-03-08 17:59:4298 gpu_info->gl_renderer = GetGLString(GL_RENDERER);
99 gpu_info->gl_vendor = GetGLString(GL_VENDOR);
[email protected]4df0884f2012-12-17 21:10:09100 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]45895032013-05-30 17:06:43103
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]2436a6b2012-04-13 21:08:51111 // TODO(kbr): remove once the destruction of a current context automatically
112 // clears the current context.
113 context->ReleaseCurrent(surface.get());
114
[email protected]4df0884f2012-12-17 21:10:09115 gpu_info->gl_version = GetVersionFromString(gpu_info->gl_version_string);
[email protected]6cc8ece62011-03-14 20:05:47116 std::string glsl_version = GetVersionFromString(glsl_version_string);
[email protected]a61508e52011-03-08 17:59:42117 gpu_info->pixel_shader_version = glsl_version;
118 gpu_info->vertex_shader_version = glsl_version;
[email protected]7004d7eb2011-01-21 00:27:53119
[email protected]4df0884f2012-12-17 21:10:09120 return CollectDriverInfoGL(gpu_info);
121}
122
[email protected]d7b5cc72013-05-23 20:05:00123void MergeGPUInfoGL(GPUInfo* basic_gpu_info,
124 const GPUInfo& context_gpu_info) {
[email protected]4df0884f2012-12-17 21:10:09125 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]45895032013-05-30 17:06:43135 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]4df0884f2012-12-17 21:10:09138
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]97e231f2013-02-27 19:45:21146 basic_gpu_info->gpu_accessible = context_gpu_info.gpu_accessible;
[email protected]4df0884f2012-12-17 21:10:09147 basic_gpu_info->finalized = context_gpu_info.finalized;
148 basic_gpu_info->initialization_time = context_gpu_info.initialization_time;
[email protected]7004d7eb2011-01-21 00:27:53149}
150
[email protected]d7b5cc72013-05-23 20:05:00151} // namespace gpu
[email protected]7004d7eb2011-01-21 00:27:53152