1/*
2 * Copyright 2017 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 "MtlTestContext.h"
9
10#include "GrContext.h"
11#include "GrContextOptions.h"
12
13#import <Metal/Metal.h>
14
15#ifdef SK_METAL
16
17namespace {
18/**
19 * Implements sk_gpu_test::FenceSync for Metal.
20 */
21
22// TODO
23#if 0
24class MtlFenceSync : public sk_gpu_test::FenceSync {
25public:
26    MtlFenceSync(sk_sp<const GrVkInterface> vk, VkDevice device, VkQueue queue,
27                uint32_t queueFamilyIndex)
28            : fVk(std::move(vk))
29            , fDevice(device)
30            , fQueue(queue) {
31        SkDEBUGCODE(fUnfinishedSyncs = 0;)
32        VkCommandPoolCreateInfo createInfo;
33        createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
34        createInfo.pNext = nullptr;
35        createInfo.flags = 0;
36        createInfo.queueFamilyIndex = queueFamilyIndex;
37        GR_VK_CALL_ERRCHECK(fVk, CreateCommandPool(fDevice, &createInfo, nullptr, &fCommandPool));
38
39        VkCommandBufferAllocateInfo allocateInfo;
40        allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
41        allocateInfo.pNext = nullptr;
42        allocateInfo.commandBufferCount = 1;
43        allocateInfo.commandPool = fCommandPool;
44        allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
45        GR_VK_CALL_ERRCHECK(fVk, AllocateCommandBuffers(fDevice, &allocateInfo, &fCommandBuffer));
46
47        VkCommandBufferBeginInfo beginInfo;
48        beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
49        beginInfo.pNext = nullptr;
50        beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
51        beginInfo.pInheritanceInfo = nullptr;
52        GR_VK_CALL_ERRCHECK(fVk, BeginCommandBuffer(fCommandBuffer, &beginInfo));
53        GR_VK_CALL_ERRCHECK(fVk, EndCommandBuffer(fCommandBuffer));
54    }
55
56    ~VkFenceSync() override {
57        SkASSERT(!fUnfinishedSyncs);
58        // If the above assertion is true then the command buffer should not be in flight.
59        GR_VK_CALL(fVk, FreeCommandBuffers(fDevice, fCommandPool, 1, &fCommandBuffer));
60        GR_VK_CALL(fVk, DestroyCommandPool(fDevice, fCommandPool, nullptr));
61    }
62
63    sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override {
64        VkFence fence;
65        VkFenceCreateInfo info;
66        info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
67        info.pNext = nullptr;
68        info.flags = 0;
69        GR_VK_CALL_ERRCHECK(fVk, CreateFence(fDevice, &info, nullptr, &fence));
70        VkSubmitInfo submitInfo;
71        submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
72        submitInfo.pNext = nullptr;
73        submitInfo.waitSemaphoreCount = 0;
74        submitInfo.pWaitSemaphores = nullptr;
75        submitInfo.pWaitDstStageMask = nullptr;
76        submitInfo.commandBufferCount = 1;
77        submitInfo.pCommandBuffers = &fCommandBuffer;
78        submitInfo.signalSemaphoreCount = 0;
79        submitInfo.pSignalSemaphores = nullptr;
80        GR_VK_CALL_ERRCHECK(fVk, QueueSubmit(fQueue, 1, &submitInfo, fence));
81        SkDEBUGCODE(++fUnfinishedSyncs;)
82        return (sk_gpu_test::PlatformFence)fence;
83    }
84
85    bool waitFence(sk_gpu_test::PlatformFence opaqueFence) const override {
86        VkFence fence = (VkFence)opaqueFence;
87        static constexpr uint64_t kForever = ~((uint64_t)0);
88        auto result = GR_VK_CALL(fVk, WaitForFences(fDevice, 1, &fence, true, kForever));
89        return result != VK_TIMEOUT;
90    }
91
92    void deleteFence(sk_gpu_test::PlatformFence opaqueFence) const override {
93        VkFence fence = (VkFence)opaqueFence;
94        GR_VK_CALL(fVk, DestroyFence(fDevice, fence, nullptr));
95        SkDEBUGCODE(--fUnfinishedSyncs;)
96    }
97
98private:
99    sk_sp<const GrVkInterface>  fVk;
100    VkDevice                    fDevice;
101    VkQueue                     fQueue;
102    VkCommandPool               fCommandPool;
103    VkCommandBuffer             fCommandBuffer;
104    SkDEBUGCODE(mutable int     fUnfinishedSyncs;)
105    typedef sk_gpu_test::FenceSync INHERITED;
106};
107
108GR_STATIC_ASSERT(sizeof(VkFence) <= sizeof(sk_gpu_test::PlatformFence));
109#endif
110
111class MtlTestContext : public sk_gpu_test::TestContext {
112public:
113    static MtlTestContext* Create(TestContext* sharedContext) {
114        SkASSERT(!sharedContext);
115        id<MTLDevice> device = MTLCreateSystemDefaultDevice();
116        id<MTLCommandQueue> queue = [device newCommandQueue];
117
118        return new MtlTestContext(device, queue);
119    }
120
121    ~MtlTestContext() override { this->teardown(); }
122
123    GrBackend backend() override { return kMetal_GrBackend; }
124
125    GrBackendContext backendContext() override { return 0; }
126
127    void testAbandon() override {}
128
129    // There is really nothing to here since we don't own any unqueued command buffers here.
130    void submit() override {}
131
132    void finish() override {}
133
134    sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override {
135        return GrContext::MakeMetal((__bridge_retained void*)fDevice,
136                                    (__bridge_retained void*)fQueue,
137                                    options);
138    }
139
140private:
141    MtlTestContext(id<MTLDevice> device, id<MTLCommandQueue> queue)
142            : fDevice(device), fQueue(queue) {
143        fFenceSync.reset(nullptr);
144    }
145
146    void onPlatformMakeCurrent() const override {}
147    std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; }
148    void onPlatformSwapBuffers() const override {}
149
150    id<MTLDevice> fDevice;
151    id<MTLCommandQueue>  fQueue;
152
153    typedef sk_gpu_test::TestContext INHERITED;
154};
155
156}  // anonymous namespace
157
158namespace sk_gpu_test {
159
160TestContext* CreatePlatformMtlTestContext(TestContext* sharedContext) {
161    return MtlTestContext::Create(sharedContext);
162}
163}  // namespace sk_gpu_test
164
165
166#endif
167