1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79

gpu / vulkan / vulkan_memory.h [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_VULKAN_VULKAN_MEMORY_H_
#define GPU_VULKAN_VULKAN_MEMORY_H_

#include <vulkan/vulkan_core.h>

#include "base/component_export.h"
#include "base/files/scoped_file.h"
#include "base/memory/raw_ptr.h"
#include "base/types/pass_key.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_WIN)
#include "base/win/scoped_handle.h"
#endif

#if BUILDFLAG(IS_FUCHSIA)
#include <lib/zx/vmo.h>
#endif

namespace gpu {

class VulkanDeviceQueue;

class COMPONENT_EXPORT(VULKAN) VulkanMemory {
 public:
  explicit VulkanMemory(base::PassKey<VulkanMemory> pass_key);
  ~VulkanMemory();

  VulkanMemory(VulkanMemory&) = delete;
  VulkanMemory& operator=(VulkanMemory&) = delete;

  static std::unique_ptr<VulkanMemory> Create(VulkanDeviceQueue* device_queue,
                                              VkDeviceMemory device_memory,
                                              VkDeviceSize size,
                                              uint32_t type_index);

  static std::unique_ptr<VulkanMemory> Create(
      VulkanDeviceQueue* device_queue,
      const VkMemoryRequirements* requirements,
      const void* extra_allocate_info);

  void Destroy();

#if BUILDFLAG(IS_POSIX)
  base::ScopedFD GetMemoryFd(VkExternalMemoryHandleTypeFlagBits handle_type);
#endif  // BUILDFLAG(IS_POSIX)

#if BUILDFLAG(IS_WIN)
  base::win::ScopedHandle GetMemoryHandle(
      VkExternalMemoryHandleTypeFlagBits handle_type);
#endif  // BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_FUCHSIA)
  zx::vmo GetMemoryZirconHandle();
#endif  // BUILDFLAG(IS_FUCHSIA)

  VulkanDeviceQueue* device_queue() const { return device_queue_; }
  VkDeviceSize size() const { return size_; }
  uint32_t type_index() const { return type_index_; }
  VkDeviceMemory device_memory() const { return device_memory_; }

 private:
  bool Initialize(VulkanDeviceQueue* device_queue,
                  const VkMemoryRequirements* requirements,
                  const void* extra_allocate_info);

  raw_ptr<VulkanDeviceQueue> device_queue_ = nullptr;
  VkDeviceMemory device_memory_ = VK_NULL_HANDLE;
  VkDeviceSize size_ = 0;
  uint32_t type_index_ = 0;
};

}  // namespace gpu

#endif  // GPU_VULKAN_VULKAN_MEMORY_H_