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 const gfx::NativePixmapHandle& handle, | 61 int dmabuf_fd, |
62 const gfx::Size& size) { | 62 const gfx::Size& size, |
63 return base::WrapUnique(new ClientNativePixmapDmaBuf(handle, size)); | 63 int stride) { |
| 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())); |
64 } | 71 } |
65 | 72 |
66 ClientNativePixmapDmaBuf::ClientNativePixmapDmaBuf( | 73 ClientNativePixmapDmaBuf::ClientNativePixmapDmaBuf(int dmabuf_fd, |
67 const gfx::NativePixmapHandle& handle, | 74 const gfx::Size& size, |
68 const gfx::Size& size) | 75 int stride, |
69 : pixmap_handle_(handle), size_(size), data_{0} { | 76 size_t map_size) |
| 77 : dmabuf_fd_(dmabuf_fd), map_size_(map_size), size_(size), stride_(stride) { |
70 TRACE_EVENT0("drm", "ClientNativePixmapDmaBuf"); | 78 TRACE_EVENT0("drm", "ClientNativePixmapDmaBuf"); |
71 // TODO(dcastagna): support multiple fds. | 79 data_ = mmap(nullptr, map_size_, (PROT_READ | PROT_WRITE), MAP_SHARED, |
72 DCHECK_EQ(1u, handle.fds.size()); | 80 dmabuf_fd, 0); |
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); | |
80 if (data_ == MAP_FAILED) { | 81 if (data_ == MAP_FAILED) { |
81 PLOG(ERROR) << "Failed mmap()."; | 82 PLOG(ERROR) << "Failed mmap()."; |
82 base::TerminateBecauseOutOfMemory(map_size); | 83 base::TerminateBecauseOutOfMemory(map_size_); |
83 } | 84 } |
84 } | 85 } |
85 | 86 |
86 ClientNativePixmapDmaBuf::~ClientNativePixmapDmaBuf() { | 87 ClientNativePixmapDmaBuf::~ClientNativePixmapDmaBuf() { |
87 TRACE_EVENT0("drm", "~ClientNativePixmapDmaBuf"); | 88 TRACE_EVENT0("drm", "~ClientNativePixmapDmaBuf"); |
88 size_t map_size = | 89 int ret = munmap(data_, map_size_); |
89 pixmap_handle_.planes.back().offset + pixmap_handle_.planes.back().size; | |
90 int ret = munmap(data_, map_size); | |
91 DCHECK(!ret); | 90 DCHECK(!ret); |
92 } | 91 } |
93 | 92 |
94 bool ClientNativePixmapDmaBuf::Map() { | 93 void* ClientNativePixmapDmaBuf::Map() { |
95 TRACE_EVENT0("drm", "DmaBuf:Map"); | 94 TRACE_EVENT0("drm", "DmaBuf:Map"); |
96 if (data_ != nullptr) { | 95 PrimeSyncStart(dmabuf_fd_.get()); |
97 PrimeSyncStart(dmabuf_fd_.get()); | 96 return data_; |
98 return true; | |
99 } | |
100 return false; | |
101 } | 97 } |
102 | 98 |
103 void ClientNativePixmapDmaBuf::Unmap() { | 99 void ClientNativePixmapDmaBuf::Unmap() { |
104 TRACE_EVENT0("drm", "DmaBuf:Unmap"); | 100 TRACE_EVENT0("drm", "DmaBuf:Unmap"); |
105 PrimeSyncEnd(dmabuf_fd_.get()); | 101 PrimeSyncEnd(dmabuf_fd_.get()); |
106 } | 102 } |
107 | 103 |
108 void* ClientNativePixmapDmaBuf::GetMemoryAddress(size_t plane) const { | 104 void ClientNativePixmapDmaBuf::GetStride(int* stride) const { |
109 DCHECK_LT(plane, pixmap_handle_.planes.size()); | 105 *stride = stride_; |
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; | |
117 } | 106 } |
118 | 107 |
119 } // namespace ui | 108 } // namespace ui |
OLD | NEW |