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

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 and remove left over spotted by spang 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 logging::SystemErrorCode mmap_error = logging::GetLastSystemErrorCode();
87 if (mmap_error == ENOMEM)
88 base::TerminateBecauseOutOfMemory(map_size);
89
90 // TODO(dcastagna): Remove the following diagnostic information and the
91 // associated crash keys once crbug.com/629521 is fixed.
92 bool fd_valid = fcntl(dmabuf_fd_.get(), F_GETFD) != -1 ||
93 logging::GetLastSystemErrorCode() != EBADF;
94 std::string mmap_params = base::StringPrintf(
95 "(addr=nullptr, length=%zu, prot=(PROT_READ | PROT_WRITE), "
96 "flags=MAP_SHARED, fd=%d[valid=%d], offset=0)",
97 map_size, dmabuf_fd_.get(), fd_valid);
98 std::string errno_str = logging::SystemErrorCodeToString(mmap_error);
99 std::unique_ptr<base::ProcessMetrics> process_metrics(
100 base::ProcessMetrics::CreateCurrentProcessMetrics());
101 std::string number_of_fds =
102 base::StringPrintf("%d", process_metrics->GetOpenFdCount());
103 base::debug::ScopedCrashKey params_crash_key("mmap_params", mmap_params);
104 base::debug::ScopedCrashKey size_crash_key("buffer_size", size.ToString());
105 base::debug::ScopedCrashKey errno_crash_key("errno", errno_str);
106 base::debug::ScopedCrashKey number_of_fds_crash_key("number_of_fds",
107 number_of_fds);
108 LOG(ERROR) << "Failed to mmap dmabuf; mmap_params: " << mmap_params
109 << ", buffer_size: (" << size.ToString()
110 << "), errno: " << errno_str
111 << " , number_of_fds: " << number_of_fds;
112 CHECK(false) << "Failed to mmap dmabuf.";
113 }
114 }
115
116 ClientNativePixmapDmaBuf::~ClientNativePixmapDmaBuf() {
117 TRACE_EVENT0("drm", "~ClientNativePixmapDmaBuf");
118 size_t map_size =
119 pixmap_handle_.planes.back().offset + pixmap_handle_.planes.back().size;
120 int ret = munmap(data_, map_size);
121 DCHECK(!ret);
122 }
123
124 bool ClientNativePixmapDmaBuf::Map() {
125 TRACE_EVENT0("drm", "DmaBuf:Map");
126 if (data_ != nullptr) {
127 PrimeSyncStart(dmabuf_fd_.get());
128 return true;
129 }
130 return false;
131 }
132
133 void ClientNativePixmapDmaBuf::Unmap() {
134 TRACE_EVENT0("drm", "DmaBuf:Unmap");
135 PrimeSyncEnd(dmabuf_fd_.get());
136 }
137
138 void* ClientNativePixmapDmaBuf::GetMemoryAddress(size_t plane) const {
139 DCHECK_LT(plane, pixmap_handle_.planes.size());
140 uint8_t* address = reinterpret_cast<uint8_t*>(data_);
141 return address + pixmap_handle_.planes[plane].offset;
142 }
143
144 int ClientNativePixmapDmaBuf::GetStride(size_t plane) const {
145 DCHECK_LT(plane, pixmap_handle_.planes.size());
146 return pixmap_handle_.planes[plane].stride;
147 }
148
149 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698