Chromium Code Reviews| Index: media/mojo/common/mojo_video_frame_unittest.cc |
| diff --git a/media/mojo/common/mojo_video_frame_unittest.cc b/media/mojo/common/mojo_video_frame_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..01d33a25e523828de6a94e06080f761a2ba97dee |
| --- /dev/null |
| +++ b/media/mojo/common/mojo_video_frame_unittest.cc |
| @@ -0,0 +1,129 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/mojo/common/mojo_video_frame.h" |
| + |
| +#include <stddef.h> |
| +#include <stdint.h> |
| +#include <string> |
| + |
| +#include "base/command_line.h" |
| +#include "base/macros.h" |
| +#include "mojo/edk/embedder/embedder.h" |
| +#include "mojo/public/cpp/system/buffer.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace media { |
| + |
| +class MojoVideoFrameTest : public testing::Test { |
| + public: |
| + MojoVideoFrameTest() {} |
| + ~MojoVideoFrameTest() override {} |
| + |
| + void SetUp() override { |
| + // In order to allocate shared memory, mojo::edk needs to be initialized. |
| + // This is only allowed once per process, so use a static flag to avoid |
| + // initializing it more than once. |
| + static bool already_initialized = false; |
| + if (!already_initialized) { |
| + already_initialized = true; |
| + base::CommandLine::ForCurrentProcess()->AppendSwitch("--use-new-edk"); |
|
mcasas
2016/02/25 15:12:35
I think this is not needed now.
jrummell
2016/02/25 21:51:48
Removed.
|
| + mojo::edk::Init(); |
|
mcasas
2016/02/25 15:12:35
IIRC rockot@ mentioned that this should
happen fir
jrummell
2016/02/25 21:51:48
Actually this uses "//mojo/edk/test:run_all_unitte
|
| + } |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(MojoVideoFrameTest); |
| +}; |
| + |
| +TEST(MojoVideoFrameTest, CreateFrameWithSharedMemory) { |
| + const int kWidth = 64; |
| + const int kHeight = 48; |
|
mcasas
2016/02/25 15:12:35
nit: Smaller is faster, what about a 16x9 size?
jrummell
2016/02/25 21:51:48
Done.
|
| + const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(1337); |
| + |
| + // Create a MojoVideoFrame which will allocate enough space to hold a 64x48 |
| + // video frame. |
| + gfx::Size size(kWidth, kHeight); |
| + scoped_refptr<MojoVideoFrame> frame = |
| + MojoVideoFrame::Create(size, kTimestamp); |
| + ASSERT_TRUE(frame.get()); |
| + |
| + // Verify that the correct frame was allocated along with some memory. |
| + EXPECT_EQ(media::PIXEL_FORMAT_I420, frame->format()); |
| + EXPECT_TRUE(frame->handle().is_valid()); |
| + EXPECT_GT(frame->mapped_size(), 0u); |
| + |
| + // The offsets should be set appropriately. |
| + EXPECT_EQ(frame->plane_offset(VideoFrame::kYPlane), 0u); |
| + EXPECT_GT(frame->plane_offset(VideoFrame::kUPlane), 0u); |
| + EXPECT_GT(frame->plane_offset(VideoFrame::kVPlane), 0u); |
| + |
| + // The strides should be set appropriately. |
| + EXPECT_EQ(frame->stride(VideoFrame::kYPlane), kWidth); |
| + EXPECT_EQ(frame->stride(VideoFrame::kUPlane), kWidth / 2); |
| + EXPECT_EQ(frame->stride(VideoFrame::kVPlane), kWidth / 2); |
| + |
| + // The data pointers for each plane should be set. |
| + EXPECT_TRUE(frame->data(VideoFrame::kYPlane)); |
| + EXPECT_TRUE(frame->data(VideoFrame::kUPlane)); |
| + EXPECT_TRUE(frame->data(VideoFrame::kVPlane)); |
| +} |
| + |
| +TEST(MojoVideoFrameTest, CreateFrameAndReuseSharedMemory) { |
| + const int kWidth = 128; |
| + const int kHeight = 96; |
| + const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(1338); |
| + |
| + // Some random values to use. Since we actually don't use the data inside the |
| + // frame, random values are fine (as long as the offsets are within the |
| + // memory size allocated). |
| + const VideoPixelFormat format = PIXEL_FORMAT_YV12; |
| + const size_t y_offset = kWidth * 2; |
| + const size_t u_offset = kWidth * 3; |
| + const size_t v_offset = kWidth * 5; |
| + const int32_t y_stride = kWidth; |
| + const int32_t u_stride = kWidth - 1; |
| + const int32_t v_stride = kWidth - 2; |
| + |
| + // Allocate some shared memory. |
| + gfx::Size size(kWidth, kHeight); |
| + gfx::Rect visible_rect(size); |
| + size_t requested_size = VideoFrame::AllocationSize(format, size); |
| + ASSERT_LT(y_offset, requested_size); |
| + mojo::ScopedSharedBufferHandle handle; |
| + MojoResult result = |
| + mojo::CreateSharedBuffer(nullptr, requested_size, &handle); |
| + ASSERT_EQ(result, MOJO_RESULT_OK); |
| + |
| + // Allocate frame. |
| + scoped_refptr<MojoVideoFrame> frame = MojoVideoFrame::Create( |
| + format, size, visible_rect, size, handle.get(), requested_size, y_offset, |
| + u_offset, v_offset, y_stride, u_stride, v_stride, kTimestamp); |
| + ASSERT_TRUE(frame.get()); |
| + EXPECT_EQ(frame->format(), format); |
| + |
| + // |frame| should have it's own handle to the memory, so ensure the original |
| + // is still valid. |
| + EXPECT_TRUE(handle.get().is_valid()); |
| + EXPECT_TRUE(frame->handle().is_valid()); |
| + EXPECT_NE(handle.get().value(), frame->handle().value()); |
| + EXPECT_EQ(frame->mapped_size(), requested_size); |
| + |
| + // The offsets should be set appropriately. |
| + EXPECT_EQ(frame->plane_offset(VideoFrame::kYPlane), y_offset); |
| + EXPECT_EQ(frame->plane_offset(VideoFrame::kUPlane), u_offset); |
| + EXPECT_EQ(frame->plane_offset(VideoFrame::kVPlane), v_offset); |
| + |
| + // The strides should be set appropriately. |
| + EXPECT_EQ(frame->stride(VideoFrame::kYPlane), y_stride); |
| + EXPECT_EQ(frame->stride(VideoFrame::kUPlane), u_stride); |
| + EXPECT_EQ(frame->stride(VideoFrame::kVPlane), v_stride); |
| + |
| + // The data pointers for each plane should be set. |
| + EXPECT_TRUE(frame->data(VideoFrame::kYPlane)); |
| + EXPECT_TRUE(frame->data(VideoFrame::kUPlane)); |
| + EXPECT_TRUE(frame->data(VideoFrame::kVPlane)); |
| +} |
| + |
| +} // namespace media |