Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/common/client_native_pixmap_dmabuf.h" | 5 #include "ui/ozone/platform/drm/common/client_native_pixmap_dmabuf.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
| 10 #include <xf86drm.h> | 10 #include <xf86drm.h> |
| 11 | 11 |
| 12 #include "base/process/memory.h" | 12 #include "base/process/memory.h" |
| 13 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
| 14 | 14 |
| 15 #if defined(OS_CHROMEOS) | |
| 16 // TODO(vignatti): replace the local definitions below with #include | |
| 17 // <linux/dma-buf.h> once kernel version 4.6 becomes widely used. | |
| 18 #include <linux/types.h> | |
| 19 | |
| 20 struct local_dma_buf_sync { | |
| 21 __u64 flags; | |
| 22 }; | |
| 23 | |
| 24 #define LOCAL_DMA_BUF_SYNC_READ (1 << 0) | |
| 25 #define LOCAL_DMA_BUF_SYNC_WRITE (2 << 0) | |
| 26 #define LOCAL_DMA_BUF_SYNC_START (0 << 2) | |
| 27 #define LOCAL_DMA_BUF_SYNC_END (1 << 2) | |
| 28 | |
| 29 #define LOCAL_DMA_BUF_BASE 'b' | |
| 30 #define LOCAL_DMA_BUF_IOCTL_SYNC \ | |
| 31 _IOW(LOCAL_DMA_BUF_BASE, 0, struct local_dma_buf_sync) | |
| 32 #endif | |
| 33 | |
| 15 namespace ui { | 34 namespace ui { |
| 16 | 35 |
| 36 namespace { | |
| 37 | |
| 38 void PrimeSyncStart(int dmabuf_fd) { | |
| 39 struct local_dma_buf_sync sync_start = {0}; | |
| 40 | |
| 41 sync_start.flags = LOCAL_DMA_BUF_SYNC_START | LOCAL_DMA_BUF_SYNC_READ; | |
| 42 if (drmIoctl(dmabuf_fd, LOCAL_DMA_BUF_IOCTL_SYNC, &sync_start)) | |
| 43 PLOG(ERROR) << "Failed DMA_BUF_SYNC_START"; | |
| 44 } | |
| 45 | |
| 46 void PrimeSyncEnd(int dmabuf_fd) { | |
| 47 struct local_dma_buf_sync sync_end = {0}; | |
| 48 | |
| 49 sync_end.flags = LOCAL_DMA_BUF_SYNC_END | LOCAL_DMA_BUF_SYNC_WRITE; | |
| 50 if (drmIoctl(dmabuf_fd, LOCAL_DMA_BUF_IOCTL_SYNC, &sync_end)) | |
| 51 PLOG(ERROR) << "Failed DMA_BUF_SYNC_END"; | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 17 // static | 56 // static |
| 18 scoped_ptr<ClientNativePixmap> ClientNativePixmapDmaBuf::ImportFromDmabuf( | 57 scoped_ptr<ClientNativePixmap> ClientNativePixmapDmaBuf::ImportFromDmabuf( |
| 19 int dmabuf_fd, | 58 int dmabuf_fd, |
| 20 const gfx::Size& size, | 59 const gfx::Size& size, |
| 21 int stride) { | 60 int stride) { |
| 22 DCHECK_GE(dmabuf_fd, 0); | 61 DCHECK_GE(dmabuf_fd, 0); |
| 23 return make_scoped_ptr(new ClientNativePixmapDmaBuf(dmabuf_fd, size, stride)); | 62 return make_scoped_ptr(new ClientNativePixmapDmaBuf(dmabuf_fd, size, stride)); |
| 24 } | 63 } |
| 25 | 64 |
| 26 ClientNativePixmapDmaBuf::ClientNativePixmapDmaBuf(int dmabuf_fd, | 65 ClientNativePixmapDmaBuf::ClientNativePixmapDmaBuf(int dmabuf_fd, |
| 27 const gfx::Size& size, | 66 const gfx::Size& size, |
| 28 int stride) | 67 int stride) |
| 29 : size_(size), stride_(stride) { | 68 : dmabuf_fd_(dmabuf_fd), size_(size), stride_(stride) { |
|
rickyz (no longer on Chrome)
2016/04/06 02:04:48
Out of curiosity, where/how does this fd get expos
vignatti (out of this project)
2016/04/06 13:52:46
yes. The GPU process is the privileged process her
| |
| 30 TRACE_EVENT0("drm", "ClientNativePixmapDmaBuf"); | 69 TRACE_EVENT0("drm", "ClientNativePixmapDmaBuf"); |
| 31 size_t map_size = stride_ * size_.height(); | 70 size_t map_size = stride_ * size_.height(); |
| 32 data_ = mmap(nullptr, map_size, (PROT_READ | PROT_WRITE), MAP_SHARED, | 71 data_ = mmap(nullptr, map_size, (PROT_READ | PROT_WRITE), MAP_SHARED, |
| 33 dmabuf_fd, 0); | 72 dmabuf_fd, 0); |
| 34 CHECK_NE(data_, MAP_FAILED); | 73 CHECK_NE(data_, MAP_FAILED); |
| 35 } | 74 } |
| 36 | 75 |
| 37 ClientNativePixmapDmaBuf::~ClientNativePixmapDmaBuf() { | 76 ClientNativePixmapDmaBuf::~ClientNativePixmapDmaBuf() { |
| 38 TRACE_EVENT0("drm", "~ClientNativePixmapDmaBuf"); | 77 TRACE_EVENT0("drm", "~ClientNativePixmapDmaBuf"); |
| 39 size_t size = stride_ * size_.height(); | 78 size_t size = stride_ * size_.height(); |
| 40 int ret = munmap(data_, size); | 79 int ret = munmap(data_, size); |
| 41 DCHECK(!ret); | 80 DCHECK(!ret); |
| 42 } | 81 } |
| 43 | 82 |
| 44 void* ClientNativePixmapDmaBuf::Map() { | 83 void* ClientNativePixmapDmaBuf::Map() { |
| 84 TRACE_EVENT0("drm", "DmaBuf:Map"); | |
| 85 PrimeSyncStart(dmabuf_fd_.get()); | |
| 45 return data_; | 86 return data_; |
| 46 } | 87 } |
| 47 | 88 |
| 48 void ClientNativePixmapDmaBuf::Unmap() {} | 89 void ClientNativePixmapDmaBuf::Unmap() { |
| 90 TRACE_EVENT0("drm", "DmaBuf:Unmap"); | |
| 91 PrimeSyncEnd(dmabuf_fd_.get()); | |
| 92 } | |
| 49 | 93 |
| 50 void ClientNativePixmapDmaBuf::GetStride(int* stride) const { | 94 void ClientNativePixmapDmaBuf::GetStride(int* stride) const { |
| 51 *stride = stride_; | 95 *stride = stride_; |
| 52 } | 96 } |
| 53 | 97 |
| 54 } // namespace ui | 98 } // namespace ui |
| OLD | NEW |