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 void* ClientNativePixmapVgem::Map() { |
| 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 } |
| 64 DCHECK(mmap_arg.offset); |
| 65 |
| 66 data_ = mmap(nullptr, size, (PROT_READ | PROT_WRITE), MAP_SHARED, vgem_fd_, |
| 67 mmap_arg.offset); |
| 68 DCHECK_NE(data_, MAP_FAILED); |
| 69 return data_; |
| 70 } |
| 71 |
| 72 void ClientNativePixmapVgem::Unmap() { |
| 73 TRACE_EVENT0("gpu", "ClientNativePixmapVgem::Unmap"); |
| 74 DCHECK(data_); |
| 75 |
| 76 // TODO(dshwang): keep it consistently mapped, and unmap it in dtor. |
| 77 size_t size = stride_ * size_.height(); |
| 78 int ret = munmap(data_, size); |
| 79 DCHECK(!ret) << "fail to munmap a vgem buffer."; |
| 80 data_ = nullptr; |
| 81 } |
| 82 |
| 83 void ClientNativePixmapVgem::GetStride(int* stride) const { |
| 84 *stride = stride_; |
| 85 } |
| 86 |
| 87 } // namespace ui |
OLD | NEW |