////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2022-2023 Qualcomm Technologies, Inc.
// All Rights Reserved.
// Confidential and Proprietary - Qualcomm Technologies, Inc.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @file  chicommondefs.h
/// @brief Defines Chi Common Tag interface
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef CHICOMMONDEFS_H
#define CHICOMMONDEFS_H

#include <string.h>
#include "camxcdktypes.h"

// use generated chicommondefs definitions
#include "g_chicommondefs.h"

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

#pragma pack(push, 8)

typedef UINT32         GrallocUsage;                     ///< Alternative name for UINT32. This type of flag supports only 32 flags.
typedef UINT64         GrallocUsage64;                   ///< Alternative name for UINT64. This type of flag supports only 64 flags.
typedef UINT64         GrallocConsumerUsage;             ///< Alternative name for UINT64.
                                                         ///  This type of flag is used to set only consumer flags.
typedef UINT64         GrallocProducerUsage;             ///< Alternative name for UINT64.
                                                         ///  This type of flag is used to set only producer flags.

typedef VOID*          CHIFENCEHANDLE;                   ///< Handle to a CHI fence.
typedef VOID*          CHIMETADATAHANDLE;                ///< Handle to a CHI metadata.
typedef VOID*          CHIBUFFERMANAGERHANDLE;           ///< Handle to a buffer manager.
typedef VOID*          CHIBUFFERHANDLE;                  ///< Handle to a buffer.

static const GrallocUsage64 GrallocUsageSwReadNever       = 0x00000000;  ///< Buffer is never read in the software.
static const GrallocUsage64 GrallocUsageSwReadRarely      = 0x00000002;  ///< Buffer is rarely read in the software.
static const GrallocUsage64 GrallocUsageSwReadOften       = 0x00000003;  ///< Buffer is often read in the software.
static const GrallocUsage64 GrallocUsageSwReadMask        = 0x0000000F;  ///< Mask for the software read values.

static const GrallocUsage64 GrallocUsageSwWriteNever      = 0x00000000;  ///< Buffer is never written in the software.
static const GrallocUsage64 GrallocUsageSwWriteRarely     = 0x00000020;  ///< Buffer is rarely written in the software.
static const GrallocUsage64 GrallocUsageSwWriteOften      = 0x00000030;  ///< Buffer is often written in the software.
static const GrallocUsage64 GrallocUsageSwWriteMask       = 0x000000F0;  ///< Mask for the software write values.

static const GrallocUsage64 GrallocUsageHwTexture         = 0x00000100;  ///< Buffer is used as an OpenGL ES texture.
static const GrallocUsage64 GrallocUsageHwRender          = 0x00000200;  ///< Buffer is used as an OpenGL ES render target.
static const GrallocUsage64 GrallocUsageHw2D              = 0x00000400;  ///< Buffer is used by the 2D hardware blitter.
static const GrallocUsage64 GrallocUsageHwComposer        = 0x00000800;  ///< Buffer is used by the Hardware Composer module.
static const GrallocUsage64 GrallocUsageHwFrameBuffer     = 0x00001000;  ///< Buffer is used with the frame buffer device.

static const GrallocUsage64 GrallocUsageExternalDisplay   = 0x00002000;  ///< Buffer is to be displayed full screen on an
                                                                         ///  external display when possible.

static const GrallocUsage64 GrallocUsageProtected         = 0x00004000;  ///< Buffer must have a hardware-protected path to an
                                                                         ///  external display sink.
                                                                         ///
                                                                         ///  If a hardware-protected path is not available,
                                                                         ///  following are the options:
                                                                         ///  - Do not composite only this buffer to the
                                                                         ///    external sink (preferred).
                                                                         ///  - Or, do not route the entire composition to the
                                                                         ///    external sink (not preferred).

static const GrallocUsage64 GrallocUsageCursor            = 0x00008000;  ///< Buffer can be used as a cursor.

static const GrallocUsage64 GrallocUsageHwVideoEncoder    = 0x00010000;  ///< Buffer is used with the hardware video encoder.
static const GrallocUsage64 GrallocUsageHwCameraWrite     = 0x00020000;  ///< Buffer is written by the hardware camera
                                                                         ///  pipeline.
static const GrallocUsage64 GrallocUsageHwCameraRead      = 0x00040000;  ///< Buffer is read by the hardware camera pipeline.
static const GrallocUsage64 GrallocUsageHwCameraZSL       = 0x00060000;  ///< Buffer is used in a zero-shutter-lag queue.
static const GrallocUsage64 GrallocUsageHwCameraMask      = 0x00060000;  ///< Mask for the camera access values.
static const GrallocUsage64 GrallocUsageHwMask            = 0x00071F00;  ///< Mask for the software usage bit mask.

static const GrallocUsage64 GrallocUsageRenderScript      = 0x00100000;  ///< Buffer is used as a RenderScript Allocation.

static const GrallocUsage   GrallocUsageForeignBuffers    = 0x00200000;  ///< Consumer indicates to the producer that the
                                                                         ///  producer can attach a buffer that it did not
                                                                         ///  detach from the buffer queue.
                                                                         ///
                                                                         ///  Typically, gralloc handles all the buffer
                                                                         ///  allocation, attach, deallocation, and detach
                                                                         ///  operations. However, when this flag is set to
                                                                         ///  TRUE, the buffer is owned by an external entity.
                                                                         ///  For such buffers,gralloc does not handle any of
                                                                         ///  these operations.

static const GrallocUsage64 GrallocUsageHwImageEncoder    = 0x8000000;   ///< Mask for the image encoding HEIF format.
static const GrallocUsage GrallocUsageAllocMask = ~(GrallocUsageForeignBuffers);  ///< Mask of all flags that can be passed to
                                                                                  ///  a gralloc module for buffer allocation.
                                                                                  ///  The gralloc modules are not required to
                                                                                  ///  handle flags in this mask.
static const GrallocUsage64 GrallocUsagePrivateCDSP     = (1ULL << 50);  ///< Set while the cDSP accesses the buffer.

static const UINT32 MaxNumVideoSnapshotStreams = 2;     ///< Maximum number of video snapshot stream intent assigned to streams

/// @brief Buffer Heap
const UINT32 BufferHeapSystem         = 0;
const UINT32 BufferHeapIon            = 1;
const UINT32 BufferHeapDSP            = 2;
const UINT32 BufferHeapEGL            = 3;
const UINT32 BufferHeapCamera         = 4;

///< forward declaration
struct ChiStreamBuffer;
typedef struct ChiStreamBuffer CHISTREAMBUFFER;

/// @brief Data type list
enum class ChiDataType : UINT32
{
    STRING = 0,     ///< Char[] type
    UINT32,         ///< UINT32 type
    INT32,          ///< INT32  type
    UINT64,         ///< UINT64 type
    BOOL,           ///< BOOL   type
    MAX_TYPE        ///< Number of types
};

/// @brief Textual representation of ChiDataType enum required for binary log dump
static const CHAR* ChiDataTypeStrings[] =
{
    "ChiDataType::STRING",      ///< Char[] type textual representation
    "ChiDataType::UINT32",      ///< UINT32 type textual representation
    "ChiDataType::INT32",       ///< INT32  type textual representation
    "ChiDataType::UINT64",      ///< UINT64 type textual representation
    "ChiDataType::BOOL"         ///< BOOL   type textual representation
};

/// @brief StreamActivityMonitorAction
enum class StreamActivityMonitorAction
{
    Register,           ///< Register Stream Monitor
    Unregister          ///< Unregister Stream Monitor
};

/// @brief Chi Buffer type
typedef enum ChiBufferType
{
    HALGralloc,         ///< Gralloc buffer owned by HAL. Gralloc native handle is passed as buffer handle.
    ChiGralloc,         ///< Gralloc buffer allocated and owned by CHI. Gralloc native handle is passed as buffer handle.
    ChiNative,          ///< Native buffer allocated and owned by CHI through bufferManagerOps APIs.
                        ///  Native buffer object handle is passed as buffer handle.
} CHIBUFFERTYPE;

/// @brief Pattern of color filter array used
enum class CFAPattern
{
    Bayer               = 0,  ///< Bayer
    QCFA                = 1,  ///< 2x2 CFA (QCFA)
    ThreeByThreeCFA     = 2,  ///< 3x3 CFA
    FourByFourCFA       = 3,  ///< 4x4 CFA
    RGBIR               = 4,  ///< RGBIR
    RGBWC               = 5,  ///< RGBIR
    RGBWK               = 6,  ///< RGBWK
    NoRemosaicFullCFA   = 7,  ///< NoRemosaic Full CFA
    NoRemosaicCropCFA   = 8,  ///< NoRemosaic Crop CFA
    UnconventionalBayer = 9,   ///< Unconventional Bayer Pattern
};

/// @brief Chi Stream param for stride and slice info
typedef struct ChiStreamParams
{
    UINT16              planeStride;       ///< The number of bytes between the first byte of two sequential lines on plane 1. It
                                           ///  may be greater than nWidth * nDepth / 8 if the line includes padding.
                                           ///  Macro-tile width aligned for UBWC
    UINT16              sliceHeight;       ///< The number of lines in the plane which can be equal to or larger than actual frame
                                           ///  height. Tile height aligned for UBWC
    UINT16              chromaPlaneStride; ///< chorma plain stride
    UINT16              chromaSliceHeight; ///< chorma plain slice height
                                           ///  height. Tile height aligned for UBWC
    UINT32              streamFPS;         ///< FPS of this stream
    UINT32              bpp;               ///< bitsperpixel
    CFAPattern          cfaPattern;        ///< CFAPattern for raw stream
} CHISTREAMPARAMS;

enum class ChiFeature2PropertyType : UINT32
{
    STRING = 0,     ///< Char[] type
    UINT32,         ///< UINT32 type
    INT32,          ///< INT32  type
    UINT64,         ///< UINT64 type
    BOOL,           ///< BOOL   type
    MAX_TYPE        ///< Number of types to ensure ChiFeature2PropertyTypeStrings parity
};

static constexpr ChiFeature2PropertyType DataspacePropType          = ChiFeature2PropertyType::UINT32;
static constexpr ChiFeature2PropertyType SequencePropType           = ChiFeature2PropertyType::UINT32;
static constexpr ChiFeature2PropertyType FormatPropType             = ChiFeature2PropertyType::UINT32;
static constexpr ChiFeature2PropertyType StreamType                 = ChiFeature2PropertyType::UINT32;

struct ChiFeature2Property
{
    UINT32                  propertyId;      ///< ID of the property
    const CHAR*             pPropertyName;   ///< Name of the property
    UINT32                  size;            ///< Total byte size of property value(s)
    const VOID*             pValue;          ///< Pointer to data. Type dependent on ID
    ChiFeature2PropertyType type;            ///< Type of the data
};

static constexpr UINT32 ChiStreamFormatAny = 0xFFFFFFFF;                ///< Magic format mask to signify any format
typedef UINT32 ChiStreamIntentMask;

/// @brief Stream Descriptor
struct ChiFeature2StreamDescriptor
{
    const CHAR*                         pName;                          ///< Stream descriptor name
    ChiStreamIntentMask                 intentMask;                     ///< Stream intent mask value (ChiStreamIntentAny
                                                                        ///  or bit-field from ChiStreamIntent values)
    ChiStreamFormat                     format;                         ///< Stream format value (ChiStreamFormatAny
                                                                        ///  or one of ChiStreamFormat values)
    const CHAR*                         pPhysicalCamera;                ///< Physical camera id
    UINT32                              numStreamProperties;            ///< Number of additional/custom stream properties
    ChiFeature2Property*                pStreamProperties;              ///< Additional/custom stream properties

    BOOL operator==(const ChiFeature2StreamDescriptor& rRHS) const
    {
        return (0 == strcmp(pName, rRHS.pName));
    }

    BOOL operator<(const ChiFeature2StreamDescriptor& rRHS) const
    {
        return (0 > strcmp(pName, rRHS.pName));
    }
};

/// @brief Map a pixel format to an image extension for a data dump.
struct ChiFormatExtension
{
    CHISTREAMFORMAT format;            ///< Format of the CHI stream.
    const CHAR*     extension;         ///< Pointer to the related extension.
                                       ///  @newpagetable
};

#ifndef DOXYGEN_SHOULD_SKIP_THIS  /* until it is commented */
/// need meaningful comment
///
/// @hideinitializer
const ChiFormatExtension Format2Extension[] =
{
    { ChiStreamFormat::ChiStreamFormatYCbCr420_SP,    "yuv" },
    { ChiStreamFormat::ChiStreamFormatYCrCb420_SP,    "yuv" },
    { ChiStreamFormat::ChiStreamFormatRaw16,          "raw" },
    { ChiStreamFormat::ChiStreamFormatBlob,           "blob" },
    { ChiStreamFormat::ChiStreamFormatImplDefined,    "impl" },
    { ChiStreamFormat::ChiStreamFormatYCbCr420_888,   "yuv" },
    { ChiStreamFormat::ChiStreamFormatRawOpaque,      "raw" },
    { ChiStreamFormat::ChiStreamFormatRaw10,          "raw" },
    { ChiStreamFormat::ChiStreamFormatRaw12,          "raw" },
    { ChiStreamFormat::ChiStreamFormatRaw64,          "raw64" },
    { ChiStreamFormat::ChiStreamFormatUBWCNV124R,     "ubwcnv124r" },
    { ChiStreamFormat::ChiStreamFormatNV12Encodeable, "yuv" },
    { ChiStreamFormat::ChiStreamFormatNV12HEIF,       "yuv420" },
    { ChiStreamFormat::ChiStreamFormatNV12UBWCFLEX,   "nv12ubwcflex" },
    { ChiStreamFormat::ChiStreamFormatNV12UBWCFLEX2,  "nv12ubwcflex2" },
    { ChiStreamFormat::ChiStreamFormatNV12UBWCFLEX4,  "nv12ubwcflex4" },
    { ChiStreamFormat::ChiStreamFormatNV12UBWCFLEX8,  "nv12ubwcflex8" },
    { ChiStreamFormat::ChiStreamFormatY8,             "y8" },
    { ChiStreamFormat::ChiStreamFormatY16,            "y16" },
    { ChiStreamFormat::ChiStreamFormatP010,           "p010" },
    { ChiStreamFormat::ChiStreamFormatP010Video,      "p010video"},
    { ChiStreamFormat::ChiStreamFormatUBWCTP10,       "ubwc" },
    { ChiStreamFormat::ChiStreamFormatUBWCNV12,       "ubwc" },
    { ChiStreamFormat::ChiStreamFormatPD10,           "pd10" },
    { ChiStreamFormat::ChiStreamFormatUBWCP010,       "ubwc" },
};
#endif /* DOXYGEN_SHOULD_SKIP_THIS */


/// @brief Valid stream types.
typedef enum /** @cond */ ChiStreamType /** @endcond */
{
    ChiStreamTypeOutput        = 0,
    ChiStreamTypeInput         = 1,
    ChiStreamTypeBidirectional = 2  ///< @newpage
} CHISTREAMTYPE;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Gralloc consumer flags
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// These flags are used for allocating gralloc memory. These flags and values are inline gralloc definitions
enum ChiGralloc1ConsumerFlags : UINT64
{
    ChiGralloc1ConsumerUsageNone            = 0,                               ///< 0
    ChiGralloc1ConsumerUsageCpuReadNever    = 0,                               ///< 0
    ChiGralloc1ConsumerUsageCpuRead         = 1ULL << 1,                       ///< 2
    ChiGralloc1ConsumerUsageCpuReadOften    = 1ULL << 2 |                      ///< 6
                                              ChiGralloc1ConsumerUsageCpuRead,
    ChiGralloc1ConsumerUsageGpuTexture      = 1ULL << 8,
    ChiGralloc1ConsumerUsageHwComposer      = 1ULL << 11,
    ChiGralloc1ConsumerUsageClientTarget    = 1ULL << 12,
    ChiGralloc1ConsumerUsageCursor          = 1ULL << 15,
    ChiGralloc1ConsumerUsageVideoEncoder    = 1ULL << 16,
    ChiGralloc1ConsumerUsageCamera          = 1ULL << 18,
    ChiGralloc1ConsumerUsageRenderScript    = 1ULL << 20,
    ChiGralloc1ConsumerUsageForeignBuffers  = 1ULL << 21,
    ChiGralloc1ConsumerUsageGpuDataBuffer   = 1ULL << 23,
    ChiGralloc1ConsumerUsagePrivate_0       = 1ULL << 28,
    ChiGralloc1ConsumerUsagePrivate_1       = 1ULL << 29,
    ChiGralloc1ConsumerUsagePrivate_2       = 1ULL << 30,
    ChiGralloc1ConsumerUsagePrivate_3       = 1ULL << 31,
    ChiGralloc1ConsumerUsagePrivate_19      = 1ULL << 48,
    ChiGralloc1ConsumerUsagePrivate_18      = 1ULL << 49,
    ChiGralloc1ConsumerUsagePrivate_17      = 1ULL << 50,
    ChiGralloc1ConsumerUsagePrivate_16      = 1ULL << 51,
    ChiGralloc1ConsumerUsagePrivate_15      = 1ULL << 52,
    ChiGralloc1ConsumerUsagePrivate_14      = 1ULL << 53,
    ChiGralloc1ConsumerUsagePrivate_13      = 1ULL << 54,
    ChiGralloc1ConsumerUsagePrivate_12      = 1ULL << 55,
    ChiGralloc1ConsumerUsagePrivate_11      = 1ULL << 56,
    ChiGralloc1ConsumerUsagePrivate_10      = 1ULL << 57,
    ChiGralloc1ConsumerUsagePrivate_9       = 1ULL << 58,
    ChiGralloc1ConsumerUsagePrivate_8       = 1ULL << 59,
    ChiGralloc1ConsumerUsagePrivate_7       = 1ULL << 60,
    ChiGralloc1ConsumerUsagePrivate_6       = 1ULL << 61,
    ChiGralloc1ConsumerUsagePrivate_5       = 1ULL << 62,
    ChiGralloc1ConsumerUsagePrivate_4       = 1ULL << 63,
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Gralloc producer flags
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// These flags are used for allocating gralloc memory. These flags and values are inline gralloc definitions
enum ChiGralloc1ProducerFlags : UINT64
{
    ChiGralloc1ProducerUsageNone                    = 0,                               ///< 0
    ChiGralloc1ProducerUsageCpuWriteNever           = 0,                               ///< 0
    ChiGralloc1ProducerUsageCpuRead                 = 1ULL << 1,
    ChiGralloc1ProducerUsageCpuReadOften            = 1ULL << 2 |
                                                      ChiGralloc1ProducerUsageCpuRead,
    ChiGralloc1ProducerUsageCpuWrite                = 1ULL << 5,
    ChiGralloc1ProducerUsageCpuWriteOften           = 1ULL << 6 |
                                                      ChiGralloc1ProducerUsageCpuWrite,
    ChiGralloc1ProducerUsageGpuRenderTarget         = 1ULL << 9,
    ChiGralloc1ProducerUsageProtected               = 1ULL << 14,
    ChiGralloc1ProducerUsageVideoEncoder            = 1ULL << 16,
    ChiGralloc1ProducerUsageCamera                  = 1ULL << 17,
    ChiGralloc1ProducerUsageVideoDecoder            = 1ULL << 22,
    ChiGralloc1ProducerUsageSensorDirectData        = 1ULL << 23,
    ChiGralloc1ProducerUsagePrivate_0               = 1ULL << 28,
    ChiGralloc1ProducerUsagePrivate_1               = 1ULL << 29,
    ChiGralloc1ProducerUsagePrivate_2               = 1ULL << 30,
    ChiGralloc1ProducerUsagePrivate_3               = 1ULL << 31,
    ChiGralloc1ProducerUsagePrivate_19              = 1ULL << 48,
    ChiGralloc1ProducerUsagePrivate_18              = 1ULL << 49,
    ChiGralloc1ProducerUsagePrivate_17              = 1ULL << 50,
    ChiGralloc1ProducerUsagePrivate_16              = 1ULL << 51,
    ChiGralloc1ProducerUsagePrivate_15              = 1ULL << 52,
    ChiGralloc1ProducerUsagePrivate_14              = 1ULL << 53,
    ChiGralloc1ProducerUsagePrivate_13              = 1ULL << 54,
    ChiGralloc1ProducerUsagePrivate_12              = 1ULL << 55,
    ChiGralloc1ProducerUsagePrivate_11              = 1ULL << 56,
    ChiGralloc1ProducerUsagePrivate_10              = 1ULL << 57,
    ChiGralloc1ProducerUsagePrivate_9               = 1ULL << 58,
    ChiGralloc1ProducerUsagePrivate_8               = 1ULL << 59,
    ChiGralloc1ProducerUsagePrivate_7               = 1ULL << 60,
    ChiGralloc1ProducerUsagePrivate_6               = 1ULL << 61,
    ChiGralloc1ProducerUsagePrivate_5               = 1ULL << 62,
    ChiGralloc1ProducerUsagePrivate_4               = 1ULL << 63,
};

/// @brief Types of stream rotation.
typedef enum /** @cond */ ChiStreamRotation /** @endcond */
{
    StreamRotationCCW0      = 0,               ///< No rotation.
    StreamRotationCCW90     = 1,               ///< Rotate by 90 degrees counterclockwise.
    StreamRotationCCW180    = 2,               ///< Rotate by 180 degrees counterclockwise.
    StreamRotationCCW270    = 3                ///< Rotate by 270 degrees counterclockwise.
} CHISTREAMROTATION;

/// @brief The required Sensor pixel mode for camera stream. This structure redefines the camera3_stream_sensor_pixel_mode enum
///        defined in hardware/camera3.h. Additional information can be found there.
typedef enum ChiSensorPixelMode
{
    SensorPixelModeDefault               = 0,  ///< Default mode as per HAL implementation
    SensorPixelModeMaximumResolution     = 1   ///< Maximum resolution mode ie non binned size
} CHISENSORPIXELMODE;

/// @brief Chi Hal Stream param for override pixel format and usage flags
typedef struct ChiHalStream
{
    INT32                    id;               ///<Stream ID - a nonnegative integer identifier for a stream.
    CHISTREAMFORMAT          overrideFormat;   ///< An override pixel format for the buffers in this stream
    GrallocProducerUsage     producerUsage;    ///<The gralloc producer usage flags for this stream, as needed by the HAL.
    GrallocConsumerUsage     consumerUsage;    ///<The gralloc consumer usage flags for this stream, as needed by the HAL.
    UINT32 maxBuffers;
} CHIHALSTREAM;

/// @brief Wanted color space parameter coming in from stream configuration. Is a redefinition of the ColorSpaceProfileMapValues
///        values. This enum only specifies the supported color spaces we can have for this parameter.
typedef enum ChiStreamColorSpace : INT32
{
    ColorspaceUnspecified    = -1,             ///< ColorSpaceProfileMapUnspecified
    ColorspaceSRGB           =  0,             ///< ColorSpaceProfileMapSRGB
    ColorspaceBT709          =  4,             ///< ColorSpaceProfileMapBT709
    ColorspaceBT2020         =  5,             ///< ColorSpaceProfileMapBT2020
    ColorspaceDisplayP3      =  7,             ///< ColorSpaceProfileMapDisplayP3
    ColorspaceBT2020HLG      =  16,            ///< ColorSpaceProfileMapBT2020HLG
} CHISTREAMCOLORSPACE;

/// @brief Stream structure, this must remain compatible with the camera3_stream structure
typedef struct ChiStream
{
    CHISTREAMTYPE            streamType;           ///< The type of the stream
    UINT32                   width;                ///< The width in pixels of the buffers in this stream
    UINT32                   height;               ///< The height in pixels of the buffers in this stream
    CHISTREAMFORMAT          format;               ///< The pixel format for the buffers in this stream. Format is a value from
                                                   ///  the PixelFormat* list or from device-specific headers.
    GrallocUsage             grallocUsage;         ///< The gralloc usage flags defined in gralloc.h for this stream
    UINT32                   maxNumBuffers;        ///< The maximum number of buffers that may be dequeued at the same time
    VOID*                    pPrivateInfo;         ///< A handle to private information for the stream
    CHIDATASPACE             dataspace;            ///< A field that describes the contents of the buffer
    CHISTREAMROTATION        rotation;             ///< The output rotation of the stream
    const char*              pPrivateFrameworkId;  ///< Unmapped framework id
    INT                      groupId;              ///< Stream group Id
    UINT32                   numsensorPixelModesUsed;
                                                   ///< Number of sensor pixel modes used by this stream
    CHISENSORPIXELMODE**     sensorPixelModes;     ///< List of sendor pixel modes
    UINT32                   multiResolutionInputImage;
                                                   ///< Whether stream needs multiResolution reprocess
#if defined(CAMX_ANDROID_API) && (CAMX_ANDROID_API >= 32) //Android-T or better
    UINT64                   halStreamUsecase;     ///< Stream use case
    INT32                    dynamicRangeProfile;  ///< Stream dynamic range profile
#endif
#if defined(CAMX_ANDROID_API) && (CAMX_ANDROID_API >= 33) //Android-U or better
    CHISTREAMCOLORSPACE      colorspace;           ///< Stream color space
#endif
    INT32                    overrideFormat;       ///< Override format to communicate overriden format to the framework
    GrallocUsage64           grUsage;              ///< Gralloc usage 64bit mask to communicate final gralloc usage flag back
    CHIHALSTREAM*            pHalStream;           ///< placeholder to hold internal info
    CHISTREAMPARAMS          streamParams;         ///< Stream specific definitions.
    const CHAR*              pPhysicalCameraId;    ///< The physical camera index of the stream
    ChiStreamIntentMask      streamIntent;         ///< Stream intent
} CHISTREAM;

/// @brief New BufferManager create data structure
struct CHIBufferManagerCreateData
{
    UINT32              width;                  ///< Buffer width
    UINT32              height;                 ///< Buffer height
    UINT32              format;                 ///< Buffer format
    UINT64              producerFlags;          ///< Buffer producer gralloc flags
    UINT64              consumerFlags;          ///< Buffer consumer gralloc flags
    UINT32              bufferStride;           ///< Buffer stride
    UINT                minBufferCount;         ///< The Minimun number of buffers
    UINT                maxBufferCount;         ///< The Maximum number of buffers to be allocated
    UINT                immediateBufferCount;   ///< The number of buffers to be allocated immediately
    BOOL                bEnableLateBinding;     ///< Whether enable image buffer late binding for this buffer manager.
                                                ///  Used only if unified buffer manager is used for Chi Buffers
    UINT32              bufferHeap;             ///< Buffer heap decides whether buffers to be allocated through gralloc or csl
    ChiStream*          pChiStream;             ///< Stream info needed for image format parameters initialization
    BOOL                bPreallocateBuffers;    ///< Whether preallocation is done for this buffer manager
};

/// @brief Shutter message structure
typedef struct ChiShutterMessage
{
    UINT32 frameworkFrameNum;               ///< Frame number of the request that has begun exposure or reprocessing
    UINT64 timestamp;                       ///< Timestamp for the start of capture
    UINT64 readoutTimestamp;                ///< Timestamp for the capture readout
    UINT32 frameIDMeta;                     ///< Frame state that refers to the meta for that frame in specific usecases
} CHISHUTTERMESSAGE;

/// @brief Error message codes
typedef enum ChiErrorMessageCode
{
    MessageCodeDevice           = 1,    ///< A serious failure occurred and no further frames will be produced by the device
    MessageCodeRequest          = 2,    ///< An error has occurred in processing a request and no output will be produced for this
                                        ///  request
    MessageCodeResult           = 3,    ///< An error has occurred in producing an output result metadata buffer for a request, but
                                        ///  output stream buffers for it will still be available
    MessageCodeBuffer           = 4,    ///< An error has occurred in placing an output buffer into a stream for a request. The frame
                                        ///  metadata and other buffers may still be available
    MessageCodeTriggerRecovery  = 5     ///< An error has occurred and we need to trigger recovery
} CHIERRORMESSAGECODE;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// StreamBufferAllocator
///
/// @brief  Callback function to get a HAL stream buffer
///
/// @param  [in] pPriv              Private handle
/// @param  [in] pStream            the CHISTREAM instance to get the buffer
/// @param  [out] pChiStreamBuffer  The acquired CHISTREAM buffer, caller is responsible to allocate the buffer
/// @param  [in]  pBufferMgrName    The buffer manager name
/// @param  [in]  requestId         requestId for which buffer is requested
/// @param  [in]  stallOnly         Only stall if buffer not available
///
/// @return ChiResultSuccess upon success.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef CDKResult (*PFNSTREAMBUFFERALLOCATOR)(
        VOID*               pPriv,
        CHISTREAM*          pStream,
        CHISTREAMBUFFER*    pChiStreamBuffer,
        const CHAR*         pBufferMgrName,
        UINT64              requestId,
        BOOL                stallOnly);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// StreamBufferRegisterStreamMonitor
///
/// @brief  Callback function to register for stream inactivity notification
///
/// @param  [in] pPriv              Private handle
/// @param  [in] pStream            the CHISTREAM to register callback
/// @ param [in] hBufferManager     Handle to image buffer manager
/// @param  [in] action             Register/UnRegister monitor for stream
///
/// @return ChiResultSuccess upon success.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef CDKResult (*PFNREGISTERSTREAMMONITOR)(
        VOID*                       pPriv,
        CHISTREAM*                  pStream,
        CHIBUFFERMANAGERHANDLE      hBufferManager,
        StreamActivityMonitorAction action);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// StreamBufferNotifyFlush
///
/// @brief  Callback function to notify stream flush
///
/// @param  [in] pPriv              Private context data
/// @param  [in] bInProgress        Flush status (TRUE - in progress, FALSE - otherwise)
/// @param  [in] numStreams         Number of stream(s) this flush applies to
/// @param  [in] ppStreams          Stream(s) this flush applies to
///
/// @return ChiResultSuccess upon success.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef CDKResult (*PFNSTREAMBUFFERNOTIFYFLUSH)(
        VOID*                        pPriv,
        const BOOL                   bInProgress,
        const UINT32                 numStreams,
        const CHISTREAM**            ppStreams);

/// @brief Stream buffer
typedef struct ChiBufferInfo
{
    CHIBUFFERTYPE               bufferType;              ///< Buffer Handle type
    CHIBUFFERHANDLE             phBuffer;                ///< Output Buffer handle
    PFNSTREAMBUFFERALLOCATOR    bufferAllocator;         ///< Buffer allocator if HAL3 buffer management api is enabled
    CHISTREAM*                  pHALStream;              ///< The CHISTREAM associcated with the buffer
    VOID*                       pBufferAllocatorPriv;    ///< the private handle for bufferAllocator
    PFNREGISTERSTREAMMONITOR    pRegisterStreamActivity; ///< Stream activity registry for buffer allocator
} CHIBUFFERINFO;

/// @brief Valid fence types.
typedef enum /** @cond */ ChiFenceType /** @endcond */
{
    ChiFenceTypeInternal = 0,   ///< Represents a fence that is internal to CHI.
    ChiFenceTypeNative,         ///< Represent a native fence.
    ChiFenceTypeEGL             ///< Represents an EGL synchronized object.
} CHIFENCETYPE;

/// @brief Buffer fence data
typedef struct ChiFenceInfo
{
    BOOL                valid;          ///< Whether this fence is valid
    CHIFENCETYPE        type;           ///< Fence type
    union
    {
        INT             nativeFenceFD;  ///< Android Native fence file descriptor
        CHIFENCEHANDLE  hChiFence;      ///< Internal chi fence handle
    };
} CHIFENCEINFO;

/// @brief Context types
/// These are used for MFHDR. Either all output buffers are expected to be tagged with
/// "DefaultContext" or a combination of "LongExposure/MiddleExposure/ShortExposure". Use to set
///  metadata only. This enum should not be used to reference exposure type for the request in CHI
typedef enum class ChiContextType : UINT8
{
    DefaultContext  = 0,   ///< Default
    LongExposure    = 1,   ///< Long Exposure
    MiddleExposure  = 2,   ///< Middle Exposure
    ShortExposure   = 3,   ///< Short Exposure
    MaxContext      = 4    ///< Max
} CHICONTEXTTYPE;

/// @brief Chi sub exposure type. This enum should only be used to reference exposure type of the sub request from
/// the request's multi frame data
typedef enum class ChiExposureType : UINT8
{
    LongExposure   = 0, ///< Long Exposure
    MiddleExposure = 1, ///< Middle Exposure
    ShortExposure  = 2  ///< Short Exposure
} CHIEXPOSURETYPE;

/// @brief Priority types
typedef enum class ChiFramePriority : UINT8
{
    PriorityDefault   = 0,
    PriorityHigh      = 1,
} CHIFRAMEPRIORITY;

/// @brief SHDR auto exposure use cases.
typedef enum /** @cond */ ChiSelectSHDRAutoExposureUsecase /** @endcond */
{
    SHDRAutoExposureNone = 0,
    SHDRAutoExposureFixedExpCount,               ///< Enable SHDR and run in fixed exposure count,
                                                 ///  fallback to 1exp for some special cases
    SHDRAutoExposureAutoExpCount,                ///< Enable SHDR and run in fixed exposure count,
                                                 ///  fallback to 1exp for some special cases
} CHISELECTSHDRAUTOEXPOSUREUSECASE;

/// @brief Stream buffer
typedef struct ChiStreamBuffer
{
    UINT32              size;                           ///< Size of this structure
    CHISTREAM*          pStream;                        ///< Stream information
    CHIBUFFERINFO       bufferInfo;                     ///< Chi Buffer info
    INT                 bufferStatus;                   ///< Status of the buffer
    CHIFENCEINFO        acquireFence;                   ///< Acquire fence
    CHIFENCEINFO        releaseFence;                   ///< Release fence
    CHIFENCEINFO        returnFence;                    ///< When provided to CamX for an output buffer, CamX will take
                                                        ///  the responsibility of signalling this fence when the buffer
                                                        ///  is no longer referenced internally within CamX.
                                                        ///  Providing this fence to input buffers results is not supported
                                                        ///  and results in undefined behavior.
    CHICONTEXTTYPE      context;                        ///< Context
    VOID*               pParentSinkStreamPrivateInfo;   ///< Private information(ChiStream::pPrivateInfo) of the sink
                                                        ///  stream from which this stream buffer is generated
                                                        ///  Must be NULL for output streams and framework
                                                        ///  reprocess input streams
    CHIFRAMEPRIORITY    priority;                       ///< frame priority.
} CHISTREAMBUFFER;

#pragma pack(pop)

#ifdef __cplusplus
}
#endif // __cplusplus


#endif // CHICOMMON_H
