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

gpu / command_buffer / service / shared_memory_region_wrapper_unittest.cc [blame]

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

#include "gpu/command_buffer/service/shared_memory_region_wrapper.h"

#include "gpu/ipc/common/gpu_memory_buffer_impl_shared_memory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace gpu {
namespace {

gfx::GpuMemoryBufferHandle CreateGpuMemoryBuffer(const gfx::Size& size,
                                                 gfx::BufferFormat format) {
  return GpuMemoryBufferImplSharedMemory::CreateGpuMemoryBuffer(
      gfx::GpuMemoryBufferId{1}, size, format,
      gfx::BufferUsage::SCANOUT_CPU_READ_WRITE);
}

TEST(SharedMemoryRegionWrapperTest, SinglePlaneRGBA_8888) {
  constexpr gfx::Size size(100, 100);
  constexpr gfx::BufferFormat buffer_format = gfx::BufferFormat::RGBA_8888;

  auto handle = CreateGpuMemoryBuffer(size, buffer_format);

  SharedMemoryRegionWrapper wrapper;
  EXPECT_TRUE(wrapper.Initialize(std::move(handle), size, buffer_format));
}

TEST(SharedMemoryRegionWrapperTest, MultiPlaneY_UV_420) {
  constexpr gfx::Size size(100, 100);
  constexpr gfx::BufferFormat buffer_format =
      gfx::BufferFormat::YUV_420_BIPLANAR;

  auto handle = CreateGpuMemoryBuffer(size, buffer_format);

  SharedMemoryRegionWrapper wrapper;
  EXPECT_TRUE(wrapper.Initialize(std::move(handle), size, buffer_format));

  uint8_t* memory_y_plane = wrapper.GetMemory(0);
  uint8_t* memory_uv_plane = wrapper.GetMemory(1);

  // UV plane should be 100*100*1 bytes after Y plane.
  size_t offset = memory_uv_plane - memory_y_plane;
  EXPECT_EQ(offset, 10000u);
}

TEST(SharedMemoryRegionWrapperTest, MultiPlaneY_V_U_420) {
  constexpr gfx::Size size(100, 100);
  constexpr gfx::BufferFormat buffer_format = gfx::BufferFormat::YVU_420;

  auto handle = CreateGpuMemoryBuffer(size, buffer_format);

  SharedMemoryRegionWrapper wrapper;
  EXPECT_TRUE(wrapper.Initialize(std::move(handle), size, buffer_format));

  uint8_t* memory_y_plane = wrapper.GetMemory(0);
  uint8_t* memory_v_plane = wrapper.GetMemory(1);
  uint8_t* memory_u_plane = wrapper.GetMemory(2);

  // V plane should be 100*100*1 bytes after Y plane.
  size_t v_offset = memory_v_plane - memory_y_plane;
  EXPECT_EQ(v_offset, 10000u);

  // U plane should be 50*50*1 bytes after V plane.
  size_t u_offset = memory_u_plane - memory_v_plane;
  EXPECT_EQ(u_offset, 2500u);
}

TEST(SharedMemoryRegionWrapperTest, BufferTooSmallWrongFormat) {
  constexpr gfx::Size size(100, 100);
  constexpr gfx::BufferFormat buffer_format =
      gfx::BufferFormat::YUV_420_BIPLANAR;

  auto handle = CreateGpuMemoryBuffer(size, gfx::BufferFormat::R_8);

  SharedMemoryRegionWrapper wrapper;
  EXPECT_FALSE(wrapper.Initialize(std::move(handle), size, buffer_format));
}

TEST(SharedMemoryRegionWrapperTest, BufferTooSmallWrongSize) {
  constexpr gfx::Size size(100, 100);
  constexpr gfx::BufferFormat buffer_format =
      gfx::BufferFormat::YUV_420_BIPLANAR;

  auto handle = CreateGpuMemoryBuffer(size, buffer_format);

  SharedMemoryRegionWrapper wrapper;
  EXPECT_FALSE(wrapper.Initialize(std::move(handle), gfx::Size(200, 200),
                                  buffer_format));
}

}  // namespace
}  // namespace gpu