1// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef VK_QUEUE_HPP_
16#define VK_QUEUE_HPP_
17
18#include "VkObject.hpp"
19#include "Device/Renderer.hpp"
20#include <thread>
21#include <vulkan/vk_icd.h>
22
23#include "System/Synchronization.hpp"
24
25namespace marl
26{
27 class Scheduler;
28}
29
30namespace sw
31{
32 class Context;
33 class Renderer;
34}
35
36namespace vk
37{
38
39class Device;
40class Fence;
41
42class Queue
43{
44 VK_LOADER_DATA loaderData = { ICD_LOADER_MAGIC };
45
46public:
47 Queue(Device* device, marl::Scheduler *scheduler);
48 ~Queue();
49
50 operator VkQueue()
51 {
52 return reinterpret_cast<VkQueue>(this);
53 }
54
55 VkResult submit(uint32_t submitCount, const VkSubmitInfo* pSubmits, Fence* fence);
56 VkResult waitIdle();
57#ifndef __ANDROID__
58 VkResult present(const VkPresentInfoKHR* presentInfo);
59#endif
60
61private:
62 struct Task
63 {
64 uint32_t submitCount = 0;
65 VkSubmitInfo* pSubmits = nullptr;
66 sw::TaskEvents* events = nullptr;
67
68 enum Type { KILL_THREAD, SUBMIT_QUEUE };
69 Type type = SUBMIT_QUEUE;
70 };
71
72 void taskLoop(marl::Scheduler* scheduler);
73 void garbageCollect();
74 void submitQueue(const Task& task);
75
76 Device* device;
77 std::unique_ptr<sw::Renderer> renderer;
78 sw::Chan<Task> pending;
79 sw::Chan<VkSubmitInfo*> toDelete;
80 std::thread queueThread;
81};
82
83static inline Queue* Cast(VkQueue object)
84{
85 return reinterpret_cast<Queue*>(object);
86}
87
88} // namespace vk
89
90#endif // VK_QUEUE_HPP_
91