OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/browser/gpu/browser_native_pixmap_manager_ozone.h" |
| 6 |
| 7 #include "base/memory/singleton.h" |
| 8 #include "base/posix/eintr_wrapper.h" |
| 9 #include "content/common/gpu/native_pixmap_manager_ozone_messages.h" |
| 10 #include "content/public/browser/browser_message_filter.h" |
| 11 #include "ui/ozone/public/ozone_platform.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 namespace { |
| 16 |
| 17 class BrowserNativePixmapManagerMessageFilter : public BrowserMessageFilter { |
| 18 public: |
| 19 explicit BrowserNativePixmapManagerMessageFilter( |
| 20 BrowserNativePixmapManager* manager) |
| 21 : BrowserMessageFilter(BrowserNativePixmapManagerMsgStart), |
| 22 manager_(manager) {} |
| 23 |
| 24 bool OnMessageReceived(const IPC::Message& message) override { |
| 25 bool handled = true; |
| 26 IPC_BEGIN_MESSAGE_MAP(BrowserNativePixmapManagerMessageFilter, message) |
| 27 IPC_MESSAGE_HANDLER_DELAY_REPLY( |
| 28 BrowserNativePixmapManagerMsg_SyncGetVirtualDevice, OnGetVirtualDevice) |
| 29 IPC_MESSAGE_UNHANDLED(handled = false) |
| 30 IPC_END_MESSAGE_MAP() |
| 31 return handled; |
| 32 } |
| 33 |
| 34 private: |
| 35 ~BrowserNativePixmapManagerMessageFilter() override {} |
| 36 |
| 37 void OnGetVirtualDevice(IPC::Message* reply) { |
| 38 DCHECK(manager_); |
| 39 manager_->GetVirtualDeviceForChildProcess(base::Bind( |
| 40 &BrowserNativePixmapManagerMessageFilter::VirtualDeviceRelpied, this, |
| 41 reply)); |
| 42 } |
| 43 void VirtualDeviceRelpied(IPC::Message* reply, |
| 44 const base::FileDescriptor& device) { |
| 45 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 46 BrowserNativePixmapManagerMsg_SyncGetVirtualDevice::WriteReplyParams( |
| 47 reply, device); |
| 48 Send(reply); |
| 49 } |
| 50 |
| 51 BrowserNativePixmapManager* manager_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(BrowserNativePixmapManagerMessageFilter); |
| 54 }; |
| 55 |
| 56 void DupVirtualDeviceFd( |
| 57 int device_fd, |
| 58 const BrowserNativePixmapManager::FileDescriptorCallback& callback) { |
| 59 int duped_fd = -1; |
| 60 base::ThreadRestrictions::AssertIOAllowed(); |
| 61 duped_fd = HANDLE_EINTR(dup(device_fd)); |
| 62 if (duped_fd < 0) { |
| 63 PLOG(ERROR) << "dup() failed."; |
| 64 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 65 base::Bind(callback, base::FileDescriptor())); |
| 66 return; |
| 67 } |
| 68 |
| 69 BrowserThread::PostTask( |
| 70 BrowserThread::IO, FROM_HERE, |
| 71 base::Bind(callback, base::FileDescriptor(duped_fd, true))); |
| 72 } |
| 73 |
| 74 } // namespace |
| 75 |
| 76 struct BrowserNativePixmapManagerTrait |
| 77 : public DefaultSingletonTraits<BrowserNativePixmapManager> { |
| 78 static BrowserNativePixmapManager* New() { |
| 79 BrowserNativePixmapManager* manager = new BrowserNativePixmapManager(); |
| 80 manager->Initialize(ui::OzonePlatform::GetInstance()->GetVirtualDeviceFd()); |
| 81 return manager; |
| 82 } |
| 83 }; |
| 84 |
| 85 // static |
| 86 BrowserNativePixmapManager* BrowserNativePixmapManager::GetInstance() { |
| 87 return Singleton<BrowserNativePixmapManager, |
| 88 BrowserNativePixmapManagerTrait>::get(); |
| 89 } |
| 90 |
| 91 BrowserNativePixmapManager::BrowserNativePixmapManager() { |
| 92 } |
| 93 |
| 94 BrowserNativePixmapManager::~BrowserNativePixmapManager() { |
| 95 } |
| 96 |
| 97 void BrowserNativePixmapManager::Initialize( |
| 98 const base::FileDescriptor& device_fd) { |
| 99 pixmap_manager_ = ui::NativePixmapManager::Create(device_fd); |
| 100 device_fd_ = device_fd; |
| 101 } |
| 102 |
| 103 std::vector<ui::NativePixmapManager::Configuration> |
| 104 BrowserNativePixmapManager::GetSupportedNativePixmapConfigurations() const { |
| 105 return pixmap_manager_->GetSupportedNativePixmapConfigurations(); |
| 106 } |
| 107 |
| 108 void BrowserNativePixmapManager::GetVirtualDeviceForChildProcess( |
| 109 const FileDescriptorCallback& callback) { |
| 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 111 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
| 112 pool->PostWorkerTask( |
| 113 FROM_HERE, base::Bind(&DupVirtualDeviceFd, device_fd_.fd, callback)); |
| 114 } |
| 115 |
| 116 BrowserMessageFilter* BrowserNativePixmapManager::CreateMessageFilter() { |
| 117 return new BrowserNativePixmapManagerMessageFilter(this); |
| 118 } |
| 119 |
| 120 } // namespace content |
OLD | NEW |