blob: cd0918cc9a815784508f064bca02e38b68f3e96c [file] [log] [blame]
[email protected]623c0bd2011-03-12 01:00:411// Copyright (c) 2011 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]623c0bd2011-03-12 01:00:415#include "content/gpu/gpu_info_collector.h"
[email protected]7004d7eb2011-01-21 00:27:536
7#include <string>
8#include <vector>
9
[email protected]ffae4022011-05-12 22:54:2910#include "base/memory/scoped_ptr.h"
[email protected]7004d7eb2011-01-21 00:27:5311#include "base/logging.h"
12#include "base/string_number_conversions.h"
13#include "base/string_piece.h"
14#include "base/string_split.h"
[email protected]5ae0b282011-03-28 19:24:4915#include "ui/gfx/gl/gl_bindings.h"
16#include "ui/gfx/gl/gl_context.h"
[email protected]ffae4022011-05-12 22:54:2917#include "ui/gfx/gl/gl_surface.h"
[email protected]7004d7eb2011-01-21 00:27:5318
19namespace {
20
[email protected]fbe20372011-06-01 01:46:3821scoped_refptr<gfx::GLSurface> InitializeGLSurface() {
22 scoped_refptr<gfx::GLSurface> surface(
[email protected]f81f5952011-07-21 18:52:4723 gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1)));
[email protected]ffae4022011-05-12 22:54:2924 if (!surface.get()) {
25 LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLSurface failed";
[email protected]7004d7eb2011-01-21 00:27:5326 return NULL;
27 }
[email protected]ffae4022011-05-12 22:54:2928
[email protected]fbe20372011-06-01 01:46:3829 return surface;
[email protected]f62a5ab2011-05-23 20:34:1530}
31
[email protected]fbe20372011-06-01 01:46:3832scoped_refptr<gfx::GLContext> InitializeGLContext(gfx::GLSurface* surface) {
[email protected]f62a5ab2011-05-23 20:34:1533
[email protected]7196e012011-06-16 20:54:5334 scoped_refptr<gfx::GLContext> context(
35 gfx::GLContext::CreateGLContext(NULL, surface));
[email protected]ffae4022011-05-12 22:54:2936 if (!context.get()) {
37 LOG(ERROR) << "gfx::GLContext::CreateGLContext failed";
38 return NULL;
39 }
40
[email protected]f62a5ab2011-05-23 20:34:1541 if (!context->MakeCurrent(surface)) {
[email protected]7004d7eb2011-01-21 00:27:5342 LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed";
[email protected]7004d7eb2011-01-21 00:27:5343 return NULL;
44 }
[email protected]7004d7eb2011-01-21 00:27:5345
[email protected]fbe20372011-06-01 01:46:3846 return context;
[email protected]7004d7eb2011-01-21 00:27:5347}
48
49std::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
[email protected]6cc8ece62011-03-14 20:05:4757// Return a version string in the format of "major.minor".
58std::string GetVersionFromString(const std::string& version_string) {
[email protected]7004d7eb2011-01-21 00:27:5359 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);
[email protected]6cc8ece62011-03-14 20:05:4769 if (pieces.size() >= 2)
70 return pieces[0] + "." + pieces[1];
[email protected]7004d7eb2011-01-21 00:27:5371 }
[email protected]6cc8ece62011-03-14 20:05:4772 return "";
[email protected]7004d7eb2011-01-21 00:27:5373}
74
75} // namespace anonymous
76
77namespace gpu_info_collector {
78
79bool CollectGraphicsInfoGL(GPUInfo* gpu_info) {
80 DCHECK(gpu_info);
81
[email protected]f62a5ab2011-05-23 20:34:1582 if (!gfx::GLSurface::InitializeOneOff()) {
83 LOG(ERROR) << "gfx::GLContext::InitializeOneOff() failed";
84 return false;
85 }
86
[email protected]fbe20372011-06-01 01:46:3887 scoped_refptr<gfx::GLSurface> surface(InitializeGLSurface());
[email protected]f62a5ab2011-05-23 20:34:1588 if (!surface.get())
89 return false;
90
[email protected]fbe20372011-06-01 01:46:3891 scoped_refptr<gfx::GLContext> context(InitializeGLContext(surface.get()));
[email protected]ffae4022011-05-12 22:54:2992 if (!context.get())
[email protected]7004d7eb2011-01-21 00:27:5393 return false;
94
[email protected]a61508e52011-03-08 17:59:4295 gpu_info->gl_renderer = GetGLString(GL_RENDERER);
96 gpu_info->gl_vendor = GetGLString(GL_VENDOR);
97 gpu_info->gl_version_string =GetGLString(GL_VERSION);
98 gpu_info->gl_extensions =GetGLString(GL_EXTENSIONS);
[email protected]7004d7eb2011-01-21 00:27:5399
100 bool validGLVersionInfo = CollectGLVersionInfo(gpu_info);
101 bool validVideoCardInfo = CollectVideoCardInfo(gpu_info);
[email protected]766653092011-03-01 16:45:23102 bool validDriverInfo = CollectDriverInfoGL(gpu_info);
[email protected]7004d7eb2011-01-21 00:27:53103
[email protected]7004d7eb2011-01-21 00:27:53104 return (validGLVersionInfo && validVideoCardInfo && validDriverInfo);
105}
106
107bool CollectGLVersionInfo(GPUInfo* gpu_info) {
108 DCHECK(gpu_info);
109
[email protected]a61508e52011-03-08 17:59:42110 std::string gl_version_string = gpu_info->gl_version_string;
[email protected]7004d7eb2011-01-21 00:27:53111 std::string glsl_version_string =
112 GetGLString(GL_SHADING_LANGUAGE_VERSION);
113
[email protected]6cc8ece62011-03-14 20:05:47114 gpu_info->gl_version = GetVersionFromString(gl_version_string);
[email protected]7004d7eb2011-01-21 00:27:53115
[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
120 return true;
121}
122
123} // namespace gpu_info_collector
124