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