1// 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// This file contains the command parser class.
6
7#ifndef GPU_COMMAND_BUFFER_SERVICE_CMD_PARSER_H_
8#define GPU_COMMAND_BUFFER_SERVICE_CMD_PARSER_H_
9
10#include "gpu/command_buffer/common/constants.h"
11#include "gpu/command_buffer/common/cmd_buffer_common.h"
12#include "gpu/gpu_export.h"
13
14namespace gpu {
15
16class AsyncAPIInterface;
17
18// Command parser class. This class parses commands from a shared memory
19// buffer, to implement some asynchronous RPC mechanism.
20class GPU_EXPORT CommandParser {
21 public:
22  static const int kParseCommandsSlice = 20;
23
24  explicit CommandParser(AsyncAPIInterface* handler);
25
26  // Sets the buffer to read commands from.
27  void SetBuffer(
28      void* shm_address,
29      size_t shm_size,
30      ptrdiff_t offset,
31      size_t size);
32
33  // Gets the "get" pointer. The get pointer is an index into the command
34  // buffer considered as an array of CommandBufferEntry.
35  CommandBufferOffset get() const { return get_; }
36
37  // Sets the "get" pointer. The get pointer is an index into the command buffer
38  // considered as an array of CommandBufferEntry.
39  bool set_get(CommandBufferOffset get) {
40    if (get >= 0 && get < entry_count_) {
41      get_ = get;
42      return true;
43    }
44    return false;
45  }
46
47  // Sets the "put" pointer. The put pointer is an index into the command
48  // buffer considered as an array of CommandBufferEntry.
49  void set_put(CommandBufferOffset put) { put_ = put; }
50
51  // Gets the "put" pointer. The put pointer is an index into the command
52  // buffer considered as an array of CommandBufferEntry.
53  CommandBufferOffset put() const { return put_; }
54
55  // Checks whether there are commands to process.
56  bool IsEmpty() const { return put_ == get_; }
57
58  // Processes one command, updating the get pointer. This will return an error
59  // if there are no commands in the buffer.
60  error::Error ProcessCommands(int num_commands);
61
62  // Processes all commands until get == put.
63  error::Error ProcessAllCommands();
64
65 private:
66  CommandBufferOffset get_;
67  CommandBufferOffset put_;
68  CommandBufferEntry* buffer_;
69  int32 entry_count_;
70  AsyncAPIInterface* handler_;
71};
72
73// This class defines the interface for an asynchronous API handler, that
74// is responsible for de-multiplexing commands and their arguments.
75class GPU_EXPORT AsyncAPIInterface {
76 public:
77  AsyncAPIInterface() {}
78  virtual ~AsyncAPIInterface() {}
79
80  // Executes a single command.
81  // Parameters:
82  //    command: the command index.
83  //    arg_count: the number of CommandBufferEntry arguments.
84  //    cmd_data: the command data.
85  // Returns:
86  //   error::kNoError if no error was found, one of
87  //   error::Error otherwise.
88  virtual error::Error DoCommand(
89      unsigned int command,
90      unsigned int arg_count,
91      const void* cmd_data) = 0;
92
93  // Executes multiple commands.
94  // Parameters:
95  //    num_commands: maximum number of commands to execute from buffer.
96  //    buffer: pointer to first command entry to process.
97  //    num_entries: number of sequential command buffer entries in buffer.
98  //    entries_processed: if not 0, is set to the number of entries processed.
99  virtual error::Error DoCommands(unsigned int num_commands,
100                                  const void* buffer,
101                                  int num_entries,
102                                  int* entries_processed);
103
104  // Returns a name for a command. Useful for logging / debuging.
105  virtual const char* GetCommandName(unsigned int command_id) const = 0;
106};
107
108}  // namespace gpu
109
110#endif  // GPU_COMMAND_BUFFER_SERVICE_CMD_PARSER_H_
111