Bump Syzygy deps to r543 to pick up latest release binaries.
[chromium-blink-merge.git] / content / gpu / gpu_info_collector.cc
blob78e7888f1807f761d7691d6109d30d46a312591f
1 // Copyright (c) 2011 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.
5 #include "content/gpu/gpu_info_collector.h"
7 #include <string>
8 #include <vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/logging.h"
12 #include "base/string_number_conversions.h"
13 #include "base/string_piece.h"
14 #include "base/string_split.h"
15 #include "ui/gfx/gl/gl_bindings.h"
16 #include "ui/gfx/gl/gl_context.h"
17 #include "ui/gfx/gl/gl_surface.h"
19 namespace {
21 scoped_refptr<gfx::GLSurface> InitializeGLSurface() {
22 scoped_refptr<gfx::GLSurface> surface(
23 gfx::GLSurface::CreateOffscreenGLSurface(false, gfx::Size(1, 1)));
24 if (!surface.get()) {
25 LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLSurface failed";
26 return NULL;
29 return surface;
32 scoped_refptr<gfx::GLContext> InitializeGLContext(gfx::GLSurface* surface) {
34 scoped_refptr<gfx::GLContext> context(
35 gfx::GLContext::CreateGLContext(NULL,
36 surface,
37 gfx::PreferDiscreteGpu));
38 if (!context.get()) {
39 LOG(ERROR) << "gfx::GLContext::CreateGLContext failed";
40 return NULL;
43 if (!context->MakeCurrent(surface)) {
44 LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed";
45 return NULL;
48 return context;
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);
56 return "";
59 // Return a version string in the format of "major.minor".
60 std::string GetVersionFromString(const std::string& version_string) {
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);
71 if (pieces.size() >= 2)
72 return pieces[0] + "." + pieces[1];
74 return "";
77 } // namespace anonymous
79 namespace gpu_info_collector {
81 bool CollectGraphicsInfoGL(content::GPUInfo* gpu_info) {
82 if (!gfx::GLSurface::InitializeOneOff()) {
83 LOG(ERROR) << "gfx::GLSurface::InitializeOneOff() failed";
84 return false;
87 scoped_refptr<gfx::GLSurface> surface(InitializeGLSurface());
88 if (!surface.get())
89 return false;
91 scoped_refptr<gfx::GLContext> context(InitializeGLContext(surface.get()));
92 if (!context.get())
93 return false;
95 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);
100 bool validGLVersionInfo = CollectGLVersionInfo(gpu_info);
101 bool validVideoCardInfo = CollectVideoCardInfo(gpu_info);
102 bool validDriverInfo = CollectDriverInfoGL(gpu_info);
104 return (validGLVersionInfo && validVideoCardInfo && validDriverInfo);
107 bool CollectGLVersionInfo(content::GPUInfo* gpu_info) {
108 std::string gl_version_string = gpu_info->gl_version_string;
109 std::string glsl_version_string =
110 GetGLString(GL_SHADING_LANGUAGE_VERSION);
112 gpu_info->gl_version = GetVersionFromString(gl_version_string);
114 std::string glsl_version = GetVersionFromString(glsl_version_string);
115 gpu_info->pixel_shader_version = glsl_version;
116 gpu_info->vertex_shader_version = glsl_version;
118 return true;
121 } // namespace gpu_info_collector