1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140

media / gpu / chromeos / native_pixmap_frame_resource_unittest.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/gpu/chromeos/native_pixmap_frame_resource.h"

#include <stddef.h>
#include <stdint.h>

#include <optional>

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "media/base/color_plane_layout.h"
#include "media/base/format_utils.h"
#include "media/base/video_frame.h"
#include "media/base/video_frame_layout.h"
#include "media/base/video_types.h"
#include "media/gpu/chromeos/mock_native_pixmap_dmabuf.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"

namespace media {

namespace {

// Creates a vector of file descriptors. Used to simulate a vector of DMABuf
// FDs.
std::vector<base::ScopedFD> CreateMockDMABufs(size_t num_planes) {
  std::vector<base::ScopedFD> dmabuf_fds;
  for (size_t i = 0; i < num_planes; i++) {
    // Instead of creating an FD of a DMABuf, this makes an FD for /dev/null.
    base::File file(base::FilePath("/dev/null"),
                    base::File::FLAG_OPEN | base::File::FLAG_READ);
    if (!file.IsValid()) {
      LOG(ERROR) << "Failed to open a file";
      return std::vector<base::ScopedFD>();
    }
    dmabuf_fds.emplace_back(file.TakePlatformFile());
    if (!dmabuf_fds.back().is_valid()) {
      LOG(ERROR) << "The FD taken from file is not valid";
      return std::vector<base::ScopedFD>();
    }
  }
  return dmabuf_fds;
}

}  // namespace

class CreateTest : public ::testing::Test,
                   public ::testing::WithParamInterface<
                       std::tuple<gfx::Size, gfx::Rect, gfx::Size, bool>> {
 public:
  void SetUp() override {}
  void TearDown() override {}

  // The parameters.
  static constexpr size_t kCodedSizeIndex = 0;
  static constexpr size_t kVisibleRectIndex = 1;
  static constexpr size_t kNaturalSizeIndex = 2;
  static constexpr size_t kShouldSucceedIndex = 3;
};

// Tests the size checks of NativePixmapFrameResource's DMABuf FD-based factory
// function.
TEST_P(CreateTest, CreateFromDMABufs) {
  constexpr VideoPixelFormat kPixelFormat = PIXEL_FORMAT_NV12;
  constexpr base::TimeDelta kTimestamp;

  const gfx::Size coded_size = std::get<kCodedSizeIndex>(GetParam());
  const gfx::Rect visible_rect = std::get<kVisibleRectIndex>(GetParam());
  const gfx::Size natural_size = std::get<kNaturalSizeIndex>(GetParam());
  const bool should_succeed = std::get<kShouldSucceedIndex>(GetParam());

  const std::optional<VideoFrameLayout> layout =
      VideoFrameLayout::Create(kPixelFormat, coded_size);
  ASSERT_TRUE(layout.has_value()) << "Failed to create video frame layout";

  auto dmabuf_fds = CreateMockDMABufs(layout->num_planes());
  ASSERT_FALSE(dmabuf_fds.empty());

  auto frame = NativePixmapFrameResource::Create(
      *layout, visible_rect, natural_size, std::move(dmabuf_fds), kTimestamp);
  if (should_succeed) {
    EXPECT_TRUE(!!frame);
  } else {
    EXPECT_EQ(frame, nullptr);
  }
}

// Tests the size checks of NativePixmapFrameResource's NativePixmapDmaBuf-based
// factory function.
TEST_P(CreateTest, CreateFromNativePixmapDmabuf) {
  constexpr VideoPixelFormat kPixelFormat = PIXEL_FORMAT_NV12;
  constexpr gfx::BufferUsage kBufferUsage = gfx::BufferUsage::GPU_READ;
  constexpr base::TimeDelta kTimestamp;

  const gfx::Size coded_size = std::get<kCodedSizeIndex>(GetParam());
  const gfx::Rect visible_rect = std::get<kVisibleRectIndex>(GetParam());
  const gfx::Size natural_size = std::get<kNaturalSizeIndex>(GetParam());
  const bool should_succeed = std::get<kShouldSucceedIndex>(GetParam());

  scoped_refptr<const gfx::NativePixmapDmaBuf> pixmap =
      CreateMockNativePixmapDmaBuf(kPixelFormat, coded_size);
  ASSERT_TRUE(!!pixmap);
  auto frame = NativePixmapFrameResource::Create(
      visible_rect, natural_size, kTimestamp, kBufferUsage, std::move(pixmap));
  if (should_succeed) {
    EXPECT_TRUE(!!frame);
  } else {
    EXPECT_EQ(frame, nullptr);
  }
}

INSTANTIATE_TEST_SUITE_P(NativePixmapFrameResourceValidSize,
                         CreateTest,
                         ::testing::Values(std::make_tuple(gfx::Size(320, 240),
                                                           gfx::Rect(320, 240),
                                                           gfx::Size(320, 240),
                                                           true)));

INSTANTIATE_TEST_SUITE_P(NativePixmapFrameResourceInvalidSize,
                         CreateTest,
                         ::testing::Values(std::make_tuple(gfx::Size(),
                                                           gfx::Rect(),
                                                           gfx::Size(),
                                                           false),
                                           std::make_tuple(gfx::Size(320, 240),
                                                           gfx::Rect(),
                                                           gfx::Size(320, 240),
                                                           false),
                                           std::make_tuple(gfx::Size(320, 240),
                                                           gfx::Rect(320, 240),
                                                           gfx::Size(),
                                                           false)));

}  // namespace media