1 /**
2  * Copyright 2012 JogAmp Community. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice, this list of
8  *       conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *       of conditions and the following disclaimer in the documentation and/or other materials
12  *       provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  * or implied, of JogAmp Community.
27  */
28 
29 #ifndef BCM_VC_IV_H
30 #define BCM_VC_IV_H
31 
32 /**
33  * http://en.wikipedia.org/wiki/VideoCore
34  * https://github.com/raspberrypi/userland
35  */
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 #include <stdint.h>
42 
43 typedef uint32_t DISPMANX_PROTECTION_T;
44 typedef uint32_t DISPMANX_RESOURCE_HANDLE_T;
45 typedef uint32_t DISPMANX_DISPLAY_HANDLE_T;
46 typedef uint32_t DISPMANX_UPDATE_HANDLE_T;
47 typedef uint32_t DISPMANX_ELEMENT_HANDLE_T;
48 
49 #define DISPMANX_NO_HANDLE 0
50 
51 #define DISPMANX_PROTECTION_MAX   0x0f
52 #define DISPMANX_PROTECTION_NONE  0
53 #define DISPMANX_PROTECTION_HDCP  11   // Derived from the WM DRM levels, 101-300
54 
55 
56 
57 /* Default display IDs.
58    Note: if you overwrite with you own dispmanx_platfrom_init function, you
59    should use IDs you provided during dispmanx_display_attach.
60 */
61 #define DISPMANX_ID_MAIN_LCD  0
62 #define DISPMANX_ID_AUX_LCD   1
63 #define DISPMANX_ID_HDMI      2
64 #define DISPMANX_ID_SDTV      3
65 
66 /* Return codes. Nonzero ones indicate failure. */
67 typedef enum {
68   DISPMANX_SUCCESS      = 0,
69   DISPMANX_INVALID      = -1
70   /* XXX others TBA */
71 } DISPMANX_STATUS_T;
72 
73 typedef enum {
74   /* Bottom 2 bits sets the orientation */
75   DISPMANX_NO_ROTATE = 0,
76   DISPMANX_ROTATE_90 = 1,
77   DISPMANX_ROTATE_180 = 2,
78   DISPMANX_ROTATE_270 = 3,
79 
80   DISPMANX_FLIP_HRIZ = 1 << 16,
81   DISPMANX_FLIP_VERT = 1 << 17
82 } DISPMANX_TRANSFORM_T;
83 
84 typedef enum {
85   /* Bottom 2 bits sets the alpha mode */
86   DISPMANX_FLAGS_ALPHA_FROM_SOURCE = 0,
87   DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS = 1,
88   DISPMANX_FLAGS_ALPHA_FIXED_NON_ZERO = 2,
89   DISPMANX_FLAGS_ALPHA_FIXED_EXCEED_0X07 = 3,
90 
91   DISPMANX_FLAGS_ALPHA_PREMULT = 1 << 16,
92   DISPMANX_FLAGS_ALPHA_MIX = 1 << 17
93 } DISPMANX_FLAGS_ALPHA_T;
94 
95 struct VC_IMAGE_T;
96 typedef struct VC_IMAGE_T VC_IMAGE_T;
97 
98 typedef struct {
99   DISPMANX_FLAGS_ALPHA_T flags;
100   uint32_t opacity;
101   VC_IMAGE_T *mask;
102 } DISPMANX_ALPHA_T;
103 
104 typedef struct {
105   DISPMANX_FLAGS_ALPHA_T flags;
106   uint32_t opacity;
107   DISPMANX_RESOURCE_HANDLE_T mask;
108 } VC_DISPMANX_ALPHA_T;  /* for use with vmcs_host */
109 
110 
111 typedef enum {
112   DISPMANX_FLAGS_CLAMP_NONE = 0,
113   DISPMANX_FLAGS_CLAMP_LUMA_TRANSPARENT = 1,
114 #if __VCCOREVER__ >= 0x04000000
115   DISPMANX_FLAGS_CLAMP_TRANSPARENT = 2,
116   DISPMANX_FLAGS_CLAMP_REPLACE = 3
117 #else
118   DISPMANX_FLAGS_CLAMP_CHROMA_TRANSPARENT = 2,
119   DISPMANX_FLAGS_CLAMP_TRANSPARENT = 3
120 #endif
121 } DISPMANX_FLAGS_CLAMP_T;
122 
123 typedef enum {
124   DISPMANX_FLAGS_KEYMASK_OVERRIDE = 1,
125   DISPMANX_FLAGS_KEYMASK_SMOOTH = 1 << 1,
126   DISPMANX_FLAGS_KEYMASK_CR_INV = 1 << 2,
127   DISPMANX_FLAGS_KEYMASK_CB_INV = 1 << 3,
128   DISPMANX_FLAGS_KEYMASK_YY_INV = 1 << 4
129 } DISPMANX_FLAGS_KEYMASK_T;
130 
131 typedef union {
132   struct {
133     uint8_t yy_upper;
134     uint8_t yy_lower;
135     uint8_t cr_upper;
136     uint8_t cr_lower;
137     uint8_t cb_upper;
138     uint8_t cb_lower;
139   } yuv;
140   struct {
141     uint8_t red_upper;
142     uint8_t red_lower;
143     uint8_t blue_upper;
144     uint8_t blue_lower;
145     uint8_t green_upper;
146     uint8_t green_lower;
147   } rgb;
148 } DISPMANX_CLAMP_KEYS_T;
149 
150 typedef struct {
151   DISPMANX_FLAGS_CLAMP_T mode;
152   DISPMANX_FLAGS_KEYMASK_T key_mask;
153   DISPMANX_CLAMP_KEYS_T key_value;
154   uint32_t replace_value;
155 } DISPMANX_CLAMP_T;
156 
157 
158 typedef struct tag_VC_RECT_T {
159  int32_t x;
160  int32_t y;
161  int32_t width;
162  int32_t height;
163 } VC_RECT_T;
164 
165 /* Types of image supported. */
166 /* Please add any new types to the *end* of this list.  Also update
167  * case_VC_IMAGE_ANY_xxx macros (below), and the vc_image_type_info table in
168  * vc_image/vc_image_helper.c.
169  */
170 typedef enum
171 {
172    VC_IMAGE_MIN = 0, //bounds for error checking
173 
174    VC_IMAGE_RGB565 = 1,
175    VC_IMAGE_1BPP,
176    VC_IMAGE_YUV420,
177    VC_IMAGE_48BPP,
178    VC_IMAGE_RGB888,
179    VC_IMAGE_8BPP,
180    VC_IMAGE_4BPP,    // 4bpp palettised image
181    VC_IMAGE_3D32,    /* A separated format of 16 colour/light shorts followed by 16 z values */
182    VC_IMAGE_3D32B,   /* 16 colours followed by 16 z values */
183    VC_IMAGE_3D32MAT, /* A separated format of 16 material/colour/light shorts followed by 16 z values */
184    VC_IMAGE_RGB2X9,   /* 32 bit format containing 18 bits of 6.6.6 RGB, 9 bits per short */
185    VC_IMAGE_RGB666,   /* 32-bit format holding 18 bits of 6.6.6 RGB */
186    VC_IMAGE_PAL4_OBSOLETE,     // 4bpp palettised image with embedded palette
187    VC_IMAGE_PAL8_OBSOLETE,     // 8bpp palettised image with embedded palette
188    VC_IMAGE_RGBA32,   /* RGB888 with an alpha byte after each pixel */ /* xxx: isn't it BEFORE each pixel? */
189    VC_IMAGE_YUV422,   /* a line of Y (32-byte padded), a line of U (16-byte padded), and a line of V (16-byte padded) */
190    VC_IMAGE_RGBA565,  /* RGB565 with a transparent patch */
191    VC_IMAGE_RGBA16,   /* Compressed (4444) version of RGBA32 */
192    VC_IMAGE_YUV_UV,   /* VCIII codec format */
193    VC_IMAGE_TF_RGBA32, /* VCIII T-format RGBA8888 */
194    VC_IMAGE_TF_RGBX32,  /* VCIII T-format RGBx8888 */
195    VC_IMAGE_TF_FLOAT, /* VCIII T-format float */
196    VC_IMAGE_TF_RGBA16, /* VCIII T-format RGBA4444 */
197    VC_IMAGE_TF_RGBA5551, /* VCIII T-format RGB5551 */
198    VC_IMAGE_TF_RGB565, /* VCIII T-format RGB565 */
199    VC_IMAGE_TF_YA88, /* VCIII T-format 8-bit luma and 8-bit alpha */
200    VC_IMAGE_TF_BYTE, /* VCIII T-format 8 bit generic sample */
201    VC_IMAGE_TF_PAL8, /* VCIII T-format 8-bit palette */
202    VC_IMAGE_TF_PAL4, /* VCIII T-format 4-bit palette */
203    VC_IMAGE_TF_ETC1, /* VCIII T-format Ericsson Texture Compressed */
204    VC_IMAGE_BGR888,  /* RGB888 with R & B swapped */
205    VC_IMAGE_BGR888_NP,  /* RGB888 with R & B swapped, but with no pitch, i.e. no padding after each row of pixels */
206    VC_IMAGE_BAYER,  /* Bayer image, extra defines which variant is being used */
207    VC_IMAGE_CODEC,  /* General wrapper for codec images e.g. JPEG from camera */
208    VC_IMAGE_YUV_UV32,   /* VCIII codec format */
209    VC_IMAGE_TF_Y8,   /* VCIII T-format 8-bit luma */
210    VC_IMAGE_TF_A8,   /* VCIII T-format 8-bit alpha */
211    VC_IMAGE_TF_SHORT,/* VCIII T-format 16-bit generic sample */
212    VC_IMAGE_TF_1BPP, /* VCIII T-format 1bpp black/white */
213    VC_IMAGE_OPENGL,
214    VC_IMAGE_YUV444I, /* VCIII-B0 HVS YUV 4:4:4 interleaved samples */
215    VC_IMAGE_YUV422PLANAR,  /* Y, U, & V planes separately (VC_IMAGE_YUV422 has them interleaved on a per line basis) */
216    VC_IMAGE_ARGB8888,   /* 32bpp with 8bit alpha at MS byte, with R, G, B (LS byte) */
217    VC_IMAGE_XRGB8888,   /* 32bpp with 8bit unused at MS byte, with R, G, B (LS byte) */
218 
219    VC_IMAGE_YUV422YUYV,  /* interleaved 8 bit samples of Y, U, Y, V */
220    VC_IMAGE_YUV422YVYU,  /* interleaved 8 bit samples of Y, V, Y, U */
221    VC_IMAGE_YUV422UYVY,  /* interleaved 8 bit samples of U, Y, V, Y */
222    VC_IMAGE_YUV422VYUY,  /* interleaved 8 bit samples of V, Y, U, Y */
223 
224    VC_IMAGE_RGBX32,      /* 32bpp like RGBA32 but with unused alpha */
225    VC_IMAGE_RGBX8888,    /* 32bpp, corresponding to RGBA with unused alpha */
226    VC_IMAGE_BGRX8888,    /* 32bpp, corresponding to BGRA with unused alpha */
227 
228    VC_IMAGE_YUV420SP,    /* Y as a plane, then UV byte interleaved in plane with with same pitch, half height */
229 
230    VC_IMAGE_YUV444PLANAR,  /* Y, U, & V planes separately 4:4:4 */
231 
232    VC_IMAGE_MAX,     //bounds for error checking
233    VC_IMAGE_FORCE_ENUM_16BIT = 0xffff,
234 } VC_IMAGE_TYPE_T;
235 
236 /**
237  * From https://github.com/raspberrypi/userland/blob/master/interface/vmcs_host/vc_vchi_dispmanx.h
238  */
239 typedef enum {
240    DISPMANX_ELEMENT_CHANGE_LAYER         =  (1<<0),
241    DISPMANX_ELEMENT_CHANGE_OPACITY       =  (1<<1),
242    DISPMANX_ELEMENT_CHANGE_DEST_RECT     =  (1<<2),
243    DISPMANX_ELEMENT_CHANGE_SRC_RECT      =  (1<<3),
244    DISPMANX_ELEMENT_CHANGE_MASK_RESOURCE =  (1<<4),
245    DISPMANX_ELEMENT_CHANGE_TRANSFORM     =  (1<<5)
246 /**
247  * Not working /validated !
248    DISPMANX_ELEMENT_CHANGE_MIN         =  0x00,
249    DISPMANX_ELEMENT_CHANGE_SOURCE      =  0x01,
250    DISPMANX_ELEMENT_INSERT_ABOVE       =  0x80,
251    DISPMANX_ELEMENT_CHANGE_FLAGS       =  0x100,
252    DISPMANX_ELEMENT_CHANGE_NOTHING     =  0x200,
253    DISPMANX_ELEMENT_CHANGE_ALPHA_FLAGS =  0x400,
254    DISPMANX_ELEMENT_CHANGE_PROTECTION  =  0x800,
255    DISPMANX_ELEMENT_CHANGE_MAX         =  0x1000
256  */
257 } DISPMANX_ELEMENT_CHANGE_T;
258 
259 
260 
261 extern void bcm_host_init(void);
262 extern void bcm_host_deinit(void);
263 
264 extern int32_t graphics_get_display_size( const uint16_t display_number,
265                                          uint32_t *width,
266                                          uint32_t *height);
267 
268 extern DISPMANX_DISPLAY_HANDLE_T vc_dispmanx_display_open( uint32_t device );
269 extern int vc_dispmanx_display_close( DISPMANX_DISPLAY_HANDLE_T display );
270 
271 extern DISPMANX_RESOURCE_HANDLE_T vc_dispmanx_resource_create(VC_IMAGE_TYPE_T type, uint32_t width, uint32_t height, uint32_t *native_image_handle);
272 extern int vc_dispmanx_resource_write_data( DISPMANX_RESOURCE_HANDLE_T res, VC_IMAGE_TYPE_T src_type, int src_pitch, void * src_address, const VC_RECT_T * rect );
273 //extern int vc_dispmanx_resource_write_data_handle( DISPMANX_RESOURCE_HANDLE_T res, VC_IMAGE_TYPE_T src_type, int src_pitch,
274 //                                                   VCHI_MEM_HANDLE_T handle, uint32_t offset, const VC_RECT_T * rect );
275 extern int vc_dispmanx_resource_delete( DISPMANX_RESOURCE_HANDLE_T res );
276 
277 
278 
279 extern DISPMANX_UPDATE_HANDLE_T vc_dispmanx_update_start( int32_t priority );
280 extern DISPMANX_ELEMENT_HANDLE_T vc_dispmanx_element_add ( DISPMANX_UPDATE_HANDLE_T update, DISPMANX_DISPLAY_HANDLE_T display,
281                                                           int32_t layer, const VC_RECT_T *dest_rect, DISPMANX_RESOURCE_HANDLE_T src,
282                                                           const VC_RECT_T *src_rect, DISPMANX_PROTECTION_T protection,
283                                                           VC_DISPMANX_ALPHA_T *alpha,
284                                                           DISPMANX_CLAMP_T *clamp, DISPMANX_TRANSFORM_T transform );
285 extern int vc_dispmanx_element_remove( DISPMANX_UPDATE_HANDLE_T update, DISPMANX_ELEMENT_HANDLE_T element );
286 
287 
288 extern int vc_dispmanx_update_submit_sync( DISPMANX_UPDATE_HANDLE_T update );
289 
290 //New function added to VCHI to change attributes, set_opacity does not work there.
291 extern int vc_dispmanx_element_change_attributes( DISPMANX_UPDATE_HANDLE_T update,
292                                                   DISPMANX_ELEMENT_HANDLE_T element,
293                                                   uint32_t change_flags,
294                                                   int32_t layer,
295                                                   uint8_t opacity,
296                                                   const VC_RECT_T *dest_rect,
297                                                   const VC_RECT_T *src_rect,
298                                                   DISPMANX_RESOURCE_HANDLE_T mask,
299                                                   DISPMANX_TRANSFORM_T transform );
300 
301 #ifdef __cplusplus
302 }
303 #endif
304 
305 #endif
306