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

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: Addressed sadrul's remark 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
« no previous file with comments | « ui/ozone/platform/drm/common/client_native_pixmap_dmabuf.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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<gfx::ClientNativePixmap>
66 ClientNativePixmapDmaBuf::ImportFromDmabuf(
67 const gfx::NativePixmapHandle& handle,
68 const gfx::Size& size) {
69 return base::WrapUnique(new ClientNativePixmapDmaBuf(handle, size));
70 }
71
72 ClientNativePixmapDmaBuf::ClientNativePixmapDmaBuf(
73 const gfx::NativePixmapHandle& handle,
74 const gfx::Size& size)
75 : pixmap_handle_(handle), size_(size), data_{0} {
76 TRACE_EVENT0("drm", "ClientNativePixmapDmaBuf");
77 // TODO(dcastagna): support multiple fds.
78 DCHECK_EQ(1u, handle.fds.size());
79 DCHECK_GE(handle.fds.front().fd, 0);
80 dmabuf_fd_.reset(handle.fds.front().fd);
81
82 DCHECK_GE(handle.planes.back().size, 0u);
83 size_t map_size = handle.planes.back().offset + handle.planes.back().size;
84 data_ = mmap(nullptr, map_size, (PROT_READ | PROT_WRITE), MAP_SHARED,
85 dmabuf_fd_.get(), 0);
86 if (data_ == MAP_FAILED) {
87 logging::SystemErrorCode mmap_error = logging::GetLastSystemErrorCode();
88 if (mmap_error == ENOMEM)
89 base::TerminateBecauseOutOfMemory(map_size);
90
91 // TODO(dcastagna): Remove the following diagnostic information and the
92 // associated crash keys once crbug.com/629521 is fixed.
93 bool fd_valid = fcntl(dmabuf_fd_.get(), F_GETFD) != -1 ||
94 logging::GetLastSystemErrorCode() != EBADF;
95 std::string mmap_params = base::StringPrintf(
96 "(addr=nullptr, length=%zu, prot=(PROT_READ | PROT_WRITE), "
97 "flags=MAP_SHARED, fd=%d[valid=%d], offset=0)",
98 map_size, dmabuf_fd_.get(), fd_valid);
99 std::string errno_str = logging::SystemErrorCodeToString(mmap_error);
100 std::unique_ptr<base::ProcessMetrics> process_metrics(
101 base::ProcessMetrics::CreateCurrentProcessMetrics());
102 std::string number_of_fds =
103 base::StringPrintf("%d", process_metrics->GetOpenFdCount());
104 base::debug::ScopedCrashKey params_crash_key("mmap_params", mmap_params);
105 base::debug::ScopedCrashKey size_crash_key("buffer_size", size.ToString());
106 base::debug::ScopedCrashKey errno_crash_key("errno", errno_str);
107 base::debug::ScopedCrashKey number_of_fds_crash_key("number_of_fds",
108 number_of_fds);
109 LOG(ERROR) << "Failed to mmap dmabuf; mmap_params: " << mmap_params
110 << ", buffer_size: (" << size.ToString()
111 << "), errno: " << errno_str
112 << " , number_of_fds: " << number_of_fds;
113 CHECK(false) << "Failed to mmap dmabuf.";
114 }
115 }
116
117 ClientNativePixmapDmaBuf::~ClientNativePixmapDmaBuf() {
118 TRACE_EVENT0("drm", "~ClientNativePixmapDmaBuf");
119 size_t map_size =
120 pixmap_handle_.planes.back().offset + pixmap_handle_.planes.back().size;
121 int ret = munmap(data_, map_size);
122 DCHECK(!ret);
123 }
124
125 bool ClientNativePixmapDmaBuf::Map() {
126 TRACE_EVENT0("drm", "DmaBuf:Map");
127 if (data_ != nullptr) {
128 PrimeSyncStart(dmabuf_fd_.get());
129 return true;
130 }
131 return false;
132 }
133
134 void ClientNativePixmapDmaBuf::Unmap() {
135 TRACE_EVENT0("drm", "DmaBuf:Unmap");
136 PrimeSyncEnd(dmabuf_fd_.get());
137 }
138
139 void* ClientNativePixmapDmaBuf::GetMemoryAddress(size_t plane) const {
140 DCHECK_LT(plane, pixmap_handle_.planes.size());
141 uint8_t* address = reinterpret_cast<uint8_t*>(data_);
142 return address + pixmap_handle_.planes[plane].offset;
143 }
144
145 int ClientNativePixmapDmaBuf::GetStride(size_t plane) const {
146 DCHECK_LT(plane, pixmap_handle_.planes.size());
147 return pixmap_handle_.planes[plane].stride;
148 }
149
150 } // namespace ui
OLDNEW
« no previous file with comments | « 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