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  explicit CommandParser(AsyncAPIInterface* handler);
23
24  // Sets the buffer to read commands from.
25  void SetBuffer(
26      void* shm_address,
27      size_t shm_size,
28      ptrdiff_t offset,
29      size_t size);
30
31  // Gets the "get" pointer. The get pointer is an index into the command
32  // buffer considered as an array of CommandBufferEntry.
33  CommandBufferOffset get() const { return get_; }
34
35  // Sets the "get" pointer. The get pointer is an index into the command buffer
36  // considered as an array of CommandBufferEntry.
37  bool set_get(CommandBufferOffset get) {
38    if (get >= 0 && get < entry_count_) {
39      get_ = get;
40      return true;
41    }
42    return false;
43  }
44
45  // Sets the "put" pointer. The put pointer is an index into the command
46  // buffer considered as an array of CommandBufferEntry.
47  void set_put(CommandBufferOffset put) { put_ = put; }
48
49  // Gets the "put" pointer. The put pointer is an index into the command
50  // buffer considered as an array of CommandBufferEntry.
51  CommandBufferOffset put() const { return put_; }
52
53  // Checks whether there are commands to process.
54  bool IsEmpty() const { return put_ == get_; }
55
56  // Processes one command, updating the get pointer. This will return an error
57  // if there are no commands in the buffer.
58  error::Error ProcessCommand();
59
60  // Processes all commands until get == put.
61  error::Error ProcessAllCommands();
62
63  // Reports an error.
64  void ReportError(unsigned int command_id, error::Error result);
65
66 private:
67  CommandBufferOffset get_;
68  CommandBufferOffset put_;
69  CommandBufferEntry* buffer_;
70  int32 entry_count_;
71  AsyncAPIInterface* handler_;
72  bool trace_gl_commands_;
73};
74
75// This class defines the interface for an asynchronous API handler, that
76// is responsible for de-multiplexing commands and their arguments.
77class AsyncAPIInterface {
78 public:
79  AsyncAPIInterface() {}
80  virtual ~AsyncAPIInterface() {}
81
82  // Executes a command.
83  // Parameters:
84  //    command: the command index.
85  //    arg_count: the number of CommandBufferEntry arguments.
86  //    cmd_data: the command data.
87  // Returns:
88  //   error::kNoError if no error was found, one of
89  //   error::Error otherwise.
90  virtual error::Error DoCommand(
91      unsigned int command,
92      unsigned int arg_count,
93      const void* cmd_data) = 0;
94
95  // Returns a name for a command. Useful for logging / debuging.
96  virtual const char* GetCommandName(unsigned int command_id) const = 0;
97};
98
99}  // namespace gpu
100
101#endif  // GPU_COMMAND_BUFFER_SERVICE_CMD_PARSER_H_
102