/* *************************************************************
* Copyright 2017-2020 ANSYS, Inc.
* All Rights Reserved.
*
* Restricted Rights Legend
*
* Use, duplication, or disclosure of this
* software and its documentation by the
* Government is subject to restrictions as
* set forth in subdivision [(b)(3)(ii)] of
* the Rights in Technical Data and Computer
* Software clause at 52.227-7013.
* *************************************************************
*/
#ifndef ___ENSIGHT_GRPC_SHMEM_CLIENT_PRIV_H___
#define ___ENSIGHT_GRPC_SHMEM_CLIENT_PRIV_H___
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shared_memory_image_client.h"
#ifdef WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* additional, private, creation options. These are normally used by EnSight, not clients */
/* if set, the BufferHeader structures are initialized to IMAGESTREAM_BUFFER_STATE_SERVER */
#define IMAGESTREAM_OPTIONS_INITBUFFERHEADERS 0x00000001
#define IMAGESTREAM_OPTIONS_NOTHREADS 0x00000002
/* common server creation options */
#define IMAGESTREAM_OPTIONS_SERVER IMAGESTREAM_OPTIONS_INITBUFFERHEADERS|IMAGESTREAM_OPTIONS_NOTHREADS
/*
* Shared memory layout:
*
* Note: all pixels are 3 bytes RGB byte ordering, no scanline gaps/alignment
*
* base memory mapped pointer
* struct BufferHeader for buffer 0
* uint_8 data[n] image payload data
* struct BufferHeader for buffer 1
* uint_8 data[n] image payload data
*
* BufferHeader
* uint32_t frame_number
* uint32_t width in pixels of the entire frame
* uint32_t height in pixels of the entire frame
* uint32_t offset offset from the top of the image in scanlines for this block (0=end of frame)
* uint32_t num_scanlines number of scanlines of data in the current payload
* uint8_t buffer_state: 0=server owned, 1=server writing, 2=client owned, 3=client reading
*
* The server is responsible for initializing the shared memory header states.
*
*
* These are the values for 'buffer_state' flag in the BufferHeader
* structures. Only the process that has control over the buffer can
* change its state. State progression:
*
* server sets to 4
* client sets to 0
* server sets to 1
* server sets to 2
* client sets to 3
* client sets to 0
*
*/
/* 0=server has control over this buffer */
#define IMAGESTREAM_BUFFER_STATE_SERVER 0
/* 1=server has control over this buffer and it actively writing to it */
#define IMAGESTREAM_BUFFER_STATE_SERVER_LOCKED 1
/* 2=client has control over this buffer and it has content written to by the server */
#define IMAGESTREAM_BUFFER_STATE_CLIENT 2
/* 3=client has control over this buffer and has mapped it to a frame */
#define IMAGESTREAM_BUFFER_STATE_CLIENT_LOCKED 3
/* 4=(unconnected) client has control over the buffer. The server initializes the buffer
* state to this value and waits for a client to connect. When a client connects, it
* changes the state to 0 */
#define IMAGESTREAM_BUFFER_STATE_INIT 4
typedef struct {
uint32_t frame_number;
uint32_t width;
uint32_t height;
uint32_t offset;
uint32_t num_scanlines;
uint8_t buffer_state;
uint8_t pad[3];
} BufferHeader;
/*
* These are the values for the 'frame_state' flag in the _SharedMemoryImageStream
* structure.
* The frame starts in UNDEFINED state. The lcl_thread_process() thread will
* transition the frame to the READY state. SharedMemoryImageStream_lock() will
* transition the frame to the LOCKED state. SharedMemoryImageStream_unlock() will
* transition the frame back to the UNDEFINED state.
*
* Note: if the image frame is locked, any associated buffer (frame_buffer_number),
* will also be moved to IMAGESTREAM_BUFFER_STATE_CLIENT_LOCKED and then released to
* IMAGESTREAM_BUFFER_STATE_SERVER when the frame goes back to
* IMAGESTREAM_FRAME_STATE_UNDEFINED.
*/
#define IMAGESTREAM_FRAME_STATE_UNDEFINED 0
#define IMAGESTREAM_FRAME_STATE_IMAGE_READY 1
#define IMAGESTREAM_FRAME_STATE_IMAGE_LOCKED 2
struct _SharedMemoryImageStream {
/* memory mapped pointer and its length (note: this is not marked volatile as it is not
* intended to be accessed directly, but the data it points to is volatile) */
uint8_t *ptr;
size_t length;
uint8_t shutting_down;
uint8_t frame_state;
struct _SharedMemoryFrame frame;
int32_t frame_buffer_number;
/* pointers into the memory mapped space (ptr) */
BufferHeader volatile *buffer_header[2];
uint8_t *buffer_data[2];
size_t payload_length;
uint32_t options;
/* platform specific bits for memory mapped I/O, a thread and a mutex */
#ifdef WIN32
HANDLE fd;
HANDLE file_map;
CRITICAL_SECTION mutex;
HANDLE thread;
#else
int fd;
pthread_mutex_t mutex;
pthread_t thread;
#endif
};
SharedMemoryImageError SharedMemoryImageStream_putframe(SharedMemoryImageStream stream,
SharedMemoryFrame frame);
#ifdef __cplusplus
}
#endif
#endif