[Offline pages] Moving browser code to android path
[chromium-blink-merge.git] / content / browser / gpu / gpu_internals_ui.cc
blob7991aa3a79a234c4416a6ad1c4b93a457ab0b36e
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 "content/browser/gpu/gpu_internals_ui.h"
7 #if defined(OS_LINUX) && defined(USE_X11)
8 #include <X11/Xlib.h>
9 #endif
11 #include <string>
13 #include "base/bind.h"
14 #include "base/bind_helpers.h"
15 #include "base/command_line.h"
16 #include "base/environment.h"
17 #include "base/i18n/time_formatting.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/sys_info.h"
21 #include "base/values.h"
22 #include "content/browser/gpu/compositor_util.h"
23 #include "content/browser/gpu/gpu_data_manager_impl.h"
24 #include "content/grit/content_resources.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/gpu_data_manager_observer.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_ui.h"
29 #include "content/public/browser/web_ui_data_source.h"
30 #include "content/public/browser/web_ui_message_handler.h"
31 #include "content/public/common/content_client.h"
32 #include "content/public/common/content_switches.h"
33 #include "content/public/common/url_constants.h"
34 #include "gpu/config/gpu_feature_type.h"
35 #include "gpu/config/gpu_info.h"
36 #include "third_party/angle/src/common/version.h"
37 #include "ui/gl/gpu_switching_manager.h"
39 #if defined(OS_WIN)
40 #include "ui/base/win/shell.h"
41 #endif
43 #if defined(OS_LINUX) && defined(USE_X11)
44 #include "ui/base/x/x11_util.h"
45 #include "ui/gfx/x/x11_atom_cache.h"
46 #endif
48 namespace content {
49 namespace {
51 WebUIDataSource* CreateGpuHTMLSource() {
52 WebUIDataSource* source = WebUIDataSource::Create(kChromeUIGpuHost);
54 source->SetJsonPath("strings.js");
55 source->AddResourcePath("gpu_internals.js", IDR_GPU_INTERNALS_JS);
56 source->SetDefaultResource(IDR_GPU_INTERNALS_HTML);
57 return source;
60 base::DictionaryValue* NewDescriptionValuePair(const std::string& desc,
61 const std::string& value) {
62 base::DictionaryValue* dict = new base::DictionaryValue();
63 dict->SetString("description", desc);
64 dict->SetString("value", value);
65 return dict;
68 base::DictionaryValue* NewDescriptionValuePair(const std::string& desc,
69 base::Value* value) {
70 base::DictionaryValue* dict = new base::DictionaryValue();
71 dict->SetString("description", desc);
72 dict->Set("value", value);
73 return dict;
76 #if defined(OS_WIN)
77 // Output DxDiagNode tree as nested array of {description,value} pairs
78 base::ListValue* DxDiagNodeToList(const gpu::DxDiagNode& node) {
79 base::ListValue* list = new base::ListValue();
80 for (std::map<std::string, std::string>::const_iterator it =
81 node.values.begin();
82 it != node.values.end();
83 ++it) {
84 list->Append(NewDescriptionValuePair(it->first, it->second));
87 for (std::map<std::string, gpu::DxDiagNode>::const_iterator it =
88 node.children.begin();
89 it != node.children.end();
90 ++it) {
91 base::ListValue* sublist = DxDiagNodeToList(it->second);
92 list->Append(NewDescriptionValuePair(it->first, sublist));
94 return list;
96 #endif
98 std::string GPUDeviceToString(const gpu::GPUInfo::GPUDevice& gpu) {
99 std::string vendor = base::StringPrintf("0x%04x", gpu.vendor_id);
100 if (!gpu.vendor_string.empty())
101 vendor += " [" + gpu.vendor_string + "]";
102 std::string device = base::StringPrintf("0x%04x", gpu.device_id);
103 if (!gpu.device_string.empty())
104 device += " [" + gpu.device_string + "]";
105 return base::StringPrintf("VENDOR = %s, DEVICE= %s%s",
106 vendor.c_str(), device.c_str(), gpu.active ? " *ACTIVE*" : "");
109 base::DictionaryValue* GpuInfoAsDictionaryValue() {
110 gpu::GPUInfo gpu_info = GpuDataManagerImpl::GetInstance()->GetGPUInfo();
111 base::ListValue* basic_info = new base::ListValue();
112 basic_info->Append(NewDescriptionValuePair(
113 "Initialization time",
114 base::Int64ToString(gpu_info.initialization_time.InMilliseconds())));
115 basic_info->Append(NewDescriptionValuePair(
116 "Sandboxed", new base::FundamentalValue(gpu_info.sandboxed)));
117 basic_info->Append(NewDescriptionValuePair(
118 "GPU0", GPUDeviceToString(gpu_info.gpu)));
119 for (size_t i = 0; i < gpu_info.secondary_gpus.size(); ++i) {
120 basic_info->Append(NewDescriptionValuePair(
121 base::StringPrintf("GPU%d", static_cast<int>(i + 1)),
122 GPUDeviceToString(gpu_info.secondary_gpus[i])));
124 basic_info->Append(NewDescriptionValuePair(
125 "Optimus", new base::FundamentalValue(gpu_info.optimus)));
126 basic_info->Append(NewDescriptionValuePair(
127 "AMD switchable", new base::FundamentalValue(gpu_info.amd_switchable)));
128 if (gpu_info.lenovo_dcute) {
129 basic_info->Append(NewDescriptionValuePair(
130 "Lenovo dCute", new base::FundamentalValue(true)));
132 if (gpu_info.display_link_version.IsValid()) {
133 basic_info->Append(NewDescriptionValuePair(
134 "DisplayLink Version", gpu_info.display_link_version.GetString()));
136 #if defined(OS_WIN)
137 std::string compositor =
138 ui::win::IsAeroGlassEnabled() ? "Aero Glass" : "none";
139 basic_info->Append(
140 NewDescriptionValuePair("Desktop compositing", compositor));
141 if (GpuDataManagerImpl::GetInstance()->ShouldUseWarp()) {
142 basic_info->Append(NewDescriptionValuePair("Using WARP",
143 new base::FundamentalValue(true)));
145 #endif
147 std::string disabled_extensions;
148 GpuDataManagerImpl::GetInstance()->GetDisabledExtensions(
149 &disabled_extensions);
151 basic_info->Append(
152 NewDescriptionValuePair("Driver vendor", gpu_info.driver_vendor));
153 basic_info->Append(NewDescriptionValuePair("Driver version",
154 gpu_info.driver_version));
155 basic_info->Append(NewDescriptionValuePair("Driver date",
156 gpu_info.driver_date));
157 basic_info->Append(NewDescriptionValuePair("Pixel shader version",
158 gpu_info.pixel_shader_version));
159 basic_info->Append(NewDescriptionValuePair("Vertex shader version",
160 gpu_info.vertex_shader_version));
161 basic_info->Append(NewDescriptionValuePair("Max. MSAA samples",
162 gpu_info.max_msaa_samples));
163 basic_info->Append(NewDescriptionValuePair("Machine model name",
164 gpu_info.machine_model_name));
165 basic_info->Append(NewDescriptionValuePair("Machine model version",
166 gpu_info.machine_model_version));
167 basic_info->Append(NewDescriptionValuePair("GL_VENDOR",
168 gpu_info.gl_vendor));
169 basic_info->Append(NewDescriptionValuePair("GL_RENDERER",
170 gpu_info.gl_renderer));
171 basic_info->Append(NewDescriptionValuePair("GL_VERSION",
172 gpu_info.gl_version));
173 basic_info->Append(NewDescriptionValuePair("GL_EXTENSIONS",
174 gpu_info.gl_extensions));
175 basic_info->Append(NewDescriptionValuePair("Disabled Extensions",
176 disabled_extensions));
177 basic_info->Append(NewDescriptionValuePair("Window system binding vendor",
178 gpu_info.gl_ws_vendor));
179 basic_info->Append(NewDescriptionValuePair("Window system binding version",
180 gpu_info.gl_ws_version));
181 basic_info->Append(NewDescriptionValuePair("Window system binding extensions",
182 gpu_info.gl_ws_extensions));
183 #if defined(OS_LINUX) && defined(USE_X11)
184 basic_info->Append(NewDescriptionValuePair("Window manager",
185 ui::GuessWindowManagerName()));
187 scoped_ptr<base::Environment> env(base::Environment::Create());
188 std::string value;
189 const char kXDGCurrentDesktop[] = "XDG_CURRENT_DESKTOP";
190 if (env->GetVar(kXDGCurrentDesktop, &value))
191 basic_info->Append(NewDescriptionValuePair(kXDGCurrentDesktop, value));
192 const char kGDMSession[] = "GDMSESSION";
193 if (env->GetVar(kGDMSession, &value))
194 basic_info->Append(NewDescriptionValuePair(kGDMSession, value));
195 const char* kAtomsToCache[] = {
196 "_NET_WM_CM_S0",
197 NULL
199 ui::X11AtomCache atom_cache(gfx::GetXDisplay(), kAtomsToCache);
200 std::string compositing_manager = XGetSelectionOwner(
201 gfx::GetXDisplay(),
202 atom_cache.GetAtom("_NET_WM_CM_S0")) != None ? "Yes" : "No";
203 basic_info->Append(
204 NewDescriptionValuePair("Compositing manager", compositing_manager));
206 #endif
207 std::string direct_rendering = gpu_info.direct_rendering ? "Yes" : "No";
208 basic_info->Append(
209 NewDescriptionValuePair("Direct rendering", direct_rendering));
211 std::string reset_strategy =
212 base::StringPrintf("0x%04x", gpu_info.gl_reset_notification_strategy);
213 basic_info->Append(NewDescriptionValuePair(
214 "Reset notification strategy", reset_strategy));
216 basic_info->Append(NewDescriptionValuePair(
217 "GPU process crash count",
218 new base::FundamentalValue(gpu_info.process_crash_count)));
220 base::DictionaryValue* info = new base::DictionaryValue();
221 info->Set("basic_info", basic_info);
223 #if defined(OS_WIN)
224 scoped_ptr<base::Value> dx_info = base::Value::CreateNullValue();
225 if (gpu_info.dx_diagnostics.children.size())
226 dx_info.reset(DxDiagNodeToList(gpu_info.dx_diagnostics));
227 info->Set("diagnostics", dx_info.Pass());
228 #endif
230 return info;
233 // This class receives javascript messages from the renderer.
234 // Note that the WebUI infrastructure runs on the UI thread, therefore all of
235 // this class's methods are expected to run on the UI thread.
236 class GpuMessageHandler
237 : public WebUIMessageHandler,
238 public base::SupportsWeakPtr<GpuMessageHandler>,
239 public GpuDataManagerObserver,
240 public ui::GpuSwitchingObserver {
241 public:
242 GpuMessageHandler();
243 ~GpuMessageHandler() override;
245 // WebUIMessageHandler implementation.
246 void RegisterMessages() override;
248 // GpuDataManagerObserver implementation.
249 void OnGpuInfoUpdate() override;
251 // ui::GpuSwitchingObserver implementation.
252 void OnGpuSwitched() override;
254 // Messages
255 void OnBrowserBridgeInitialized(const base::ListValue* list);
256 void OnCallAsync(const base::ListValue* list);
258 // Submessages dispatched from OnCallAsync
259 base::Value* OnRequestClientInfo(const base::ListValue* list);
260 base::Value* OnRequestLogMessages(const base::ListValue* list);
262 private:
263 // True if observing the GpuDataManager (re-attaching as observer would
264 // DCHECK).
265 bool observing_;
267 DISALLOW_COPY_AND_ASSIGN(GpuMessageHandler);
270 ////////////////////////////////////////////////////////////////////////////////
272 // GpuMessageHandler
274 ////////////////////////////////////////////////////////////////////////////////
276 GpuMessageHandler::GpuMessageHandler()
277 : observing_(false) {
280 GpuMessageHandler::~GpuMessageHandler() {
281 ui::GpuSwitchingManager::GetInstance()->RemoveObserver(this);
282 GpuDataManagerImpl::GetInstance()->RemoveObserver(this);
285 /* BrowserBridge.callAsync prepends a requestID to these messages. */
286 void GpuMessageHandler::RegisterMessages() {
287 DCHECK_CURRENTLY_ON(BrowserThread::UI);
289 web_ui()->RegisterMessageCallback("browserBridgeInitialized",
290 base::Bind(&GpuMessageHandler::OnBrowserBridgeInitialized,
291 base::Unretained(this)));
292 web_ui()->RegisterMessageCallback("callAsync",
293 base::Bind(&GpuMessageHandler::OnCallAsync,
294 base::Unretained(this)));
297 void GpuMessageHandler::OnCallAsync(const base::ListValue* args) {
298 DCHECK_GE(args->GetSize(), static_cast<size_t>(2));
299 // unpack args into requestId, submessage and submessageArgs
300 bool ok;
301 const base::Value* requestId;
302 ok = args->Get(0, &requestId);
303 DCHECK(ok);
305 std::string submessage;
306 ok = args->GetString(1, &submessage);
307 DCHECK(ok);
309 base::ListValue* submessageArgs = new base::ListValue();
310 for (size_t i = 2; i < args->GetSize(); ++i) {
311 const base::Value* arg;
312 ok = args->Get(i, &arg);
313 DCHECK(ok);
315 base::Value* argCopy = arg->DeepCopy();
316 submessageArgs->Append(argCopy);
319 // call the submessage handler
320 base::Value* ret = NULL;
321 if (submessage == "requestClientInfo") {
322 ret = OnRequestClientInfo(submessageArgs);
323 } else if (submessage == "requestLogMessages") {
324 ret = OnRequestLogMessages(submessageArgs);
325 } else { // unrecognized submessage
326 NOTREACHED();
327 delete submessageArgs;
328 return;
330 delete submessageArgs;
332 // call BrowserBridge.onCallAsyncReply with result
333 if (ret) {
334 web_ui()->CallJavascriptFunction("browserBridge.onCallAsyncReply",
335 *requestId,
336 *ret);
337 delete ret;
338 } else {
339 web_ui()->CallJavascriptFunction("browserBridge.onCallAsyncReply",
340 *requestId);
344 void GpuMessageHandler::OnBrowserBridgeInitialized(
345 const base::ListValue* args) {
346 DCHECK_CURRENTLY_ON(BrowserThread::UI);
348 // Watch for changes in GPUInfo
349 if (!observing_) {
350 GpuDataManagerImpl::GetInstance()->AddObserver(this);
351 ui::GpuSwitchingManager::GetInstance()->AddObserver(this);
353 observing_ = true;
355 // Tell GpuDataManager it should have full GpuInfo. If the
356 // Gpu process has not run yet, this will trigger its launch.
357 GpuDataManagerImpl::GetInstance()->RequestCompleteGpuInfoIfNeeded();
359 // Run callback immediately in case the info is ready and no update in the
360 // future.
361 OnGpuInfoUpdate();
364 base::Value* GpuMessageHandler::OnRequestClientInfo(
365 const base::ListValue* list) {
366 DCHECK_CURRENTLY_ON(BrowserThread::UI);
368 base::DictionaryValue* dict = new base::DictionaryValue();
370 dict->SetString("version", GetContentClient()->GetProduct());
371 dict->SetString("command_line",
372 base::CommandLine::ForCurrentProcess()->GetCommandLineString());
373 dict->SetString("operating_system",
374 base::SysInfo::OperatingSystemName() + " " +
375 base::SysInfo::OperatingSystemVersion());
376 dict->SetString("angle_commit_id", ANGLE_COMMIT_HASH);
377 dict->SetString("graphics_backend", "Skia");
378 dict->SetString("blacklist_version",
379 GpuDataManagerImpl::GetInstance()->GetBlacklistVersion());
380 dict->SetString("driver_bug_list_version",
381 GpuDataManagerImpl::GetInstance()->GetDriverBugListVersion());
383 return dict;
386 base::Value* GpuMessageHandler::OnRequestLogMessages(const base::ListValue*) {
387 DCHECK_CURRENTLY_ON(BrowserThread::UI);
389 return GpuDataManagerImpl::GetInstance()->GetLogMessages();
392 void GpuMessageHandler::OnGpuInfoUpdate() {
393 // Get GPU Info.
394 scoped_ptr<base::DictionaryValue> gpu_info_val(GpuInfoAsDictionaryValue());
397 // Add in blacklisting features
398 base::DictionaryValue* feature_status = new base::DictionaryValue;
399 feature_status->Set("featureStatus", GetFeatureStatus());
400 feature_status->Set("problems", GetProblems());
401 base::ListValue* workarounds = new base::ListValue();
402 for (const std::string& workaround : GetDriverBugWorkarounds())
403 workarounds->AppendString(workaround);
404 feature_status->Set("workarounds", workarounds);
405 gpu_info_val->Set("featureStatus", feature_status);
407 // Send GPU Info to javascript.
408 web_ui()->CallJavascriptFunction("browserBridge.onGpuInfoUpdate",
409 *(gpu_info_val.get()));
412 void GpuMessageHandler::OnGpuSwitched() {
413 GpuDataManagerImpl::GetInstance()->RequestCompleteGpuInfoIfNeeded();
416 } // namespace
419 ////////////////////////////////////////////////////////////////////////////////
421 // GpuInternalsUI
423 ////////////////////////////////////////////////////////////////////////////////
425 GpuInternalsUI::GpuInternalsUI(WebUI* web_ui)
426 : WebUIController(web_ui) {
427 web_ui->AddMessageHandler(new GpuMessageHandler());
429 // Set up the chrome://gpu/ source.
430 BrowserContext* browser_context =
431 web_ui->GetWebContents()->GetBrowserContext();
432 WebUIDataSource::Add(browser_context, CreateGpuHTMLSource());
435 } // namespace content