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)
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"
40 #include "ui/base/win/shell.h"
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"
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
);
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
);
68 base::DictionaryValue
* NewDescriptionValuePair(const std::string
& desc
,
70 base::DictionaryValue
* dict
= new base::DictionaryValue();
71 dict
->SetString("description", desc
);
72 dict
->Set("value", value
);
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
=
82 it
!= node
.values
.end();
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();
91 base::ListValue
* sublist
= DxDiagNodeToList(it
->second
);
92 list
->Append(NewDescriptionValuePair(it
->first
, sublist
));
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()));
137 std::string compositor
=
138 ui::win::IsAeroGlassEnabled() ? "Aero Glass" : "none";
140 NewDescriptionValuePair("Desktop compositing", compositor
));
141 if (GpuDataManagerImpl::GetInstance()->ShouldUseWarp()) {
142 basic_info
->Append(NewDescriptionValuePair("Using WARP",
143 new base::FundamentalValue(true)));
148 NewDescriptionValuePair("Driver vendor", gpu_info
.driver_vendor
));
149 basic_info
->Append(NewDescriptionValuePair("Driver version",
150 gpu_info
.driver_version
));
151 basic_info
->Append(NewDescriptionValuePair("Driver date",
152 gpu_info
.driver_date
));
153 basic_info
->Append(NewDescriptionValuePair("Pixel shader version",
154 gpu_info
.pixel_shader_version
));
155 basic_info
->Append(NewDescriptionValuePair("Vertex shader version",
156 gpu_info
.vertex_shader_version
));
157 basic_info
->Append(NewDescriptionValuePair("Machine model name",
158 gpu_info
.machine_model_name
));
159 basic_info
->Append(NewDescriptionValuePair("Machine model version",
160 gpu_info
.machine_model_version
));
161 basic_info
->Append(NewDescriptionValuePair("GL_VENDOR",
162 gpu_info
.gl_vendor
));
163 basic_info
->Append(NewDescriptionValuePair("GL_RENDERER",
164 gpu_info
.gl_renderer
));
165 basic_info
->Append(NewDescriptionValuePair("GL_VERSION",
166 gpu_info
.gl_version
));
167 basic_info
->Append(NewDescriptionValuePair("GL_EXTENSIONS",
168 gpu_info
.gl_extensions
));
169 basic_info
->Append(NewDescriptionValuePair("Window system binding vendor",
170 gpu_info
.gl_ws_vendor
));
171 basic_info
->Append(NewDescriptionValuePair("Window system binding version",
172 gpu_info
.gl_ws_version
));
173 basic_info
->Append(NewDescriptionValuePair("Window system binding extensions",
174 gpu_info
.gl_ws_extensions
));
175 #if defined(OS_LINUX) && defined(USE_X11)
176 basic_info
->Append(NewDescriptionValuePair("Window manager",
177 ui::GuessWindowManagerName()));
179 scoped_ptr
<base::Environment
> env(base::Environment::Create());
181 const char kXDGCurrentDesktop
[] = "XDG_CURRENT_DESKTOP";
182 if (env
->GetVar(kXDGCurrentDesktop
, &value
))
183 basic_info
->Append(NewDescriptionValuePair(kXDGCurrentDesktop
, value
));
184 const char kGDMSession
[] = "GDMSESSION";
185 if (env
->GetVar(kGDMSession
, &value
))
186 basic_info
->Append(NewDescriptionValuePair(kGDMSession
, value
));
187 const char* kAtomsToCache
[] = {
191 ui::X11AtomCache
atom_cache(gfx::GetXDisplay(), kAtomsToCache
);
192 std::string compositing_manager
= XGetSelectionOwner(
194 atom_cache
.GetAtom("_NET_WM_CM_S0")) != None
? "Yes" : "No";
196 NewDescriptionValuePair("Compositing manager", compositing_manager
));
199 std::string direct_rendering
= gpu_info
.direct_rendering
? "Yes" : "No";
201 NewDescriptionValuePair("Direct rendering", direct_rendering
));
203 std::string reset_strategy
=
204 base::StringPrintf("0x%04x", gpu_info
.gl_reset_notification_strategy
);
205 basic_info
->Append(NewDescriptionValuePair(
206 "Reset notification strategy", reset_strategy
));
208 basic_info
->Append(NewDescriptionValuePair(
209 "GPU process crash count",
210 new base::FundamentalValue(gpu_info
.process_crash_count
)));
212 base::DictionaryValue
* info
= new base::DictionaryValue();
213 info
->Set("basic_info", basic_info
);
216 base::Value
* dx_info
= gpu_info
.dx_diagnostics
.children
.size() ?
217 DxDiagNodeToList(gpu_info
.dx_diagnostics
) :
218 base::Value::CreateNullValue();
219 info
->Set("diagnostics", dx_info
);
225 // This class receives javascript messages from the renderer.
226 // Note that the WebUI infrastructure runs on the UI thread, therefore all of
227 // this class's methods are expected to run on the UI thread.
228 class GpuMessageHandler
229 : public WebUIMessageHandler
,
230 public base::SupportsWeakPtr
<GpuMessageHandler
>,
231 public GpuDataManagerObserver
,
232 public ui::GpuSwitchingObserver
{
235 ~GpuMessageHandler() override
;
237 // WebUIMessageHandler implementation.
238 void RegisterMessages() override
;
240 // GpuDataManagerObserver implementation.
241 void OnGpuInfoUpdate() override
;
243 // ui::GpuSwitchingObserver implementation.
244 void OnGpuSwitched() override
;
247 void OnBrowserBridgeInitialized(const base::ListValue
* list
);
248 void OnCallAsync(const base::ListValue
* list
);
250 // Submessages dispatched from OnCallAsync
251 base::Value
* OnRequestClientInfo(const base::ListValue
* list
);
252 base::Value
* OnRequestLogMessages(const base::ListValue
* list
);
255 // True if observing the GpuDataManager (re-attaching as observer would
259 DISALLOW_COPY_AND_ASSIGN(GpuMessageHandler
);
262 ////////////////////////////////////////////////////////////////////////////////
266 ////////////////////////////////////////////////////////////////////////////////
268 GpuMessageHandler::GpuMessageHandler()
269 : observing_(false) {
272 GpuMessageHandler::~GpuMessageHandler() {
273 ui::GpuSwitchingManager::GetInstance()->RemoveObserver(this);
274 GpuDataManagerImpl::GetInstance()->RemoveObserver(this);
277 /* BrowserBridge.callAsync prepends a requestID to these messages. */
278 void GpuMessageHandler::RegisterMessages() {
279 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
281 web_ui()->RegisterMessageCallback("browserBridgeInitialized",
282 base::Bind(&GpuMessageHandler::OnBrowserBridgeInitialized
,
283 base::Unretained(this)));
284 web_ui()->RegisterMessageCallback("callAsync",
285 base::Bind(&GpuMessageHandler::OnCallAsync
,
286 base::Unretained(this)));
289 void GpuMessageHandler::OnCallAsync(const base::ListValue
* args
) {
290 DCHECK_GE(args
->GetSize(), static_cast<size_t>(2));
291 // unpack args into requestId, submessage and submessageArgs
293 const base::Value
* requestId
;
294 ok
= args
->Get(0, &requestId
);
297 std::string submessage
;
298 ok
= args
->GetString(1, &submessage
);
301 base::ListValue
* submessageArgs
= new base::ListValue();
302 for (size_t i
= 2; i
< args
->GetSize(); ++i
) {
303 const base::Value
* arg
;
304 ok
= args
->Get(i
, &arg
);
307 base::Value
* argCopy
= arg
->DeepCopy();
308 submessageArgs
->Append(argCopy
);
311 // call the submessage handler
312 base::Value
* ret
= NULL
;
313 if (submessage
== "requestClientInfo") {
314 ret
= OnRequestClientInfo(submessageArgs
);
315 } else if (submessage
== "requestLogMessages") {
316 ret
= OnRequestLogMessages(submessageArgs
);
317 } else { // unrecognized submessage
319 delete submessageArgs
;
322 delete submessageArgs
;
324 // call BrowserBridge.onCallAsyncReply with result
326 web_ui()->CallJavascriptFunction("browserBridge.onCallAsyncReply",
331 web_ui()->CallJavascriptFunction("browserBridge.onCallAsyncReply",
336 void GpuMessageHandler::OnBrowserBridgeInitialized(
337 const base::ListValue
* args
) {
338 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
340 // Watch for changes in GPUInfo
342 GpuDataManagerImpl::GetInstance()->AddObserver(this);
343 ui::GpuSwitchingManager::GetInstance()->AddObserver(this);
347 // Tell GpuDataManager it should have full GpuInfo. If the
348 // Gpu process has not run yet, this will trigger its launch.
349 GpuDataManagerImpl::GetInstance()->RequestCompleteGpuInfoIfNeeded();
351 // Run callback immediately in case the info is ready and no update in the
356 base::Value
* GpuMessageHandler::OnRequestClientInfo(
357 const base::ListValue
* list
) {
358 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
360 base::DictionaryValue
* dict
= new base::DictionaryValue();
362 dict
->SetString("version", GetContentClient()->GetProduct());
363 dict
->SetString("command_line",
364 base::CommandLine::ForCurrentProcess()->GetCommandLineString());
365 dict
->SetString("operating_system",
366 base::SysInfo::OperatingSystemName() + " " +
367 base::SysInfo::OperatingSystemVersion());
368 dict
->SetString("angle_commit_id", ANGLE_COMMIT_HASH
);
369 dict
->SetString("graphics_backend", "Skia");
370 dict
->SetString("blacklist_version",
371 GpuDataManagerImpl::GetInstance()->GetBlacklistVersion());
372 dict
->SetString("driver_bug_list_version",
373 GpuDataManagerImpl::GetInstance()->GetDriverBugListVersion());
378 base::Value
* GpuMessageHandler::OnRequestLogMessages(const base::ListValue
*) {
379 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
381 return GpuDataManagerImpl::GetInstance()->GetLogMessages();
384 void GpuMessageHandler::OnGpuInfoUpdate() {
386 scoped_ptr
<base::DictionaryValue
> gpu_info_val(GpuInfoAsDictionaryValue());
389 // Add in blacklisting features
390 base::DictionaryValue
* feature_status
= new base::DictionaryValue
;
391 feature_status
->Set("featureStatus", GetFeatureStatus());
392 feature_status
->Set("problems", GetProblems());
393 base::ListValue
* workarounds
= new base::ListValue();
394 for (const std::string
& workaround
: GetDriverBugWorkarounds())
395 workarounds
->AppendString(workaround
);
396 feature_status
->Set("workarounds", workarounds
);
397 gpu_info_val
->Set("featureStatus", feature_status
);
399 // Send GPU Info to javascript.
400 web_ui()->CallJavascriptFunction("browserBridge.onGpuInfoUpdate",
401 *(gpu_info_val
.get()));
404 void GpuMessageHandler::OnGpuSwitched() {
405 GpuDataManagerImpl::GetInstance()->RequestCompleteGpuInfoIfNeeded();
411 ////////////////////////////////////////////////////////////////////////////////
415 ////////////////////////////////////////////////////////////////////////////////
417 GpuInternalsUI::GpuInternalsUI(WebUI
* web_ui
)
418 : WebUIController(web_ui
) {
419 web_ui
->AddMessageHandler(new GpuMessageHandler());
421 // Set up the chrome://gpu/ source.
422 BrowserContext
* browser_context
=
423 web_ui
->GetWebContents()->GetBrowserContext();
424 WebUIDataSource::Add(browser_context
, CreateGpuHTMLSource());
427 } // namespace content