| 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 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/posix/eintr_wrapper.h" | 14 #include "base/posix/eintr_wrapper.h" |
| 15 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
| 16 #include "ui/gfx/geometry/size_conversions.h" | 16 #include "ui/gfx/geometry/size_conversions.h" |
| 17 #include "ui/gfx/native_pixmap_handle_ozone.h" | 17 #include "ui/gfx/native_pixmap_handle_ozone.h" |
| 18 #include "ui/ozone/platform/drm/common/drm_util.h" | 18 #include "ui/ozone/platform/drm/common/drm_util.h" |
| 19 #include "ui/ozone/platform/drm/gpu/drm_window.h" | 19 #include "ui/ozone/platform/drm/gpu/drm_window.h" |
| 20 #include "ui/ozone/platform/drm/gpu/gbm_device.h" | 20 #include "ui/ozone/platform/drm/gpu/gbm_device.h" |
| 21 #include "ui/ozone/platform/drm/gpu/gbm_surface_factory.h" | 21 #include "ui/ozone/platform/drm/gpu/gbm_surface_factory.h" |
| 22 #include "ui/ozone/platform/drm/gpu/gbm_surfaceless.h" | 22 #include "ui/ozone/platform/drm/gpu/gbm_surfaceless.h" |
| 23 #include "ui/ozone/public/ozone_platform.h" // nogncheck | 23 #include "ui/ozone/public/ozone_platform.h" // nogncheck |
| 24 #include "ui/ozone/public/surface_factory_ozone.h" | 24 #include "ui/ozone/public/surface_factory_ozone.h" |
| 25 | 25 |
| 26 namespace { | |
| 27 // Optimal format for rendering on overlay. | |
| 28 const gfx::BufferFormat kOverlayRenderFormat = gfx::BufferFormat::UYVY_422; | |
| 29 } // namespace | |
| 30 | |
| 31 namespace ui { | 26 namespace ui { |
| 32 | 27 |
| 33 GbmBuffer::GbmBuffer(const scoped_refptr<GbmDevice>& gbm, | 28 GbmBuffer::GbmBuffer(const scoped_refptr<GbmDevice>& gbm, |
| 34 gbm_bo* bo, | 29 gbm_bo* bo, |
| 35 gfx::BufferFormat format, | 30 gfx::BufferFormat format, |
| 36 gfx::BufferUsage usage) | 31 gfx::BufferUsage usage) |
| 37 : GbmBufferBase(gbm, bo, format, usage), format_(format), usage_(usage) {} | 32 : GbmBufferBase(gbm, bo, format, usage), format_(format), usage_(usage) {} |
| 38 | 33 |
| 39 GbmBuffer::~GbmBuffer() { | 34 GbmBuffer::~GbmBuffer() { |
| 40 if (bo()) | 35 if (bo()) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 PLOG(ERROR) << "Failed to export buffer to dma_buf"; | 78 PLOG(ERROR) << "Failed to export buffer to dma_buf"; |
| 84 return false; | 79 return false; |
| 85 } | 80 } |
| 86 Initialize(std::move(dma_buf), gbm_bo_get_stride(buffer->bo())); | 81 Initialize(std::move(dma_buf), gbm_bo_get_stride(buffer->bo())); |
| 87 buffer_ = buffer; | 82 buffer_ = buffer; |
| 88 return true; | 83 return true; |
| 89 } | 84 } |
| 90 | 85 |
| 91 void GbmPixmap::SetProcessingCallback( | 86 void GbmPixmap::SetProcessingCallback( |
| 92 const ProcessingCallback& processing_callback) { | 87 const ProcessingCallback& processing_callback) { |
| 88 DCHECK(processing_callback_.is_null()); |
| 93 processing_callback_ = processing_callback; | 89 processing_callback_ = processing_callback; |
| 94 } | 90 } |
| 95 | 91 |
| 96 scoped_refptr<NativePixmap> GbmPixmap::GetProcessedPixmap( | 92 scoped_refptr<NativePixmap> GbmPixmap::GetProcessedPixmap( |
| 97 gfx::Size target_size, | 93 gfx::Size target_size, |
| 98 gfx::BufferFormat target_format) { | 94 gfx::BufferFormat target_format) { |
| 99 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance(); | 95 return nullptr; |
| 100 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone(); | |
| 101 // Create a buffer from Ozone. | |
| 102 scoped_refptr<ui::NativePixmap> processed_pixmap = | |
| 103 factory->CreateNativePixmap(gfx::kNullAcceleratedWidget, target_size, | |
| 104 target_format, gfx::BufferUsage::SCANOUT); | |
| 105 if (!processed_pixmap) { | |
| 106 LOG(ERROR) << "Failed creating an Ozone NativePixmap for processing"; | |
| 107 return nullptr; | |
| 108 } | |
| 109 if (!processing_callback_.Run(this, processed_pixmap)) { | |
| 110 LOG(ERROR) << "Failed processing NativePixmap"; | |
| 111 return nullptr; | |
| 112 } | |
| 113 return processed_pixmap; | |
| 114 } | 96 } |
| 115 | 97 |
| 116 gfx::NativePixmapHandle GbmPixmap::ExportHandle() { | 98 gfx::NativePixmapHandle GbmPixmap::ExportHandle() { |
| 117 gfx::NativePixmapHandle handle; | 99 gfx::NativePixmapHandle handle; |
| 118 | 100 |
| 119 base::ScopedFD dmabuf_fd(HANDLE_EINTR(dup(dma_buf_.get()))); | 101 base::ScopedFD dmabuf_fd(HANDLE_EINTR(dup(dma_buf_.get()))); |
| 120 if (!dmabuf_fd.is_valid()) { | 102 if (!dmabuf_fd.is_valid()) { |
| 121 PLOG(ERROR) << "dup"; | 103 PLOG(ERROR) << "dup"; |
| 122 return handle; | 104 return handle; |
| 123 } | 105 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 148 | 130 |
| 149 gfx::Size GbmPixmap::GetBufferSize() const { | 131 gfx::Size GbmPixmap::GetBufferSize() const { |
| 150 return buffer_->GetSize(); | 132 return buffer_->GetSize(); |
| 151 } | 133 } |
| 152 | 134 |
| 153 bool GbmPixmap::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 135 bool GbmPixmap::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
| 154 int plane_z_order, | 136 int plane_z_order, |
| 155 gfx::OverlayTransform plane_transform, | 137 gfx::OverlayTransform plane_transform, |
| 156 const gfx::Rect& display_bounds, | 138 const gfx::Rect& display_bounds, |
| 157 const gfx::RectF& crop_rect) { | 139 const gfx::RectF& crop_rect) { |
| 158 gfx::Size target_size; | |
| 159 gfx::BufferFormat target_format; | |
| 160 if (plane_z_order && ShouldApplyProcessing(display_bounds, crop_rect, | |
| 161 &target_size, &target_format)) { | |
| 162 scoped_refptr<NativePixmap> processed_pixmap = | |
| 163 GetProcessedPixmap(target_size, target_format); | |
| 164 if (processed_pixmap) { | |
| 165 return processed_pixmap->ScheduleOverlayPlane( | |
| 166 widget, plane_z_order, plane_transform, display_bounds, crop_rect); | |
| 167 } else { | |
| 168 return false; | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 // TODO(reveman): Add support for imported buffers. crbug.com/541558 | 140 // TODO(reveman): Add support for imported buffers. crbug.com/541558 |
| 173 if (!buffer_) { | 141 if (!buffer_) { |
| 174 PLOG(ERROR) << "ScheduleOverlayPlane requires a buffer."; | 142 PLOG(ERROR) << "ScheduleOverlayPlane requires a buffer."; |
| 175 return false; | 143 return false; |
| 176 } | 144 } |
| 177 | 145 |
| 178 DCHECK(buffer_->GetUsage() == gfx::BufferUsage::SCANOUT); | 146 DCHECK(buffer_->GetUsage() == gfx::BufferUsage::SCANOUT); |
| 179 surface_manager_->GetSurface(widget)->QueueOverlayPlane(OverlayPlane( | 147 surface_manager_->GetSurface(widget)->QueueOverlayPlane( |
| 180 buffer_, plane_z_order, plane_transform, display_bounds, crop_rect)); | 148 OverlayPlane(buffer_, plane_z_order, plane_transform, display_bounds, |
| 149 crop_rect, base::Bind(&GbmPixmap::ProcessBuffer, this))); |
| 150 |
| 181 return true; | 151 return true; |
| 182 } | 152 } |
| 183 | 153 |
| 184 bool GbmPixmap::ShouldApplyProcessing(const gfx::Rect& display_bounds, | 154 scoped_refptr<ScanoutBuffer> GbmPixmap::ProcessBuffer(const gfx::Size& size, |
| 185 const gfx::RectF& crop_rect, | 155 uint32_t format) { |
| 186 gfx::Size* target_size, | 156 DCHECK(GetBufferSize() != size || |
| 187 gfx::BufferFormat* target_format) { | 157 buffer_->GetFramebufferPixelFormat() != format); |
| 188 if (crop_rect.width() == 0 || crop_rect.height() == 0) { | 158 |
| 189 PLOG(ERROR) << "ShouldApplyProcessing passed zero processing target."; | 159 if (!processed_pixmap_ || size != processed_pixmap_->GetBufferSize() || |
| 190 return false; | 160 format != processed_pixmap_->buffer()->GetFramebufferPixelFormat()) { |
| 161 // Release any old processed pixmap. |
| 162 processed_pixmap_ = nullptr; |
| 163 gfx::BufferFormat buffer_format = GetBufferFormatFromFourCCFormat(format); |
| 164 |
| 165 scoped_refptr<GbmBuffer> buffer = GbmBuffer::CreateBuffer( |
| 166 buffer_->drm().get(), buffer_format, size, buffer_->GetUsage()); |
| 167 |
| 168 // ProcessBuffer is called on DrmThread. We could have used |
| 169 // CreateNativePixmap to initialize the pixmap, however it posts a |
| 170 // synchronous task to DrmThread resulting in a deadlock. |
| 171 processed_pixmap_ = new GbmPixmap(surface_manager_); |
| 172 if (!processed_pixmap_->InitializeFromBuffer(buffer)) |
| 173 return nullptr; |
| 191 } | 174 } |
| 192 | 175 |
| 193 if (!buffer_) { | 176 DCHECK(!processing_callback_.is_null()); |
| 194 PLOG(ERROR) << "ShouldApplyProcessing requires a buffer."; | 177 if (!processing_callback_.Run(this, processed_pixmap_)) { |
| 195 return false; | 178 LOG(ERROR) << "Failed processing NativePixmap"; |
| 179 return nullptr; |
| 196 } | 180 } |
| 197 | 181 |
| 198 // TODO(william.xie): Figure out the optimal render format for overlay. | 182 return processed_pixmap_->buffer(); |
| 199 // See http://crbug.com/553264. | |
| 200 *target_format = kOverlayRenderFormat; | |
| 201 gfx::Size pixmap_size = buffer_->GetSize(); | |
| 202 // If the required size is not integer-sized, round it to the next integer. | |
| 203 *target_size = gfx::ToCeiledSize( | |
| 204 gfx::SizeF(display_bounds.width() / crop_rect.width(), | |
| 205 display_bounds.height() / crop_rect.height())); | |
| 206 | |
| 207 return pixmap_size != *target_size || GetBufferFormat() != *target_format; | |
| 208 } | 183 } |
| 209 | 184 |
| 210 } // namespace ui | 185 } // namespace ui |
| OLD | NEW |