vulkan_test.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <memory>
  5. #include "gpu/vulkan/tests/basic_vulkan_test.h"
  6. #include "gpu/vulkan/vulkan_command_buffer.h"
  7. #include "gpu/vulkan/vulkan_command_pool.h"
  8. #include "gpu/vulkan/vulkan_function_pointers.h"
  9. #include "gpu/vulkan/vulkan_surface.h"
  10. #include "gpu/vulkan/vulkan_swap_chain.h"
  11. #include "gpu/vulkan/vulkan_util.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. // This file tests basic vulkan initialization steps.
  14. namespace gpu {
  15. TEST_F(BasicVulkanTest, BasicVulkanSurface) {
  16. if (!supports_swapchain())
  17. return;
  18. std::unique_ptr<VulkanSurface> surface = CreateViewSurface(window());
  19. EXPECT_TRUE(surface);
  20. EXPECT_TRUE(surface->Initialize(GetDeviceQueue(),
  21. VulkanSurface::DEFAULT_SURFACE_FORMAT));
  22. EXPECT_TRUE(
  23. surface->Reshape(gfx::Size(100, 100), gfx::OVERLAY_TRANSFORM_NONE));
  24. surface->Destroy();
  25. }
  26. TEST_F(BasicVulkanTest, EmptyVulkanSwaps) {
  27. if (!supports_swapchain())
  28. return;
  29. auto command_pool = std::make_unique<VulkanCommandPool>(GetDeviceQueue());
  30. EXPECT_TRUE(command_pool->Initialize());
  31. std::unique_ptr<VulkanSurface> surface = CreateViewSurface(window());
  32. ASSERT_TRUE(surface);
  33. ASSERT_TRUE(surface->Initialize(GetDeviceQueue(),
  34. VulkanSurface::DEFAULT_SURFACE_FORMAT));
  35. ASSERT_TRUE(
  36. surface->Reshape(gfx::Size(100, 100), gfx::OVERLAY_TRANSFORM_NONE));
  37. constexpr VkSemaphore kNullSemaphore = VK_NULL_HANDLE;
  38. absl::optional<VulkanSwapChain::ScopedWrite> scoped_write;
  39. scoped_write.emplace(surface->swap_chain());
  40. EXPECT_TRUE(scoped_write->success());
  41. VkSemaphore begin_semaphore = scoped_write->begin_semaphore();
  42. EXPECT_NE(begin_semaphore, kNullSemaphore);
  43. VkSemaphore end_semaphore = scoped_write->end_semaphore();
  44. EXPECT_NE(end_semaphore, kNullSemaphore);
  45. auto command_buffer = command_pool->CreatePrimaryCommandBuffer();
  46. {
  47. ScopedSingleUseCommandBufferRecorder recorder(*command_buffer);
  48. command_buffer->TransitionImageLayout(scoped_write->image(),
  49. scoped_write->image_layout(),
  50. VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
  51. }
  52. EXPECT_TRUE(command_buffer->Submit(1, &begin_semaphore, 1, &end_semaphore));
  53. scoped_write.reset();
  54. // First swap is a special case, call it first to get better errors.
  55. EXPECT_EQ(gfx::SwapResult::SWAP_ACK,
  56. surface->SwapBuffers(
  57. base::DoNothingAs<void(const gfx::PresentationFeedback&)>()));
  58. vkQueueWaitIdle(GetDeviceQueue()->GetVulkanQueue());
  59. command_buffer->Destroy();
  60. command_buffer.reset();
  61. // Also make sure we can swap multiple times.
  62. for (int i = 0; i < 10; ++i) {
  63. scoped_write.emplace(surface->swap_chain());
  64. EXPECT_TRUE(scoped_write->success());
  65. begin_semaphore = scoped_write->begin_semaphore();
  66. EXPECT_NE(begin_semaphore, kNullSemaphore);
  67. end_semaphore = scoped_write->end_semaphore();
  68. EXPECT_NE(end_semaphore, kNullSemaphore);
  69. command_buffer = command_pool->CreatePrimaryCommandBuffer();
  70. {
  71. ScopedSingleUseCommandBufferRecorder recorder(*command_buffer);
  72. command_buffer->TransitionImageLayout(scoped_write->image(),
  73. scoped_write->image_layout(),
  74. VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
  75. }
  76. EXPECT_TRUE(command_buffer->Submit(1, &begin_semaphore, 1, &end_semaphore));
  77. scoped_write.reset();
  78. EXPECT_EQ(gfx::SwapResult::SWAP_ACK,
  79. surface->SwapBuffers(
  80. base::DoNothingAs<void(const gfx::PresentationFeedback&)>()));
  81. vkQueueWaitIdle(GetDeviceQueue()->GetVulkanQueue());
  82. command_buffer->Destroy();
  83. command_buffer.reset();
  84. }
  85. surface->Finish();
  86. surface->Destroy();
  87. command_pool->Destroy();
  88. }
  89. } // namespace gpu