blob: 11755e487fdeed1ad2b812f7110929a97ac40e6d [file] [log] [blame]
// Copyright 2016 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef LIB_ESCHER_VK_GPU_ALLOCATOR_H_
#define LIB_ESCHER_VK_GPU_ALLOCATOR_H_
#include <vulkan/vulkan.hpp>
#include "lib/escher/vk/buffer.h"
#include "lib/escher/vk/gpu_mem.h"
#include "lib/escher/vk/image.h"
#include "lib/fxl/memory/weak_ptr.h"
namespace escher {
// GpuAllocator is an interface for allocating vulkan-aware blocks of memory,
// and objects that are backed by said memory (i.e., buffers and images).
//
// Vulkan implementations are not required to support large numbers of raw
// memory allocations. Instead, applications are expected to allocate larger
// chunks of memory, and sub-allocate from within these chunks.
//
// GpuAllocator defines the interface that clients use to obtain
// sub-allocated memory; the specific sub-allocation strategy employed is
// the responsibility of concrete subclasses of GpuAllocator.
class GpuAllocator {
public:
GpuAllocator() : weak_factory_(this) {}
virtual ~GpuAllocator() = default;
fxl::WeakPtr<GpuAllocator> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }
virtual GpuMemPtr AllocateMemory(
vk::MemoryRequirements reqs,
vk::MemoryPropertyFlags memory_property_flags) = 0;
// If |out_ptr| is non-null, this buffer must be backed by a dedicated
// piece of memory (i.e.,
// VkMemoryDedicatedRequirements.requiresDedicatedAllocation
// == true). That memory must be accessible through the GpuMem returned in
// |out_ptr|.
virtual BufferPtr AllocateBuffer(
ResourceManager* manager, vk::DeviceSize size,
vk::BufferUsageFlags usage_flags,
vk::MemoryPropertyFlags memory_property_flags,
GpuMemPtr* out_ptr = nullptr) = 0;
virtual ImagePtr AllocateImage(ResourceManager* manager,
const escher::ImageInfo& info,
GpuMemPtr* out_ptr = nullptr) = 0;
virtual uint32_t GetTotalBytesAllocated() const = 0;
fxl::WeakPtrFactory<GpuAllocator> weak_factory_; // must be last
FXL_DISALLOW_COPY_AND_ASSIGN(GpuAllocator);
};
typedef fxl::WeakPtr<GpuAllocator> GpuAllocatorWeakPtr;
} // namespace escher
#endif // LIB_ESCHER_VK_GPU_ALLOCATOR_H_