| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/ozone/platform/drm/host/drm_device_handle.h" | 5 #include "ui/ozone/platform/drm/host/drm_device_handle.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <xf86drm.h> | 8 #include <xf86drm.h> |
| 9 #include <xf86drmMode.h> | 9 #include <xf86drmMode.h> |
| 10 | 10 |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/posix/eintr_wrapper.h" | 12 #include "base/posix/eintr_wrapper.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 | 14 |
| 15 namespace ui { | 15 namespace ui { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 bool Authenticate(int fd) { | 19 bool Authenticate(int fd) { |
| 20 drm_magic_t magic; | 20 drm_magic_t magic; |
| 21 memset(&magic, 0, sizeof(magic)); | 21 memset(&magic, 0, sizeof(magic)); |
| 22 // We need to make sure the DRM device has enough privilege. Use the DRM | 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. | 23 // authentication logic to figure out if the device has enough permissions. |
| 24 return !drmGetMagic(fd, &magic) && !drmAuthMagic(fd, magic); | 24 return !drmGetMagic(fd, &magic) && !drmAuthMagic(fd, magic); |
| 25 } | 25 } |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 DrmDeviceHandle::DrmDeviceHandle() { | 29 DrmDeviceHandle::DrmDeviceHandle() : is_vgem_(false) { |
| 30 } | 30 } |
| 31 | 31 |
| 32 DrmDeviceHandle::~DrmDeviceHandle() { | 32 DrmDeviceHandle::~DrmDeviceHandle() { |
| 33 base::ThreadRestrictions::AssertIOAllowed(); | 33 base::ThreadRestrictions::AssertIOAllowed(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 bool DrmDeviceHandle::Initialize(const base::FilePath& path) { | 36 bool DrmDeviceHandle::Initialize(const base::FilePath& path, bool is_vgem) { |
| 37 CHECK(path.DirName() == base::FilePath("/dev/dri")); | 37 CHECK(path.DirName() == base::FilePath("/dev/dri")); |
| 38 base::ThreadRestrictions::AssertIOAllowed(); | 38 base::ThreadRestrictions::AssertIOAllowed(); |
| 39 is_vgem_ = is_vgem; |
| 39 bool print_warning = true; | 40 bool print_warning = true; |
| 40 while (true) { | 41 while (true) { |
| 41 file_.reset(); | 42 file_.reset(); |
| 42 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDWR | O_CLOEXEC)); | 43 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDWR | O_CLOEXEC)); |
| 43 if (fd < 0) { | 44 if (fd < 0) { |
| 44 PLOG(ERROR) << "Failed to open " << path.value(); | 45 PLOG(ERROR) << "Failed to open " << path.value(); |
| 45 return false; | 46 return false; |
| 46 } | 47 } |
| 47 | 48 |
| 48 file_.reset(fd); | 49 file_.reset(fd); |
| 50 if (is_vgem_) |
| 51 break; |
| 49 | 52 |
| 50 if (Authenticate(file_.get())) | 53 if (Authenticate(file_.get())) |
| 51 break; | 54 break; |
| 52 | 55 |
| 53 LOG_IF(WARNING, print_warning) << "Failed to authenticate " << path.value(); | 56 LOG_IF(WARNING, print_warning) << "Failed to authenticate " << path.value(); |
| 54 print_warning = false; | 57 print_warning = false; |
| 55 usleep(100000); | 58 usleep(100000); |
| 56 } | 59 } |
| 57 | 60 |
| 58 VLOG(1) << "Succeeded authenticating " << path.value(); | 61 VLOG(1) << "Succeeded authenticating " << path.value(); |
| 59 return true; | 62 return true; |
| 60 } | 63 } |
| 61 | 64 |
| 62 bool DrmDeviceHandle::IsValid() const { | 65 bool DrmDeviceHandle::IsValid() const { |
| 63 return file_.is_valid(); | 66 return file_.is_valid(); |
| 64 } | 67 } |
| 65 | 68 |
| 66 base::ScopedFD DrmDeviceHandle::Duplicate() { | 69 base::ScopedFD DrmDeviceHandle::Duplicate() { |
| 67 DCHECK(file_.is_valid()); | 70 DCHECK(file_.is_valid()); |
| 68 int fd = dup(file_.get()); | 71 int fd = dup(file_.get()); |
| 69 if (fd < 0) { | 72 if (fd < 0) { |
| 70 PLOG(ERROR) << "Failed to dup"; | 73 PLOG(ERROR) << "Failed to dup"; |
| 71 return base::ScopedFD(); | 74 return base::ScopedFD(); |
| 72 } | 75 } |
| 73 | 76 |
| 74 return base::ScopedFD(fd); | 77 return base::ScopedFD(fd); |
| 75 } | 78 } |
| 76 | 79 |
| 77 } // namespace ui | 80 } // namespace ui |
| OLD | NEW |