| 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> |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 sync_end.flags = LOCAL_DMA_BUF_SYNC_END | LOCAL_DMA_BUF_SYNC_RW; | 52 sync_end.flags = LOCAL_DMA_BUF_SYNC_END | LOCAL_DMA_BUF_SYNC_RW; |
| 53 if (drmIoctl(dmabuf_fd, LOCAL_DMA_BUF_IOCTL_SYNC, &sync_end)) | 53 if (drmIoctl(dmabuf_fd, LOCAL_DMA_BUF_IOCTL_SYNC, &sync_end)) |
| 54 PLOG(ERROR) << "Failed DMA_BUF_SYNC_END"; | 54 PLOG(ERROR) << "Failed DMA_BUF_SYNC_END"; |
| 55 } | 55 } |
| 56 | 56 |
| 57 } // namespace | 57 } // namespace |
| 58 | 58 |
| 59 // static | 59 // static |
| 60 std::unique_ptr<ClientNativePixmap> ClientNativePixmapDmaBuf::ImportFromDmabuf( | 60 std::unique_ptr<ClientNativePixmap> ClientNativePixmapDmaBuf::ImportFromDmabuf( |
| 61 int dmabuf_fd, | 61 const gfx::NativePixmapHandle& handle, |
| 62 const gfx::Size& size, | 62 const gfx::Size& size) { |
| 63 int stride) { | 63 return base::WrapUnique(new ClientNativePixmapDmaBuf(handle, size)); |
| 64 DCHECK_GE(dmabuf_fd, 0); | |
| 65 base::CheckedNumeric<size_t> map_size = stride; | |
| 66 map_size *= size.height(); | |
| 67 if (!map_size.IsValid()) | |
| 68 return nullptr; | |
| 69 return base::WrapUnique(new ClientNativePixmapDmaBuf(dmabuf_fd, size, stride, | |
| 70 map_size.ValueOrDie())); | |
| 71 } | 64 } |
| 72 | 65 |
| 73 ClientNativePixmapDmaBuf::ClientNativePixmapDmaBuf(int dmabuf_fd, | 66 ClientNativePixmapDmaBuf::ClientNativePixmapDmaBuf( |
| 74 const gfx::Size& size, | 67 const gfx::NativePixmapHandle& handle, |
| 75 int stride, | 68 const gfx::Size& size) |
| 76 size_t map_size) | 69 : pixmap_handle_(handle), size_(size), data_{0} { |
| 77 : dmabuf_fd_(dmabuf_fd), map_size_(map_size), size_(size), stride_(stride) { | |
| 78 TRACE_EVENT0("drm", "ClientNativePixmapDmaBuf"); | 70 TRACE_EVENT0("drm", "ClientNativePixmapDmaBuf"); |
| 79 data_ = mmap(nullptr, map_size_, (PROT_READ | PROT_WRITE), MAP_SHARED, | 71 // TODO(dcastagna): support multiple fds. |
| 80 dmabuf_fd, 0); | 72 DCHECK_EQ(1u, handle.fds.size()); |
| 73 DCHECK_GE(handle.fds.front().fd, 0); |
| 74 dmabuf_fd_.reset(handle.fds.front().fd); |
| 75 |
| 76 DCHECK_GE(handle.planes.back().size, 0u); |
| 77 size_t map_size = handle.planes.back().offset + handle.planes.back().size; |
| 78 data_ = mmap(nullptr, map_size, (PROT_READ | PROT_WRITE), MAP_SHARED, |
| 79 dmabuf_fd_.get(), 0); |
| 81 if (data_ == MAP_FAILED) { | 80 if (data_ == MAP_FAILED) { |
| 82 PLOG(ERROR) << "Failed mmap()."; | 81 PLOG(ERROR) << "Failed mmap()."; |
| 83 base::TerminateBecauseOutOfMemory(map_size_); | 82 base::TerminateBecauseOutOfMemory(map_size); |
| 84 } | 83 } |
| 85 } | 84 } |
| 86 | 85 |
| 87 ClientNativePixmapDmaBuf::~ClientNativePixmapDmaBuf() { | 86 ClientNativePixmapDmaBuf::~ClientNativePixmapDmaBuf() { |
| 88 TRACE_EVENT0("drm", "~ClientNativePixmapDmaBuf"); | 87 TRACE_EVENT0("drm", "~ClientNativePixmapDmaBuf"); |
| 89 int ret = munmap(data_, map_size_); | 88 size_t map_size = |
| 89 pixmap_handle_.planes.back().offset + pixmap_handle_.planes.back().size; |
| 90 int ret = munmap(data_, map_size); |
| 90 DCHECK(!ret); | 91 DCHECK(!ret); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void* ClientNativePixmapDmaBuf::Map() { | 94 bool ClientNativePixmapDmaBuf::Map() { |
| 94 TRACE_EVENT0("drm", "DmaBuf:Map"); | 95 TRACE_EVENT0("drm", "DmaBuf:Map"); |
| 95 PrimeSyncStart(dmabuf_fd_.get()); | 96 if (data_ != nullptr) { |
| 96 return data_; | 97 PrimeSyncStart(dmabuf_fd_.get()); |
| 98 return true; |
| 99 } |
| 100 return false; |
| 97 } | 101 } |
| 98 | 102 |
| 99 void ClientNativePixmapDmaBuf::Unmap() { | 103 void ClientNativePixmapDmaBuf::Unmap() { |
| 100 TRACE_EVENT0("drm", "DmaBuf:Unmap"); | 104 TRACE_EVENT0("drm", "DmaBuf:Unmap"); |
| 101 PrimeSyncEnd(dmabuf_fd_.get()); | 105 PrimeSyncEnd(dmabuf_fd_.get()); |
| 102 } | 106 } |
| 103 | 107 |
| 104 void ClientNativePixmapDmaBuf::GetStride(int* stride) const { | 108 void* ClientNativePixmapDmaBuf::GetMemoryAddress(size_t plane) const { |
| 105 *stride = stride_; | 109 DCHECK_LT(plane, pixmap_handle_.planes.size()); |
| 110 uint8_t* address = reinterpret_cast<uint8_t*>(data_); |
| 111 return address + pixmap_handle_.planes[plane].offset; |
| 112 } |
| 113 |
| 114 int ClientNativePixmapDmaBuf::GetStride(size_t plane) const { |
| 115 DCHECK_LT(plane, pixmap_handle_.planes.size()); |
| 116 return pixmap_handle_.planes[plane].stride; |
| 106 } | 117 } |
| 107 | 118 |
| 108 } // namespace ui | 119 } // namespace ui |
| OLD | NEW |