Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(545)

Unified Diff: media/mojo/services/mojo_video_frame.cc

Issue 1704273002: Add MojoVideoFrame (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: few protected methods Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/mojo/services/mojo_video_frame.cc
diff --git a/media/mojo/services/mojo_video_frame.cc b/media/mojo/services/mojo_video_frame.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3c097a13ce1234a7b95ca46a1ec73ebf66b26734
--- /dev/null
+++ b/media/mojo/services/mojo_video_frame.cc
@@ -0,0 +1,151 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
xhwang 2016/02/23 21:27:36 Can we add a unittest for MojoVideoFrame?
jrummell 2016/02/25 02:18:26 Done.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/mojo/services/mojo_video_frame.h"
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "build/build_config.h"
+
+namespace media {
+
+// static
+scoped_refptr<MojoVideoFrame> MojoVideoFrame::Create(
+ const gfx::Size& dimensions,
+ base::TimeDelta timestamp) {
+ const StorageType storage = STORAGE_MOJO_SHARED_MEMORY;
xhwang 2016/02/23 21:27:36 s/storage/storage_type/
jrummell 2016/02/25 02:18:26 Done.
+ const VideoPixelFormat format = PIXEL_FORMAT_I420;
+ gfx::Rect visible_rect(dimensions);
+ if (!IsValidConfig(format, storage, dimensions, visible_rect, dimensions)) {
+ LOG(DFATAL) << __FUNCTION__ << " Invalid config."
+ << ConfigToString(format, storage, dimensions, visible_rect,
+ dimensions);
+ return nullptr;
+ }
+
+ // Allocate a shared memory buffer big enough to hold the desired frame.
+ size_t requested_size = VideoFrame::AllocationSize(format, dimensions);
+ mojo::ScopedSharedBufferHandle handle;
+ MojoResult result =
+ mojo::CreateSharedBuffer(nullptr, requested_size, &handle);
+ if (result != MOJO_RESULT_OK)
+ return nullptr;
+
+ // Now allocate the frame and initialize it.
+ scoped_refptr<MojoVideoFrame> frame(
+ new MojoVideoFrame(format, storage, dimensions, visible_rect, dimensions,
+ std::move(handle), requested_size, timestamp));
+ if (!frame->Init(dimensions.width(), dimensions.width() / 2,
+ dimensions.width() / 2, 0 /* y_offset */,
+ dimensions.GetArea(), dimensions.GetArea() * 5 / 4))
xhwang 2016/02/23 21:27:36 Call MojoVideoFrame::Create() here?
jrummell 2016/02/25 02:18:26 Problem is that the other Create() call duplicates
+ return nullptr;
+
+ return frame;
+}
+
+// static
+scoped_refptr<MojoVideoFrame> MojoVideoFrame::Create(
+ VideoPixelFormat format,
+ const gfx::Size& coded_size,
+ const gfx::Rect& visible_rect,
+ const gfx::Size& natural_size,
+ mojo::ScopedSharedBufferHandle handle,
+ size_t data_size,
+ size_t y_offset,
+ size_t u_offset,
+ size_t v_offset,
+ int32_t y_stride,
+ int32_t u_stride,
+ int32_t v_stride,
+ base::TimeDelta timestamp) {
+ const StorageType storage = STORAGE_MOJO_SHARED_MEMORY;
xhwang 2016/02/23 21:27:36 storage_type
jrummell 2016/02/25 02:18:26 Done.
+ if (!IsValidConfig(format, storage, coded_size, visible_rect, natural_size)) {
+ LOG(DFATAL) << __FUNCTION__ << " Invalid config."
+ << ConfigToString(format, storage, coded_size, visible_rect,
+ natural_size);
+ return nullptr;
+ }
+
+ // Make a copy of the shared memory handle.
+ mojo::ScopedSharedBufferHandle duplicated_handle;
+ MojoResult result =
+ mojo::DuplicateBuffer(handle.get(), nullptr, &duplicated_handle);
+ if (result != MOJO_RESULT_OK)
+ return nullptr;
+
+ // Now allocate the frame and initialize it.
+ scoped_refptr<MojoVideoFrame> frame(new MojoVideoFrame(
+ format, storage, coded_size, visible_rect, natural_size,
+ std::move(duplicated_handle), data_size, timestamp));
+ if (!frame->Init(y_stride, u_stride, v_stride, y_offset, u_offset, v_offset))
+ return nullptr;
+
+ return frame;
+}
+
+MojoVideoFrame::MojoVideoFrame(VideoPixelFormat format,
+ StorageType storage_type,
+ const gfx::Size& coded_size,
+ const gfx::Rect& visible_rect,
+ const gfx::Size& natural_size,
+ mojo::ScopedSharedBufferHandle handle,
+ size_t mapped_size,
+ base::TimeDelta timestamp)
+ : VideoFrame(format,
+ storage_type,
+ coded_size,
+ visible_rect,
+ natural_size,
+ timestamp),
+ shared_buffer_handle_(std::move(handle)),
+ shared_buffer_size_(mapped_size) {
+ DCHECK(shared_buffer_handle_.is_valid());
+}
+
+bool MojoVideoFrame::Init(int32_t y_stride,
+ int32_t u_stride,
+ int32_t v_stride,
+ size_t y_offset,
+ size_t u_offset,
+ size_t v_offset) {
+ uint8_t* frame_data = nullptr;
+ MojoResult result = mojo::MapBuffer(
+ shared_buffer_handle_.get(), 0 /* offset */, shared_buffer_size_,
+ reinterpret_cast<void**>(&frame_data), MOJO_MAP_BUFFER_FLAG_NONE);
+ if (result != MOJO_RESULT_OK || !frame_data)
+ return false;
+
+ set_stride(kYPlane, y_stride);
+ set_stride(kUPlane, u_stride);
+ set_stride(kVPlane, v_stride);
+ set_offset(kYPlane, y_offset);
+ set_offset(kUPlane, u_offset);
+ set_offset(kVPlane, v_offset);
+ set_data(kYPlane, frame_data + y_offset);
+ set_data(kUPlane, frame_data + u_offset);
+ set_data(kVPlane, frame_data + v_offset);
+ return true;
+}
+
+MojoVideoFrame::~MojoVideoFrame() {}
xhwang 2016/02/23 21:27:36 Do we need UnmapBuffer() anywhere?
jrummell 2016/02/25 02:18:26 Since it's stored as a ScopedSharedBufferHandle, I
+
+mojo::SharedBufferHandle MojoVideoFrame::handle() const {
+ return shared_buffer_handle_.get();
+}
+
+size_t MojoVideoFrame::mapped_size() const {
+ return shared_buffer_size_;
+}
+
+size_t MojoVideoFrame::plane_offset(size_t plane) const {
+ DCHECK(IsValidPlane(plane, format()));
+ return offsets_[plane];
+}
+
+void MojoVideoFrame::set_offset(size_t plane, size_t offset) {
+ DCHECK(IsValidPlane(plane, format()));
+ offsets_[plane] = offset;
+}
+
+} // namespace media
« media/mojo/services/mojo_video_frame.h ('K') | « media/mojo/services/mojo_video_frame.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698