GpuMemoryBufferTest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "pch.h"
  2. #include "GpuMemoryBuffer.h"
  3. #include "GraphicsDevice/IGraphicsDevice.h"
  4. #include "GraphicsDevice/ITexture2D.h"
  5. #include "GraphicsDeviceContainer.h"
  6. #include "Size.h"
  7. #include "UnityVideoTrackSource.h"
  8. #include "VideoFrameAdapter.h"
  9. #include "VideoFrameUtil.h"
  10. namespace unity
  11. {
  12. namespace webrtc
  13. {
  14. class GpuMemoryBufferTest : public testing::TestWithParam<UnityGfxRenderer>
  15. {
  16. public:
  17. explicit GpuMemoryBufferTest()
  18. : container_(CreateGraphicsDeviceContainer(GetParam()))
  19. , device_(container_->device())
  20. {
  21. }
  22. protected:
  23. void SetUp() override
  24. {
  25. if (!device_)
  26. GTEST_SKIP() << "The graphics driver is not installed on the device.";
  27. std::unique_ptr<ITexture2D> texture(device_->CreateDefaultTextureV(kWidth, kHeight, kFormat));
  28. if (!texture)
  29. GTEST_SKIP() << "The graphics driver cannot create a texture resource.";
  30. }
  31. std::unique_ptr<GraphicsDeviceContainer> container_;
  32. IGraphicsDevice* device_;
  33. const uint32_t kWidth = 256;
  34. const uint32_t kHeight = 256;
  35. const Size kSize = { static_cast<int>(kWidth), static_cast<int>(kHeight) };
  36. const UnityRenderingExtTextureFormat kFormat = kUnityRenderingExtFormatR8G8B8A8_SRGB;
  37. };
  38. TEST_P(GpuMemoryBufferTest, WidthAndHeight)
  39. {
  40. IGraphicsDevice* device = container_->device();
  41. std::unique_ptr<const ITexture2D> texture(
  42. device->CreateDefaultTextureV(kWidth, kHeight, kFormat));
  43. auto testFrame = CreateTestFrame(device, texture.get(), kFormat);
  44. EXPECT_TRUE(device_->WaitIdleForTest());
  45. auto frame = VideoFrameAdapter::CreateVideoFrame(testFrame);
  46. auto buffer = frame.video_frame_buffer();
  47. EXPECT_EQ(buffer->width(), kSize.width());
  48. EXPECT_EQ(buffer->height(), kSize.height());
  49. }
  50. TEST_P(GpuMemoryBufferTest, Scale)
  51. {
  52. const Size kSize2(static_cast<int>(kWidth * 2), static_cast<int>(kHeight * 2));
  53. std::unique_ptr<const ITexture2D> texture(
  54. device_->CreateDefaultTextureV(kWidth, kHeight, kFormat));
  55. auto testFrame = CreateTestFrame(device_, texture.get(), kFormat);
  56. EXPECT_TRUE(device_->WaitIdleForTest());
  57. auto frame = VideoFrameAdapter::CreateVideoFrame(testFrame);
  58. auto buffer = frame.video_frame_buffer();
  59. auto buffer2 = buffer->Scale(kSize2.width(), kSize2.height());
  60. EXPECT_NE(buffer2, nullptr);
  61. EXPECT_EQ(buffer2->type(), buffer->type());
  62. EXPECT_EQ(buffer2->width(), kSize2.width());
  63. EXPECT_EQ(buffer2->height(), kSize2.height());
  64. // check ScaledBuffer::ToI420()
  65. {
  66. auto i420Buffer = buffer2->ToI420();
  67. EXPECT_NE(i420Buffer, nullptr);
  68. EXPECT_EQ(i420Buffer->type(), VideoFrameBuffer::Type::kI420);
  69. EXPECT_EQ(i420Buffer->width(), kSize2.width());
  70. EXPECT_EQ(i420Buffer->height(), kSize2.height());
  71. }
  72. // check ScaledBuffer::GetI420()
  73. {
  74. auto i420Buffer = buffer2->GetI420();
  75. EXPECT_NE(i420Buffer, nullptr);
  76. EXPECT_EQ(i420Buffer->type(), VideoFrameBuffer::Type::kI420);
  77. EXPECT_EQ(i420Buffer->width(), kSize2.width());
  78. EXPECT_EQ(i420Buffer->height(), kSize2.height());
  79. }
  80. }
  81. INSTANTIATE_TEST_SUITE_P(GfxDevice, GpuMemoryBufferTest, testing::ValuesIn(supportedGfxDevices));
  82. } // end namespace webrtc
  83. } // end namespace unity