GpuMemoryBufferPoolTest.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "pch.h"
  2. #include "GpuMemoryBufferPool.h"
  3. #include "GraphicsDevice/ITexture2D.h"
  4. #include "GraphicsDeviceContainer.h"
  5. namespace unity
  6. {
  7. namespace webrtc
  8. {
  9. class GpuMemoryBufferPoolTest : public testing::TestWithParam<UnityGfxRenderer>
  10. {
  11. public:
  12. explicit GpuMemoryBufferPoolTest()
  13. : container_(CreateGraphicsDeviceContainer(GetParam()))
  14. , device_(container_->device())
  15. , clock_(0)
  16. {
  17. }
  18. protected:
  19. void SetUp() override
  20. {
  21. if (!device_)
  22. GTEST_SKIP() << "The graphics driver is not installed on the device.";
  23. std::unique_ptr<ITexture2D> texture(device_->CreateDefaultTextureV(kWidth, kHeight, kFormat));
  24. if (!texture)
  25. GTEST_SKIP() << "The graphics driver cannot create a texture resource.";
  26. bufferPool_ = std::make_unique<GpuMemoryBufferPool>(device_, &clock_);
  27. }
  28. std::unique_ptr<ITexture2D> CreateTexture(const Size& size, UnityRenderingExtTextureFormat format)
  29. {
  30. ITexture2D* tex = device_->CreateDefaultTextureV(size.width(), size.height(), format);
  31. EXPECT_TRUE(device_->WaitIdleForTest());
  32. return std::unique_ptr<ITexture2D>(tex);
  33. }
  34. void ReleaseStaleBuffers(Timestamp timestamp) { bufferPool_->ReleaseStaleBuffers(timestamp); }
  35. std::unique_ptr<GraphicsDeviceContainer> container_;
  36. IGraphicsDevice* device_;
  37. std::unique_ptr<GpuMemoryBufferPool> bufferPool_;
  38. SimulatedClock clock_;
  39. const uint32_t kWidth = 256;
  40. const uint32_t kHeight = 256;
  41. const UnityRenderingExtTextureFormat kFormat = kUnityRenderingExtFormatR8G8B8A8_SRGB;
  42. };
  43. TEST_P(GpuMemoryBufferPoolTest, CreateFrame)
  44. {
  45. const Size kSize(kWidth, kHeight);
  46. auto tex = CreateTexture(kSize, kFormat);
  47. void* ptr = tex->GetNativeTexturePtrV();
  48. auto frame = bufferPool_->CreateFrame(ptr, kSize, kFormat, clock_.CurrentTime());
  49. EXPECT_TRUE(device_->WaitIdleForTest());
  50. EXPECT_EQ(frame->size(), kSize);
  51. EXPECT_EQ(kFormat, frame->format());
  52. EXPECT_EQ(1u, bufferPool_->bufferCount());
  53. }
  54. TEST_P(GpuMemoryBufferPoolTest, ReuseFirstResource)
  55. {
  56. const Size kSize(kWidth, kHeight);
  57. auto tex = CreateTexture(kSize, kFormat);
  58. void* ptr = tex->GetNativeTexturePtrV();
  59. auto frame1 = bufferPool_->CreateFrame(ptr, kSize, kFormat, clock_.CurrentTime());
  60. EXPECT_TRUE(device_->WaitIdleForTest());
  61. EXPECT_NE(frame1, nullptr);
  62. EXPECT_EQ(1u, bufferPool_->bufferCount());
  63. auto frame2 = bufferPool_->CreateFrame(ptr, kSize, kFormat, clock_.CurrentTime());
  64. EXPECT_TRUE(device_->WaitIdleForTest());
  65. EXPECT_NE(frame2, nullptr);
  66. EXPECT_EQ(2u, bufferPool_->bufferCount());
  67. frame1 = nullptr;
  68. frame2 = nullptr;
  69. auto frame3 = bufferPool_->CreateFrame(ptr, kSize, kFormat, clock_.CurrentTime());
  70. EXPECT_TRUE(device_->WaitIdleForTest());
  71. EXPECT_EQ(2u, bufferPool_->bufferCount());
  72. }
  73. TEST_P(GpuMemoryBufferPoolTest, DropResourceWhenSizeIsDifferent)
  74. {
  75. const Size kSize1(kWidth, kHeight);
  76. auto tex1 = CreateTexture(kSize1, kFormat);
  77. void* ptr1 = tex1->GetNativeTexturePtrV();
  78. auto frame1 = bufferPool_->CreateFrame(ptr1, kSize1, kFormat, clock_.CurrentTime());
  79. EXPECT_TRUE(device_->WaitIdleForTest());
  80. EXPECT_EQ(1u, bufferPool_->bufferCount());
  81. frame1 = nullptr;
  82. const Size kSize2(512, 512);
  83. auto tex2 = CreateTexture(kSize2, kFormat);
  84. void* ptr2 = tex2->GetNativeTexturePtrV();
  85. auto frame2 = bufferPool_->CreateFrame(ptr2, kSize2, kFormat, clock_.CurrentTime());
  86. EXPECT_TRUE(device_->WaitIdleForTest());
  87. EXPECT_EQ(2u, bufferPool_->bufferCount());
  88. }
  89. TEST_P(GpuMemoryBufferPoolTest, StaleFramesAreExpired)
  90. {
  91. const Size kSize1(kWidth, kHeight);
  92. auto tex1 = CreateTexture(kSize1, kFormat);
  93. void* ptr1 = tex1->GetNativeTexturePtrV();
  94. auto frame1 = bufferPool_->CreateFrame(ptr1, kSize1, kFormat, clock_.CurrentTime());
  95. EXPECT_TRUE(device_->WaitIdleForTest());
  96. EXPECT_EQ(1u, bufferPool_->bufferCount());
  97. // The buffer is not older.
  98. ReleaseStaleBuffers(clock_.CurrentTime());
  99. EXPECT_EQ(1u, bufferPool_->bufferCount());
  100. clock_.AdvanceTime(TimeDelta::Seconds(60));
  101. // The buffer is older, but using yet.
  102. ReleaseStaleBuffers(clock_.CurrentTime());
  103. EXPECT_EQ(1u, bufferPool_->bufferCount());
  104. frame1 = nullptr;
  105. // Refreshed the timestamp when releasing the buffer.
  106. ReleaseStaleBuffers(clock_.CurrentTime());
  107. EXPECT_EQ(1u, bufferPool_->bufferCount());
  108. clock_.AdvanceTime(TimeDelta::Seconds(60));
  109. // The buffer is older and already unused. Release.
  110. ReleaseStaleBuffers(clock_.CurrentTime());
  111. EXPECT_EQ(0u, bufferPool_->bufferCount());
  112. }
  113. INSTANTIATE_TEST_SUITE_P(GfxDevice, GpuMemoryBufferPoolTest, testing::ValuesIn(supportedGfxDevices));
  114. } // end namespace webrtc
  115. } // end namespace unity