gpu_memory_buffer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2013 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. #ifndef UI_GFX_GPU_MEMORY_BUFFER_H_
  5. #define UI_GFX_GPU_MEMORY_BUFFER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/memory/unsafe_shared_memory_region.h"
  9. #include "build/build_config.h"
  10. #include "ui/gfx/buffer_types.h"
  11. #include "ui/gfx/generic_shared_memory_id.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. #include "ui/gfx/gfx_export.h"
  14. #if defined(USE_OZONE) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  15. #include "ui/gfx/native_pixmap_handle.h"
  16. #elif BUILDFLAG(IS_MAC)
  17. #include "ui/gfx/mac/io_surface.h"
  18. #elif BUILDFLAG(IS_WIN)
  19. #include "base/types/token_type.h"
  20. #include "base/win/scoped_handle.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. #elif BUILDFLAG(IS_ANDROID)
  23. #include "base/android/scoped_hardware_buffer_handle.h"
  24. #endif
  25. namespace base {
  26. namespace trace_event {
  27. class ProcessMemoryDump;
  28. class MemoryAllocatorDumpGuid;
  29. } // namespace trace_event
  30. } // namespace base
  31. namespace gfx {
  32. class ColorSpace;
  33. enum GpuMemoryBufferType {
  34. EMPTY_BUFFER,
  35. SHARED_MEMORY_BUFFER,
  36. IO_SURFACE_BUFFER,
  37. NATIVE_PIXMAP,
  38. DXGI_SHARED_HANDLE,
  39. ANDROID_HARDWARE_BUFFER,
  40. GPU_MEMORY_BUFFER_TYPE_LAST = ANDROID_HARDWARE_BUFFER
  41. };
  42. using GpuMemoryBufferId = GenericSharedMemoryId;
  43. #if BUILDFLAG(IS_WIN)
  44. using DXGIHandleToken = base::TokenType<class DXGIHandleTokenTypeMarker>;
  45. #endif
  46. // TODO(crbug.com/863011): Convert this to a proper class to ensure the state is
  47. // always consistent, particularly that the only one handle is set at the same
  48. // time and it corresponds to |type|.
  49. struct GFX_EXPORT GpuMemoryBufferHandle {
  50. static constexpr GpuMemoryBufferId kInvalidId = GpuMemoryBufferId(-1);
  51. GpuMemoryBufferHandle();
  52. #if BUILDFLAG(IS_ANDROID)
  53. explicit GpuMemoryBufferHandle(
  54. base::android::ScopedHardwareBufferHandle handle);
  55. #endif
  56. GpuMemoryBufferHandle(GpuMemoryBufferHandle&& other);
  57. GpuMemoryBufferHandle& operator=(GpuMemoryBufferHandle&& other);
  58. ~GpuMemoryBufferHandle();
  59. GpuMemoryBufferHandle Clone() const;
  60. bool is_null() const { return type == EMPTY_BUFFER; }
  61. GpuMemoryBufferType type = GpuMemoryBufferType::EMPTY_BUFFER;
  62. GpuMemoryBufferId id{0};
  63. base::UnsafeSharedMemoryRegion region;
  64. uint32_t offset = 0;
  65. uint32_t stride = 0;
  66. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_FUCHSIA)
  67. NativePixmapHandle native_pixmap_handle;
  68. #elif BUILDFLAG(IS_MAC)
  69. ScopedIOSurface io_surface;
  70. #elif BUILDFLAG(IS_WIN)
  71. base::win::ScopedHandle dxgi_handle;
  72. absl::optional<DXGIHandleToken> dxgi_token;
  73. #elif BUILDFLAG(IS_ANDROID)
  74. base::android::ScopedHardwareBufferHandle android_hardware_buffer;
  75. #endif
  76. };
  77. // This interface typically correspond to a type of shared memory that is also
  78. // shared with the GPU. A GPU memory buffer can be written to directly by
  79. // regular CPU code, but can also be read by the GPU.
  80. class GFX_EXPORT GpuMemoryBuffer {
  81. public:
  82. virtual ~GpuMemoryBuffer() {}
  83. // Maps each plane of the buffer into the client's address space so it can be
  84. // written to by the CPU. This call may block, for instance if the GPU needs
  85. // to finish accessing the buffer or if CPU caches need to be synchronized.
  86. // Returns false on failure.
  87. virtual bool Map() = 0;
  88. // Returns a pointer to the memory address of a plane. Buffer must have been
  89. // successfully mapped using a call to Map() before calling this function.
  90. virtual void* memory(size_t plane) = 0;
  91. // Unmaps the buffer. It's illegal to use any pointer returned by memory()
  92. // after this has been called.
  93. virtual void Unmap() = 0;
  94. // Returns the size in pixels of the first plane of the buffer.
  95. virtual Size GetSize() const = 0;
  96. // Returns the format for the buffer.
  97. virtual BufferFormat GetFormat() const = 0;
  98. // Fills the stride in bytes for each plane of the buffer. The stride of
  99. // plane K is stored at index K-1 of the |stride| array.
  100. virtual int stride(size_t plane) const = 0;
  101. // Set the color space in which this buffer should be interpreted when used
  102. // as an overlay. Note that this will not impact texturing from the buffer.
  103. virtual void SetColorSpace(const ColorSpace& color_space);
  104. // Returns a unique identifier associated with buffer.
  105. virtual GpuMemoryBufferId GetId() const = 0;
  106. // Returns the type of this buffer.
  107. virtual GpuMemoryBufferType GetType() const = 0;
  108. // Returns a platform specific handle for this buffer which in particular can
  109. // be sent over IPC. This duplicates file handles as appropriate, so that a
  110. // caller takes ownership of the returned handle.
  111. virtual GpuMemoryBufferHandle CloneHandle() const = 0;
  112. // Dumps information about the memory backing the GpuMemoryBuffer to |pmd|.
  113. // The memory usage is attributed to |buffer_dump_guid|.
  114. // |tracing_process_id| uniquely identifies the process owning the memory.
  115. // |importance| is relevant only for the cases of co-ownership, the memory
  116. // gets attributed to the owner with the highest importance.
  117. virtual void OnMemoryDump(
  118. base::trace_event::ProcessMemoryDump* pmd,
  119. const base::trace_event::MemoryAllocatorDumpGuid& buffer_dump_guid,
  120. uint64_t tracing_process_id,
  121. int importance) const = 0;
  122. };
  123. } // namespace gfx
  124. #endif // UI_GFX_GPU_MEMORY_BUFFER_H_