summaryrefslogtreecommitdiffstats
path: root/o3d/core/cross/message_commands.h
blob: ce92d5923f2d45aaf76b9d2b0cfae24079f5e4ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright 2009, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// This file contains the declarations of various IMC messages for O3D.

#ifndef O3D_CORE_CROSS_MESSAGE_COMMANDS_H_
#define O3D_CORE_CROSS_MESSAGE_COMMANDS_H_

#include "core/cross/types.h"
#include "core/cross/packing.h"

namespace o3d {

// Make sure the compiler does not add extra padding to any of the message
// structures.
O3D_PUSH_STRUCTURE_PACKING_1;

// This macro is used to safely and convienently expand the list of possible IMC
// messages in to various lists and never have them get out of sync. To add a
// new message add a this list, the first argument is the enum Id, the second
// argument is the name of the structure that describes the message. Once you've
// added it to this list, create the structure below and then add a function in
// message_queue.cc called ProcessMessageStructureName where
// MessageStructureName is the name of your message structure.
//
// NOTE: THE ORDER OF THESE MUST NOT CHANGE (their id is derived by order)
#define O3D_IMC_MESSAGE_LIST(OP) \
  OP(INVALID_ID, MessageInvalidId) \
  OP(HELLO, MessageHello) \
  OP(ALLOCATE_SHARED_MEMORY, MessageAllocateSharedMemory) \
  OP(UPDATE_TEXTURE2D, MessageUpdateTexture2D) \
  OP(REGISTER_SHARED_MEMORY, MessageRegisterSharedMemory) \
  OP(UNREGISTER_SHARED_MEMORY, MessageUnregisterSharedMemory) \
  OP(UPDATE_TEXTURE2D_RECT, MessageUpdateTexture2DRect) \
  OP(RENDER, MessageRender) \


namespace imc {
enum MessageId {
  #define O3D_IMC_MESSAGE_OP(id, class_name)  id,
  O3D_IMC_MESSAGE_LIST(O3D_IMC_MESSAGE_OP)
  #undef O3D_IMC_MESSAGE_OP

  MAX_NUM_IDS,

  ID_FORCE_DWORD = 0x7fffffff  // Forces a 32-bit size enum
};

// Returns a string by message ID.
const char* GetMessageDescription(MessageId id);
};

// An invalid message. This is mostly a place holder for id 0.
struct MessageInvalidId {
  // Message Content.
  struct Msg {
    static const imc::MessageId kMessageId = imc::INVALID_ID;

    imc::MessageId message_id;
  };

  MessageInvalidId() {
    msg.message_id = Msg::kMessageId;
  }

  Msg msg;
};

// The first message you send.
struct MessageHello {
  // Message Content.
  struct Msg {
    static const imc::MessageId kMessageId = imc::HELLO;

    imc::MessageId message_id;
  };

  MessageHello() {
    msg.message_id = Msg::kMessageId;
  }

  Msg msg;
};

// A message to allocate shared memory
struct MessageAllocateSharedMemory {
  // Message Content.
  struct Msg {
    static const imc::MessageId kMessageId = imc::ALLOCATE_SHARED_MEMORY;
    static const int32 kMaxSharedMemSize = 1024 * 1024 * 128;   // 128MB

    imc::MessageId message_id;

    // The amount of memory to allocate.
    int32 mem_size;
  };

  MessageAllocateSharedMemory() {
    msg.message_id = Msg::kMessageId;
  }

  // Parameters:
  //   in_mem_size: The number of bytes to allocate.
  explicit MessageAllocateSharedMemory(int32 in_mem_size) {
    msg.message_id = Msg::kMessageId;
    msg.mem_size = in_mem_size;
  }

  Msg msg;
};

// A message to update the entire contents of a 2D texture. The number
// of bytes MUST equal the size of the entire texture to be updated including
// all mips.
struct MessageUpdateTexture2D {
  // Message Content.
  struct Msg {
    static const imc::MessageId kMessageId = imc::UPDATE_TEXTURE2D;

    imc::MessageId message_id;

    // The id of the texture to set.
    Id texture_id;

    // The mip level of the texture to set.
    int32 level;

    // The id of the shared memory the contains the data to use to set the
    // texture.
    int32 shared_memory_id;

    // The offset inside the shared memory where the texture data starts.
    int32 offset;

    // The number of bytes to get out of shared memory.
    // NOTE: this number MUST match the size of the texture. For example for an
    // ARGB texture it must be mip_width * mip_height * 4 * sizeof(uint8)
    int32 number_of_bytes;
  };

  MessageUpdateTexture2D() {
    msg.message_id = Msg::kMessageId;
  }

  // Parameters:
  //   in_texture_id: The id of the texture to set.
  //   in_level: The mip level of the texture to set.
  //   in_shared_memory_id: The id of the shared memory the contains the data
  //       to use to set the texture.
  //   in_offset: The offset inside the shared memory where the texture data
  //       starts.
  //   in_number_of_bytes: The number of bytes to get out of shared memory.
  //       NOTE: this number MUST match the size of the texture. For example for
  //       an ARGB texture it must be mip_width * mip_height * 4 * sizeof(uint8)
  MessageUpdateTexture2D(Id in_texture_id,
                         int32 in_level,
                         int32 in_shared_memory_id,
                         int32 in_offset,
                         int32 in_number_of_bytes) {
    msg.message_id = Msg::kMessageId;
    msg.texture_id = in_texture_id;
    msg.level = in_level;
    msg.shared_memory_id = in_shared_memory_id;
    msg.offset = in_offset;
    msg.number_of_bytes = in_number_of_bytes;
  }

  Msg msg;
};

// A message to register shared memory.
struct MessageRegisterSharedMemory {
  // Message Content.
  struct Msg {
    static const imc::MessageId kMessageId = imc::REGISTER_SHARED_MEMORY;
    static const int32 kMaxSharedMemSize = 1024 * 1024 * 128;   // 128MB

    imc::MessageId message_id;
    int32 mem_size;
  };

  MessageRegisterSharedMemory() {
    msg.message_id = Msg::kMessageId;
  }

  explicit MessageRegisterSharedMemory(int32 in_mem_size) {
    msg.message_id = Msg::kMessageId;
    msg.mem_size = in_mem_size;
  }

  Msg msg;
};

// A message to unregister shared memory.
struct MessageUnregisterSharedMemory {
  // Message Content.
  struct Msg {
    static const imc::MessageId kMessageId = imc::UNREGISTER_SHARED_MEMORY;

    imc::MessageId message_id;
    int32 buffer_id;
  };

  MessageUnregisterSharedMemory() {
    msg.message_id = Msg::kMessageId;
  }

  // Parameters:
  //   in_buffer_id: The id of the buffer to unregister.
  explicit MessageUnregisterSharedMemory(int32 in_buffer_id) {
    msg.message_id = Msg::kMessageId;
    msg.buffer_id = in_buffer_id;
  }

  Msg msg;
};

// A message to update a portion of a 2D texture. The number of bytes MUST equal
// the size of the portion of the texture to be updated.
struct MessageUpdateTexture2DRect {
  // Message Content.
  struct Msg {
    static const imc::MessageId kMessageId = imc::UPDATE_TEXTURE2D_RECT;

    imc::MessageId message_id;

    // The id of the texture to set.
    Id texture_id;

    // The mip level of the texture to set.
    int32 level;

    // The left edge of the rectangle to update in the texture.
    int32 x;

    // The top edge of the rectangle to update in the texture.
    int32 y;

    // The width of the rectangle to update in the texture.
    int32 width;

    // The height of the rectangle to update in the texture.
    int32 height;

    // The id of the shared memory the contains the data to use to set the
    // texture.
    int32 shared_memory_id;

    // The offset inside the shared memory where the texture data starts.
    int32 offset;

    // The number of bytes bytes across 1 row in the source data.
    int32 pitch;
  };

  MessageUpdateTexture2DRect() {
    msg.message_id = Msg::kMessageId;
  }

  // Parameters:
  //   in_texture_id: The id of the texture to set.
  //   in_level: The mip level of the texture to set.
  //   in_x: The left edge of the rectangle to update in the texture.
  //   in_y: The top edge of the rectangle to update in the texture.
  //   in_width: The width of the rectangle to update in the texture.
  //   in_height: The height of the rectangle to update in the texture.
  //   in_shared_memory_id: The id of the shared memory the contains the data to
  //       use to set the texture.
  //   in_offset: The offset inside the shared memory where the texture data
  //       starts.
  //   in_pitch: The number of bytes bytes across 1 row in the source data.
  MessageUpdateTexture2DRect(Id in_texture_id,
                             int32 in_level,
                             int32 in_x,
                             int32 in_y,
                             int32 in_width,
                             int32 in_height,
                             int32 in_shared_memory_id,
                             int32 in_offset,
                             int32 in_pitch) {
    msg.message_id = Msg::kMessageId;
    msg.texture_id = in_texture_id;
    msg.level = in_level;
    msg.x = in_x;
    msg.y = in_y;
    msg.width = in_width;
    msg.height = in_height;
    msg.shared_memory_id = in_shared_memory_id;
    msg.offset = in_offset;
    msg.pitch = in_pitch;
  }

  Msg msg;
};

// Tell O3D to render. This is generally used when O3D is in Render on demand
// mode.
struct MessageRender {
  // Message Content.
  struct Msg {
    static const imc::MessageId kMessageId = imc::RENDER;

    imc::MessageId message_id;
  };

  MessageRender() {
    msg.message_id = Msg::kMessageId;
  }

  Msg msg;
};


O3D_POP_STRUCTURE_PACKING;

}  // namespace o3d

#endif  // O3D_CORE_CROSS_MESSAGE_COMMANDS_H_