Fix crash in SpeechRecognizerImpl introduced in AudioParams refactor.
[chromium-blink-merge.git] / gpu / command_buffer / service / common_decoder.h
blob53de875625ad33e88d6751514a22c46525cd8102
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.
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_COMMON_DECODER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_COMMON_DECODER_H_
8 #include <map>
9 #include <stack>
10 #include <string>
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "gpu/command_buffer/common/buffer.h"
14 #include "gpu/command_buffer/service/cmd_parser.h"
15 #include "gpu/gpu_export.h"
17 // Forwardly declare a few GL types to avoid including GL header files.
18 typedef int GLsizei;
19 typedef int GLint;
21 namespace gpu {
23 class CommandBufferEngine;
25 // This class is a helper base class for implementing the common parts of the
26 // o3d/gl2 command buffer decoder.
27 class GPU_EXPORT CommonDecoder : NON_EXPORTED_BASE(public AsyncAPIInterface) {
28 public:
29 typedef error::Error Error;
31 static const unsigned int kMaxStackDepth = 32;
33 // A bucket is a buffer to help collect memory across a command buffer. When
34 // creating a command buffer implementation of an existing API, sometimes that
35 // API has functions that take a pointer to data. A good example is OpenGL's
36 // glBufferData. Because the data is separated between client and service,
37 // there are 2 ways to get this data across. 1 is to put all the data in
38 // shared memory. The problem with this is the data can be arbitarily large
39 // and the host OS may not support that much shared memory. Another solution
40 // is to shuffle memory across a little bit at a time, collecting it on the
41 // service side and when it is all there then call glBufferData. Buckets
42 // implement this second solution. Using the common commands, SetBucketSize,
43 // SetBucketData, SetBucketDataImmediate the client can fill a bucket. It can
44 // then call a command that uses that bucket (like BufferDataBucket in the
45 // GLES2 command buffer implementation).
47 // If you are designing an API from scratch you can avoid this need for
48 // Buckets by making your API always take an offset and a size
49 // similar to glBufferSubData.
51 // Buckets also help pass strings to/from the service. To return a string of
52 // arbitary size, the service puts the string in a bucket. The client can
53 // then query the size of a bucket and request sections of the bucket to
54 // be passed across shared memory.
55 class GPU_EXPORT Bucket {
56 public:
57 Bucket();
58 ~Bucket();
60 size_t size() const {
61 return size_;
64 // Gets a pointer to a section the bucket. Returns NULL if offset or size is
65 // out of range.
66 void* GetData(size_t offset, size_t size) const;
68 template <typename T>
69 T GetDataAs(size_t offset, size_t size) const {
70 return reinterpret_cast<T>(GetData(offset, size));
73 // Sets the size of the bucket.
74 void SetSize(size_t size);
76 // Sets a part of the bucket.
77 // Returns false if offset or size is out of range.
78 bool SetData(const void* src, size_t offset, size_t size);
80 // Sets the bucket data from a string. Strings are passed NULL terminated to
81 // distinguish between empty string and no string.
82 void SetFromString(const char* str);
84 // Gets the bucket data as a string. Strings are passed NULL terminated to
85 // distrinquish between empty string and no string. Returns False if there
86 // is no string.
87 bool GetAsString(std::string* str);
89 // Gets the bucket data as strings.
90 // On success, the number of strings are in |_count|, the string data are
91 // in |_string|, and string sizes are in |_length|..
92 bool GetAsStrings(GLsizei* _count,
93 std::vector<char*>* _string,
94 std::vector<GLint>* _length);
96 private:
97 bool OffsetSizeValid(size_t offset, size_t size) const {
98 size_t temp = offset + size;
99 return temp <= size_ && temp >= offset;
102 size_t size_;
103 ::scoped_ptr<int8[]> data_;
105 DISALLOW_COPY_AND_ASSIGN(Bucket);
108 CommonDecoder();
109 ~CommonDecoder() override;
111 // Sets the engine, to get shared memory buffers from, and to set the token
112 // to.
113 void set_engine(CommandBufferEngine* engine) {
114 engine_ = engine;
116 CommandBufferEngine* engine() const { return engine_; }
118 // Creates a bucket. If the bucket already exists returns that bucket.
119 Bucket* CreateBucket(uint32 bucket_id);
121 // Gets a bucket. Returns NULL if the bucket does not exist.
122 Bucket* GetBucket(uint32 bucket_id) const;
124 // Gets the address of shared memory data, given a shared memory ID and an
125 // offset. Also checks that the size is consistent with the shared memory
126 // size.
127 // Parameters:
128 // shm_id: the id of the shared memory buffer.
129 // offset: the offset of the data in the shared memory buffer.
130 // size: the size of the data.
131 // Returns:
132 // NULL if shm_id isn't a valid shared memory buffer ID or if the size
133 // check fails. Return a pointer to the data otherwise.
134 void* GetAddressAndCheckSize(unsigned int shm_id,
135 unsigned int offset,
136 unsigned int size);
138 // Typed version of GetAddressAndCheckSize.
139 template <typename T>
140 T GetSharedMemoryAs(unsigned int shm_id, unsigned int offset,
141 unsigned int size) {
142 return static_cast<T>(GetAddressAndCheckSize(shm_id, offset, size));
145 // Get the actual shared memory buffer.
146 scoped_refptr<gpu::Buffer> GetSharedMemoryBuffer(unsigned int shm_id);
148 protected:
149 // Executes a common command.
150 // Parameters:
151 // command: the command index.
152 // arg_count: the number of CommandBufferEntry arguments.
153 // cmd_data: the command data.
154 // Returns:
155 // error::kNoError if no error was found, one of
156 // error::Error otherwise.
157 error::Error DoCommonCommand(
158 unsigned int command,
159 unsigned int arg_count,
160 const void* cmd_data);
162 // Gets an name for a common command.
163 const char* GetCommonCommandName(cmd::CommandId command_id) const;
165 private:
166 // Generate a member function prototype for each command in an automated and
167 // typesafe way.
168 #define COMMON_COMMAND_BUFFER_CMD_OP(name) \
169 error::Error Handle##name(uint32 immediate_data_size, const void* data);
171 COMMON_COMMAND_BUFFER_CMDS(COMMON_COMMAND_BUFFER_CMD_OP)
173 #undef COMMON_COMMAND_BUFFER_CMD_OP
175 CommandBufferEngine* engine_;
177 typedef std::map<uint32, linked_ptr<Bucket> > BucketMap;
178 BucketMap buckets_;
180 typedef Error (CommonDecoder::*CmdHandler)(
181 uint32 immediate_data_size,
182 const void* data);
184 // A struct to hold info about each command.
185 struct CommandInfo {
186 CmdHandler cmd_handler;
187 uint8 arg_flags; // How to handle the arguments for this command
188 uint8 cmd_flags; // How to handle this command
189 uint16 arg_count; // How many arguments are expected for this command.
192 // A table of CommandInfo for all the commands.
193 static const CommandInfo command_info[];
197 } // namespace gpu
199 #endif // GPU_COMMAND_BUFFER_SERVICE_COMMON_DECODER_H_