Skip to main content

Post-processing tools 2023 R2

shared_memory_image_client_priv.h

Last update: 17.04.2023
Go to the documentation of this file.
1 /* *************************************************************
2  * Copyright 2017-2020 ANSYS, Inc.
3  * All Rights Reserved.
4  *
5  * Restricted Rights Legend
6  *
7  * Use, duplication, or disclosure of this
8  * software and its documentation by the
9  * Government is subject to restrictions as
10  * set forth in subdivision [(b)(3)(ii)] of
11  * the Rights in Technical Data and Computer
12  * Software clause at 52.227-7013.
13  * *************************************************************
14  */
15 
16 #ifndef ___ENSIGHT_GRPC_SHMEM_CLIENT_PRIV_H___
17 #define ___ENSIGHT_GRPC_SHMEM_CLIENT_PRIV_H___
18 
109 #include <stdio.h>
110 #include <stdlib.h>
111 #include <string.h>
112 
114 
115 #ifdef WIN32
116 #include <windows.h>
117 #else
118 #include <sys/types.h>
119 #include <sys/stat.h>
120 #include <fcntl.h>
121 #include <sys/mman.h>
122 #include <pthread.h>
123 #include <unistd.h>
124 #endif
125 
126 #ifdef __cplusplus
127 extern "C" {
128 #endif
129 
130 /* additional, private, creation options. These are normally used by EnSight, not clients */
131 /* if set, the BufferHeader structures are initialized to IMAGESTREAM_BUFFER_STATE_SERVER */
132 #define IMAGESTREAM_OPTIONS_INITBUFFERHEADERS 0x00000001
133 #define IMAGESTREAM_OPTIONS_NOTHREADS 0x00000002
134 /* common server creation options */
135 #define IMAGESTREAM_OPTIONS_SERVER IMAGESTREAM_OPTIONS_INITBUFFERHEADERS|IMAGESTREAM_OPTIONS_NOTHREADS
138 
139  /*
140  * Shared memory layout:
141  *
142  * Note: all pixels are 3 bytes RGB byte ordering, no scanline gaps/alignment
143  *
144  * base memory mapped pointer
145  * struct BufferHeader for buffer 0
146  * uint_8 data[n] image payload data
147  * struct BufferHeader for buffer 1
148  * uint_8 data[n] image payload data
149  *
150  * BufferHeader
151  * uint32_t frame_number
152  * uint32_t width in pixels of the entire frame
153  * uint32_t height in pixels of the entire frame
154  * uint32_t offset offset from the top of the image in scanlines for this block (0=end of frame)
155  * uint32_t num_scanlines number of scanlines of data in the current payload
156  * uint8_t buffer_state: 0=server owned, 1=server writing, 2=client owned, 3=client reading
157  *
158  * The server is responsible for initializing the shared memory header states.
159  *
160  *
161  * These are the values for 'buffer_state' flag in the BufferHeader
162  * structures. Only the process that has control over the buffer can
163  * change its state. State progression:
164  *
165  * server sets to 4
166  * client sets to 0
167  * server sets to 1
168  * server sets to 2
169  * client sets to 3
170  * client sets to 0
171  *
172  */
173 
174 /* 0=server has control over this buffer */
175 #define IMAGESTREAM_BUFFER_STATE_SERVER 0
176 /* 1=server has control over this buffer and it actively writing to it */
177 #define IMAGESTREAM_BUFFER_STATE_SERVER_LOCKED 1
178 /* 2=client has control over this buffer and it has content written to by the server */
179 #define IMAGESTREAM_BUFFER_STATE_CLIENT 2
180 /* 3=client has control over this buffer and has mapped it to a frame */
181 #define IMAGESTREAM_BUFFER_STATE_CLIENT_LOCKED 3
182 /* 4=(unconnected) client has control over the buffer. The server initializes the buffer
183  * state to this value and waits for a client to connect. When a client connects, it
184  * changes the state to 0 */
185 #define IMAGESTREAM_BUFFER_STATE_INIT 4
186 
187 typedef struct {
188  uint32_t frame_number;
189  uint32_t width;
190  uint32_t height;
191  uint32_t offset;
192  uint32_t num_scanlines;
193  uint8_t buffer_state;
194  uint8_t pad[3];
195 } BufferHeader;
196 
197 /*
198  * These are the values for the 'frame_state' flag in the _SharedMemoryImageStream
199  * structure.
200  * The frame starts in UNDEFINED state. The lcl_thread_process() thread will
201  * transition the frame to the READY state. SharedMemoryImageStream_lock() will
202  * transition the frame to the LOCKED state. SharedMemoryImageStream_unlock() will
203  * transition the frame back to the UNDEFINED state.
204  *
205  * Note: if the image frame is locked, any associated buffer (frame_buffer_number),
206  * will also be moved to IMAGESTREAM_BUFFER_STATE_CLIENT_LOCKED and then released to
207  * IMAGESTREAM_BUFFER_STATE_SERVER when the frame goes back to
208  * IMAGESTREAM_FRAME_STATE_UNDEFINED.
209  */
210 #define IMAGESTREAM_FRAME_STATE_UNDEFINED 0
211 #define IMAGESTREAM_FRAME_STATE_IMAGE_READY 1
212 #define IMAGESTREAM_FRAME_STATE_IMAGE_LOCKED 2
213 
214 struct _SharedMemoryImageStream {
215  /* memory mapped pointer and its length (note: this is not marked volatile as it is not
216  * intended to be accessed directly, but the data it points to is volatile) */
217  uint8_t *ptr;
218  size_t length;
219  uint8_t shutting_down;
220  uint8_t frame_state;
221  struct _SharedMemoryFrame frame;
222  int32_t frame_buffer_number;
223  /* pointers into the memory mapped space (ptr) */
224  BufferHeader volatile *buffer_header[2];
225  uint8_t *buffer_data[2];
226  size_t payload_length;
227 
228  uint32_t options;
229  /* platform specific bits for memory mapped I/O, a thread and a mutex */
230 #ifdef WIN32
231  HANDLE fd;
232  HANDLE file_map;
233  CRITICAL_SECTION mutex;
234  HANDLE thread;
235 #else
236  int fd;
237  pthread_mutex_t mutex;
238  pthread_t thread;
239 #endif
240 };
241 
243 
265  SharedMemoryFrame frame);
266 
267 #ifdef __cplusplus
268 }
269 #endif
270 
271 #endif
C API for using the EnSight grpc shared memory image transport API.
int SharedMemoryImageError
common error codes see the non-hex, non-string IMAGESTREAM macros
struct _SharedMemoryImageStream * SharedMemoryImageStream
image transport stream pointer
DEV SharedMemoryImageError SharedMemoryImageStream_putframe(SharedMemoryImageStream stream, SharedMemoryFrame frame)
SharedMemoryImageStream_putframe push a new image into a server image stream channel.