OLD | NEW |
| (Empty) |
1 // Copyright 2015 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_vgem.h" | |
6 | |
7 #include <fcntl.h> | |
8 #include <libdrm/vgem_drm.h> | |
9 #include <stddef.h> | |
10 #include <sys/mman.h> | |
11 #include <xf86drm.h> | |
12 | |
13 #include "base/process/memory.h" | |
14 #include "base/trace_event/trace_event.h" | |
15 | |
16 namespace ui { | |
17 | |
18 // static | |
19 scoped_ptr<ClientNativePixmap> ClientNativePixmapVgem::ImportFromDmabuf( | |
20 int vgem_fd, | |
21 int dmabuf_fd, | |
22 const gfx::Size& size, | |
23 int stride) { | |
24 DCHECK_GE(vgem_fd, 0); | |
25 DCHECK_GE(dmabuf_fd, 0); | |
26 uint32_t vgem_bo_handle = 0; | |
27 int ret = drmPrimeFDToHandle(vgem_fd, dmabuf_fd, &vgem_bo_handle); | |
28 DCHECK(!ret) << "drmPrimeFDToHandle failed."; | |
29 return make_scoped_ptr( | |
30 new ClientNativePixmapVgem(vgem_fd, vgem_bo_handle, size, stride)); | |
31 } | |
32 | |
33 ClientNativePixmapVgem::ClientNativePixmapVgem(int vgem_fd, | |
34 uint32_t vgem_bo_handle, | |
35 const gfx::Size& size, | |
36 int stride) | |
37 : vgem_fd_(vgem_fd), | |
38 vgem_bo_handle_(vgem_bo_handle), | |
39 size_(size), | |
40 stride_(stride), | |
41 data_(nullptr) { | |
42 DCHECK(vgem_bo_handle_); | |
43 struct drm_mode_map_dumb mmap_arg = {0}; | |
44 mmap_arg.handle = vgem_bo_handle_; | |
45 size_t map_size = stride_ * size_.height(); | |
46 int ret = drmIoctl(vgem_fd_, DRM_IOCTL_VGEM_MODE_MAP_DUMB, &mmap_arg); | |
47 if (ret) { | |
48 PLOG(ERROR) << "fail to map a vgem buffer."; | |
49 base::TerminateBecauseOutOfMemory(map_size); | |
50 } | |
51 DCHECK(mmap_arg.offset); | |
52 | |
53 data_ = mmap(nullptr, map_size, (PROT_READ | PROT_WRITE), MAP_SHARED, | |
54 vgem_fd_, mmap_arg.offset); | |
55 DCHECK_NE(data_, MAP_FAILED); | |
56 } | |
57 | |
58 ClientNativePixmapVgem::~ClientNativePixmapVgem() { | |
59 size_t size = stride_ * size_.height(); | |
60 int ret = munmap(data_, size); | |
61 DCHECK(!ret) << "fail to munmap a vgem buffer."; | |
62 | |
63 struct drm_gem_close close = {0}; | |
64 close.handle = vgem_bo_handle_; | |
65 ret = drmIoctl(vgem_fd_, DRM_IOCTL_GEM_CLOSE, &close); | |
66 DCHECK(!ret) << "fail to free a vgem buffer."; | |
67 } | |
68 | |
69 void* ClientNativePixmapVgem::Map() { | |
70 return data_; | |
71 } | |
72 | |
73 void ClientNativePixmapVgem::Unmap() {} | |
74 | |
75 void ClientNativePixmapVgem::GetStride(int* stride) const { | |
76 *stride = stride_; | |
77 } | |
78 | |
79 } // namespace ui | |
OLD | NEW |