1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef __GL_UTILS_H__
17 #define __GL_UTILS_H__
18 
19 #if PLATFORM_SDK_VERSION < 26
20 #include <cutils/log.h>
21 #else
22 #include <log/log.h>
23 #endif
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 #ifdef GL_API
29     #undef GL_API
30 #endif
31 #define GL_API
32 
33 #ifdef GL_APIENTRY
34     #undef GL_APIENTRY
35 #endif
36 
37 #ifdef GL_APIENTRYP
38     #undef GL_APIENTRYP
39 #endif
40 #define GL_APIENTRYP
41 
42 #ifndef ANDROID
43 #define GL_APIENTRY
44 #endif
45 
46 #include <GLES/gl.h>
47 #include <GLES/glext.h>
48 #include <GLES2/gl2.h>
49 #include <GLES2/gl2ext.h>
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 typedef enum {
56     INDIRECT_COMMAND_DRAWARRAYS = 0,
57     INDIRECT_COMMAND_DRAWELEMENTS = 1,
58 } IndirectCommandType;
59 
60     size_t glSizeof(GLenum type);
61     size_t glUtilsParamSize(GLenum param);
62     void   glUtilsPackPointerData(unsigned char *dst, unsigned char *str,
63                            int size, GLenum type, unsigned int stride,
64                            unsigned int datalen);
65     void glUtilsWritePackPointerData(void* stream, unsigned char *src,
66                                     int size, GLenum type, unsigned int stride,
67                                     unsigned int datalen);
68     int glUtilsPixelBitSize(GLenum format, GLenum type);
69     void   glUtilsPackStrings(char *ptr, char **strings, GLint *length, GLsizei count);
70     int glUtilsCalcShaderSourceLen(char **strings, GLint *length, GLsizei count);
71     GLenum glUtilsColorAttachmentName(int i);
72     int glUtilsColorAttachmentIndex(GLenum attachment);
73 
74     GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType);
75 
76 #ifdef __cplusplus
77 };
78 #endif
79 
80 namespace GLUtils {
81 
minmax(const T * indices,int count,int * min,int * max)82     template <class T> void minmax(const T *indices, int count, int *min, int *max) {
83         *min = -1;
84         *max = -1;
85         const T *ptr = indices;
86         for (int i = 0; i < count; i++) {
87             if (*min == -1 || *ptr < *min) *min = *ptr;
88             if (*max == -1 || *ptr > *max) *max = *ptr;
89             ptr++;
90         }
91     }
92 
minmaxExcept(const T * indices,int count,int * min,int * max,bool shouldExclude,T whatExclude)93     template <class T> void minmaxExcept
94         (const T *indices, int count, int *min, int *max,
95          bool shouldExclude, T whatExclude) {
96 
97         if (!shouldExclude) return minmax(indices, count, min, max);
98 
99         *min = -1;
100         *max = -1;
101         const T *ptr = indices;
102         for (int i = 0; i < count; i++) {
103             if (*ptr != whatExclude) {
104                 if (*min == -1 || *ptr < *min) *min = *ptr;
105                 if (*max == -1 || *ptr > *max) *max = *ptr;
106             }
107             ptr++;
108         }
109     }
110 
shiftIndices(T * indices,int count,int offset)111     template <class T> void shiftIndices(T *indices, int count,  int offset) {
112         T *ptr = indices;
113         for (int i = 0; i < count; i++) {
114             *ptr += offset;
115             ptr++;
116         }
117     }
118 
119 
shiftIndices(const T * src,T * dst,int count,int offset)120     template <class T> void shiftIndices(const T *src, T *dst, int count, int offset)
121     {
122         for (int i = 0; i < count; i++) {
123             *dst = *src + offset;
124             dst++;
125             src++;
126         }
127     }
128 
shiftIndicesExcept(T * indices,int count,int offset,bool shouldExclude,T whatExclude)129     template <class T> void shiftIndicesExcept
130         (T *indices, int count, int offset,
131          bool shouldExclude, T whatExclude) {
132 
133         if (!shouldExclude) return shiftIndices(indices, count, offset);
134 
135         T *ptr = indices;
136         for (int i = 0; i < count; i++) {
137             if (*ptr != whatExclude) {
138                 *ptr += offset;
139             }
140             ptr++;
141         }
142     }
143 
shiftIndicesExcept(const T * src,T * dst,int count,int offset,bool shouldExclude,T whatExclude)144     template <class T> void shiftIndicesExcept
145         (const T *src, T *dst, int count, int offset,
146          bool shouldExclude, T whatExclude) {
147 
148         if (!shouldExclude) return shiftIndices(src, dst, count, offset);
149 
150         for (int i = 0; i < count; i++) {
151             if (*src == whatExclude) {
152                 *dst = *src;
153             } else {
154                 *dst = *src + offset;
155             }
156             dst++;
157             src++;
158         }
159     }
160 
primitiveRestartIndex()161     template<class T> T primitiveRestartIndex() {
162         return -1;
163     }
164 
165 }; // namespace GLUtils
166 #endif
167