OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/vgem_pixmap.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <sys/mman.h> |
| 9 #include <xf86drm.h> |
| 10 |
| 11 #include "base/trace_event/trace_event.h" |
| 12 #include "ui/ozone/public/surface_factory_ozone.h" |
| 13 |
| 14 namespace ui { |
| 15 |
| 16 namespace { |
| 17 |
| 18 size_t StrideInBytes(size_t width, SurfaceFactoryOzone::BufferFormat format) { |
| 19 switch (format) { |
| 20 case SurfaceFactoryOzone::BGRA_8888: |
| 21 case SurfaceFactoryOzone::RGBX_8888: |
| 22 return width * 4; |
| 23 case SurfaceFactoryOzone::UNKNOWN: |
| 24 NOTREACHED(); |
| 25 } |
| 26 NOTREACHED(); |
| 27 return 0; |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 VgemPixmap::VgemPixmap(base::FileDescriptor handle, |
| 33 base::FileDescriptor vgem_handle, |
| 34 gfx::Size size, |
| 35 SurfaceFactoryOzone::BufferFormat format, |
| 36 BufferUsage usage) |
| 37 : vgem_bo_handle_(0), |
| 38 vgem_fd_(vgem_handle), |
| 39 size_(size), |
| 40 format_(format), |
| 41 usage_(usage), |
| 42 stride_(StrideInBytes(size_.width(), format_)), |
| 43 mmap_ptr_(nullptr) { |
| 44 if (usage_ == MAP) { |
| 45 DCHECK(vgem_fd_.fd > 0); |
| 46 DCHECK(handle.fd > 0); |
| 47 dma_buf_.reset(handle.fd); |
| 48 } |
| 49 } |
| 50 |
| 51 bool VgemPixmap::Initialize() { |
| 52 if (usage_ == SCANOUT) |
| 53 return true; |
| 54 |
| 55 DCHECK(vgem_fd_.fd); |
| 56 int ret = drmPrimeFDToHandle(vgem_fd_.fd, dma_buf_.get(), &vgem_bo_handle_); |
| 57 if (ret) { |
| 58 LOG(ERROR) << "drmPrimeFDToHandle failed, handle:" << vgem_bo_handle_; |
| 59 return false; |
| 60 } |
| 61 return true; |
| 62 } |
| 63 |
| 64 VgemPixmap::~VgemPixmap() { |
| 65 if (vgem_bo_handle_) { |
| 66 DCHECK_EQ(usage_, MAP); |
| 67 struct drm_mode_destroy_dumb destroy; |
| 68 memset(&destroy, 0, sizeof(destroy)); |
| 69 destroy.handle = vgem_bo_handle_; |
| 70 int ret = drmIoctl(vgem_fd_.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy); |
| 71 if (!ret) |
| 72 LOG(ERROR) << "fail to free a vgem buffer. error:" << ret; |
| 73 vgem_bo_handle_ = 0; |
| 74 } |
| 75 } |
| 76 |
| 77 void* VgemPixmap::GetEGLClientBuffer() { |
| 78 return nullptr; |
| 79 } |
| 80 |
| 81 int VgemPixmap::GetDmaBufFd() { |
| 82 return dma_buf_.get(); |
| 83 } |
| 84 |
| 85 int VgemPixmap::GetDmaBufPitch() { |
| 86 return stride_; |
| 87 } |
| 88 |
| 89 NativePixmap::BufferUsage VgemPixmap::GetBufferUsage() const { |
| 90 return usage_; |
| 91 } |
| 92 |
| 93 void* VgemPixmap::Map() { |
| 94 TRACE_EVENT0("gpu", "VgemPixmap::Map"); |
| 95 DCHECK(!mmap_ptr_); |
| 96 DCHECK(!vgem_bo_handle_); |
| 97 DCHECK_EQ(usage_, MAP); |
| 98 |
| 99 struct drm_mode_map_dumb mmap_arg; |
| 100 memset(&mmap_arg, 0, sizeof(mmap_arg)); |
| 101 mmap_arg.handle = vgem_bo_handle_; |
| 102 |
| 103 int ret = drmIoctl(vgem_fd_.fd, DRM_IOCTL_MODE_MAP_DUMB, &mmap_arg); |
| 104 if (ret) { |
| 105 LOG(ERROR) << "fail to map a vgem buffer. error:" << ret; |
| 106 return nullptr; |
| 107 } |
| 108 DCHECK(mmap_arg.offset); |
| 109 |
| 110 size_t size = stride_ * size_.height(); |
| 111 mmap_ptr_ = mmap(nullptr, size, (PROT_READ | PROT_WRITE), MAP_SHARED, |
| 112 vgem_fd_.fd, mmap_arg.offset); |
| 113 DCHECK(mmap_ptr_ != MAP_FAILED); |
| 114 return mmap_ptr_; |
| 115 } |
| 116 |
| 117 void VgemPixmap::Unmap() { |
| 118 TRACE_EVENT0("gpu", "VgemPixmap::Unmap"); |
| 119 DCHECK(mmap_ptr_); |
| 120 DCHECK(!vgem_bo_handle_); |
| 121 DCHECK_EQ(usage_, MAP); |
| 122 |
| 123 size_t size = stride_ * size_.height(); |
| 124 int ret = munmap(mmap_ptr_, size); |
| 125 DCHECK(!ret); |
| 126 mmap_ptr_ = nullptr; |
| 127 } |
| 128 |
| 129 } // namespace ui |
OLD | NEW |