| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/dri/dri_wrapper.h" | 5 #include "ui/ozone/platform/dri/dri_wrapper.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 #include <xf86drm.h> | 10 #include <xf86drm.h> |
| 11 #include <xf86drmMode.h> | 11 #include <xf86drmMode.h> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/task_runner.h" | 17 #include "base/task_runner.h" |
| 17 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
| 19 #include "third_party/skia/include/core/SkImageInfo.h" | 20 #include "third_party/skia/include/core/SkImageInfo.h" |
| 20 #include "ui/ozone/platform/dri/dri_util.h" | 21 #include "ui/ozone/platform/dri/dri_util.h" |
| 21 #include "ui/ozone/platform/dri/hardware_display_plane_manager_legacy.h" | 22 #include "ui/ozone/platform/dri/hardware_display_plane_manager_legacy.h" |
| 22 | 23 |
| 23 namespace ui { | 24 namespace ui { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 98 } |
| 98 | 99 |
| 99 } // namespace | 100 } // namespace |
| 100 | 101 |
| 101 class DriWrapper::IOWatcher | 102 class DriWrapper::IOWatcher |
| 102 : public base::RefCountedThreadSafe<DriWrapper::IOWatcher>, | 103 : public base::RefCountedThreadSafe<DriWrapper::IOWatcher>, |
| 103 public base::MessagePumpLibevent::Watcher { | 104 public base::MessagePumpLibevent::Watcher { |
| 104 public: | 105 public: |
| 105 IOWatcher(int fd, | 106 IOWatcher(int fd, |
| 106 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 107 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 107 : io_task_runner_(io_task_runner) { | 108 : io_task_runner_(io_task_runner), paused_(true), fd_(fd) { |
| 108 io_task_runner_->PostTask(FROM_HERE, | 109 } |
| 109 base::Bind(&IOWatcher::RegisterOnIO, this, fd)); | 110 |
| 111 void SetPaused(bool paused) { |
| 112 if (paused_ == paused) |
| 113 return; |
| 114 |
| 115 paused_ = paused; |
| 116 base::WaitableEvent done(false, false); |
| 117 io_task_runner_->PostTask( |
| 118 FROM_HERE, base::Bind(&IOWatcher::SetPausedOnIO, this, &done)); |
| 110 } | 119 } |
| 111 | 120 |
| 112 void Shutdown() { | 121 void Shutdown() { |
| 113 io_task_runner_->PostTask(FROM_HERE, | 122 if (!paused_) |
| 114 base::Bind(&IOWatcher::UnregisterOnIO, this)); | 123 io_task_runner_->PostTask(FROM_HERE, |
| 124 base::Bind(&IOWatcher::UnregisterOnIO, this)); |
| 115 } | 125 } |
| 116 | 126 |
| 117 private: | 127 private: |
| 118 friend class base::RefCountedThreadSafe<IOWatcher>; | 128 friend class base::RefCountedThreadSafe<IOWatcher>; |
| 119 | 129 |
| 120 ~IOWatcher() override {} | 130 ~IOWatcher() override { |
| 131 SetPaused(true); |
| 132 } |
| 121 | 133 |
| 122 void RegisterOnIO(int fd) { | 134 void RegisterOnIO() { |
| 123 DCHECK(base::MessageLoopForIO::IsCurrent()); | 135 DCHECK(base::MessageLoopForIO::IsCurrent()); |
| 124 base::MessageLoopForIO::current()->WatchFileDescriptor( | 136 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 125 fd, true, base::MessageLoopForIO::WATCH_READ, &controller_, this); | 137 fd_, true, base::MessageLoopForIO::WATCH_READ, &controller_, this); |
| 126 } | 138 } |
| 127 | 139 |
| 128 void UnregisterOnIO() { | 140 void UnregisterOnIO() { |
| 129 DCHECK(base::MessageLoopForIO::IsCurrent()); | 141 DCHECK(base::MessageLoopForIO::IsCurrent()); |
| 130 controller_.StopWatchingFileDescriptor(); | 142 controller_.StopWatchingFileDescriptor(); |
| 131 } | 143 } |
| 132 | 144 |
| 145 void SetPausedOnIO(base::WaitableEvent* done) { |
| 146 DCHECK(base::MessageLoopForIO::IsCurrent()); |
| 147 if (paused_) |
| 148 UnregisterOnIO(); |
| 149 else |
| 150 RegisterOnIO(); |
| 151 done->Signal(); |
| 152 } |
| 153 |
| 133 // base::MessagePumpLibevent::Watcher overrides: | 154 // base::MessagePumpLibevent::Watcher overrides: |
| 134 void OnFileCanReadWithoutBlocking(int fd) override { | 155 void OnFileCanReadWithoutBlocking(int fd) override { |
| 135 DCHECK(base::MessageLoopForIO::IsCurrent()); | 156 DCHECK(base::MessageLoopForIO::IsCurrent()); |
| 136 TRACE_EVENT1("dri", "OnDrmEvent", "socket", fd); | 157 TRACE_EVENT1("dri", "OnDrmEvent", "socket", fd); |
| 137 | 158 |
| 138 drmEventContext event; | 159 drmEventContext event; |
| 139 event.version = DRM_EVENT_CONTEXT_VERSION; | 160 event.version = DRM_EVENT_CONTEXT_VERSION; |
| 140 event.page_flip_handler = HandlePageFlipEventOnIO; | 161 event.page_flip_handler = HandlePageFlipEventOnIO; |
| 141 event.vblank_handler = nullptr; | 162 event.vblank_handler = nullptr; |
| 142 | 163 |
| 143 drmHandleEvent(fd, &event); | 164 drmHandleEvent(fd, &event); |
| 144 } | 165 } |
| 145 | 166 |
| 146 void OnFileCanWriteWithoutBlocking(int fd) override { NOTREACHED(); } | 167 void OnFileCanWriteWithoutBlocking(int fd) override { NOTREACHED(); } |
| 147 | 168 |
| 148 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | 169 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 149 | 170 |
| 150 base::MessagePumpLibevent::FileDescriptorWatcher controller_; | 171 base::MessagePumpLibevent::FileDescriptorWatcher controller_; |
| 151 | 172 |
| 173 bool paused_; |
| 174 int fd_; |
| 175 |
| 152 DISALLOW_COPY_AND_ASSIGN(IOWatcher); | 176 DISALLOW_COPY_AND_ASSIGN(IOWatcher); |
| 153 }; | 177 }; |
| 154 | 178 |
| 155 DriWrapper::DriWrapper(const base::FilePath& device_path) | 179 DriWrapper::DriWrapper(const base::FilePath& device_path) |
| 156 : device_path_(device_path), | 180 : device_path_(device_path), |
| 157 file_(device_path, | 181 file_(device_path, |
| 158 base::File::FLAG_OPEN | base::File::FLAG_READ | | 182 base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 159 base::File::FLAG_WRITE) { | 183 base::File::FLAG_WRITE) { |
| 160 LOG_IF(FATAL, !file_.IsValid()) | 184 LOG_IF(FATAL, !file_.IsValid()) |
| 161 << "Failed to open '" << device_path_.value() | 185 << "Failed to open '" << device_path_.value() |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 287 |
| 264 bool DriWrapper::RemoveFramebuffer(uint32_t framebuffer) { | 288 bool DriWrapper::RemoveFramebuffer(uint32_t framebuffer) { |
| 265 DCHECK(file_.IsValid()); | 289 DCHECK(file_.IsValid()); |
| 266 TRACE_EVENT1("dri", "DriWrapper::RemoveFramebuffer", | 290 TRACE_EVENT1("dri", "DriWrapper::RemoveFramebuffer", |
| 267 "framebuffer", framebuffer); | 291 "framebuffer", framebuffer); |
| 268 return !drmModeRmFB(file_.GetPlatformFile(), framebuffer); | 292 return !drmModeRmFB(file_.GetPlatformFile(), framebuffer); |
| 269 } | 293 } |
| 270 | 294 |
| 271 bool DriWrapper::PageFlip(uint32_t crtc_id, | 295 bool DriWrapper::PageFlip(uint32_t crtc_id, |
| 272 uint32_t framebuffer, | 296 uint32_t framebuffer, |
| 297 bool is_sync, |
| 273 const PageFlipCallback& callback) { | 298 const PageFlipCallback& callback) { |
| 274 DCHECK(file_.IsValid()); | 299 DCHECK(file_.IsValid()); |
| 275 TRACE_EVENT2("dri", "DriWrapper::PageFlip", | 300 TRACE_EVENT2("dri", "DriWrapper::PageFlip", |
| 276 "crtc", crtc_id, | 301 "crtc", crtc_id, |
| 277 "framebuffer", framebuffer); | 302 "framebuffer", framebuffer); |
| 278 | 303 |
| 304 if (watcher_) |
| 305 watcher_->SetPaused(is_sync); |
| 306 |
| 279 // NOTE: Calling drmModeSetCrtc will immediately update the state, though | 307 // NOTE: Calling drmModeSetCrtc will immediately update the state, though |
| 280 // callbacks to already scheduled page flips will be honored by the kernel. | 308 // callbacks to already scheduled page flips will be honored by the kernel. |
| 281 scoped_ptr<PageFlipPayload> payload( | 309 scoped_ptr<PageFlipPayload> payload( |
| 282 new PageFlipPayload(base::ThreadTaskRunnerHandle::Get(), callback)); | 310 new PageFlipPayload(base::ThreadTaskRunnerHandle::Get(), callback)); |
| 283 if (!drmModePageFlip(file_.GetPlatformFile(), crtc_id, framebuffer, | 311 if (!drmModePageFlip(file_.GetPlatformFile(), crtc_id, framebuffer, |
| 284 DRM_MODE_PAGE_FLIP_EVENT, payload.get())) { | 312 DRM_MODE_PAGE_FLIP_EVENT, payload.get())) { |
| 285 // If successful the payload will be removed by a PageFlip event. | 313 // If successful the payload will be removed by a PageFlip event. |
| 286 ignore_result(payload.release()); | 314 ignore_result(payload.release()); |
| 287 | 315 |
| 288 // If a task runner isn't installed then fall back to synchronously handling | 316 // If the flip was requested synchronous or if no watcher has been installed |
| 289 // the page flip events. | 317 // yet, then synchronously handle the page flip events. |
| 290 if (!task_runner_) { | 318 if (is_sync || !watcher_) { |
| 291 TRACE_EVENT1("dri", "OnDrmEvent", "socket", file_.GetPlatformFile()); | 319 TRACE_EVENT1("dri", "OnDrmEvent", "socket", file_.GetPlatformFile()); |
| 292 | 320 |
| 293 drmEventContext event; | 321 drmEventContext event; |
| 294 event.version = DRM_EVENT_CONTEXT_VERSION; | 322 event.version = DRM_EVENT_CONTEXT_VERSION; |
| 295 event.page_flip_handler = HandlePageFlipEventOnUI; | 323 event.page_flip_handler = HandlePageFlipEventOnUI; |
| 296 event.vblank_handler = nullptr; | 324 event.vblank_handler = nullptr; |
| 297 | 325 |
| 298 drmHandleEvent(file_.GetPlatformFile(), &event); | 326 drmHandleEvent(file_.GetPlatformFile(), &event); |
| 299 } | 327 } |
| 300 | 328 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 DCHECK(file_.IsValid()); | 455 DCHECK(file_.IsValid()); |
| 428 return (drmSetMaster(file_.GetPlatformFile()) == 0); | 456 return (drmSetMaster(file_.GetPlatformFile()) == 0); |
| 429 } | 457 } |
| 430 | 458 |
| 431 bool DriWrapper::DropMaster() { | 459 bool DriWrapper::DropMaster() { |
| 432 DCHECK(file_.IsValid()); | 460 DCHECK(file_.IsValid()); |
| 433 return (drmDropMaster(file_.GetPlatformFile()) == 0); | 461 return (drmDropMaster(file_.GetPlatformFile()) == 0); |
| 434 } | 462 } |
| 435 | 463 |
| 436 } // namespace ui | 464 } // namespace ui |
| OLD | NEW |