GraphicsDeviceTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "pch.h"
  2. #include <rtc_base/thread.h>
  3. #include "GpuMemoryBuffer.h"
  4. #include "GraphicsDevice/IGraphicsDevice.h"
  5. #include "GraphicsDevice/ITexture2D.h"
  6. #include "GraphicsDeviceTestBase.h"
  7. namespace unity
  8. {
  9. namespace webrtc
  10. {
  11. class GraphicsDeviceTest : public GraphicsDeviceTestBase
  12. {
  13. protected:
  14. void SetUp() override
  15. {
  16. if (!device())
  17. GTEST_SKIP() << "The graphics driver is not installed on the device.";
  18. std::unique_ptr<ITexture2D> texture(device()->CreateDefaultTextureV(kWidth, kHeight, format()));
  19. if (!texture)
  20. GTEST_SKIP() << "The graphics driver cannot create a texture resource.";
  21. }
  22. const uint32_t kWidth = 256;
  23. const uint32_t kHeight = 256;
  24. };
  25. TEST_P(GraphicsDeviceTest, GraphicsDeviceIsNotNull) { EXPECT_NE(nullptr, device()); }
  26. TEST_P(GraphicsDeviceTest, CreateDefaultTextureV)
  27. {
  28. const auto width = 256;
  29. const auto height = 256;
  30. const std::unique_ptr<ITexture2D> tex(device()->CreateDefaultTextureV(width, height, format()));
  31. EXPECT_TRUE(device()->WaitIdleForTest());
  32. EXPECT_TRUE(tex->IsSize(width, height));
  33. EXPECT_NE(nullptr, tex->GetNativeTexturePtrV());
  34. EXPECT_FALSE(tex->IsSize(0, 0));
  35. }
  36. TEST_P(GraphicsDeviceTest, CreateCPUReadTextureV)
  37. {
  38. const auto width = 256;
  39. const auto height = 256;
  40. const std::unique_ptr<ITexture2D> tex(device()->CreateCPUReadTextureV(width, height, format()));
  41. EXPECT_TRUE(device()->WaitIdleForTest());
  42. EXPECT_TRUE(tex->IsSize(width, height));
  43. EXPECT_NE(nullptr, tex->GetNativeTexturePtrV());
  44. EXPECT_FALSE(tex->IsSize(0, 0));
  45. }
  46. TEST_P(GraphicsDeviceTest, ReleaseTextureOnOtherThread)
  47. {
  48. const uint32_t width = 256;
  49. const uint32_t height = 256;
  50. std::unique_ptr<rtc::Thread> thread = rtc::Thread::CreateWithSocketServer();
  51. thread->Start();
  52. std::unique_ptr<ITexture2D> texture(device()->CreateDefaultTextureV(width, height, format()));
  53. EXPECT_TRUE(device()->WaitIdleForTest());
  54. thread->Invoke<void>(RTC_FROM_HERE, [&]() { texture = nullptr; });
  55. EXPECT_EQ(texture, nullptr);
  56. }
  57. TEST_P(GraphicsDeviceTest, CopyResourceV)
  58. {
  59. const auto width = 256;
  60. const auto height = 256;
  61. const std::unique_ptr<ITexture2D> src(device()->CreateDefaultTextureV(width, height, format()));
  62. const std::unique_ptr<ITexture2D> dst(device()->CreateDefaultTextureV(width, height, format()));
  63. EXPECT_TRUE(device()->WaitIdleForTest());
  64. EXPECT_TRUE(device()->CopyResourceV(dst.get(), src.get()));
  65. EXPECT_TRUE(device()->WaitIdleForTest());
  66. }
  67. TEST_P(GraphicsDeviceTest, CopyResourceVFromCPURead)
  68. {
  69. const auto width = 256;
  70. const auto height = 256;
  71. const std::unique_ptr<ITexture2D> src(device()->CreateCPUReadTextureV(width, height, format()));
  72. const std::unique_ptr<ITexture2D> dst(device()->CreateDefaultTextureV(width, height, format()));
  73. EXPECT_TRUE(device()->WaitIdleForTest());
  74. EXPECT_TRUE(device()->CopyResourceV(dst.get(), src.get()));
  75. EXPECT_TRUE(device()->WaitIdleForTest());
  76. }
  77. TEST_P(GraphicsDeviceTest, CopyResourceNativeV)
  78. {
  79. const auto width = 256;
  80. const auto height = 256;
  81. const std::unique_ptr<ITexture2D> src(device()->CreateDefaultTextureV(width, height, format()));
  82. const std::unique_ptr<ITexture2D> dst(device()->CreateDefaultTextureV(width, height, format()));
  83. EXPECT_TRUE(device()->WaitIdleForTest());
  84. EXPECT_TRUE(device()->CopyResourceFromNativeV(dst.get(), src->GetNativeTexturePtrV()));
  85. EXPECT_TRUE(device()->WaitIdleForTest());
  86. }
  87. TEST_P(GraphicsDeviceTest, ConvertRGBToI420)
  88. {
  89. const uint32_t width = 256;
  90. const uint32_t height = 256;
  91. const std::unique_ptr<ITexture2D> src(device()->CreateDefaultTextureV(width, height, format()));
  92. const std::unique_ptr<ITexture2D> dst(device()->CreateCPUReadTextureV(width, height, format()));
  93. EXPECT_TRUE(device()->WaitIdleForTest());
  94. EXPECT_TRUE(device()->CopyResourceFromNativeV(dst.get(), src->GetNativeTexturePtrV()));
  95. EXPECT_TRUE(device()->WaitIdleForTest());
  96. const auto frameBuffer = device()->ConvertRGBToI420(dst.get());
  97. EXPECT_NE(nullptr, frameBuffer);
  98. EXPECT_EQ(width, frameBuffer->width());
  99. EXPECT_EQ(height, frameBuffer->height());
  100. }
  101. TEST_P(GraphicsDeviceTest, Map)
  102. {
  103. const uint32_t width = 256;
  104. const uint32_t height = 256;
  105. const std::unique_ptr<ITexture2D> src(device()->CreateDefaultTextureV(width, height, format()));
  106. EXPECT_TRUE(device()->WaitIdleForTest());
  107. std::unique_ptr<GpuMemoryBufferHandle> handle = device()->Map(src.get());
  108. #if CUDA_PLATFORM
  109. if (device()->IsCudaSupport())
  110. EXPECT_NE(handle, nullptr);
  111. else
  112. EXPECT_EQ(handle, nullptr);
  113. #else
  114. EXPECT_EQ(handle, nullptr);
  115. #endif
  116. }
  117. TEST_P(GraphicsDeviceTest, MapWithCPUReadTexture)
  118. {
  119. // On Vulkan device, the Map method don't success when using the texture
  120. // which creating for reading from CPU.
  121. // It is unclear whether this is the bug or the specification of CUDA.
  122. if (device()->GetGfxRenderer() == kUnityGfxRendererVulkan)
  123. GTEST_SKIP() << "The Map method throw exception on vulkan platform";
  124. const uint32_t width = 256;
  125. const uint32_t height = 256;
  126. const std::unique_ptr<ITexture2D> src2(device()->CreateCPUReadTextureV(width, height, format()));
  127. EXPECT_TRUE(device()->WaitIdleForTest());
  128. std::unique_ptr<GpuMemoryBufferHandle> handle2 = device()->Map(src2.get());
  129. }
  130. INSTANTIATE_TEST_SUITE_P(GfxDeviceAndColorSpece, GraphicsDeviceTest, testing::ValuesIn(VALUES_TEST_ENV));
  131. } // end namespace webrtc
  132. } // end namespace unity