blob: 28a5f6f7a9b381f10da930b4cccc761c9245c63b [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef GPU_COMMAND_BUFFER_SERVICE_COMMAND_BUFFER_SERVICE_H_
6#define GPU_COMMAND_BUFFER_SERVICE_COMMAND_BUFFER_SERVICE_H_
7
8#include "base/callback.h"
9#include "base/memory/shared_memory.h"
10#include "gpu/command_buffer/common/command_buffer.h"
11#include "gpu/command_buffer/common/command_buffer_shared.h"
12
13namespace gpu {
14
15class TransferBufferManagerInterface;
16
17class GPU_EXPORT CommandBufferServiceBase : public CommandBuffer {
18 public:
19 // Sets the current get offset. This can be called from any thread.
20 virtual void SetGetOffset(int32 get_offset) = 0;
21
22 // Get the transfer buffer associated with an ID. Returns a null buffer for
23 // ID 0.
24 virtual scoped_refptr<gpu::Buffer> GetTransferBuffer(int32 id) = 0;
25
26 // Allows the reader to update the current token value.
27 virtual void SetToken(int32 token) = 0;
28
29 // Allows the reader to set the current parse error.
30 virtual void SetParseError(error::Error) = 0;
31
32 // Allows the reader to set the current context lost reason.
33 // NOTE: if calling this in conjunction with SetParseError,
34 // call this first.
35 virtual void SetContextLostReason(error::ContextLostReason) = 0;
James Robinson1ae030a2014-11-07 08:32:47 -080036
37 // Allows the reader to obtain the current put offset.
38 virtual int32 GetPutOffset() = 0;
James Robinson646469d2014-10-03 15:33:28 -070039};
40
41// An object that implements a shared memory command buffer and a synchronous
42// API to manage the put and get pointers.
43class GPU_EXPORT CommandBufferService : public CommandBufferServiceBase {
44 public:
45 typedef base::Callback<bool(int32)> GetBufferChangedCallback;
46 explicit CommandBufferService(
47 TransferBufferManagerInterface* transfer_buffer_manager);
James Robinsone1b30cf2014-10-21 12:25:40 -070048 ~CommandBufferService() override;
James Robinson646469d2014-10-03 15:33:28 -070049
50 // CommandBuffer implementation:
James Robinsone1b30cf2014-10-21 12:25:40 -070051 bool Initialize() override;
52 State GetLastState() override;
53 int32 GetLastToken() override;
54 void Flush(int32 put_offset) override;
James Robinson7b766f42015-02-06 15:14:04 -080055 void OrderingBarrier(int32 put_offset) override;
James Robinsone1b30cf2014-10-21 12:25:40 -070056 void WaitForTokenInRange(int32 start, int32 end) override;
57 void WaitForGetOffsetInRange(int32 start, int32 end) override;
58 void SetGetBuffer(int32 transfer_buffer_id) override;
59 scoped_refptr<Buffer> CreateTransferBuffer(size_t size, int32* id) override;
60 void DestroyTransferBuffer(int32 id) override;
James Robinson646469d2014-10-03 15:33:28 -070061
62 // CommandBufferServiceBase implementation:
James Robinsone1b30cf2014-10-21 12:25:40 -070063 void SetGetOffset(int32 get_offset) override;
64 scoped_refptr<Buffer> GetTransferBuffer(int32 id) override;
65 void SetToken(int32 token) override;
66 void SetParseError(error::Error error) override;
67 void SetContextLostReason(error::ContextLostReason) override;
James Robinson1ae030a2014-11-07 08:32:47 -080068 int32 GetPutOffset() override;
James Robinson646469d2014-10-03 15:33:28 -070069
70 // Sets a callback that is called whenever the put offset is changed. When
71 // called with sync==true, the callback must not return until some progress
72 // has been made (unless the command buffer is empty), i.e. the get offset
73 // must have changed. It need not process the entire command buffer though.
74 // This allows concurrency between the writer and the reader while giving the
75 // writer a means of waiting for the reader to make some progress before
76 // attempting to write more to the command buffer. Takes ownership of
77 // callback.
78 virtual void SetPutOffsetChangeCallback(const base::Closure& callback);
79 // Sets a callback that is called whenever the get buffer is changed.
80 virtual void SetGetBufferChangeCallback(
81 const GetBufferChangedCallback& callback);
82 virtual void SetParseErrorCallback(const base::Closure& callback);
83
84 // Setup the shared memory that shared state should be copied into.
85 void SetSharedStateBuffer(scoped_ptr<BufferBacking> shared_state_buffer);
86
87 // Copy the current state into the shared state transfer buffer.
88 void UpdateState();
89
90 // Registers an existing shared memory object and get an ID that can be used
91 // to identify it in the command buffer.
92 bool RegisterTransferBuffer(int32 id, scoped_ptr<BufferBacking> buffer);
93
94 private:
95 int32 ring_buffer_id_;
96 scoped_refptr<Buffer> ring_buffer_;
97 scoped_ptr<BufferBacking> shared_state_buffer_;
98 CommandBufferSharedState* shared_state_;
99 int32 num_entries_;
100 int32 get_offset_;
101 int32 put_offset_;
102 base::Closure put_offset_change_callback_;
103 GetBufferChangedCallback get_buffer_change_callback_;
104 base::Closure parse_error_callback_;
105 TransferBufferManagerInterface* transfer_buffer_manager_;
106 int32 token_;
107 uint32 generation_;
108 error::Error error_;
109 error::ContextLostReason context_lost_reason_;
110
111 DISALLOW_COPY_AND_ASSIGN(CommandBufferService);
112};
113
114} // namespace gpu
115
116#endif // GPU_COMMAND_BUFFER_SERVICE_COMMAND_BUFFER_SERVICE_H_