Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "media/mojo/common/mojo_video_frame.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 | |
| 10 namespace media { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 const VideoFrame::StorageType kMojoStorageType = | |
| 15 VideoFrame::STORAGE_MOJO_SHARED_BUFFER; | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 19 // static | |
| 20 scoped_refptr<MojoVideoFrame> MojoVideoFrame::Create( | |
| 21 const gfx::Size& dimensions, | |
|
jrummell
2016/02/25 21:51:48
Question: VideoFrame::CreateFrameInternal() rounds
jrummell
2016/03/01 00:41:05
Done.
| |
| 22 base::TimeDelta timestamp) { | |
| 23 const VideoPixelFormat format = PIXEL_FORMAT_I420; | |
| 24 const gfx::Rect visible_rect(dimensions); | |
| 25 if (!IsValidConfig(format, kMojoStorageType, dimensions, visible_rect, | |
|
xhwang
2016/02/29 18:28:05
Can you s/kMojoStorageType/STORAGE_MOJO_SHARED_BUF
jrummell
2016/03/01 00:41:05
Done.
| |
| 26 dimensions)) { | |
| 27 LOG(DFATAL) << __FUNCTION__ << " Invalid config. " | |
| 28 << ConfigToString(format, kMojoStorageType, dimensions, | |
| 29 visible_rect, dimensions); | |
| 30 return nullptr; | |
| 31 } | |
| 32 | |
| 33 // Allocate a shared memory buffer big enough to hold the desired frame. | |
| 34 const size_t requested_size = VideoFrame::AllocationSize(format, dimensions); | |
| 35 mojo::ScopedSharedBufferHandle handle; | |
| 36 const MojoResult result = | |
| 37 mojo::CreateSharedBuffer(nullptr, requested_size, &handle); | |
| 38 if (result != MOJO_RESULT_OK) | |
| 39 return nullptr; | |
| 40 | |
| 41 // Now allocate the frame and initialize it. | |
| 42 scoped_refptr<MojoVideoFrame> frame( | |
| 43 new MojoVideoFrame(format, dimensions, visible_rect, dimensions, | |
| 44 std::move(handle), requested_size, timestamp)); | |
| 45 if (!frame->Init(dimensions.width(), dimensions.width() / 2, | |
| 46 dimensions.width() / 2, 0 /* y_offset */, | |
| 47 dimensions.GetArea(), dimensions.GetArea() * 5 / 4)) | |
|
xhwang
2016/02/29 18:28:05
Do we handle odd width/height well here? If so, ad
jrummell
2016/03/01 00:41:05
Done.
| |
| 48 return nullptr; | |
| 49 | |
| 50 return frame; | |
| 51 } | |
| 52 | |
| 53 // static | |
| 54 scoped_refptr<MojoVideoFrame> MojoVideoFrame::Create( | |
| 55 VideoPixelFormat format, | |
| 56 const gfx::Size& coded_size, | |
| 57 const gfx::Rect& visible_rect, | |
| 58 const gfx::Size& natural_size, | |
| 59 mojo::SharedBufferHandle handle, | |
| 60 size_t data_size, | |
| 61 size_t y_offset, | |
| 62 size_t u_offset, | |
| 63 size_t v_offset, | |
| 64 int32_t y_stride, | |
| 65 int32_t u_stride, | |
| 66 int32_t v_stride, | |
| 67 base::TimeDelta timestamp) { | |
| 68 if (!IsValidConfig(format, kMojoStorageType, coded_size, visible_rect, | |
| 69 natural_size)) { | |
| 70 LOG(DFATAL) << __FUNCTION__ << " Invalid config. " | |
| 71 << ConfigToString(format, kMojoStorageType, coded_size, | |
| 72 visible_rect, natural_size); | |
| 73 return nullptr; | |
| 74 } | |
| 75 | |
| 76 // Make a copy of the shared memory handle. | |
| 77 mojo::ScopedSharedBufferHandle duplicated_handle; | |
| 78 const MojoResult result = | |
| 79 mojo::DuplicateBuffer(handle, nullptr, &duplicated_handle); | |
| 80 if (result != MOJO_RESULT_OK) | |
| 81 return nullptr; | |
| 82 | |
| 83 // Now allocate the frame and initialize it. | |
| 84 scoped_refptr<MojoVideoFrame> frame( | |
| 85 new MojoVideoFrame(format, coded_size, visible_rect, natural_size, | |
| 86 std::move(duplicated_handle), data_size, timestamp)); | |
| 87 if (!frame->Init(y_stride, u_stride, v_stride, y_offset, u_offset, v_offset)) | |
| 88 return nullptr; | |
| 89 | |
| 90 return frame; | |
| 91 } | |
| 92 | |
| 93 MojoVideoFrame::MojoVideoFrame(VideoPixelFormat format, | |
| 94 const gfx::Size& coded_size, | |
| 95 const gfx::Rect& visible_rect, | |
| 96 const gfx::Size& natural_size, | |
| 97 mojo::ScopedSharedBufferHandle handle, | |
| 98 size_t mapped_size, | |
| 99 base::TimeDelta timestamp) | |
| 100 : VideoFrame(format, | |
| 101 kMojoStorageType, | |
| 102 coded_size, | |
| 103 visible_rect, | |
| 104 natural_size, | |
| 105 timestamp), | |
| 106 shared_buffer_handle_(std::move(handle)), | |
| 107 shared_buffer_size_(mapped_size) { | |
| 108 DCHECK(shared_buffer_handle_.is_valid()); | |
| 109 } | |
| 110 | |
| 111 bool MojoVideoFrame::Init(int32_t y_stride, | |
| 112 int32_t u_stride, | |
| 113 int32_t v_stride, | |
| 114 size_t y_offset, | |
| 115 size_t u_offset, | |
| 116 size_t v_offset) { | |
| 117 uint8_t* frame_data = nullptr; | |
| 118 const MojoResult result = mojo::MapBuffer( | |
| 119 shared_buffer_handle_.get(), 0 /* offset */, shared_buffer_size_, | |
| 120 reinterpret_cast<void**>(&frame_data), MOJO_MAP_BUFFER_FLAG_NONE); | |
|
xhwang
2016/02/29 18:28:05
Could you please double check with mojo team what'
jrummell
2016/03/01 00:41:05
Unmap is required. Added.
| |
| 121 if (result != MOJO_RESULT_OK || !frame_data) | |
| 122 return false; | |
| 123 | |
| 124 set_stride(kYPlane, y_stride); | |
| 125 set_stride(kUPlane, u_stride); | |
| 126 set_stride(kVPlane, v_stride); | |
| 127 offsets_[kYPlane] = y_offset; | |
| 128 offsets_[kUPlane] = u_offset; | |
| 129 offsets_[kVPlane] = v_offset; | |
| 130 set_data(kYPlane, frame_data + y_offset); | |
| 131 set_data(kUPlane, frame_data + u_offset); | |
| 132 set_data(kVPlane, frame_data + v_offset); | |
| 133 return true; | |
| 134 } | |
| 135 | |
| 136 MojoVideoFrame::~MojoVideoFrame() {} | |
| 137 | |
| 138 const mojo::SharedBufferHandle& MojoVideoFrame::handle() const { | |
| 139 return shared_buffer_handle_.get(); | |
| 140 } | |
| 141 | |
| 142 size_t MojoVideoFrame::mapped_size() const { | |
| 143 return shared_buffer_size_; | |
| 144 } | |
| 145 | |
| 146 size_t MojoVideoFrame::plane_offset(size_t plane) const { | |
| 147 DCHECK(IsValidPlane(plane, format())); | |
| 148 return offsets_[plane]; | |
| 149 } | |
| 150 | |
| 151 } // namespace media | |
| OLD | NEW |