Roll src/third_party/skia 7ef63c8:164d5b0
[chromium-blink-merge.git] / gpu / config / gpu_info_collector.cc
blob2403340fb2c602b03cde0de619b178060cfa3e90
1 // Copyright (c) 2012 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 "gpu/config/gpu_info_collector.h"
7 #include <string>
8 #include <vector>
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/trace_event/trace_event.h"
17 #include "ui/gl/gl_bindings.h"
18 #include "ui/gl/gl_context.h"
19 #include "ui/gl/gl_implementation.h"
20 #include "ui/gl/gl_surface.h"
22 namespace {
24 scoped_refptr<gfx::GLSurface> InitializeGLSurface() {
25 scoped_refptr<gfx::GLSurface> surface(
26 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
27 if (!surface.get()) {
28 LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLSurface failed";
29 return NULL;
32 return surface;
35 scoped_refptr<gfx::GLContext> InitializeGLContext(gfx::GLSurface* surface) {
37 scoped_refptr<gfx::GLContext> context(
38 gfx::GLContext::CreateGLContext(NULL,
39 surface,
40 gfx::PreferIntegratedGpu));
41 if (!context.get()) {
42 LOG(ERROR) << "gfx::GLContext::CreateGLContext failed";
43 return NULL;
46 if (!context->MakeCurrent(surface)) {
47 LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed";
48 return NULL;
51 return context;
54 std::string GetGLString(unsigned int pname) {
55 const char* gl_string =
56 reinterpret_cast<const char*>(glGetString(pname));
57 if (gl_string)
58 return std::string(gl_string);
59 return std::string();
62 // Return a version string in the format of "major.minor".
63 std::string GetVersionFromString(const std::string& version_string) {
64 size_t begin = version_string.find_first_of("0123456789");
65 if (begin != std::string::npos) {
66 size_t end = version_string.find_first_not_of("01234567890.", begin);
67 std::string sub_string;
68 if (end != std::string::npos)
69 sub_string = version_string.substr(begin, end - begin);
70 else
71 sub_string = version_string.substr(begin);
72 std::vector<std::string> pieces;
73 base::SplitString(sub_string, '.', &pieces);
74 if (pieces.size() >= 2)
75 return pieces[0] + "." + pieces[1];
77 return std::string();
80 } // namespace anonymous
82 namespace gpu {
84 CollectInfoResult CollectGraphicsInfoGL(GPUInfo* gpu_info) {
85 TRACE_EVENT0("startup", "gpu_info_collector::CollectGraphicsInfoGL");
86 DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone);
88 scoped_refptr<gfx::GLSurface> surface(InitializeGLSurface());
89 if (!surface.get()) {
90 LOG(ERROR) << "Could not create surface for info collection.";
91 return kCollectInfoFatalFailure;
94 scoped_refptr<gfx::GLContext> context(InitializeGLContext(surface.get()));
95 if (!context.get()) {
96 LOG(ERROR) << "Could not create context for info collection.";
97 return kCollectInfoFatalFailure;
100 gpu_info->gl_renderer = GetGLString(GL_RENDERER);
101 gpu_info->gl_vendor = GetGLString(GL_VENDOR);
102 gpu_info->gl_extensions = GetGLString(GL_EXTENSIONS);
103 gpu_info->gl_version = GetGLString(GL_VERSION);
104 std::string glsl_version_string = GetGLString(GL_SHADING_LANGUAGE_VERSION);
105 GLint max_samples = 0;
106 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
107 gpu_info->max_msaa_samples = base::StringPrintf("%d", max_samples);
109 gfx::GLWindowSystemBindingInfo window_system_binding_info;
110 if (GetGLWindowSystemBindingInfo(&window_system_binding_info)) {
111 gpu_info->gl_ws_vendor = window_system_binding_info.vendor;
112 gpu_info->gl_ws_version = window_system_binding_info.version;
113 gpu_info->gl_ws_extensions = window_system_binding_info.extensions;
114 gpu_info->direct_rendering = window_system_binding_info.direct_rendering;
117 bool supports_robustness =
118 gpu_info->gl_extensions.find("GL_EXT_robustness") != std::string::npos ||
119 gpu_info->gl_extensions.find("GL_KHR_robustness") != std::string::npos ||
120 gpu_info->gl_extensions.find("GL_ARB_robustness") != std::string::npos;
121 if (supports_robustness) {
122 glGetIntegerv(GL_RESET_NOTIFICATION_STRATEGY_ARB,
123 reinterpret_cast<GLint*>(&gpu_info->gl_reset_notification_strategy));
126 // TODO(kbr): remove once the destruction of a current context automatically
127 // clears the current context.
128 context->ReleaseCurrent(surface.get());
130 std::string glsl_version = GetVersionFromString(glsl_version_string);
131 gpu_info->pixel_shader_version = glsl_version;
132 gpu_info->vertex_shader_version = glsl_version;
134 return CollectDriverInfoGL(gpu_info);
137 void MergeGPUInfoGL(GPUInfo* basic_gpu_info,
138 const GPUInfo& context_gpu_info) {
139 DCHECK(basic_gpu_info);
140 basic_gpu_info->gl_renderer = context_gpu_info.gl_renderer;
141 basic_gpu_info->gl_vendor = context_gpu_info.gl_vendor;
142 basic_gpu_info->gl_version = context_gpu_info.gl_version;
143 basic_gpu_info->gl_extensions = context_gpu_info.gl_extensions;
144 basic_gpu_info->pixel_shader_version =
145 context_gpu_info.pixel_shader_version;
146 basic_gpu_info->vertex_shader_version =
147 context_gpu_info.vertex_shader_version;
148 basic_gpu_info->max_msaa_samples =
149 context_gpu_info.max_msaa_samples;
150 basic_gpu_info->gl_ws_vendor = context_gpu_info.gl_ws_vendor;
151 basic_gpu_info->gl_ws_version = context_gpu_info.gl_ws_version;
152 basic_gpu_info->gl_ws_extensions = context_gpu_info.gl_ws_extensions;
153 basic_gpu_info->gl_reset_notification_strategy =
154 context_gpu_info.gl_reset_notification_strategy;
156 if (!context_gpu_info.driver_vendor.empty())
157 basic_gpu_info->driver_vendor = context_gpu_info.driver_vendor;
158 if (!context_gpu_info.driver_version.empty())
159 basic_gpu_info->driver_version = context_gpu_info.driver_version;
161 basic_gpu_info->can_lose_context = context_gpu_info.can_lose_context;
162 basic_gpu_info->sandboxed = context_gpu_info.sandboxed;
163 basic_gpu_info->direct_rendering = context_gpu_info.direct_rendering;
164 basic_gpu_info->context_info_state = context_gpu_info.context_info_state;
165 basic_gpu_info->initialization_time = context_gpu_info.initialization_time;
166 basic_gpu_info->video_decode_accelerator_supported_profiles =
167 context_gpu_info.video_decode_accelerator_supported_profiles;
168 basic_gpu_info->video_encode_accelerator_supported_profiles =
169 context_gpu_info.video_encode_accelerator_supported_profiles;
172 } // namespace gpu