Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / config / gpu_info_collector.cc
blob55a294731e4d8d9c49cede0586ea714b64b9eeac
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/trace_event/trace_event.h"
16 #include "ui/gl/gl_bindings.h"
17 #include "ui/gl/gl_context.h"
18 #include "ui/gl/gl_implementation.h"
19 #include "ui/gl/gl_surface.h"
21 namespace {
23 scoped_refptr<gfx::GLSurface> InitializeGLSurface() {
24 scoped_refptr<gfx::GLSurface> surface(
25 gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size()));
26 if (!surface.get()) {
27 LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLSurface failed";
28 return NULL;
31 return surface;
34 scoped_refptr<gfx::GLContext> InitializeGLContext(gfx::GLSurface* surface) {
36 scoped_refptr<gfx::GLContext> context(
37 gfx::GLContext::CreateGLContext(NULL,
38 surface,
39 gfx::PreferIntegratedGpu));
40 if (!context.get()) {
41 LOG(ERROR) << "gfx::GLContext::CreateGLContext failed";
42 return NULL;
45 if (!context->MakeCurrent(surface)) {
46 LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed";
47 return NULL;
50 return context;
53 std::string GetGLString(unsigned int pname) {
54 const char* gl_string =
55 reinterpret_cast<const char*>(glGetString(pname));
56 if (gl_string)
57 return std::string(gl_string);
58 return std::string();
61 // Return a version string in the format of "major.minor".
62 std::string GetVersionFromString(const std::string& version_string) {
63 size_t begin = version_string.find_first_of("0123456789");
64 if (begin != std::string::npos) {
65 size_t end = version_string.find_first_not_of("01234567890.", begin);
66 std::string sub_string;
67 if (end != std::string::npos)
68 sub_string = version_string.substr(begin, end - begin);
69 else
70 sub_string = version_string.substr(begin);
71 std::vector<std::string> pieces;
72 base::SplitString(sub_string, '.', &pieces);
73 if (pieces.size() >= 2)
74 return pieces[0] + "." + pieces[1];
76 return std::string();
79 } // namespace anonymous
81 namespace gpu {
83 CollectInfoResult CollectGraphicsInfoGL(GPUInfo* gpu_info) {
84 TRACE_EVENT0("startup", "gpu_info_collector::CollectGraphicsInfoGL");
85 DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone);
87 scoped_refptr<gfx::GLSurface> surface(InitializeGLSurface());
88 if (!surface.get()) {
89 LOG(ERROR) << "Could not create surface for info collection.";
90 return kCollectInfoFatalFailure;
93 scoped_refptr<gfx::GLContext> context(InitializeGLContext(surface.get()));
94 if (!context.get()) {
95 LOG(ERROR) << "Could not create context for info collection.";
96 return kCollectInfoFatalFailure;
99 gpu_info->gl_renderer = GetGLString(GL_RENDERER);
100 gpu_info->gl_vendor = GetGLString(GL_VENDOR);
101 gpu_info->gl_extensions = gfx::GetGLExtensionsFromCurrentContext();
102 gpu_info->gl_version = GetGLString(GL_VERSION);
103 std::string glsl_version_string = GetGLString(GL_SHADING_LANGUAGE_VERSION);
104 GLint max_samples = 0;
105 glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
106 gpu_info->max_msaa_samples = base::IntToString(max_samples);
108 gfx::GLWindowSystemBindingInfo window_system_binding_info;
109 if (GetGLWindowSystemBindingInfo(&window_system_binding_info)) {
110 gpu_info->gl_ws_vendor = window_system_binding_info.vendor;
111 gpu_info->gl_ws_version = window_system_binding_info.version;
112 gpu_info->gl_ws_extensions = window_system_binding_info.extensions;
113 gpu_info->direct_rendering = window_system_binding_info.direct_rendering;
116 bool supports_robustness =
117 gpu_info->gl_extensions.find("GL_EXT_robustness") != std::string::npos ||
118 gpu_info->gl_extensions.find("GL_KHR_robustness") != std::string::npos ||
119 gpu_info->gl_extensions.find("GL_ARB_robustness") != std::string::npos;
120 if (supports_robustness) {
121 glGetIntegerv(GL_RESET_NOTIFICATION_STRATEGY_ARB,
122 reinterpret_cast<GLint*>(&gpu_info->gl_reset_notification_strategy));
125 // TODO(kbr): remove once the destruction of a current context automatically
126 // clears the current context.
127 context->ReleaseCurrent(surface.get());
129 std::string glsl_version = GetVersionFromString(glsl_version_string);
130 gpu_info->pixel_shader_version = glsl_version;
131 gpu_info->vertex_shader_version = glsl_version;
133 return CollectDriverInfoGL(gpu_info);
136 void MergeGPUInfoGL(GPUInfo* basic_gpu_info,
137 const GPUInfo& context_gpu_info) {
138 DCHECK(basic_gpu_info);
139 basic_gpu_info->gl_renderer = context_gpu_info.gl_renderer;
140 basic_gpu_info->gl_vendor = context_gpu_info.gl_vendor;
141 basic_gpu_info->gl_version = context_gpu_info.gl_version;
142 basic_gpu_info->gl_extensions = context_gpu_info.gl_extensions;
143 basic_gpu_info->pixel_shader_version =
144 context_gpu_info.pixel_shader_version;
145 basic_gpu_info->vertex_shader_version =
146 context_gpu_info.vertex_shader_version;
147 basic_gpu_info->max_msaa_samples =
148 context_gpu_info.max_msaa_samples;
149 basic_gpu_info->gl_ws_vendor = context_gpu_info.gl_ws_vendor;
150 basic_gpu_info->gl_ws_version = context_gpu_info.gl_ws_version;
151 basic_gpu_info->gl_ws_extensions = context_gpu_info.gl_ws_extensions;
152 basic_gpu_info->gl_reset_notification_strategy =
153 context_gpu_info.gl_reset_notification_strategy;
155 if (!context_gpu_info.driver_vendor.empty())
156 basic_gpu_info->driver_vendor = context_gpu_info.driver_vendor;
157 if (!context_gpu_info.driver_version.empty())
158 basic_gpu_info->driver_version = context_gpu_info.driver_version;
160 basic_gpu_info->can_lose_context = context_gpu_info.can_lose_context;
161 basic_gpu_info->sandboxed = context_gpu_info.sandboxed;
162 basic_gpu_info->direct_rendering = context_gpu_info.direct_rendering;
163 basic_gpu_info->in_process_gpu = context_gpu_info.in_process_gpu;
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;
170 basic_gpu_info->jpeg_decode_accelerator_supported =
171 context_gpu_info.jpeg_decode_accelerator_supported;
174 } // namespace gpu