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