gpu_fence_handle.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2017 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. #include "ui/gfx/gpu_fence_handle.h"
  5. #include "base/debug/alias.h"
  6. #include "base/notreached.h"
  7. #include "build/build_config.h"
  8. #if BUILDFLAG(IS_POSIX)
  9. #include <unistd.h>
  10. #include "base/posix/eintr_wrapper.h"
  11. #endif
  12. #if BUILDFLAG(IS_FUCHSIA)
  13. #include "base/fuchsia/fuchsia_logging.h"
  14. #endif
  15. #if BUILDFLAG(IS_WIN)
  16. #include <windows.h>
  17. #include "base/process/process_handle.h"
  18. #endif
  19. namespace gfx {
  20. GpuFenceHandle::GpuFenceHandle() = default;
  21. GpuFenceHandle::GpuFenceHandle(GpuFenceHandle&& other) = default;
  22. GpuFenceHandle& GpuFenceHandle::operator=(GpuFenceHandle&& other) = default;
  23. GpuFenceHandle::~GpuFenceHandle() = default;
  24. bool GpuFenceHandle::is_null() const {
  25. #if BUILDFLAG(IS_POSIX)
  26. return !owned_fd.is_valid();
  27. #elif BUILDFLAG(IS_FUCHSIA)
  28. return !owned_event.is_valid();
  29. #elif BUILDFLAG(IS_WIN)
  30. return !owned_handle.IsValid();
  31. #else
  32. return true;
  33. #endif
  34. }
  35. GpuFenceHandle GpuFenceHandle::Clone() const {
  36. if (is_null())
  37. return GpuFenceHandle();
  38. gfx::GpuFenceHandle handle;
  39. #if BUILDFLAG(IS_POSIX)
  40. const int duped_handle = HANDLE_EINTR(dup(owned_fd.get()));
  41. if (duped_handle < 0)
  42. return GpuFenceHandle();
  43. handle.owned_fd = base::ScopedFD(duped_handle);
  44. #elif BUILDFLAG(IS_FUCHSIA)
  45. zx_status_t status =
  46. owned_event.duplicate(ZX_RIGHT_SAME_RIGHTS, &handle.owned_event);
  47. if (status != ZX_OK) {
  48. ZX_DLOG(ERROR, status) << "zx_handle_duplicate";
  49. return GpuFenceHandle();
  50. }
  51. #elif BUILDFLAG(IS_WIN)
  52. const base::ProcessHandle process = ::GetCurrentProcess();
  53. HANDLE duplicated_handle = INVALID_HANDLE_VALUE;
  54. const BOOL result =
  55. ::DuplicateHandle(process, owned_handle.Get(), process,
  56. &duplicated_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
  57. if (!result) {
  58. const DWORD last_error = ::GetLastError();
  59. base::debug::Alias(&last_error);
  60. CHECK(false);
  61. }
  62. handle.owned_handle.Set(duplicated_handle);
  63. #else
  64. NOTREACHED();
  65. #endif
  66. return handle;
  67. }
  68. } // namespace gfx