native_pixmap_handle.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2016 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/native_pixmap_handle.h"
  5. #include <utility>
  6. #include "base/logging.h"
  7. #include "build/build_config.h"
  8. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  9. #include <drm_fourcc.h>
  10. #include <unistd.h>
  11. #include "base/posix/eintr_wrapper.h"
  12. #endif
  13. #if BUILDFLAG(IS_FUCHSIA)
  14. #include <lib/zx/vmo.h>
  15. #include "base/fuchsia/fuchsia_logging.h"
  16. #endif
  17. namespace gfx {
  18. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  19. static_assert(NativePixmapHandle::kNoModifier == DRM_FORMAT_MOD_INVALID,
  20. "gfx::NativePixmapHandle::kNoModifier should be an alias for"
  21. "DRM_FORMAT_MOD_INVALID");
  22. #endif
  23. NativePixmapPlane::NativePixmapPlane() : stride(0), offset(0), size(0) {}
  24. NativePixmapPlane::NativePixmapPlane(int stride,
  25. int offset,
  26. uint64_t size
  27. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  28. ,
  29. base::ScopedFD fd
  30. #elif BUILDFLAG(IS_FUCHSIA)
  31. ,
  32. zx::vmo vmo
  33. #endif
  34. )
  35. : stride(stride),
  36. offset(offset),
  37. size(size)
  38. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  39. ,
  40. fd(std::move(fd))
  41. #elif BUILDFLAG(IS_FUCHSIA)
  42. ,
  43. vmo(std::move(vmo))
  44. #endif
  45. {
  46. }
  47. NativePixmapPlane::NativePixmapPlane(NativePixmapPlane&& other) = default;
  48. NativePixmapPlane::~NativePixmapPlane() = default;
  49. NativePixmapPlane& NativePixmapPlane::operator=(NativePixmapPlane&& other) =
  50. default;
  51. NativePixmapHandle::NativePixmapHandle() = default;
  52. NativePixmapHandle::NativePixmapHandle(NativePixmapHandle&& other) = default;
  53. NativePixmapHandle::~NativePixmapHandle() = default;
  54. NativePixmapHandle& NativePixmapHandle::operator=(NativePixmapHandle&& other) =
  55. default;
  56. NativePixmapHandle CloneHandleForIPC(const NativePixmapHandle& handle) {
  57. NativePixmapHandle clone;
  58. for (auto& plane : handle.planes) {
  59. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  60. DCHECK(plane.fd.is_valid());
  61. // Combining the HANDLE_EINTR and ScopedFD's constructor causes the compiler
  62. // to emit some very strange assembly that tends to cause FD ownership
  63. // violations. see crbug.com/c/1287325.
  64. int checked_dup = HANDLE_EINTR(dup(plane.fd.get()));
  65. base::ScopedFD fd_dup(checked_dup);
  66. if (!fd_dup.is_valid()) {
  67. PLOG(ERROR) << "dup";
  68. return NativePixmapHandle();
  69. }
  70. clone.planes.emplace_back(plane.stride, plane.offset, plane.size,
  71. std::move(fd_dup));
  72. #elif BUILDFLAG(IS_FUCHSIA)
  73. zx::vmo vmo_dup;
  74. // VMO may be set to NULL for pixmaps that cannot be mapped.
  75. if (plane.vmo) {
  76. zx_status_t status = plane.vmo.duplicate(ZX_RIGHT_SAME_RIGHTS, &vmo_dup);
  77. if (status != ZX_OK) {
  78. ZX_DLOG(ERROR, status) << "zx_handle_duplicate";
  79. return NativePixmapHandle();
  80. }
  81. }
  82. clone.planes.emplace_back(plane.stride, plane.offset, plane.size,
  83. std::move(vmo_dup));
  84. #else
  85. #error Unsupported OS
  86. #endif
  87. }
  88. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  89. clone.modifier = handle.modifier;
  90. clone.supports_zero_copy_webgpu_import =
  91. handle.supports_zero_copy_webgpu_import;
  92. #endif
  93. #if BUILDFLAG(IS_FUCHSIA)
  94. clone.buffer_collection_id = handle.buffer_collection_id;
  95. clone.buffer_index = handle.buffer_index;
  96. clone.ram_coherency = handle.ram_coherency;
  97. #endif
  98. return clone;
  99. }
  100. } // namespace gfx