1/*
2Copyright (c) 2013, Broadcom Europe Ltd
3Copyright (c) 2013, Tim Gover
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the copyright holder nor the
14 names of its contributors may be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
21DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#ifndef RASPITEX_UTIL_H_
30#define RASPITEX_UTIL_H_
31
32#define VCOS_LOG_CATEGORY (&raspitex_log_category)
33#include <math.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <EGL/egl.h>
38#include <EGL/eglext.h>
39#include "RaspiTex.h"
40#include "interface/vcos/vcos.h"
41
42extern VCOS_LOG_CAT_T raspitex_log_category;
43
44#define SHADER_MAX_ATTRIBUTES 16
45#define SHADER_MAX_UNIFORMS 16
46/**
47 * Container for a simple shader program. The uniform and attribute locations
48 * are automatically setup by raspitex_build_shader_program.
49 */
50typedef struct RASPITEXUTIL_SHADER_PROGRAM_T
51{
52 const char *vertex_source; /// Pointer to vertex shader source
53 const char *fragment_source; /// Pointer to fragment shader source
54
55 /// Array of uniform names for raspitex_build_shader_program to process
56 const char *uniform_names[SHADER_MAX_UNIFORMS];
57 /// Array of attribute names for raspitex_build_shader_program to process
58 const char *attribute_names[SHADER_MAX_ATTRIBUTES];
59
60 GLint vs; /// Vertex shader handle
61 GLint fs; /// Fragment shader handle
62 GLint program; /// Shader program handle
63
64 /// The locations for uniforms defined in uniform_names
65 GLint uniform_locations[SHADER_MAX_UNIFORMS];
66
67 /// The locations for attributes defined in attribute_names
68 GLint attribute_locations[SHADER_MAX_ATTRIBUTES];
69} RASPITEXUTIL_SHADER_PROGRAM_T;
70
71
72/* Uncomment to enable extra GL error checking */
73//#define CHECK_GL_ERRORS
74#if defined(CHECK_GL_ERRORS)
75#define GLCHK(X) \
76 do { \
77 GLenum err = GL_NO_ERROR; \
78 X; \
79 while ((err = glGetError())) \
80 { \
81 vcos_log_error("GL error 0x%x in " #X "file %s line %d", err, __FILE__,__LINE__); \
82 vcos_assert(err == GL_NO_ERROR); \
83 exit(err); \
84 } \
85 } \
86 while(0)
87#else
88#define GLCHK(X) X
89#endif /* CHECK_GL_ERRORS */
90
91/* Default GL scene ops functions */
92int raspitexutil_create_native_window(RASPITEX_STATE *raspitex_state);
93int raspitexutil_gl_init_1_0(RASPITEX_STATE *raspitex_state);
94int raspitexutil_gl_init_2_0(RASPITEX_STATE *raspitex_state);
95int raspitexutil_update_model(RASPITEX_STATE* raspitex_state);
96int raspitexutil_redraw(RASPITEX_STATE* raspitex_state);
97void raspitexutil_gl_term(RASPITEX_STATE *raspitex_state);
98void raspitexutil_destroy_native_window(RASPITEX_STATE *raspitex_state);
99int raspitexutil_create_textures(RASPITEX_STATE *raspitex_state);
100int raspitexutil_update_texture(RASPITEX_STATE *raspitex_state,
101 EGLClientBuffer mm_buf);
102int raspitexutil_update_y_texture(RASPITEX_STATE *raspitex_state,
103 EGLClientBuffer mm_buf);
104int raspitexutil_update_u_texture(RASPITEX_STATE *raspitex_state,
105 EGLClientBuffer mm_buf);
106int raspitexutil_update_v_texture(RASPITEX_STATE *raspitex_state,
107 EGLClientBuffer mm_buf);
108int raspitexutil_capture_bgra(struct RASPITEX_STATE *state,
109 uint8_t **buffer, size_t *buffer_size);
110void raspitexutil_close(RASPITEX_STATE* raspitex_state);
111
112/* Utility functions */
113int raspitexutil_build_shader_program(RASPITEXUTIL_SHADER_PROGRAM_T *p);
114void raspitexutil_brga_to_rgba(uint8_t *buffer, size_t size);
115
116#endif /* RASPITEX_UTIL_H_ */
117