OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/ozone/platform/drm/gpu/gbm_buffer.h" | 5 #include "ui/ozone/platform/drm/gpu/gbm_buffer.h" |
6 | 6 |
7 #include <drm.h> | 7 #include <drm.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <gbm.h> | 9 #include <gbm.h> |
10 #include <xf86drm.h> | 10 #include <xf86drm.h> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/posix/eintr_wrapper.h" |
13 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
| 15 #include "ui/gfx/gpu_memory_buffer.h" |
14 #include "ui/ozone/platform/drm/gpu/gbm_device.h" | 16 #include "ui/ozone/platform/drm/gpu/gbm_device.h" |
15 | 17 |
16 namespace ui { | 18 namespace ui { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 int GetGbmFormatFromBufferFormat(SurfaceFactoryOzone::BufferFormat fmt) { | 22 int GetGbmFormatFromBufferFormat(SurfaceFactoryOzone::BufferFormat fmt) { |
21 switch (fmt) { | 23 switch (fmt) { |
22 case SurfaceFactoryOzone::BGRA_8888: | 24 case SurfaceFactoryOzone::BGRA_8888: |
23 return GBM_BO_FORMAT_ARGB8888; | 25 return GBM_BO_FORMAT_ARGB8888; |
24 case SurfaceFactoryOzone::RGBX_8888: | 26 case SurfaceFactoryOzone::RGBX_8888: |
25 return GBM_BO_FORMAT_XRGB8888; | 27 return GBM_BO_FORMAT_XRGB8888; |
26 default: | 28 default: |
27 NOTREACHED(); | 29 NOTREACHED(); |
28 return 0; | 30 return 0; |
29 } | 31 } |
30 } | 32 } |
31 | 33 |
| 34 bool ShareToRenderProcess(int fd, base::FileDescriptor* new_handle) { |
| 35 int duped_handle = HANDLE_EINTR(dup(fd)); |
| 36 if (duped_handle < 0) { |
| 37 DPLOG(ERROR) << "dup() failed."; |
| 38 *new_handle = base::FileDescriptor(); |
| 39 return false; |
| 40 } |
| 41 |
| 42 *new_handle = base::FileDescriptor(duped_handle, true); |
| 43 return true; |
| 44 } |
| 45 |
| 46 unsigned long StrideInBytes(int width) { |
| 47 // Both BGRA_8888 and RGBX_8888 has 4 byte per pixel. |
| 48 return width * 4; |
| 49 } |
| 50 |
32 } // namespace | 51 } // namespace |
33 | 52 |
34 GbmBuffer::GbmBuffer(const scoped_refptr<GbmDevice>& gbm, | 53 GbmBuffer::GbmBuffer(const scoped_refptr<GbmDevice>& gbm, |
35 gbm_bo* bo, | 54 gbm_bo* bo, |
36 bool scanout) | 55 bool scanout) |
37 : GbmBufferBase(gbm, bo, scanout) { | 56 : GbmBufferBase(gbm, bo, scanout) { |
38 } | 57 } |
39 | 58 |
40 GbmBuffer::~GbmBuffer() { | 59 GbmBuffer::~GbmBuffer() { |
41 if (bo()) | 60 if (bo()) |
42 gbm_bo_destroy(bo()); | 61 gbm_bo_destroy(bo()); |
43 } | 62 } |
44 | 63 |
45 // static | 64 // static |
46 scoped_refptr<GbmBuffer> GbmBuffer::CreateBuffer( | 65 scoped_refptr<GbmBuffer> GbmBuffer::CreateBuffer( |
47 const scoped_refptr<GbmDevice>& gbm, | 66 const scoped_refptr<GbmDevice>& gbm, |
48 SurfaceFactoryOzone::BufferFormat format, | 67 SurfaceFactoryOzone::BufferFormat format, |
49 const gfx::Size& size, | 68 const gfx::Size& size, |
50 bool scanout) { | 69 bool scanout) { |
51 TRACE_EVENT2("drm", "GbmBuffer::CreateBuffer", "device", | 70 TRACE_EVENT2("drm", "GbmBuffer::CreateBuffer", "device", |
52 gbm->device_path().value(), "size", size.ToString()); | 71 gbm->device_path().value(), "size", size.ToString()); |
53 unsigned flags = GBM_BO_USE_RENDERING; | 72 unsigned flags = GBM_BO_USE_RENDERING; |
54 if (scanout) | 73 // SCANOUT allocates continuous memory, so better for performance even if |
55 flags |= GBM_BO_USE_SCANOUT; | 74 // |scanout| is false. |
| 75 flags |= GBM_BO_USE_SCANOUT; |
56 gbm_bo* bo = gbm_bo_create(gbm->device(), size.width(), size.height(), | 76 gbm_bo* bo = gbm_bo_create(gbm->device(), size.width(), size.height(), |
57 GetGbmFormatFromBufferFormat(format), flags); | 77 GetGbmFormatFromBufferFormat(format), flags); |
58 if (!bo) | 78 if (!bo) |
59 return NULL; | 79 return NULL; |
60 | 80 |
61 scoped_refptr<GbmBuffer> buffer(new GbmBuffer(gbm, bo, scanout)); | 81 scoped_refptr<GbmBuffer> buffer(new GbmBuffer(gbm, bo, scanout)); |
62 if (scanout && !buffer->GetFramebufferId()) | 82 if (scanout && !buffer->GetFramebufferId()) |
63 return NULL; | 83 return NULL; |
64 | 84 |
65 return buffer; | 85 return buffer; |
(...skipping 13 matching lines...) Expand all Loading... |
79 return false; | 99 return false; |
80 } | 100 } |
81 return true; | 101 return true; |
82 } | 102 } |
83 | 103 |
84 GbmPixmap::~GbmPixmap() { | 104 GbmPixmap::~GbmPixmap() { |
85 if (dma_buf_ > 0) | 105 if (dma_buf_ > 0) |
86 close(dma_buf_); | 106 close(dma_buf_); |
87 } | 107 } |
88 | 108 |
| 109 bool GbmPixmap::ExportDmaBuf(int vgem_fd, gfx::GpuMemoryBufferHandle* handle) { |
| 110 DCHECK(StrideInBytes(buffer_->GetSize().width()) == |
| 111 gbm_bo_get_stride(buffer_->bo())); |
| 112 |
| 113 base::FileDescriptor dup_handle; |
| 114 if (!ShareToRenderProcess(dma_buf_, &dup_handle)) { |
| 115 DLOG(ERROR) << "Fail to duplicate a DMA-BUF file descriptor"; |
| 116 return false; |
| 117 } |
| 118 |
| 119 if (vgem_fd < 0) { |
| 120 LOG(ERROR) << "This device doesn't support vGEM."; |
| 121 return false; |
| 122 } |
| 123 |
| 124 base::FileDescriptor dup_device_handle; |
| 125 if (!ShareToRenderProcess(vgem_fd, &dup_device_handle)) { |
| 126 base::ScopedFD fd(dup_handle.fd); |
| 127 DLOG(ERROR) << "Fail to duplicate a DRM file descriptor"; |
| 128 return false; |
| 129 } |
| 130 |
| 131 handle->handle = dup_handle; |
| 132 // TODO(dshwang): pass |device_handle| only one time for each render process. |
| 133 handle->device_handle = dup_device_handle; |
| 134 return true; |
| 135 } |
| 136 |
89 void* GbmPixmap::GetEGLClientBuffer() { | 137 void* GbmPixmap::GetEGLClientBuffer() { |
90 return nullptr; | 138 return nullptr; |
91 } | 139 } |
92 | 140 |
93 int GbmPixmap::GetDmaBufFd() { | 141 int GbmPixmap::GetDmaBufFd() { |
94 return dma_buf_; | 142 return dma_buf_; |
95 } | 143 } |
96 | 144 |
97 int GbmPixmap::GetDmaBufPitch() { | 145 int GbmPixmap::GetDmaBufPitch() { |
98 return gbm_bo_get_stride(buffer_->bo()); | 146 return gbm_bo_get_stride(buffer_->bo()); |
99 } | 147 } |
100 | 148 |
| 149 NativePixmap::BufferUsage GbmPixmap::GetBufferUsage() const { |
| 150 return buffer_->GetFramebufferId() ? SCANOUT : MAP; |
| 151 } |
| 152 |
101 } // namespace ui | 153 } // namespace ui |
OLD | NEW |