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
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189

gpu / vulkan / vulkan_fence_helper_unittest.cc [blame]

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

#include "testing/gtest/include/gtest/gtest.h"

#include "base/functional/bind.h"
#include "gpu/vulkan/tests/basic_vulkan_test.h"
#include "gpu/vulkan/vulkan_device_queue.h"
#include "gpu/vulkan/vulkan_fence_helper.h"
#include "gpu/vulkan/vulkan_function_pointers.h"
#include "gpu/vulkan/vulkan_util.h"

namespace gpu {

using VulkanFenceHelperTest = BasicVulkanTest;

TEST_F(VulkanFenceHelperTest, BasicFenceUsage) {
  VulkanFenceHelper* fence_helper = GetDeviceQueue()->GetFenceHelper();
  VkFence fence = VK_NULL_HANDLE;
  ASSERT_TRUE(VK_SUCCESS == fence_helper->GetFence(&fence));
  ASSERT_TRUE(fence != VK_NULL_HANDLE);
  ASSERT_TRUE(VK_SUCCESS == vkQueueSubmit(GetDeviceQueue()->GetVulkanQueue(), 0,
                                          nullptr, fence));
  VulkanFenceHelper::FenceHandle fence_handle =
      fence_helper->EnqueueFence(fence);
  EXPECT_TRUE(fence_helper->Wait(fence_handle, UINT64_MAX));
  EXPECT_TRUE(fence_helper->HasPassed(fence_handle));
}

TEST_F(VulkanFenceHelperTest, TestBasicCallback) {
  VulkanFenceHelper* fence_helper = GetDeviceQueue()->GetFenceHelper();
  bool cleanup_run = false;
  fence_helper->EnqueueCleanupTaskForSubmittedWork(
      base::BindOnce([](bool* cleanup_run, VulkanDeviceQueue* device_queue,
                        bool is_lost) { *cleanup_run = true; },
                     &cleanup_run));
  VulkanFenceHelper::FenceHandle fence_handle =
      fence_helper->GenerateCleanupFence();
  EXPECT_TRUE(fence_handle.is_valid());
  fence_helper->Wait(fence_handle, UINT64_MAX);
  EXPECT_TRUE(cleanup_run);
}

TEST_F(VulkanFenceHelperTest, TestBasicCallbackExternalSubmission) {
  VulkanFenceHelper* fence_helper = GetDeviceQueue()->GetFenceHelper();
  bool cleanup_run = false;
  fence_helper->EnqueueCleanupTaskForSubmittedWork(
      base::BindOnce([](bool* cleanup_run, VulkanDeviceQueue* device_queue,
                        bool is_lost) { *cleanup_run = true; },
                     &cleanup_run));
  VkFence fence = VK_NULL_HANDLE;
  ASSERT_TRUE(VK_SUCCESS == fence_helper->GetFence(&fence));
  ASSERT_TRUE(fence != VK_NULL_HANDLE);
  ASSERT_TRUE(VK_SUCCESS == vkQueueSubmit(GetDeviceQueue()->GetVulkanQueue(), 0,
                                          nullptr, fence));
  VulkanFenceHelper::FenceHandle fence_handle =
      fence_helper->EnqueueFence(fence);
  EXPECT_TRUE(fence_handle.is_valid());
  fence_helper->Wait(fence_handle, UINT64_MAX);
  EXPECT_TRUE(cleanup_run);
}

TEST_F(VulkanFenceHelperTest, TestMultipleCallbacks) {
  VulkanFenceHelper* fence_helper = GetDeviceQueue()->GetFenceHelper();
  uint32_t cleanups_run = 0;
  auto increment_cleanups_callback =
      [](uint32_t expected_index, uint32_t* cleanups_run,
         VulkanDeviceQueue* device_queue, bool is_lost) {
        EXPECT_EQ(expected_index, *cleanups_run);
        *cleanups_run = *cleanups_run + 1;
      };

  // Enqueue 5 callbacks.
  for (int i = 0; i < 5; i++) {
    fence_helper->EnqueueCleanupTaskForSubmittedWork(
        base::BindOnce(increment_cleanups_callback, i, &cleanups_run));
  }

  // Generate a cleanup fence for the first 5 callbacks.
  VulkanFenceHelper::FenceHandle fence_handle =
      fence_helper->GenerateCleanupFence();

  // Enqueue 5 more callbacks.
  for (int i = 5; i < 10; i++) {
    fence_helper->EnqueueCleanupTaskForSubmittedWork(
        base::BindOnce(increment_cleanups_callback, i, &cleanups_run));
  }

  // Generate a cleanup fence for the next 5 callbacks.
  fence_handle = fence_helper->GenerateCleanupFence();
  EXPECT_TRUE(fence_handle.is_valid());
  fence_helper->Wait(fence_handle, UINT64_MAX);
  EXPECT_EQ(10u, cleanups_run);
}

TEST_F(VulkanFenceHelperTest, TestSkiaCallback) {
  VulkanFenceHelper* fence_helper = GetDeviceQueue()->GetFenceHelper();
  bool cleanup_run = false;
  fence_helper->EnqueueCleanupTaskForSubmittedWork(
      base::BindOnce([](bool* cleanup_run, VulkanDeviceQueue* device_queue,
                        bool is_lost) { *cleanup_run = true; },
                     &cleanup_run));
  auto cleanup_closure = fence_helper->CreateExternalCallback();
  EXPECT_FALSE(cleanup_run);
  std::move(cleanup_closure).Run();
  EXPECT_TRUE(cleanup_run);
}

TEST_F(VulkanFenceHelperTest, SkiaCallbackBeforeFences) {
  VulkanFenceHelper* fence_helper = GetDeviceQueue()->GetFenceHelper();
  uint32_t cleanups_run = 0;
  auto increment_cleanups_callback =
      [](uint32_t expected_index, uint32_t* cleanups_run,
         VulkanDeviceQueue* device_queue, bool is_lost) {
        EXPECT_EQ(expected_index, *cleanups_run);
        *cleanups_run = *cleanups_run + 1;
      };

  // Enqueue 5 callbacks.
  for (int i = 0; i < 5; i++) {
    fence_helper->EnqueueCleanupTaskForSubmittedWork(
        base::BindOnce(increment_cleanups_callback, i, &cleanups_run));
  }

  // The first 5 callbacks use a callback to trigger.
  auto cleanup_closure = fence_helper->CreateExternalCallback();

  // Enqueue 5 more callbacks.
  for (int i = 5; i < 10; i++) {
    fence_helper->EnqueueCleanupTaskForSubmittedWork(
        base::BindOnce(increment_cleanups_callback, i, &cleanups_run));
  }

  // Generate a cleanup fence for the next 5 callbacks.
  VulkanFenceHelper::FenceHandle fence_handle =
      fence_helper->GenerateCleanupFence();
  EXPECT_TRUE(fence_handle.is_valid());

  // After waiting for the second fence, all callbacks should have run, Skia
  // callbacks can be delayed, so we check future fences as well.
  EXPECT_TRUE(fence_helper->Wait(fence_handle, UINT64_MAX));
  EXPECT_EQ(10u, cleanups_run);

  // Running the callback now should be a no-op.
  std::move(cleanup_closure).Run();
  EXPECT_EQ(10u, cleanups_run);
}

TEST_F(VulkanFenceHelperTest, SkiaCallbackAfterFences) {
  VulkanFenceHelper* fence_helper = GetDeviceQueue()->GetFenceHelper();
  uint32_t cleanups_run = 0;
  auto increment_cleanups_callback =
      [](uint32_t expected_index, uint32_t* cleanups_run,
         VulkanDeviceQueue* device_queue, bool is_lost) {
        EXPECT_EQ(expected_index, *cleanups_run);
        *cleanups_run = *cleanups_run + 1;
      };

  // Enqueue 5 callbacks.
  for (int i = 0; i < 5; i++) {
    fence_helper->EnqueueCleanupTaskForSubmittedWork(
        base::BindOnce(increment_cleanups_callback, i, &cleanups_run));
  }

  // The first 5 callbacks use a fence to trigger.
  VulkanFenceHelper::FenceHandle fence_handle =
      fence_helper->GenerateCleanupFence();
  EXPECT_TRUE(fence_handle.is_valid());

  // Call vkQueueWaitIdle() to make sure the |fence_handle| is passed.
  vkQueueWaitIdle(queue());

  // Enqueue 5 more callbacks.
  for (int i = 5; i < 10; i++) {
    fence_helper->EnqueueCleanupTaskForSubmittedWork(
        base::BindOnce(increment_cleanups_callback, i, &cleanups_run));
  }

  // The next 5 callbacks use a callback to trigger.
  auto cleanup_closure = fence_helper->CreateExternalCallback();

  // Call the cleanup closure, all callbacks should run.
  // Generate a cleanup fence for the next 5 callbacks.
  std::move(cleanup_closure).Run();
  EXPECT_EQ(10u, cleanups_run);
}

}  // namespace gpu