blob: 6b37c27da8302f00d47bad53c455356db2f04b51 [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]7004d7eb2011-01-21 00:27:5310#include "base/logging.h"
11#include "base/string_number_conversions.h"
12#include "base/string_piece.h"
13#include "base/string_split.h"
[email protected]5ae0b282011-03-28 19:24:4914#include "ui/gfx/gl/gl_bindings.h"
15#include "ui/gfx/gl/gl_context.h"
[email protected]7004d7eb2011-01-21 00:27:5316
17namespace {
18
19// This creates an offscreen GL context for gl queries. Returned GLContext
20// should be deleted in FinalizeGLContext.
21gfx::GLContext* InitializeGLContext() {
22 if (!gfx::GLContext::InitializeOneOff()) {
23 LOG(ERROR) << "gfx::GLContext::InitializeOneOff() failed";
24 return NULL;
25 }
26 gfx::GLContext* context = gfx::GLContext::CreateOffscreenGLContext(NULL);
27 if (context == NULL) {
28 LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLContext(NULL) failed";
29 return NULL;
30 }
31 if (!context->MakeCurrent()) {
32 LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed";
33 context->Destroy();
34 delete context;
35 return NULL;
36 }
37 return context;
38}
39
40// This destroy and delete the GL context.
41void FinalizeGLContext(gfx::GLContext** context) {
42 DCHECK(context);
43 if (*context) {
44 (*context)->Destroy();
45 delete *context;
46 *context = NULL;
47 }
48}
49
50std::string GetGLString(unsigned int pname) {
51 const char* gl_string =
52 reinterpret_cast<const char*>(glGetString(pname));
53 if (gl_string)
54 return std::string(gl_string);
55 return "";
56}
57
[email protected]6cc8ece62011-03-14 20:05:4758// Return a version string in the format of "major.minor".
59std::string GetVersionFromString(const std::string& version_string) {
[email protected]7004d7eb2011-01-21 00:27:5360 size_t begin = version_string.find_first_of("0123456789");
61 if (begin != std::string::npos) {
62 size_t end = version_string.find_first_not_of("01234567890.", begin);
63 std::string sub_string;
64 if (end != std::string::npos)
65 sub_string = version_string.substr(begin, end - begin);
66 else
67 sub_string = version_string.substr(begin);
68 std::vector<std::string> pieces;
69 base::SplitString(sub_string, '.', &pieces);
[email protected]6cc8ece62011-03-14 20:05:4770 if (pieces.size() >= 2)
71 return pieces[0] + "." + pieces[1];
[email protected]7004d7eb2011-01-21 00:27:5372 }
[email protected]6cc8ece62011-03-14 20:05:4773 return "";
[email protected]7004d7eb2011-01-21 00:27:5374}
75
76} // namespace anonymous
77
78namespace gpu_info_collector {
79
80bool CollectGraphicsInfoGL(GPUInfo* gpu_info) {
81 DCHECK(gpu_info);
82
83 gfx::GLContext* context = InitializeGLContext();
84 if (context == NULL)
85 return false;
86
[email protected]a61508e52011-03-08 17:59:4287 gpu_info->gl_renderer = GetGLString(GL_RENDERER);
88 gpu_info->gl_vendor = GetGLString(GL_VENDOR);
89 gpu_info->gl_version_string =GetGLString(GL_VERSION);
90 gpu_info->gl_extensions =GetGLString(GL_EXTENSIONS);
[email protected]7004d7eb2011-01-21 00:27:5391
92 bool validGLVersionInfo = CollectGLVersionInfo(gpu_info);
93 bool validVideoCardInfo = CollectVideoCardInfo(gpu_info);
[email protected]766653092011-03-01 16:45:2394 bool validDriverInfo = CollectDriverInfoGL(gpu_info);
[email protected]7004d7eb2011-01-21 00:27:5395
96 FinalizeGLContext(&context);
97
98 return (validGLVersionInfo && validVideoCardInfo && validDriverInfo);
99}
100
101bool CollectGLVersionInfo(GPUInfo* gpu_info) {
102 DCHECK(gpu_info);
103
[email protected]a61508e52011-03-08 17:59:42104 std::string gl_version_string = gpu_info->gl_version_string;
[email protected]7004d7eb2011-01-21 00:27:53105 std::string glsl_version_string =
106 GetGLString(GL_SHADING_LANGUAGE_VERSION);
107
[email protected]6cc8ece62011-03-14 20:05:47108 gpu_info->gl_version = GetVersionFromString(gl_version_string);
[email protected]7004d7eb2011-01-21 00:27:53109
[email protected]6cc8ece62011-03-14 20:05:47110 std::string glsl_version = GetVersionFromString(glsl_version_string);
[email protected]a61508e52011-03-08 17:59:42111 gpu_info->pixel_shader_version = glsl_version;
112 gpu_info->vertex_shader_version = glsl_version;
[email protected]7004d7eb2011-01-21 00:27:53113
114 return true;
115}
116
117} // namespace gpu_info_collector
118