Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: ui/ozone/platform/drm/common/client_native_pixmap_dmabuf.cc

Issue 2711933002: Rename ClientNativePixmapFactoryGbm to ClientNativePixmapFactoryDmabuf amd move to ui/gfx (Closed)
Patch Set: Rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/ozone/platform/drm/common/client_native_pixmap_dmabuf.h"
6
7 #include <fcntl.h>
8 #include <linux/version.h>
9 #include <stddef.h>
10 #include <sys/mman.h>
11 #include <xf86drm.h>
12
13 #include "base/debug/crash_logging.h"
14 #include "base/memory/ptr_util.h"
15 #include "base/process/memory.h"
16 #include "base/process/process_metrics.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/trace_event/trace_event.h"
19
20 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
21 #include <linux/types.h>
22
23 struct local_dma_buf_sync {
24 __u64 flags;
25 };
26
27 #define LOCAL_DMA_BUF_SYNC_READ (1 << 0)
28 #define LOCAL_DMA_BUF_SYNC_WRITE (2 << 0)
29 #define LOCAL_DMA_BUF_SYNC_RW \
30 (LOCAL_DMA_BUF_SYNC_READ | LOCAL_DMA_BUF_SYNC_WRITE)
31 #define LOCAL_DMA_BUF_SYNC_START (0 << 2)
32 #define LOCAL_DMA_BUF_SYNC_END (1 << 2)
33
34 #define LOCAL_DMA_BUF_BASE 'b'
35 #define LOCAL_DMA_BUF_IOCTL_SYNC \
36 _IOW(LOCAL_DMA_BUF_BASE, 0, struct local_dma_buf_sync)
37
38 #else
39 #include <linux/dma-buf.h>
40 #endif
41
42 namespace ui {
43
44 namespace {
45
46 void PrimeSyncStart(int dmabuf_fd) {
47 struct local_dma_buf_sync sync_start = {0};
48
49 sync_start.flags = LOCAL_DMA_BUF_SYNC_START | LOCAL_DMA_BUF_SYNC_RW;
50 if (drmIoctl(dmabuf_fd, LOCAL_DMA_BUF_IOCTL_SYNC, &sync_start))
51 PLOG(ERROR) << "Failed DMA_BUF_SYNC_START";
52 }
53
54 void PrimeSyncEnd(int dmabuf_fd) {
55 struct local_dma_buf_sync sync_end = {0};
56
57 sync_end.flags = LOCAL_DMA_BUF_SYNC_END | LOCAL_DMA_BUF_SYNC_RW;
58 if (drmIoctl(dmabuf_fd, LOCAL_DMA_BUF_IOCTL_SYNC, &sync_end))
59 PLOG(ERROR) << "Failed DMA_BUF_SYNC_END";
60 }
61
62 } // namespace
63
64 // static
65 std::unique_ptr<ClientNativePixmap> ClientNativePixmapDmaBuf::ImportFromDmabuf(
66 const gfx::NativePixmapHandle& handle,
67 const gfx::Size& size) {
68 return base::WrapUnique(new ClientNativePixmapDmaBuf(handle, size));
69 }
70
71 ClientNativePixmapDmaBuf::ClientNativePixmapDmaBuf(
72 const gfx::NativePixmapHandle& handle,
73 const gfx::Size& size)
74 : pixmap_handle_(handle), size_(size), data_{0} {
75 TRACE_EVENT0("drm", "ClientNativePixmapDmaBuf");
76 // TODO(dcastagna): support multiple fds.
77 DCHECK_EQ(1u, handle.fds.size());
78 DCHECK_GE(handle.fds.front().fd, 0);
79 dmabuf_fd_.reset(handle.fds.front().fd);
80
81 DCHECK_GE(handle.planes.back().size, 0u);
82 size_t map_size = handle.planes.back().offset + handle.planes.back().size;
83 data_ = mmap(nullptr, map_size, (PROT_READ | PROT_WRITE), MAP_SHARED,
84 dmabuf_fd_.get(), 0);
85 if (data_ == MAP_FAILED) {
86 // TODO(dcastagna): Remove the following diagnostic information and the
87 // associated crash keys once crbug.com/629521 is fixed.
88 logging::SystemErrorCode mmap_error = logging::GetLastSystemErrorCode();
89 bool fd_valid = fcntl(dmabuf_fd_.get(), F_GETFD) != -1 ||
90 logging::GetLastSystemErrorCode() != EBADF;
91 std::string mmap_params = base::StringPrintf(
92 "(addr=nullptr, length=%zu, prot=(PROT_READ | PROT_WRITE), "
93 "flags=MAP_SHARED, fd=%d[valid=%d], offset=0)",
94 map_size, dmabuf_fd_.get(), fd_valid);
95 std::string errno_str = logging::SystemErrorCodeToString(mmap_error);
96 std::unique_ptr<base::ProcessMetrics> process_metrics(
97 base::ProcessMetrics::CreateCurrentProcessMetrics());
98 std::string number_of_fds =
99 base::StringPrintf("%d", process_metrics->GetOpenFdCount());
100 base::debug::ScopedCrashKey params_crash_key("mmap_params", mmap_params);
101 base::debug::ScopedCrashKey size_crash_key("buffer_size", size.ToString());
102 base::debug::ScopedCrashKey errno_crash_key("errno", errno_str);
103 base::debug::ScopedCrashKey number_of_fds_crash_key("number_of_fds",
104 number_of_fds);
105 LOG(ERROR) << "Failed to mmap dmabuf; mmap_params: " << mmap_params
106 << ", buffer_size: (" << size.ToString()
107 << "), errno: " << errno_str
108 << " , number_of_fds: " << number_of_fds;
109 if (mmap_error == ENOMEM)
110 base::TerminateBecauseOutOfMemory(map_size);
111 CHECK(false) << "Failed to mmap dmabuf.";
112 }
113 }
114
115 ClientNativePixmapDmaBuf::~ClientNativePixmapDmaBuf() {
116 TRACE_EVENT0("drm", "~ClientNativePixmapDmaBuf");
117 size_t map_size =
118 pixmap_handle_.planes.back().offset + pixmap_handle_.planes.back().size;
119 int ret = munmap(data_, map_size);
120 DCHECK(!ret);
121 }
122
123 bool ClientNativePixmapDmaBuf::Map() {
124 TRACE_EVENT0("drm", "DmaBuf:Map");
125 if (data_ != nullptr) {
126 PrimeSyncStart(dmabuf_fd_.get());
127 return true;
128 }
129 return false;
130 }
131
132 void ClientNativePixmapDmaBuf::Unmap() {
133 TRACE_EVENT0("drm", "DmaBuf:Unmap");
134 PrimeSyncEnd(dmabuf_fd_.get());
135 }
136
137 void* ClientNativePixmapDmaBuf::GetMemoryAddress(size_t plane) const {
138 DCHECK_LT(plane, pixmap_handle_.planes.size());
139 uint8_t* address = reinterpret_cast<uint8_t*>(data_);
140 return address + pixmap_handle_.planes[plane].offset;
141 }
142
143 int ClientNativePixmapDmaBuf::GetStride(size_t plane) const {
144 DCHECK_LT(plane, pixmap_handle_.planes.size());
145 return pixmap_handle_.planes[plane].stride;
146 }
147
148 } // namespace ui
OLDNEW
« ui/gfx/BUILD.gn ('K') | « ui/ozone/platform/drm/common/client_native_pixmap_dmabuf.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698