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
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153

media / gpu / buffer_validation_unittest.cc [blame]

// Copyright 2021 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/buffer_validation.h"

#include <fcntl.h>

#include "base/files/scoped_file.h"
#include "base/functional/bind.h"
#include "media/base/video_frame.h"
#include "media/base/video_types.h"
#include "media/video/fake_gpu_memory_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/gpu_memory_buffer.h"
#include "ui/gfx/native_pixmap_handle.h"

namespace media {
namespace {

base::ScopedFD GetDummyFD() {
  base::ScopedFD fd(open("/dev/zero", O_RDWR));
  DCHECK(fd.is_valid());
  return fd;
}

size_t FileSizeForTesting(size_t size) {
  return size;
}

TEST(BufferValidationTest, VerifyGmbHandlePasses) {
  gfx::GpuMemoryBufferHandle gmb_handle;
  gmb_handle.type = gfx::NATIVE_PIXMAP;
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/50, /*offset=*/0, /*size=*/2500, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/2500, /*size=*/625, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/3125, /*size=*/625, GetDummyFD());

  constexpr size_t buffer_size = 2500 + 2 * 625;
  const gfx::Size size(50, 50);

  EXPECT_TRUE(VerifyGpuMemoryBufferHandle(
      PIXEL_FORMAT_I420, size, gmb_handle,
      base::BindRepeating(FileSizeForTesting, buffer_size)));
}

TEST(BufferValidationTest, VerifyGmbHandleInvalidType) {
  gfx::GpuMemoryBufferHandle gmb_handle;
  gmb_handle.type = gfx::EMPTY_BUFFER;
  const gfx::Size size(50, 50);

  EXPECT_FALSE(VerifyGpuMemoryBufferHandle(
      PIXEL_FORMAT_I420, size, gmb_handle,
      base::BindRepeating(FileSizeForTesting, size.GetArea() * 2)));
}

TEST(BufferValidationTest, VerifyGmbHandlePlanesCountMatches) {
  gfx::GpuMemoryBufferHandle gmb_handle;
  gmb_handle.type = gfx::NATIVE_PIXMAP;
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/50, /*offset=*/0, /*size=*/2500, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/2500, /*size=*/625, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/3125, /*size=*/625, GetDummyFD());
  constexpr size_t buffer_size = 2500 + 2 * 625;
  const gfx::Size size(50, 50);

  // PIXEL_FORMAT_UYVY has only 1 plane
  // (https://source.chromium.org/chromium/chromium/src/+/main:media/base/video_frame_layout.cc;l=46;drc=eb5094ebb2c2a4128d36d27806b286d288965746)
  // The gmb handle will have only 3 planes by default.
  EXPECT_FALSE(VerifyGpuMemoryBufferHandle(
      PIXEL_FORMAT_UYVY, size, gmb_handle,
      base::BindRepeating(FileSizeForTesting, buffer_size)));
}

TEST(BufferValidationTest, VerifyGmbHandleStridesMonotonicallyDecrease) {
  gfx::GpuMemoryBufferHandle gmb_handle;
  gmb_handle.type = gfx::NATIVE_PIXMAP;
  // Make strides increase. Normal expectation is that they monotonically
  // decrease.
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/50, /*offset=*/0, /*size=*/2500, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/60, /*offset=*/2500, /*size=*/1500, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/70, /*offset=*/4000, /*size=*/1750, GetDummyFD());
  constexpr size_t buffer_size = 2500 + 1500 + 1750;
  const gfx::Size size(50, 50);

  EXPECT_FALSE(VerifyGpuMemoryBufferHandle(
      PIXEL_FORMAT_I420, size, gmb_handle,
      base::BindRepeating(FileSizeForTesting, buffer_size)));
}

TEST(BufferValidationTest, VerifyGmbHandleInvalidPlaneHeight) {
  gfx::GpuMemoryBufferHandle gmb_handle;
  gmb_handle.type = gfx::NATIVE_PIXMAP;
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/50, /*offset=*/0, /*size=*/2500, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/2500, /*size=*/625, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/3125, /*size=*/625, GetDummyFD());
  constexpr size_t buffer_size = 2500 + 2 * 625;
  // Make the height greater than the planes'.
  const gfx::Size size(50, 100);

  EXPECT_FALSE(VerifyGpuMemoryBufferHandle(
      PIXEL_FORMAT_I420, size, gmb_handle,
      base::BindRepeating(FileSizeForTesting, buffer_size)));
}

TEST(BufferValidationTest, VerifyGmbHandleInvalidPlaneWidth) {
  gfx::GpuMemoryBufferHandle gmb_handle;
  gmb_handle.type = gfx::NATIVE_PIXMAP;
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/50, /*offset=*/0, /*size=*/2500, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/2500, /*size=*/625, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/3125, /*size=*/625, GetDummyFD());
  constexpr size_t buffer_size = 2500 + 2 * 625;
  // Make the width greater than the planes'.
  const gfx::Size size(100, 50);

  EXPECT_FALSE(VerifyGpuMemoryBufferHandle(
      PIXEL_FORMAT_I420, size, gmb_handle,
      base::BindRepeating(FileSizeForTesting, buffer_size)));
}

TEST(BufferValidationTest, VerifyGmbHandleOffsetValid) {
  gfx::GpuMemoryBufferHandle gmb_handle;
  gmb_handle.type = gfx::NATIVE_PIXMAP;
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/50, /*offset=*/0, /*size=*/2500, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/2500, /*size=*/625, GetDummyFD());
  gmb_handle.native_pixmap_handle.planes.emplace_back(
      /*stride=*/25, /*offset=*/3125, /*size=*/625, GetDummyFD());
  const gfx::Size size(50, 50);

  // Make the file size less than the plane size.
  EXPECT_FALSE(VerifyGpuMemoryBufferHandle(
      PIXEL_FORMAT_I420, size, gmb_handle,
      base::BindRepeating(FileSizeForTesting, 2499)));
}

}  // namespace
}  // namespace media