Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / ozone / platform / drm / host / drm_device_handle.cc
blob8d9fc0c53af256964ff95b8175229814145ae113
1 // Copyright 2015 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 "ui/ozone/platform/drm/host/drm_device_handle.h"
7 #include <fcntl.h>
8 #include <xf86drm.h>
9 #include <xf86drmMode.h>
11 #include "base/files/file_path.h"
12 #include "base/posix/eintr_wrapper.h"
13 #include "base/threading/thread_restrictions.h"
15 namespace ui {
17 namespace {
19 bool Authenticate(int fd) {
20 drm_magic_t magic;
21 memset(&magic, 0, sizeof(magic));
22 // We need to make sure the DRM device has enough privilege. Use the DRM
23 // authentication logic to figure out if the device has enough permissions.
24 return !drmGetMagic(fd, &magic) && !drmAuthMagic(fd, magic);
27 } // namespace
29 DrmDeviceHandle::DrmDeviceHandle() {
32 DrmDeviceHandle::~DrmDeviceHandle() {
33 if (file_.is_valid())
34 base::ThreadRestrictions::AssertIOAllowed();
37 bool DrmDeviceHandle::Initialize(const base::FilePath& path) {
38 CHECK(path.DirName() == base::FilePath("/dev/dri"));
39 base::ThreadRestrictions::AssertIOAllowed();
40 bool print_warning = true;
41 while (true) {
42 file_.reset();
43 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDWR | O_CLOEXEC));
44 if (fd < 0) {
45 PLOG(ERROR) << "Failed to open " << path.value();
46 return false;
49 file_.reset(fd);
51 if (Authenticate(file_.get()))
52 break;
54 LOG_IF(WARNING, print_warning) << "Failed to authenticate " << path.value();
55 print_warning = false;
56 usleep(100000);
59 VLOG(1) << "Succeeded authenticating " << path.value();
60 return true;
63 bool DrmDeviceHandle::IsValid() const {
64 return file_.is_valid();
67 base::ScopedFD DrmDeviceHandle::PassFD() {
68 return file_.Pass();
71 } // namespace ui