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

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