|
| 1 | +// Copyright 2019 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_SEMAPHORE_EXTERNAL_FUCHSIA_H_ |
| 16 | +#define VK_SEMAPHORE_EXTERNAL_FUCHSIA_H_ |
| 17 | + |
| 18 | +#include "VkDebug.hpp" |
| 19 | + |
| 20 | +#include <zircon/syscalls.h> |
| 21 | + |
| 22 | +// An external semaphore implementation for the Zircon kernel using a simple |
| 23 | +// Zircon event handle. This matches |
| 24 | +// VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_TEMP_ZIRCON_EVENT_BIT_FUCHSIA |
| 25 | +// which is not official yet but used by Fuchsia at the moment. |
| 26 | + |
| 27 | +namespace vk |
| 28 | +{ |
| 29 | + |
| 30 | +class Semaphore::External { |
| 31 | +public: |
| 32 | + // The type of external semaphore handle types supported by this implementation. |
| 33 | + static const VkExternalSemaphoreHandleTypeFlags kExternalSemaphoreHandleType = |
| 34 | + VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_TEMP_ZIRCON_EVENT_BIT_FUCHSIA; |
| 35 | + |
| 36 | + // Default constructor. Note that one should call either init() or |
| 37 | + // importFd() before any call to wait() or signal(). |
| 38 | + External() = default; |
| 39 | + |
| 40 | + ~External() |
| 41 | + { |
| 42 | + zx_handle_close(handle); |
| 43 | + } |
| 44 | + |
| 45 | + void init() |
| 46 | + { |
| 47 | + zx_status_t status = zx_event_create(0, &handle); |
| 48 | + if (status != ZX_OK) |
| 49 | + { |
| 50 | + ABORT("zx_event_create() returned %d", status); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + void importHandle(zx_handle_t new_handle) |
| 55 | + { |
| 56 | + zx_handle_close(handle); |
| 57 | + handle = new_handle; |
| 58 | + } |
| 59 | + |
| 60 | + VkResult exportHandle(zx_handle_t* pHandle) const |
| 61 | + { |
| 62 | + zx_handle_t new_handle = ZX_HANDLE_INVALID; |
| 63 | + zx_status_t status = zx_handle_duplicate(handle, ZX_RIGHT_SAME_RIGHTS, &new_handle); |
| 64 | + if (status != ZX_OK) |
| 65 | + { |
| 66 | + TRACE("zx_handle_duplicate() returned %d", status); |
| 67 | + return VK_ERROR_INVALID_EXTERNAL_HANDLE; |
| 68 | + } |
| 69 | + *pHandle = new_handle; |
| 70 | + return VK_SUCCESS; |
| 71 | + } |
| 72 | + |
| 73 | + void wait() |
| 74 | + { |
| 75 | + zx_signals_t observed = 0; |
| 76 | + zx_status_t status = zx_object_wait_one( |
| 77 | + handle, ZX_EVENT_SIGNALED, ZX_TIME_INFINITE, &observed); |
| 78 | + if (status != ZX_OK) |
| 79 | + { |
| 80 | + ABORT("zx_object_wait_one() returned %d", status); |
| 81 | + } |
| 82 | + if (observed != ZX_EVENT_SIGNALED) |
| 83 | + { |
| 84 | + ABORT("zx_object_wait_one() returned observed %x (%x expected)", observed, ZX_EVENT_SIGNALED); |
| 85 | + } |
| 86 | + // Need to unsignal the event now, as required by the Vulkan spec. |
| 87 | + status = zx_object_signal(handle, ZX_EVENT_SIGNALED, 0); |
| 88 | + if (status != ZX_OK) |
| 89 | + { |
| 90 | + ABORT("zx_object_signal() returned %d", status); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + bool tryWait() |
| 95 | + { |
| 96 | + zx_signals_t observed = 0; |
| 97 | + zx_status_t status = zx_object_wait_one( |
| 98 | + handle, ZX_EVENT_SIGNALED, zx_clock_get_monotonic(), &observed); |
| 99 | + if (status != ZX_OK) |
| 100 | + { |
| 101 | + ABORT("zx_object_wait_one() returned %d", status); |
| 102 | + } |
| 103 | + if (observed != ZX_EVENT_SIGNALED) |
| 104 | + { |
| 105 | + return false; |
| 106 | + } |
| 107 | + // Need to unsignal the event now, as required by the Vulkan spec. |
| 108 | + status = zx_object_signal(handle, ZX_EVENT_SIGNALED, 0); |
| 109 | + if (status != ZX_OK) |
| 110 | + { |
| 111 | + ABORT("zx_object_signal() returned %d", status); |
| 112 | + } |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + void signal() |
| 117 | + { |
| 118 | + zx_status_t status = zx_object_signal(handle, 0, ZX_EVENT_SIGNALED); |
| 119 | + if (status != ZX_OK) |
| 120 | + { |
| 121 | + ABORT("zx_object_signal() returned %d", status); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +private: |
| 126 | + zx_handle_t handle = ZX_HANDLE_INVALID; |
| 127 | +}; |
| 128 | + |
| 129 | +} // namespace vk |
| 130 | + |
| 131 | +#endif // VK_SEMAPHORE_EXTERNAL_FUCHSIA_H_ |
0 commit comments