Initial version of libomxil-vc4 for RPI3
[platform/adaptation/broadcom/libomxil-vc4.git] / interface / khronos / common / khrn_int_util.c
1 /*
2 Copyright (c) 2012, Broadcom Europe Ltd
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in the
11       documentation and/or other materials provided with the distribution.
12     * Neither the name of the copyright holder nor the
13       names of its contributors may be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "interface/khronos/common/khrn_int_common.h"
29
30 #include "interface/khronos/common/khrn_int_util.h"
31 #include "interface/khronos/include/GLES/gl.h"
32 #include "interface/khronos/include/GLES2/gl2.h"
33 #include "interface/khronos/include/GLES2/gl2ext.h"
34
35 void khrn_clip_range(
36    int32_t *min_x0_io, int32_t *l0_io,
37    int32_t min_x1, int32_t l1)
38 {
39    int32_t min_x0;
40    int32_t l0;
41    int32_t max_x0;
42    int32_t max_x1;
43
44    vcos_assert((*l0_io >= 0) && (l1 >= 0));
45
46    min_x0 = *min_x0_io;
47    l0 = *l0_io;
48
49    max_x0 = _adds(min_x0, l0);
50    max_x1 = min_x1 + l1;
51
52    if (min_x0 < min_x1) {
53       min_x0 = min_x1;
54    }
55    if (max_x0 > max_x1) {
56       max_x0 = max_x1;
57    }
58
59    l0 = _max(_subs(max_x0, min_x0), 0);
60
61    *min_x0_io = (l0 == 0) ? min_x1 : min_x0;
62    *l0_io = l0;
63 }
64
65 void khrn_clip_range2(
66    int32_t *min_ax0_io, int32_t *min_bx0_io, int32_t *l0_io,
67    int32_t min_ax1, int32_t al1,
68    int32_t min_bx1, int32_t bl1)
69 {
70    int32_t min_ax0;
71    int32_t min_bx0;
72    int32_t l0;
73    int32_t max_ax0;
74    int32_t max_bx0;
75    int32_t max_ax1;
76    int32_t max_bx1;
77
78    vcos_assert((*l0_io >= 0) && (al1 >= 0) && (bl1 >= 0));
79
80    min_ax0 = *min_ax0_io;
81    min_bx0 = *min_bx0_io;
82    l0 = *l0_io;
83
84    l0 = _adds(_max(min_ax0, min_bx0), l0) - _max(min_ax0, min_bx0); /* clamp l0 to avoid overflow */
85    max_ax0 = min_ax0 + l0;
86    max_bx0 = min_bx0 + l0;
87    max_ax1 = min_ax1 + al1;
88    max_bx1 = min_bx1 + bl1;
89
90    /*
91       if any of the following _adds/_subs overflow, we will get either
92       min_ax0 >= max_ax0 or min_bx0 >= max_bx0, so l0 will be 0
93    */
94
95    if (min_ax0 < min_ax1) {
96       min_bx0 = _adds(min_bx0, _subs(min_ax1, min_ax0));
97       min_ax0 = min_ax1;
98    }
99    if (max_ax0 > max_ax1) {
100       max_bx0 = _subs(max_bx0, _subs(max_ax0, max_ax1));
101       max_ax0 = max_ax1;
102    }
103
104    if (min_bx0 < min_bx1) {
105       min_ax0 = _adds(min_ax0, _subs(min_bx1, min_bx0));
106       min_bx0 = min_bx1;
107    }
108    if (max_bx0 > max_bx1) {
109       max_ax0 = _subs(max_ax0, _subs(max_bx0, max_bx1));
110       max_bx0 = max_bx1;
111    }
112
113    l0 = _max(_subs(max_ax0, min_ax0), 0);
114    vcos_assert(l0 == _max(_subs(max_bx0, min_bx0), 0));
115
116    *min_ax0_io = (l0 == 0) ? min_ax1 : min_ax0;
117    *min_bx0_io = (l0 == 0) ? min_bx1 : min_bx0;
118    *l0_io = l0;
119 }
120
121 void khrn_clip_rect(
122    int32_t *x0, int32_t *y0, int32_t *w0, int32_t *h0,
123    int32_t x1, int32_t y1, int32_t w1, int32_t h1)
124 {
125    khrn_clip_range(x0, w0, x1, w1);
126    khrn_clip_range(y0, h0, y1, h1);
127 }
128
129 void khrn_clip_rect2(
130    int32_t *ax0, int32_t *ay0, int32_t *bx0, int32_t *by0, int32_t *w0, int32_t *h0,
131    int32_t ax1, int32_t ay1, int32_t aw1, int32_t ah1,
132    int32_t bx1, int32_t by1, int32_t bw1, int32_t bh1)
133 {
134    khrn_clip_range2(ax0, bx0, w0, ax1, aw1, bx1, bw1);
135    khrn_clip_range2(ay0, by0, h0, ay1, ah1, by1, bh1);
136 }
137
138 int khrn_get_type_size(int type)
139 {
140    switch ((GLenum)type) {
141    case GL_BYTE:
142    case GL_UNSIGNED_BYTE:
143       return 1;
144    case GL_SHORT:
145    case GL_UNSIGNED_SHORT:
146    case GL_HALF_FLOAT_OES:
147       return 2;
148    case GL_FIXED:
149    case GL_FLOAT:
150       return 4;
151    default:
152       UNREACHABLE();
153       return 0;
154    }
155 }
156
157 #ifdef _VIDEOCORE
158
159 void khrn_barrier(void)
160 {
161 }
162
163 #endif