#ifndef GPU_COMMAND_BUFFER_CLIENT_CLIENT_DISCARDABLE_MANAGER_H_
#define GPU_COMMAND_BUFFER_CLIENT_CLIENT_DISCARDABLE_MANAGER_H_
#include <map>
#include <set>
#include "base/containers/queue.h"
#include "gpu/command_buffer/client/gpu_command_buffer_client_export.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "gpu/command_buffer/common/discardable_handle.h"
namespace gpu {
class GPU_COMMAND_BUFFER_CLIENT_EXPORT ClientDiscardableManager {
public:
ClientDiscardableManager();
ClientDiscardableManager(const ClientDiscardableManager&) = delete;
ClientDiscardableManager& operator=(const ClientDiscardableManager&) = delete;
~ClientDiscardableManager();
ClientDiscardableHandle::Id CreateHandle(CommandBuffer* command_buffer);
bool LockHandle(ClientDiscardableHandle::Id handle_id);
void FreeHandle(ClientDiscardableHandle::Id handle_id);
bool HandleIsValid(ClientDiscardableHandle::Id handle_id) const;
ClientDiscardableHandle GetHandle(ClientDiscardableHandle::Id handle_id);
bool HandleIsDeleted(ClientDiscardableHandle::Id handle_id);
void CheckPendingForTesting(CommandBuffer* command_buffer) {
CheckPending(command_buffer);
}
void SetElementCountForTesting(uint32_t count) {
elements_per_allocation_ = count;
allocation_size_ = count * element_size_;
}
private:
bool FindAllocation(CommandBuffer* command_buffer,
scoped_refptr<Buffer>* buffer,
int32_t* shm_id,
uint32_t* offset);
bool FindExistingAllocation(CommandBuffer* command_buffer,
scoped_refptr<Buffer>* buffer,
int32_t* shm_id,
uint32_t* offset);
void ReturnAllocation(CommandBuffer* command_buffer,
const ClientDiscardableHandle& handle);
void CheckPending(CommandBuffer* command_buffer);
bool CheckDeleted(CommandBuffer* command_buffer);
bool CreateNewAllocation(CommandBuffer* command_buffer);
private:
size_t allocation_size_;
size_t element_size_ = sizeof(base::subtle::Atomic32);
size_t elements_per_allocation_ = allocation_size_ / element_size_;
struct Allocation;
std::vector<std::unique_ptr<Allocation>> allocations_;
std::map<ClientDiscardableHandle::Id, ClientDiscardableHandle> handles_;
base::queue<ClientDiscardableHandle> pending_handles_;
};
}
#endif