1/* $Id: VBoxVideoIPRT.h,v 1.3 2024/07/04 06:40:40 mrg Exp $ */
2/*
3 * Copyright (C) 2017, 2024, Oracle and/or its affiliates.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24/* In builds inside of the VirtualBox source tree we override the default
25 * VBoxVideoIPRT.h using -include, therefore this define must match the one
26 * there. */
27#ifndef ___VBox_Graphics_VBoxVideoIPRT_h
28#define ___VBox_Graphics_VBoxVideoIPRT_h
29
30# include "VBoxVideoErr.h"
31
32#ifndef __cplusplus
33#if __STDC_VERSION__ >= 199901L
34#include <stdbool.h>
35#else
36typedef enum
37{
38    false = 0,
39    true
40} bool;
41#endif
42# define RT_C_DECLS_BEGIN
43# define RT_C_DECLS_END
44#else
45# define RT_C_DECLS_BEGIN extern "C" {
46# define RT_C_DECLS_END }
47#endif
48
49#if defined(IN_XF86_MODULE) && !defined(NO_ANSIC)
50# ifdef __cplusplus
51/* xf86Module.h redefines this. */
52#  define NULL 0
53# endif
54RT_C_DECLS_BEGIN
55# include "xf86_ansic.h"
56RT_C_DECLS_END
57#endif  /* defined(IN_XF86_MODULE) && !defined(NO_ANSIC) */
58#define __STDC_LIMIT_MACROS  /* define *INT*_MAX on C++ too. */
59#include "compiler.h"  /* Can pull in <sdtint.h>.  Must come after xf86_ansic.h on XFree86. */
60#include <X11/Xfuncproto.h>
61#include <stdint.h>
62#if defined(IN_XF86_MODULE) && !defined(NO_ANSIC)
63/* XFree86 did not have these.  Not that I care much for micro-optimisations
64 * in most cases anyway. */
65# define _X_LIKELY(x) (x)
66# define _X_UNLIKELY(x) (x)
67# ifndef offsetof
68#  define offsetof(type, member) ( (int)(uintptr_t)&( ((type *)(void *)0)->member) )
69# endif
70#else  /* !(defined(IN_XF86_MODULE) && !defined(NO_ANSIC)) */
71# include <stdarg.h>
72# include <stddef.h>
73# include <string.h>
74#endif  /* !(defined(IN_XF86_MODULE) && !defined(NO_ANSIC)) */
75
76#ifndef __has_feature
77# define __has_feature(x) 0
78#endif
79#if __has_feature(cxx_static_assert) || __has_feature(c_static_assert)
80# define RTASSERT_HAVE_STATIC_ASSERT
81#elif defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
82# define RTASSERT_HAVE_STATIC_ASSERT
83#elif !defined(__cplusplus) && (__STDC_VERSION__ >= 201112L)
84# define RTASSERT_HAVE_STATIC_ASSERT
85#endif
86
87#if !defined(__cplusplus) || !defined(RTASSERT_HAVE_STATIC_ASSERT)
88RT_C_DECLS_BEGIN
89extern int RTASSERTVAR[1];
90RT_C_DECLS_END
91#endif
92
93#ifdef RTASSERT_HAVE_STATIC_ASSERT
94# ifdef __cplusplus
95#  define AssertCompile(expr)    static_assert(!!(expr), #expr)
96# else
97#  define AssertCompile(expr)    _Static_assert(!!(expr), #expr)
98# endif
99#else
100# define AssertCompile(expr) \
101    extern int RTASSERTVAR[1] __attribute__((__unused__)), \
102    RTASSERTVAR[(expr) ? 1 : 0] __attribute__((__unused__))
103#endif
104#define AssertCompileSize(type, size) \
105    AssertCompile(sizeof(type) == (size))
106#define AssertPtrNullReturnVoid(a) do { } while(0)
107
108#if !defined(IN_XF86_MODULE) && defined(DEBUG)
109# include <assert.h>
110# define Assert assert
111# define AssertFailed() assert(0)
112# define AssertMsg(expr, msg) \
113  do { \
114      if (!(expr)) xf86ErrorF msg; \
115      assert((expr)); \
116  } while (0)
117# define AssertPtr assert
118# define AssertRC(expr) assert (!expr)
119#else
120# define Assert(expr) do { } while(0)
121# define AssertFailed() do { } while(0)
122# define AssertMsg(expr, msg) do { } while(0)
123# define AssertPtr(expr) do { } while(0)
124# define AssertRC(expr) do { } while(0)
125#endif
126
127#define DECLCALLBACK(type) type
128#define DECLCALLBACKMEMBER(type, name) type (* name)
129#if __GNUC__ >= 4
130# define DECLHIDDEN(type) __attribute__((visibility("hidden"))) type
131#else
132# define DECLHIDDEN(type) type
133#endif
134#define DECLINLINE(type) static __inline__ type
135
136#define _1K 1024
137#define ASMCompilerBarrier mem_barrier
138#define RT_BIT(bit)                             ( 1U << (bit) )
139#define RT_BOOL(Value)                          ( !!(Value) )
140#define RT_BZERO(pv, cb)    do { memset((pv), 0, cb); } while (0)
141#define RT_CLAMP(Value, Min, Max)               ( (Value) > (Max) ? (Max) : (Value) < (Min) ? (Min) : (Value) )
142#define RT_ELEMENTS(aArray)                     ( sizeof(aArray) / sizeof((aArray)[0]) )
143#define RTIOPORT unsigned short
144#ifdef __clang__
145#define RT_NOREF(...)       ((void)0)
146#else
147#define RT_NOREF(...)       (void)(__VA_ARGS__)
148#endif
149#define RT_OFFSETOF(type, member) offsetof(type, member)
150#define RT_ZERO(Obj)        RT_BZERO(&(Obj), sizeof(Obj))
151#define VALID_PTR(ptr)    (   (uintptr_t)(ptr) + 0x1000U >= 0x2000U )
152#ifndef INT16_C
153# define INT16_C(Value) (Value)
154#endif
155#ifndef UINT16_C
156# define UINT16_C(Value) (Value)
157#endif
158#ifndef INT32_C
159# define INT32_C(Value) (Value ## U)
160#endif
161#ifndef UINT32_C
162# define UINT32_C(Value) (Value ## U)
163#endif
164
165#define likely _X_LIKELY
166#define unlikely _X_UNLIKELY
167
168/**
169 * A point in a two dimensional coordinate system.
170 */
171typedef struct RTPOINT
172{
173    /** X coordinate. */
174    int32_t     x;
175    /** Y coordinate. */
176    int32_t     y;
177} RTPOINT;
178
179/**
180 * Rectangle data type, double point.
181 */
182typedef struct RTRECT
183{
184    /** left X coordinate. */
185    int32_t     xLeft;
186    /** top Y coordinate. */
187    int32_t     yTop;
188    /** right X coordinate. (exclusive) */
189    int32_t     xRight;
190    /** bottom Y coordinate. (exclusive) */
191    int32_t     yBottom;
192} RTRECT;
193
194/**
195 * Rectangle data type, point + size.
196 */
197typedef struct RTRECT2
198{
199    /** X coordinate.
200     * Unless stated otherwise, this is the top left corner. */
201    int32_t     x;
202    /** Y coordinate.
203     * Unless stated otherwise, this is the top left corner.  */
204    int32_t     y;
205    /** The width.
206     * Unless stated otherwise, this is to the right of (x,y) and will not
207     * be a negative number. */
208    int32_t     cx;
209    /** The height.
210     * Unless stated otherwise, this is down from (x,y) and will not be a
211     * negative number. */
212    int32_t     cy;
213} RTRECT2;
214
215/**
216 * The size of a rectangle.
217 */
218typedef struct RTRECTSIZE
219{
220    /** The width (along the x-axis). */
221    uint32_t    cx;
222    /** The height (along the y-axis). */
223    uint32_t    cy;
224} RTRECTSIZE;
225
226/** @name Port I/O helpers
227 * @{ */
228
229/** Write an 8-bit value to an I/O port. */
230#define VBVO_PORT_WRITE_U8(Port, Value) \
231    outb(Port, Value)
232/** Write a 16-bit value to an I/O port. */
233#define VBVO_PORT_WRITE_U16(Port, Value) \
234    outw(Port, Value)
235/** Write a 32-bit value to an I/O port. */
236#define VBVO_PORT_WRITE_U32(Port, Value) \
237    outl(Port, Value)
238/** Read an 8-bit value from an I/O port. */
239#define VBVO_PORT_READ_U8(Port) \
240    inb(Port)
241/** Read a 16-bit value from an I/O port. */
242#define VBVO_PORT_READ_U16(Port) \
243    inw(Port)
244/** Read a 32-bit value from an I/O port. */
245#define VBVO_PORT_READ_U32(Port) \
246    inl(Port)
247
248/** @}  */
249
250#endif
251