#ifndef GPU_COMMAND_BUFFER_SERVICE_COMMAND_BUFFER_SERVICE_H_
#define GPU_COMMAND_BUFFER_SERVICE_COMMAND_BUFFER_SERVICE_H_
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "gpu/command_buffer/common/command_buffer_shared.h"
#include "gpu/command_buffer/service/async_api_interface.h"
#include "gpu/command_buffer/service/gpu_command_buffer_service_export.h"
namespace gpu {
class MemoryTracker;
class TransferBufferManager;
class GPU_COMMAND_BUFFER_SERVICE_EXPORT CommandBufferServiceBase {
public:
virtual ~CommandBufferServiceBase() = default;
virtual CommandBuffer::State GetState() = 0;
virtual void SetReleaseCount(uint64_t release_count) = 0;
virtual scoped_refptr<gpu::Buffer> GetTransferBuffer(int32_t id) = 0;
virtual void SetToken(int32_t token) = 0;
virtual void SetParseError(error::Error) = 0;
virtual void SetContextLostReason(error::ContextLostReason) = 0;
};
class GPU_COMMAND_BUFFER_SERVICE_EXPORT CommandBufferServiceClient {
public:
enum CommandBatchProcessedResult {
kContinueExecution,
kPauseExecution,
};
virtual ~CommandBufferServiceClient() = default;
virtual CommandBatchProcessedResult OnCommandBatchProcessed() = 0;
virtual void OnParseError() = 0;
};
union CommandBufferEntry;
class GPU_COMMAND_BUFFER_SERVICE_EXPORT CommandBufferService
: public CommandBufferServiceBase {
public:
static const int kParseCommandsSliceSmall = 20;
static const int kParseCommandsSliceLarge = 100;
CommandBufferService(CommandBufferServiceClient* client,
scoped_refptr<MemoryTracker> memory_tracker);
CommandBufferService(const CommandBufferService&) = delete;
CommandBufferService& operator=(const CommandBufferService&) = delete;
~CommandBufferService() override;
CommandBuffer::State GetState() override;
void SetReleaseCount(uint64_t release_count) override;
scoped_refptr<Buffer> GetTransferBuffer(int32_t id) override;
void SetToken(int32_t token) override;
void SetParseError(error::Error error) override;
void SetContextLostReason(error::ContextLostReason) override;
void SetSharedStateBuffer(std::unique_ptr<BufferBacking> shared_state_buffer);
void UpdateState();
void Flush(int32_t put_offset, AsyncAPIInterface* handler);
void SetGetBuffer(int32_t transfer_buffer_id);
bool RegisterTransferBuffer(int32_t id, scoped_refptr<Buffer> buffer);
void DestroyTransferBuffer(int32_t id);
scoped_refptr<Buffer> CreateTransferBuffer(uint32_t size,
int32_t* id,
uint32_t alignment = 0);
scoped_refptr<Buffer> CreateTransferBufferWithId(uint32_t size,
int32_t id,
uint32_t alignment = 0);
void SetScheduled(bool scheduled);
bool scheduled() const { return scheduled_; }
int32_t put_offset() const { return put_offset_; }
void SetGetOffsetForTest(int32_t get_offset) {
state_.get_offset = get_offset;
}
size_t GetSharedMemoryBytesAllocated() const;
bool ShouldYield();
private:
raw_ptr<CommandBufferServiceClient> client_;
std::unique_ptr<TransferBufferManager> transfer_buffer_manager_;
CommandBuffer::State state_;
int32_t put_offset_ = 0;
int32_t num_entries_ = 0;
scoped_refptr<Buffer> ring_buffer_;
raw_ptr<volatile CommandBufferEntry, AllowPtrArithmetic> buffer_ = nullptr;
std::unique_ptr<BufferBacking> shared_state_buffer_;
raw_ptr<CommandBufferSharedState> shared_state_ = nullptr;
bool scheduled_ = true;
bool paused_ = false;
};
}
#endif