Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/mojo/common/mojo_video_frame.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "mojo/edk/embedder/embedder.h" | |
| 14 #include "mojo/public/cpp/system/buffer.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace media { | |
| 18 | |
| 19 class MojoVideoFrameTest : public testing::Test { | |
| 20 public: | |
| 21 MojoVideoFrameTest() {} | |
| 22 ~MojoVideoFrameTest() override {} | |
| 23 | |
| 24 void SetUp() override { | |
| 25 // In order to allocate shared memory, mojo::edk needs to be initialized. | |
| 26 // This is only allowed once per process, so use a static flag to avoid | |
| 27 // initializing it more than once. | |
| 28 static bool already_initialized = false; | |
| 29 if (!already_initialized) { | |
| 30 already_initialized = true; | |
| 31 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.
| |
| 32 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
| |
| 33 } | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 DISALLOW_COPY_AND_ASSIGN(MojoVideoFrameTest); | |
| 38 }; | |
| 39 | |
| 40 TEST(MojoVideoFrameTest, CreateFrameWithSharedMemory) { | |
| 41 const int kWidth = 64; | |
| 42 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.
| |
| 43 const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(1337); | |
| 44 | |
| 45 // Create a MojoVideoFrame which will allocate enough space to hold a 64x48 | |
| 46 // video frame. | |
| 47 gfx::Size size(kWidth, kHeight); | |
| 48 scoped_refptr<MojoVideoFrame> frame = | |
| 49 MojoVideoFrame::Create(size, kTimestamp); | |
| 50 ASSERT_TRUE(frame.get()); | |
| 51 | |
| 52 // Verify that the correct frame was allocated along with some memory. | |
| 53 EXPECT_EQ(media::PIXEL_FORMAT_I420, frame->format()); | |
| 54 EXPECT_TRUE(frame->handle().is_valid()); | |
| 55 EXPECT_GT(frame->mapped_size(), 0u); | |
| 56 | |
| 57 // The offsets should be set appropriately. | |
| 58 EXPECT_EQ(frame->plane_offset(VideoFrame::kYPlane), 0u); | |
| 59 EXPECT_GT(frame->plane_offset(VideoFrame::kUPlane), 0u); | |
| 60 EXPECT_GT(frame->plane_offset(VideoFrame::kVPlane), 0u); | |
| 61 | |
| 62 // The strides should be set appropriately. | |
| 63 EXPECT_EQ(frame->stride(VideoFrame::kYPlane), kWidth); | |
| 64 EXPECT_EQ(frame->stride(VideoFrame::kUPlane), kWidth / 2); | |
| 65 EXPECT_EQ(frame->stride(VideoFrame::kVPlane), kWidth / 2); | |
| 66 | |
| 67 // The data pointers for each plane should be set. | |
| 68 EXPECT_TRUE(frame->data(VideoFrame::kYPlane)); | |
| 69 EXPECT_TRUE(frame->data(VideoFrame::kUPlane)); | |
| 70 EXPECT_TRUE(frame->data(VideoFrame::kVPlane)); | |
| 71 } | |
| 72 | |
| 73 TEST(MojoVideoFrameTest, CreateFrameAndReuseSharedMemory) { | |
| 74 const int kWidth = 128; | |
| 75 const int kHeight = 96; | |
| 76 const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(1338); | |
| 77 | |
| 78 // Some random values to use. Since we actually don't use the data inside the | |
| 79 // frame, random values are fine (as long as the offsets are within the | |
| 80 // memory size allocated). | |
| 81 const VideoPixelFormat format = PIXEL_FORMAT_YV12; | |
| 82 const size_t y_offset = kWidth * 2; | |
| 83 const size_t u_offset = kWidth * 3; | |
| 84 const size_t v_offset = kWidth * 5; | |
| 85 const int32_t y_stride = kWidth; | |
| 86 const int32_t u_stride = kWidth - 1; | |
| 87 const int32_t v_stride = kWidth - 2; | |
| 88 | |
| 89 // Allocate some shared memory. | |
| 90 gfx::Size size(kWidth, kHeight); | |
| 91 gfx::Rect visible_rect(size); | |
| 92 size_t requested_size = VideoFrame::AllocationSize(format, size); | |
| 93 ASSERT_LT(y_offset, requested_size); | |
| 94 mojo::ScopedSharedBufferHandle handle; | |
| 95 MojoResult result = | |
| 96 mojo::CreateSharedBuffer(nullptr, requested_size, &handle); | |
| 97 ASSERT_EQ(result, MOJO_RESULT_OK); | |
| 98 | |
| 99 // Allocate frame. | |
| 100 scoped_refptr<MojoVideoFrame> frame = MojoVideoFrame::Create( | |
| 101 format, size, visible_rect, size, handle.get(), requested_size, y_offset, | |
| 102 u_offset, v_offset, y_stride, u_stride, v_stride, kTimestamp); | |
| 103 ASSERT_TRUE(frame.get()); | |
| 104 EXPECT_EQ(frame->format(), format); | |
| 105 | |
| 106 // |frame| should have it's own handle to the memory, so ensure the original | |
| 107 // is still valid. | |
| 108 EXPECT_TRUE(handle.get().is_valid()); | |
| 109 EXPECT_TRUE(frame->handle().is_valid()); | |
| 110 EXPECT_NE(handle.get().value(), frame->handle().value()); | |
| 111 EXPECT_EQ(frame->mapped_size(), requested_size); | |
| 112 | |
| 113 // The offsets should be set appropriately. | |
| 114 EXPECT_EQ(frame->plane_offset(VideoFrame::kYPlane), y_offset); | |
| 115 EXPECT_EQ(frame->plane_offset(VideoFrame::kUPlane), u_offset); | |
| 116 EXPECT_EQ(frame->plane_offset(VideoFrame::kVPlane), v_offset); | |
| 117 | |
| 118 // The strides should be set appropriately. | |
| 119 EXPECT_EQ(frame->stride(VideoFrame::kYPlane), y_stride); | |
| 120 EXPECT_EQ(frame->stride(VideoFrame::kUPlane), u_stride); | |
| 121 EXPECT_EQ(frame->stride(VideoFrame::kVPlane), v_stride); | |
| 122 | |
| 123 // The data pointers for each plane should be set. | |
| 124 EXPECT_TRUE(frame->data(VideoFrame::kYPlane)); | |
| 125 EXPECT_TRUE(frame->data(VideoFrame::kUPlane)); | |
| 126 EXPECT_TRUE(frame->data(VideoFrame::kVPlane)); | |
| 127 } | |
| 128 | |
| 129 } // namespace media | |
| OLD | NEW |