Coverage Report

Created: 2024-09-14 07:19

/src/skia/tools/gpu/vk/VkTestContext.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016 Google Inc.
3
 *
4
 * Use of this source code is governed by a BSD-style license that can be
5
 * found in the LICENSE file.
6
 */
7
8
#include "tools/gpu/vk/VkTestContext.h"
9
10
#ifdef SK_VULKAN
11
12
#include "include/gpu/ganesh/GrDirectContext.h"
13
#include "include/gpu/ganesh/vk/GrVkDirectContext.h"
14
#include "include/gpu/vk/VulkanBackendContext.h"
15
#include "include/gpu/vk/VulkanExtensions.h"
16
#include "include/gpu/vk/VulkanMemoryAllocator.h"
17
#include "tools/gpu/vk/VkTestUtils.h"
18
19
extern bool gCreateProtectedContext;
20
21
namespace {
22
23
class VkTestContextImpl : public sk_gpu_test::VkTestContext {
24
public:
25
0
    static VkTestContext* Create(VkTestContext* sharedContext) {
26
0
        skgpu::VulkanBackendContext backendContext;
27
0
        skgpu::VulkanExtensions* extensions;
28
0
        VkPhysicalDeviceFeatures2* features;
29
0
        bool ownsContext = true;
30
0
        VkDebugReportCallbackEXT debugCallback = VK_NULL_HANDLE;
31
0
        PFN_vkDestroyDebugReportCallbackEXT destroyCallback = nullptr;
32
0
        if (sharedContext) {
33
0
            backendContext = sharedContext->getVkBackendContext();
34
0
            extensions = const_cast<skgpu::VulkanExtensions*>(sharedContext->getVkExtensions());
35
0
            features = const_cast<VkPhysicalDeviceFeatures2*>(sharedContext->getVkFeatures());
36
            // We always delete the parent context last so make sure the child does not think they
37
            // own the vulkan context.
38
0
            ownsContext = false;
39
0
        } else {
40
0
            PFN_vkGetInstanceProcAddr instProc;
41
0
            if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc)) {
42
0
                return nullptr;
43
0
            }
44
45
0
            extensions = new skgpu::VulkanExtensions();
46
0
            features = new VkPhysicalDeviceFeatures2;
47
0
            memset(features, 0, sizeof(VkPhysicalDeviceFeatures2));
48
0
            if (!sk_gpu_test::CreateVkBackendContext(instProc, &backendContext, extensions,
49
0
                                                     features, &debugCallback,
50
0
                                                     nullptr, sk_gpu_test::CanPresentFn(),
51
0
                                                     gCreateProtectedContext)) {
52
0
                sk_gpu_test::FreeVulkanFeaturesStructs(features);
53
0
                delete features;
54
0
                delete extensions;
55
0
                return nullptr;
56
0
            }
57
0
            if (debugCallback != VK_NULL_HANDLE) {
58
0
                destroyCallback = (PFN_vkDestroyDebugReportCallbackEXT) instProc(
59
0
                        backendContext.fInstance, "vkDestroyDebugReportCallbackEXT");
60
0
            }
61
0
        }
62
0
        return new VkTestContextImpl(backendContext, extensions, features, ownsContext,
63
0
                                     debugCallback, destroyCallback);
64
0
    }
65
66
0
    ~VkTestContextImpl() override { this->teardown(); }
67
68
0
    void testAbandon() override {}
69
70
0
    sk_sp<GrDirectContext> makeContext(const GrContextOptions& options) override {
71
0
        return GrDirectContexts::MakeVulkan(fVk, options);
72
0
    }
73
74
protected:
75
#define ACQUIRE_VK_PROC_LOCAL(name, inst)                                            \
76
0
    PFN_vk##name grVk##name =                                                        \
77
0
            reinterpret_cast<PFN_vk##name>(fVk.fGetProc("vk" #name, inst, nullptr)); \
78
0
    do {                                                                             \
79
0
        if (grVk##name == nullptr) {                                                 \
80
0
            SkDebugf("Function ptr for vk%s could not be acquired\n", #name);        \
81
0
            return;                                                                  \
82
0
        }                                                                            \
83
0
    } while (0)
84
85
0
    void teardown() override {
86
0
        INHERITED::teardown();
87
0
        fVk.fMemoryAllocator.reset();
88
0
        if (fOwnsContext) {
89
0
            ACQUIRE_VK_PROC_LOCAL(DeviceWaitIdle, fVk.fInstance);
90
0
            ACQUIRE_VK_PROC_LOCAL(DestroyDevice, fVk.fInstance);
91
0
            ACQUIRE_VK_PROC_LOCAL(DestroyInstance, fVk.fInstance);
92
0
            grVkDeviceWaitIdle(fVk.fDevice);
93
0
            grVkDestroyDevice(fVk.fDevice, nullptr);
94
#ifdef SK_ENABLE_VK_LAYERS
95
            if (fDebugCallback != VK_NULL_HANDLE) {
96
                fDestroyDebugReportCallbackEXT(fVk.fInstance, fDebugCallback, nullptr);
97
            }
98
#endif
99
0
            grVkDestroyInstance(fVk.fInstance, nullptr);
100
0
            delete fExtensions;
101
102
0
            sk_gpu_test::FreeVulkanFeaturesStructs(fFeatures);
103
0
            delete fFeatures;
104
0
        }
105
0
    }
106
107
private:
108
    VkTestContextImpl(const skgpu::VulkanBackendContext& backendContext,
109
                      const skgpu::VulkanExtensions* extensions,
110
                      VkPhysicalDeviceFeatures2* features,
111
                      bool ownsContext,
112
                      VkDebugReportCallbackEXT debugCallback,
113
                      PFN_vkDestroyDebugReportCallbackEXT destroyCallback)
114
            : VkTestContext(backendContext,
115
                            extensions,
116
                            features,
117
                            ownsContext,
118
                            debugCallback,
119
0
                            destroyCallback) {
120
0
        fFenceSupport = true;
121
0
    }
122
123
0
    void onPlatformMakeNotCurrent() const override {}
124
0
    void onPlatformMakeCurrent() const override {}
125
0
    std::function<void()> onPlatformGetAutoContextRestore() const override  { return nullptr; }
126
127
    using INHERITED = sk_gpu_test::VkTestContext;
128
};
129
}  // anonymous namespace
130
131
namespace sk_gpu_test {
132
0
VkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) {
133
0
    return VkTestContextImpl::Create(sharedContext);
134
0
}
135
}  // namespace sk_gpu_test
136
137
#endif