| 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> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "ui/ozone/platform/drm/gpu/gbm_device.h" | 21 #include "ui/ozone/platform/drm/gpu/gbm_device.h" |
| 22 #include "ui/ozone/platform/drm/gpu/gbm_surface_factory.h" | 22 #include "ui/ozone/platform/drm/gpu/gbm_surface_factory.h" |
| 23 #include "ui/ozone/platform/drm/gpu/gbm_surfaceless.h" | 23 #include "ui/ozone/platform/drm/gpu/gbm_surfaceless.h" |
| 24 #include "ui/ozone/public/ozone_platform.h" | 24 #include "ui/ozone/public/ozone_platform.h" |
| 25 #include "ui/ozone/public/surface_factory_ozone.h" | 25 #include "ui/ozone/public/surface_factory_ozone.h" |
| 26 | 26 |
| 27 namespace ui { | 27 namespace ui { |
| 28 | 28 |
| 29 GbmBuffer::GbmBuffer(const scoped_refptr<GbmDevice>& gbm, | 29 GbmBuffer::GbmBuffer(const scoped_refptr<GbmDevice>& gbm, |
| 30 gbm_bo* bo, | 30 gbm_bo* bo, |
| 31 gfx::BufferFormat format, | 31 uint32_t format, |
| 32 gfx::BufferUsage usage, | 32 uint32_t flags, |
| 33 std::vector<base::ScopedFD>&& fds, | 33 std::vector<base::ScopedFD>&& fds, |
| 34 const gfx::Size& size, | 34 const gfx::Size& size, |
| 35 const std::vector<gfx::NativePixmapPlane>&& planes) | 35 const std::vector<gfx::NativePixmapPlane>&& planes) |
| 36 : GbmBufferBase(gbm, bo, format, usage), | 36 : GbmBufferBase(gbm, bo, format, flags), |
| 37 format_(format), | 37 format_(format), |
| 38 usage_(usage), | 38 flags_(flags), |
| 39 fds_(std::move(fds)), | 39 fds_(std::move(fds)), |
| 40 size_(size), | 40 size_(size), |
| 41 planes_(std::move(planes)) {} | 41 planes_(std::move(planes)) {} |
| 42 | 42 |
| 43 GbmBuffer::~GbmBuffer() { | 43 GbmBuffer::~GbmBuffer() { |
| 44 if (bo()) | 44 if (bo()) |
| 45 gbm_bo_destroy(bo()); | 45 gbm_bo_destroy(bo()); |
| 46 } | 46 } |
| 47 | 47 |
| 48 bool GbmBuffer::AreFdsValid() const { | 48 bool GbmBuffer::AreFdsValid() const { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 87 |
| 88 // TODO(reveman): This should not be needed once crbug.com/597932 is fixed, | 88 // TODO(reveman): This should not be needed once crbug.com/597932 is fixed, |
| 89 // as the size would be queried directly from the underlying bo. | 89 // as the size would be queried directly from the underlying bo. |
| 90 gfx::Size GbmBuffer::GetSize() const { | 90 gfx::Size GbmBuffer::GetSize() const { |
| 91 return size_; | 91 return size_; |
| 92 } | 92 } |
| 93 | 93 |
| 94 // static | 94 // static |
| 95 scoped_refptr<GbmBuffer> GbmBuffer::CreateBuffer( | 95 scoped_refptr<GbmBuffer> GbmBuffer::CreateBuffer( |
| 96 const scoped_refptr<GbmDevice>& gbm, | 96 const scoped_refptr<GbmDevice>& gbm, |
| 97 gfx::BufferFormat format, | 97 uint32_t format, |
| 98 const gfx::Size& size, | 98 const gfx::Size& size, |
| 99 gfx::BufferUsage usage) { | 99 uint32_t flags) { |
| 100 TRACE_EVENT2("drm", "GbmBuffer::CreateBuffer", "device", | 100 TRACE_EVENT2("drm", "GbmBuffer::CreateBuffer", "device", |
| 101 gbm->device_path().value(), "size", size.ToString()); | 101 gbm->device_path().value(), "size", size.ToString()); |
| 102 | 102 |
| 103 unsigned flags = 0; | 103 gbm_bo* bo = |
| 104 switch (usage) { | 104 gbm_bo_create(gbm->device(), size.width(), size.height(), format, flags); |
| 105 case gfx::BufferUsage::GPU_READ: | |
| 106 break; | |
| 107 case gfx::BufferUsage::SCANOUT: | |
| 108 flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING; | |
| 109 break; | |
| 110 case gfx::BufferUsage::GPU_READ_CPU_READ_WRITE: | |
| 111 case gfx::BufferUsage::GPU_READ_CPU_READ_WRITE_PERSISTENT: | |
| 112 flags = GBM_BO_USE_LINEAR; | |
| 113 break; | |
| 114 } | |
| 115 | |
| 116 gbm_bo* bo = gbm_bo_create(gbm->device(), size.width(), size.height(), | |
| 117 GetFourCCFormatFromBufferFormat(format), flags); | |
| 118 if (!bo) | 105 if (!bo) |
| 119 return nullptr; | 106 return nullptr; |
| 120 | 107 |
| 121 std::vector<base::ScopedFD> fds; | 108 std::vector<base::ScopedFD> fds; |
| 122 std::vector<gfx::NativePixmapPlane> planes; | 109 std::vector<gfx::NativePixmapPlane> planes; |
| 123 | 110 |
| 124 DCHECK_EQ(gbm_bo_get_num_planes(bo), | 111 for (size_t i = 0; i < gbm_bo_get_num_planes(bo); ++i) { |
| 125 gfx::NumberOfPlanesForBufferFormat(format)); | |
| 126 for (size_t i = 0; i < gfx::NumberOfPlanesForBufferFormat(format); ++i) { | |
| 127 // The fd returned by gbm_bo_get_fd is not ref-counted and need to be | 112 // The fd returned by gbm_bo_get_fd is not ref-counted and need to be |
| 128 // kept open for the lifetime of the buffer. | 113 // kept open for the lifetime of the buffer. |
| 129 base::ScopedFD fd(gbm_bo_get_plane_fd(bo, i)); | 114 base::ScopedFD fd(gbm_bo_get_plane_fd(bo, i)); |
| 130 | 115 |
| 131 // TODO(dcastagna): support multiple fds. | 116 // TODO(dcastagna): support multiple fds. |
| 132 // crbug.com/642410 | 117 // crbug.com/642410 |
| 133 if (!i) { | 118 if (!i) { |
| 134 if (!fd.is_valid()) { | 119 if (!fd.is_valid()) { |
| 135 PLOG(ERROR) << "Failed to export buffer to dma_buf"; | 120 PLOG(ERROR) << "Failed to export buffer to dma_buf"; |
| 136 gbm_bo_destroy(bo); | 121 gbm_bo_destroy(bo); |
| 137 return nullptr; | 122 return nullptr; |
| 138 } | 123 } |
| 139 fds.emplace_back(std::move(fd)); | 124 fds.emplace_back(std::move(fd)); |
| 140 } | 125 } |
| 141 | 126 |
| 142 planes.emplace_back( | 127 planes.emplace_back( |
| 143 gbm_bo_get_plane_stride(bo, i), gbm_bo_get_plane_offset(bo, i), | 128 gbm_bo_get_plane_stride(bo, i), gbm_bo_get_plane_offset(bo, i), |
| 144 gbm_bo_get_plane_size(bo, i), gbm_bo_get_plane_format_modifier(bo, i)); | 129 gbm_bo_get_plane_size(bo, i), gbm_bo_get_plane_format_modifier(bo, i)); |
| 145 } | 130 } |
| 146 scoped_refptr<GbmBuffer> buffer(new GbmBuffer( | 131 scoped_refptr<GbmBuffer> buffer(new GbmBuffer( |
| 147 gbm, bo, format, usage, std::move(fds), size, std::move(planes))); | 132 gbm, bo, format, flags, std::move(fds), size, std::move(planes))); |
| 148 if (usage == gfx::BufferUsage::SCANOUT && !buffer->GetFramebufferId()) | 133 if (flags & GBM_BO_USE_SCANOUT && !buffer->GetFramebufferId()) |
| 149 return nullptr; | 134 return nullptr; |
| 150 | 135 |
| 151 return buffer; | 136 return buffer; |
| 152 } | 137 } |
| 153 | 138 |
| 154 // static | 139 // static |
| 155 scoped_refptr<GbmBuffer> GbmBuffer::CreateBufferFromFds( | 140 scoped_refptr<GbmBuffer> GbmBuffer::CreateBufferFromFds( |
| 156 const scoped_refptr<GbmDevice>& gbm, | 141 const scoped_refptr<GbmDevice>& gbm, |
| 157 gfx::BufferFormat format, | 142 uint32_t format, |
| 158 const gfx::Size& size, | 143 const gfx::Size& size, |
| 159 std::vector<base::ScopedFD>&& fds, | 144 std::vector<base::ScopedFD>&& fds, |
| 160 const std::vector<gfx::NativePixmapPlane>& planes) { | 145 const std::vector<gfx::NativePixmapPlane>& planes) { |
| 161 TRACE_EVENT2("drm", "GbmBuffer::CreateBufferFromFD", "device", | 146 TRACE_EVENT2("drm", "GbmBuffer::CreateBufferFromFD", "device", |
| 162 gbm->device_path().value(), "size", size.ToString()); | 147 gbm->device_path().value(), "size", size.ToString()); |
| 163 DCHECK_LE(fds.size(), planes.size()); | 148 DCHECK_LE(fds.size(), planes.size()); |
| 164 DCHECK_EQ(planes[0].offset, 0); | 149 DCHECK_EQ(planes[0].offset, 0); |
| 165 | 150 |
| 166 uint32_t fourcc_format = GetFourCCFormatFromBufferFormat(format); | |
| 167 | |
| 168 // Use scanout if supported. | 151 // Use scanout if supported. |
| 169 bool use_scanout = gbm_device_is_format_supported( | 152 bool use_scanout = |
| 170 gbm->device(), fourcc_format, | 153 gbm_device_is_format_supported( |
| 171 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING) && | 154 gbm->device(), format, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING) && |
| 172 (planes.size() == 1); | 155 (planes.size() == 1); |
| 173 | 156 |
| 174 gbm_bo* bo = nullptr; | 157 gbm_bo* bo = nullptr; |
| 175 if (use_scanout) { | 158 if (use_scanout) { |
| 176 struct gbm_import_fd_data fd_data; | 159 struct gbm_import_fd_data fd_data; |
| 177 fd_data.fd = fds[0].get(); | 160 fd_data.fd = fds[0].get(); |
| 178 fd_data.width = size.width(); | 161 fd_data.width = size.width(); |
| 179 fd_data.height = size.height(); | 162 fd_data.height = size.height(); |
| 180 fd_data.stride = planes[0].stride; | 163 fd_data.stride = planes[0].stride; |
| 181 fd_data.format = fourcc_format; | 164 fd_data.format = format; |
| 182 | 165 |
| 183 // The fd passed to gbm_bo_import is not ref-counted and need to be | 166 // The fd passed to gbm_bo_import is not ref-counted and need to be |
| 184 // kept open for the lifetime of the buffer. | 167 // kept open for the lifetime of the buffer. |
| 185 bo = gbm_bo_import(gbm->device(), GBM_BO_IMPORT_FD, &fd_data, | 168 bo = gbm_bo_import(gbm->device(), GBM_BO_IMPORT_FD, &fd_data, |
| 186 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); | 169 GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); |
| 187 if (!bo) { | 170 if (!bo) { |
| 188 LOG(ERROR) << "nullptr returned from gbm_bo_import"; | 171 LOG(ERROR) << "nullptr returned from gbm_bo_import"; |
| 189 return nullptr; | 172 return nullptr; |
| 190 } | 173 } |
| 191 } | 174 } |
| 192 | 175 |
| 176 uint32_t flags = GBM_BO_USE_RENDERING; |
| 177 if (use_scanout) |
| 178 flags |= GBM_BO_USE_SCANOUT; |
| 193 scoped_refptr<GbmBuffer> buffer(new GbmBuffer( | 179 scoped_refptr<GbmBuffer> buffer(new GbmBuffer( |
| 194 gbm, bo, format, | 180 gbm, bo, format, flags, std::move(fds), size, std::move(planes))); |
| 195 use_scanout ? gfx::BufferUsage::SCANOUT : gfx::BufferUsage::GPU_READ, | |
| 196 std::move(fds), size, std::move(planes))); | |
| 197 // If scanout support for buffer is expected then make sure we managed to | 181 // If scanout support for buffer is expected then make sure we managed to |
| 198 // create a framebuffer for it as otherwise using it for scanout will fail. | 182 // create a framebuffer for it as otherwise using it for scanout will fail. |
| 199 if (use_scanout && !buffer->GetFramebufferId()) | 183 if (use_scanout && !buffer->GetFramebufferId()) |
| 200 return nullptr; | 184 return nullptr; |
| 201 | 185 |
| 202 return buffer; | 186 return buffer; |
| 203 } | 187 } |
| 204 | 188 |
| 205 GbmPixmap::GbmPixmap(GbmSurfaceFactory* surface_manager, | 189 GbmPixmap::GbmPixmap(GbmSurfaceFactory* surface_manager, |
| 206 const scoped_refptr<GbmBuffer>& buffer) | 190 const scoped_refptr<GbmBuffer>& buffer) |
| 207 : surface_manager_(surface_manager), buffer_(buffer) {} | 191 : surface_manager_(surface_manager), buffer_(buffer) {} |
| 208 | 192 |
| 209 void GbmPixmap::SetProcessingCallback( | 193 void GbmPixmap::SetProcessingCallback( |
| 210 const ProcessingCallback& processing_callback) { | 194 const ProcessingCallback& processing_callback) { |
| 211 DCHECK(processing_callback_.is_null()); | 195 DCHECK(processing_callback_.is_null()); |
| 212 processing_callback_ = processing_callback; | 196 processing_callback_ = processing_callback; |
| 213 } | 197 } |
| 214 | 198 |
| 215 gfx::NativePixmapHandle GbmPixmap::ExportHandle() { | 199 gfx::NativePixmapHandle GbmPixmap::ExportHandle() { |
| 216 gfx::NativePixmapHandle handle; | 200 gfx::NativePixmapHandle handle; |
| 217 for (size_t i = 0; | 201 gfx::BufferFormat format = |
| 218 i < gfx::NumberOfPlanesForBufferFormat(buffer_->GetFormat()); ++i) { | 202 ui::GetBufferFormatFromFourCCFormat(buffer_->GetFormat()); |
| 203 // TODO(dcastagna): Use gbm_bo_get_num_planes once all the formats we use are |
| 204 // supported by gbm. |
| 205 for (size_t i = 0; i < gfx::NumberOfPlanesForBufferFormat(format); ++i) { |
| 219 // Some formats (e.g: YVU_420) might have less than one fd per plane. | 206 // Some formats (e.g: YVU_420) might have less than one fd per plane. |
| 220 if (i < buffer_->GetFdCount()) { | 207 if (i < buffer_->GetFdCount()) { |
| 221 base::ScopedFD scoped_fd(HANDLE_EINTR(dup(buffer_->GetFd(i)))); | 208 base::ScopedFD scoped_fd(HANDLE_EINTR(dup(buffer_->GetFd(i)))); |
| 222 if (!scoped_fd.is_valid()) { | 209 if (!scoped_fd.is_valid()) { |
| 223 PLOG(ERROR) << "dup"; | 210 PLOG(ERROR) << "dup"; |
| 224 return gfx::NativePixmapHandle(); | 211 return gfx::NativePixmapHandle(); |
| 225 } | 212 } |
| 226 handle.fds.emplace_back( | 213 handle.fds.emplace_back( |
| 227 base::FileDescriptor(scoped_fd.release(), true /* auto_close */)); | 214 base::FileDescriptor(scoped_fd.release(), true /* auto_close */)); |
| 228 } | 215 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 258 | 245 |
| 259 int GbmPixmap::GetDmaBufOffset(size_t plane) const { | 246 int GbmPixmap::GetDmaBufOffset(size_t plane) const { |
| 260 return buffer_->GetOffset(plane); | 247 return buffer_->GetOffset(plane); |
| 261 } | 248 } |
| 262 | 249 |
| 263 uint64_t GbmPixmap::GetDmaBufModifier(size_t plane) const { | 250 uint64_t GbmPixmap::GetDmaBufModifier(size_t plane) const { |
| 264 return buffer_->GetFormatModifier(plane); | 251 return buffer_->GetFormatModifier(plane); |
| 265 } | 252 } |
| 266 | 253 |
| 267 gfx::BufferFormat GbmPixmap::GetBufferFormat() const { | 254 gfx::BufferFormat GbmPixmap::GetBufferFormat() const { |
| 268 return buffer_->GetFormat(); | 255 return ui::GetBufferFormatFromFourCCFormat(buffer_->GetFormat()); |
| 269 } | 256 } |
| 270 | 257 |
| 271 gfx::Size GbmPixmap::GetBufferSize() const { | 258 gfx::Size GbmPixmap::GetBufferSize() const { |
| 272 return buffer_->GetSize(); | 259 return buffer_->GetSize(); |
| 273 } | 260 } |
| 274 | 261 |
| 275 bool GbmPixmap::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 262 bool GbmPixmap::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
| 276 int plane_z_order, | 263 int plane_z_order, |
| 277 gfx::OverlayTransform plane_transform, | 264 gfx::OverlayTransform plane_transform, |
| 278 const gfx::Rect& display_bounds, | 265 const gfx::Rect& display_bounds, |
| 279 const gfx::RectF& crop_rect) { | 266 const gfx::RectF& crop_rect) { |
| 280 DCHECK(buffer_->GetUsage() == gfx::BufferUsage::SCANOUT); | 267 DCHECK(buffer_->GetFlags() & GBM_BO_USE_SCANOUT); |
| 281 OverlayPlane::ProcessBufferCallback processing_callback; | 268 OverlayPlane::ProcessBufferCallback processing_callback; |
| 282 if (!processing_callback_.is_null()) | 269 if (!processing_callback_.is_null()) |
| 283 processing_callback = base::Bind(&GbmPixmap::ProcessBuffer, this); | 270 processing_callback = base::Bind(&GbmPixmap::ProcessBuffer, this); |
| 284 | 271 |
| 285 surface_manager_->GetSurface(widget)->QueueOverlayPlane( | 272 surface_manager_->GetSurface(widget)->QueueOverlayPlane( |
| 286 OverlayPlane(buffer_, plane_z_order, plane_transform, display_bounds, | 273 OverlayPlane(buffer_, plane_z_order, plane_transform, display_bounds, |
| 287 crop_rect, processing_callback)); | 274 crop_rect, processing_callback)); |
| 288 | 275 |
| 289 return true; | 276 return true; |
| 290 } | 277 } |
| 291 | 278 |
| 292 scoped_refptr<ScanoutBuffer> GbmPixmap::ProcessBuffer(const gfx::Size& size, | 279 scoped_refptr<ScanoutBuffer> GbmPixmap::ProcessBuffer(const gfx::Size& size, |
| 293 uint32_t format) { | 280 uint32_t format) { |
| 294 DCHECK(GetBufferSize() != size || | 281 DCHECK(GetBufferSize() != size || |
| 295 buffer_->GetFramebufferPixelFormat() != format); | 282 buffer_->GetFramebufferPixelFormat() != format); |
| 296 | 283 |
| 297 if (!processed_pixmap_ || size != processed_pixmap_->GetBufferSize() || | 284 if (!processed_pixmap_ || size != processed_pixmap_->GetBufferSize() || |
| 298 format != processed_pixmap_->buffer()->GetFramebufferPixelFormat()) { | 285 format != processed_pixmap_->buffer()->GetFramebufferPixelFormat()) { |
| 299 // Release any old processed pixmap. | 286 // Release any old processed pixmap. |
| 300 processed_pixmap_ = nullptr; | 287 processed_pixmap_ = nullptr; |
| 301 gfx::BufferFormat buffer_format = GetBufferFormatFromFourCCFormat(format); | |
| 302 | |
| 303 scoped_refptr<GbmBuffer> buffer = GbmBuffer::CreateBuffer( | 288 scoped_refptr<GbmBuffer> buffer = GbmBuffer::CreateBuffer( |
| 304 buffer_->drm().get(), buffer_format, size, buffer_->GetUsage()); | 289 buffer_->drm().get(), format, size, buffer_->GetFlags()); |
| 305 if (!buffer) | 290 if (!buffer) |
| 306 return nullptr; | 291 return nullptr; |
| 307 | 292 |
| 308 // ProcessBuffer is called on DrmThread. We could have used | 293 // ProcessBuffer is called on DrmThread. We could have used |
| 309 // CreateNativePixmap to initialize the pixmap, however it posts a | 294 // CreateNativePixmap to initialize the pixmap, however it posts a |
| 310 // synchronous task to DrmThread resulting in a deadlock. | 295 // synchronous task to DrmThread resulting in a deadlock. |
| 311 processed_pixmap_ = new GbmPixmap(surface_manager_, buffer); | 296 processed_pixmap_ = new GbmPixmap(surface_manager_, buffer); |
| 312 } | 297 } |
| 313 | 298 |
| 314 DCHECK(!processing_callback_.is_null()); | 299 DCHECK(!processing_callback_.is_null()); |
| 315 if (!processing_callback_.Run(this, processed_pixmap_)) { | 300 if (!processing_callback_.Run(this, processed_pixmap_)) { |
| 316 LOG(ERROR) << "Failed processing NativePixmap"; | 301 LOG(ERROR) << "Failed processing NativePixmap"; |
| 317 return nullptr; | 302 return nullptr; |
| 318 } | 303 } |
| 319 | 304 |
| 320 return processed_pixmap_->buffer(); | 305 return processed_pixmap_->buffer(); |
| 321 } | 306 } |
| 322 | 307 |
| 323 } // namespace ui | 308 } // namespace ui |
| OLD | NEW |