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::ImportFromDmabuf( |
| 18 int vgem_fd, |
| 19 int dmabuf_fd, |
| 20 const gfx::Size& size, |
| 21 int stride) { |
| 22 DCHECK_GE(vgem_fd, 0); |
| 23 DCHECK_GE(dmabuf_fd, 0); |
| 24 uint32_t vgem_bo_handle = 0; |
| 25 int ret = drmPrimeFDToHandle(vgem_fd, dmabuf_fd, &vgem_bo_handle); |
| 26 if (ret) { |
| 27 PLOG(ERROR) << "drmPrimeFDToHandle failed, handle:" << vgem_bo_handle; |
| 28 return nullptr; |
| 29 } |
| 30 return make_scoped_ptr( |
| 31 new ClientNativePixmapVgem(vgem_fd, vgem_bo_handle, size, stride)); |
| 32 } |
| 33 |
| 34 ClientNativePixmapVgem::ClientNativePixmapVgem(int vgem_fd, |
| 35 uint32_t vgem_bo_handle, |
| 36 const gfx::Size& size, |
| 37 int stride) |
| 38 : vgem_fd_(vgem_fd), |
| 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_, DRM_IOCTL_GEM_CLOSE, &close); |
| 51 DCHECK(!ret) << "fail to free a vgem buffer."; |
| 52 } |
| 53 |
| 54 bool ClientNativePixmapVgem::Map(void** data) { |
| 55 TRACE_EVENT0("gpu", "ClientNativePixmapVgem::Map"); |
| 56 DCHECK(!data_); |
| 57 struct drm_mode_map_dumb mmap_arg = {0}; |
| 58 mmap_arg.handle = vgem_bo_handle_; |
| 59 |
| 60 int ret = drmIoctl(vgem_fd_, DRM_IOCTL_VGEM_MODE_MAP_DUMB, &mmap_arg); |
| 61 if (ret) { |
| 62 PLOG(ERROR) << "fail to map a vgem buffer."; |
| 63 return false; |
| 64 } |
| 65 DCHECK(mmap_arg.offset); |
| 66 |
| 67 size_t size = stride_ * size_.height(); |
| 68 data_ = mmap(nullptr, size, (PROT_READ | PROT_WRITE), MAP_SHARED, vgem_fd_, |
| 69 mmap_arg.offset); |
| 70 DCHECK_NE(data_, MAP_FAILED); |
| 71 *data = data_; |
| 72 return true; |
| 73 } |
| 74 |
| 75 void ClientNativePixmapVgem::Unmap() { |
| 76 TRACE_EVENT0("gpu", "ClientNativePixmapVgem::Unmap"); |
| 77 DCHECK(data_); |
| 78 |
| 79 // TODO(dshwang): keep it consistently mapped, and unmap it in dtor. |
| 80 size_t size = stride_ * size_.height(); |
| 81 int ret = munmap(data_, size); |
| 82 DCHECK(!ret) << "fail to munmap a vgem buffer."; |
| 83 data_ = nullptr; |
| 84 } |
| 85 |
| 86 void ClientNativePixmapVgem::GetStride(int* stride) const { |
| 87 *stride = stride_; |
| 88 } |
| 89 |
| 90 } // namespace ui |
OLD | NEW |