1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "gpu/vulkan/semaphore_handle.h"
6 
7 #if defined(OS_POSIX)
8 #include <unistd.h>
9 #include "base/posix/eintr_wrapper.h"
10 #endif
11 
12 #if defined(OS_FUCHSIA)
13 #include "base/fuchsia/fuchsia_logging.h"
14 #endif
15 
16 #if defined(OS_WIN)
17 #include <windows.h>
18 #endif
19 
20 namespace gpu {
21 
22 SemaphoreHandle::SemaphoreHandle() = default;
SemaphoreHandle(VkExternalSemaphoreHandleTypeFlagBits type,PlatformHandle handle)23 SemaphoreHandle::SemaphoreHandle(VkExternalSemaphoreHandleTypeFlagBits type,
24                                  PlatformHandle handle)
25     : type_(type), handle_(std::move(handle)) {}
26 SemaphoreHandle::SemaphoreHandle(SemaphoreHandle&&) = default;
27 
28 SemaphoreHandle::~SemaphoreHandle() = default;
29 
30 SemaphoreHandle& SemaphoreHandle::operator=(SemaphoreHandle&&) = default;
31 
Duplicate() const32 SemaphoreHandle SemaphoreHandle::Duplicate() const {
33   if (!is_valid())
34     return SemaphoreHandle();
35 
36 #if defined(OS_POSIX)
37   return SemaphoreHandle(type_,
38                          base::ScopedFD(HANDLE_EINTR(dup(handle_.get()))));
39 #elif defined(OS_WIN)
40   HANDLE handle_dup;
41   if (!::DuplicateHandle(::GetCurrentProcess(), handle_.Get(),
42                          ::GetCurrentProcess(), &handle_dup, 0, FALSE,
43                          DUPLICATE_SAME_ACCESS)) {
44     return SemaphoreHandle();
45   }
46   return SemaphoreHandle(type_, base::win::ScopedHandle(handle_dup));
47 #elif defined(OS_FUCHSIA)
48   zx::event event_dup;
49   zx_status_t status = handle_.duplicate(ZX_RIGHT_SAME_RIGHTS, &event_dup);
50   if (status != ZX_OK) {
51     ZX_DLOG(ERROR, status) << "zx_handle_duplicate";
52     return SemaphoreHandle();
53   }
54   return SemaphoreHandle(type_, std::move(event_dup));
55 #else
56 #error Unsupported OS
57 #endif
58 }
59 
60 }  // namespace gpu
61