Coverage Report

Created: 2024-05-20 07:14

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