blob: 8fe052b6e113f564df75fb2878f4a4b1aa5573d3 [file] [log] [blame]
// Copyright 2018 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.
#include <vector>
#include "src/ui/lib/escher/forward_declarations.h"
#include "src/ui/lib/escher/renderer/uniform_allocation.h"
namespace escher {
// Similar to BlockAllocator, except that it returns suballocations from within
// fixed-size GPU uniform buffers. A UniformBufferPool is used to obtain the
// buffers that are allocated from.
class UniformBlockAllocator {
// The pool must be guaranteed to outlive the allocator.
explicit UniformBlockAllocator(impl::UniformBufferPoolWeakPtr pool);
UniformAllocation Allocate(size_t size, size_t alignment);
// Invalidates all previously-allocated pointers, and frees memory for reuse.
// The freed buffers are immediately released back to the UniformBufferPool.
void Reset();
// Allows the caller to manage the valid lifetime of the suballocation
// buffers, which will be returned to the UniformBufferPool when their
// ref-count reaches zero. For example, if the returned buffers are
// immediately freed then the behavior is effectively identical to Reset().
// NOTE: the caller is responsible for returning the buffers to the pool
// before the pool is destroyed (this is enforced by ResourceManager DCHECKs).
// The caller must therefore know something about the lifetime of the pool (or
// at least the lifetime of this allocator, since the pool is required to
// outlive this allocator).
std::vector<BufferPtr> TakeBuffers();
void Reserve();
const impl::UniformBufferPoolWeakPtr pool_;
const vk::DeviceSize buffer_size_;
std::vector<BufferPtr> buffers_;
size_t write_index_;
} // namespace escher