blob: 86fb0c57c08c36172c43f2eac428f3afb46e6224 [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]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"
[email protected]c9e2cbbb2012-05-12 21:17:2711#include "base/memory/scoped_ptr.h"
[email protected]7004d7eb2011-01-21 00:27:5312#include "base/string_number_conversions.h"
13#include "base/string_piece.h"
[email protected]27c05732013-02-15 21:55:4914#include "base/strings/string_split.h"
[email protected]c9e2cbbb2012-05-12 21:17:2715#include "ui/gl/gl_bindings.h"
16#include "ui/gl/gl_context.h"
17#include "ui/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(
[email protected]276f89062011-10-13 22:55:5035 gfx::GLContext::CreateGLContext(NULL,
36 surface,
[email protected]56403c22012-10-04 18:27:0437 gfx::PreferIntegratedGpu));
[email protected]ffae4022011-05-12 22:54:2938 if (!context.get()) {
39 LOG(ERROR) << "gfx::GLContext::CreateGLContext failed";
40 return NULL;
41 }
42
[email protected]f62a5ab2011-05-23 20:34:1543 if (!context->MakeCurrent(surface)) {
[email protected]7004d7eb2011-01-21 00:27:5344 LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed";
[email protected]7004d7eb2011-01-21 00:27:5345 return NULL;
46 }
[email protected]7004d7eb2011-01-21 00:27:5347
[email protected]fbe20372011-06-01 01:46:3848 return context;
[email protected]7004d7eb2011-01-21 00:27:5349}
50
51std::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 "";
57}
58
[email protected]6cc8ece62011-03-14 20:05:4759// Return a version string in the format of "major.minor".
60std::string GetVersionFromString(const std::string& version_string) {
[email protected]7004d7eb2011-01-21 00:27:5361 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);
[email protected]6cc8ece62011-03-14 20:05:4771 if (pieces.size() >= 2)
72 return pieces[0] + "." + pieces[1];
[email protected]7004d7eb2011-01-21 00:27:5373 }
[email protected]6cc8ece62011-03-14 20:05:4774 return "";
[email protected]7004d7eb2011-01-21 00:27:5375}
76
77} // namespace anonymous
78
79namespace gpu_info_collector {
80
[email protected]a80f5ece2011-10-20 23:56:5581bool CollectGraphicsInfoGL(content::GPUInfo* gpu_info) {
[email protected]f62a5ab2011-05-23 20:34:1582 if (!gfx::GLSurface::InitializeOneOff()) {
[email protected]51dd1b482011-10-18 19:35:1983 LOG(ERROR) << "gfx::GLSurface::InitializeOneOff() failed";
[email protected]f62a5ab2011-05-23 20:34:1584 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);
[email protected]4df0884f2012-12-17 21:10:0997 gpu_info->gl_extensions = GetGLString(GL_EXTENSIONS);
98 gpu_info->gl_version_string = GetGLString(GL_VERSION);
99 std::string glsl_version_string = GetGLString(GL_SHADING_LANGUAGE_VERSION);
[email protected]2436a6b2012-04-13 21:08:51100 // TODO(kbr): remove once the destruction of a current context automatically
101 // clears the current context.
102 context->ReleaseCurrent(surface.get());
103
[email protected]4df0884f2012-12-17 21:10:09104 gpu_info->gl_version = GetVersionFromString(gpu_info->gl_version_string);
[email protected]6cc8ece62011-03-14 20:05:47105 std::string glsl_version = GetVersionFromString(glsl_version_string);
[email protected]a61508e52011-03-08 17:59:42106 gpu_info->pixel_shader_version = glsl_version;
107 gpu_info->vertex_shader_version = glsl_version;
[email protected]7004d7eb2011-01-21 00:27:53108
[email protected]4df0884f2012-12-17 21:10:09109 return CollectDriverInfoGL(gpu_info);
110}
111
112void MergeGPUInfoGL(content::GPUInfo* basic_gpu_info,
113 const content::GPUInfo& context_gpu_info) {
114 DCHECK(basic_gpu_info);
115 basic_gpu_info->gl_renderer = context_gpu_info.gl_renderer;
116 basic_gpu_info->gl_vendor = context_gpu_info.gl_vendor;
117 basic_gpu_info->gl_version_string = context_gpu_info.gl_version_string;
118 basic_gpu_info->gl_extensions = context_gpu_info.gl_extensions;
119 basic_gpu_info->gl_version = context_gpu_info.gl_version;
120 basic_gpu_info->pixel_shader_version =
121 context_gpu_info.pixel_shader_version;
122 basic_gpu_info->vertex_shader_version =
123 context_gpu_info.vertex_shader_version;
124
125 if (!context_gpu_info.driver_vendor.empty())
126 basic_gpu_info->driver_vendor = context_gpu_info.driver_vendor;
127 if (!context_gpu_info.driver_version.empty())
128 basic_gpu_info->driver_version = context_gpu_info.driver_version;
129
130 basic_gpu_info->can_lose_context = context_gpu_info.can_lose_context;
131 basic_gpu_info->sandboxed = context_gpu_info.sandboxed;
[email protected]97e231f2013-02-27 19:45:21132 basic_gpu_info->gpu_accessible = context_gpu_info.gpu_accessible;
[email protected]4df0884f2012-12-17 21:10:09133 basic_gpu_info->finalized = context_gpu_info.finalized;
134 basic_gpu_info->initialization_time = context_gpu_info.initialization_time;
[email protected]7004d7eb2011-01-21 00:27:53135}
136
137} // namespace gpu_info_collector
138