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 #include "glUtils.h"
17 #include <string.h>
18 #include "ErrorLog.h"
19 #include <IOStream.h>
20 
glSizeof(GLenum type)21 size_t glSizeof(GLenum type)
22 {
23     size_t retval = 0;
24     switch(type) {
25     case GL_BYTE:
26     case GL_UNSIGNED_BYTE:
27         retval = 1;
28         break;
29     case GL_SHORT:
30     case GL_UNSIGNED_SHORT:
31     case GL_HALF_FLOAT_OES:
32         retval = 2;
33         break;
34     case GL_UNSIGNED_INT:
35     case GL_INT:
36     case GL_FLOAT:
37     case GL_FIXED:
38     case GL_BOOL:
39         retval =  4;
40         break;
41 #ifdef GL_DOUBLE
42     case GL_DOUBLE:
43         retval = 8;
44         break;
45 #endif
46     case GL_FLOAT_VEC2:
47     case GL_INT_VEC2:
48     case GL_BOOL_VEC2:
49         retval = 8;
50         break;
51     case GL_INT_VEC3:
52     case GL_BOOL_VEC3:
53     case GL_FLOAT_VEC3:
54         retval = 12;
55         break;
56     case GL_FLOAT_VEC4:
57     case GL_BOOL_VEC4:
58     case GL_INT_VEC4:
59     case GL_FLOAT_MAT2:
60         retval = 16;
61         break;
62     case GL_FLOAT_MAT3:
63         retval = 36;
64         break;
65     case GL_FLOAT_MAT4:
66         retval = 64;
67         break;
68     case GL_SAMPLER_2D:
69     case GL_SAMPLER_CUBE:
70         retval = 4;
71         break;
72     default:
73         ERR("**** ERROR unknown type 0x%x (%s,%d)\n", type, __FUNCTION__,__LINE__);
74     }
75     return retval;
76 
77 }
78 
glUtilsParamSize(GLenum param)79 size_t glUtilsParamSize(GLenum param)
80 {
81     size_t s = 0;
82 
83     switch(param)
84     {
85     case GL_DEPTH_TEST:
86     case GL_DEPTH_FUNC:
87     case GL_DEPTH_BITS:
88     case GL_MAX_CLIP_PLANES:
89     case GL_GREEN_BITS:
90     case GL_MAX_MODELVIEW_STACK_DEPTH:
91     case GL_MAX_PROJECTION_STACK_DEPTH:
92     case GL_MAX_TEXTURE_STACK_DEPTH:
93     case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES:
94     case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES:
95     case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
96     case GL_MAX_TEXTURE_SIZE:
97     case GL_TEXTURE_GEN_MODE_OES:
98     case GL_TEXTURE_ENV_MODE:
99     case GL_FOG_MODE:
100     case GL_FOG_DENSITY:
101     case GL_FOG_START:
102     case GL_FOG_END:
103     case GL_SPOT_EXPONENT:
104     case GL_CONSTANT_ATTENUATION:
105     case GL_LINEAR_ATTENUATION:
106     case GL_QUADRATIC_ATTENUATION:
107     case GL_SHININESS:
108     case GL_LIGHT_MODEL_TWO_SIDE:
109     case GL_POINT_SIZE:
110     case GL_POINT_SIZE_MIN:
111     case GL_POINT_SIZE_MAX:
112     case GL_POINT_FADE_THRESHOLD_SIZE:
113     case GL_CULL_FACE:
114     case GL_CULL_FACE_MODE:
115     case GL_FRONT_FACE:
116     case GL_SHADE_MODEL:
117     case GL_DEPTH_WRITEMASK:
118     case GL_DEPTH_CLEAR_VALUE:
119     case GL_STENCIL_FAIL:
120     case GL_STENCIL_PASS_DEPTH_FAIL:
121     case GL_STENCIL_PASS_DEPTH_PASS:
122     case GL_STENCIL_REF:
123     case GL_STENCIL_WRITEMASK:
124     case GL_MATRIX_MODE:
125     case GL_MODELVIEW_STACK_DEPTH:
126     case GL_PROJECTION_STACK_DEPTH:
127     case GL_TEXTURE_STACK_DEPTH:
128     case GL_ALPHA_TEST_FUNC:
129     case GL_ALPHA_TEST_REF:
130     case GL_ALPHA_TEST:
131     case GL_DITHER:
132     case GL_BLEND_DST:
133     case GL_BLEND_SRC:
134     case GL_BLEND:
135     case GL_LOGIC_OP_MODE:
136     case GL_SCISSOR_TEST:
137     case GL_MAX_TEXTURE_UNITS:
138     case GL_ACTIVE_TEXTURE:
139     case GL_ALPHA_BITS:
140     case GL_ARRAY_BUFFER_BINDING:
141     case GL_BLUE_BITS:
142     case GL_CLIENT_ACTIVE_TEXTURE:
143     case GL_CLIP_PLANE0:
144     case GL_CLIP_PLANE1:
145     case GL_CLIP_PLANE2:
146     case GL_CLIP_PLANE3:
147     case GL_CLIP_PLANE4:
148     case GL_CLIP_PLANE5:
149     case GL_COLOR_ARRAY:
150     case GL_COLOR_ARRAY_BUFFER_BINDING:
151     case GL_COLOR_ARRAY_SIZE:
152     case GL_COLOR_ARRAY_STRIDE:
153     case GL_COLOR_ARRAY_TYPE:
154     case GL_COLOR_LOGIC_OP:
155     case GL_COLOR_MATERIAL:
156     case GL_PACK_ALIGNMENT:
157     case GL_PERSPECTIVE_CORRECTION_HINT:
158     case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES:
159     case GL_POINT_SIZE_ARRAY_STRIDE_OES:
160     case GL_POINT_SIZE_ARRAY_TYPE_OES:
161     case GL_POINT_SMOOTH:
162     case GL_POINT_SMOOTH_HINT:
163     case GL_POINT_SPRITE_OES:
164     case GL_COORD_REPLACE_OES:
165     case GL_COMBINE_ALPHA:
166     case GL_SRC0_RGB:
167     case GL_SRC1_RGB:
168     case GL_SRC2_RGB:
169     case GL_OPERAND0_RGB:
170     case GL_OPERAND1_RGB:
171     case GL_OPERAND2_RGB:
172     case GL_SRC0_ALPHA:
173     case GL_SRC1_ALPHA:
174     case GL_SRC2_ALPHA:
175     case GL_OPERAND0_ALPHA:
176     case GL_OPERAND1_ALPHA:
177     case GL_OPERAND2_ALPHA:
178     case GL_RGB_SCALE:
179     case GL_ALPHA_SCALE:
180     case GL_COMBINE_RGB:
181     case GL_POLYGON_OFFSET_FACTOR:
182     case GL_POLYGON_OFFSET_FILL:
183     case GL_POLYGON_OFFSET_UNITS:
184     case GL_RED_BITS:
185     case GL_RESCALE_NORMAL:
186     case GL_SAMPLE_ALPHA_TO_COVERAGE:
187     case GL_SAMPLE_ALPHA_TO_ONE:
188     case GL_SAMPLE_BUFFERS:
189     case GL_SAMPLE_COVERAGE:
190     case GL_SAMPLE_COVERAGE_INVERT:
191     case GL_SAMPLE_COVERAGE_VALUE:
192     case GL_SAMPLES:
193     case GL_MAX_SAMPLES_EXT:
194     case GL_STENCIL_BITS:
195     case GL_STENCIL_CLEAR_VALUE:
196     case GL_STENCIL_FUNC:
197     case GL_STENCIL_TEST:
198     case GL_STENCIL_VALUE_MASK:
199     case GL_STENCIL_BACK_FUNC:
200     case GL_STENCIL_BACK_VALUE_MASK:
201     case GL_STENCIL_BACK_REF:
202     case GL_STENCIL_BACK_FAIL:
203     case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
204     case GL_STENCIL_BACK_PASS_DEPTH_PASS:
205     case GL_STENCIL_BACK_WRITEMASK:
206     case GL_TEXTURE_2D:
207     case GL_TEXTURE_BINDING_2D:
208     case GL_TEXTURE_BINDING_CUBE_MAP:
209     case GL_TEXTURE_BINDING_EXTERNAL_OES:
210     case GL_TEXTURE_COORD_ARRAY:
211     case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING:
212     case GL_TEXTURE_COORD_ARRAY_SIZE:
213     case GL_TEXTURE_COORD_ARRAY_STRIDE:
214     case GL_TEXTURE_COORD_ARRAY_TYPE:
215     case GL_UNPACK_ALIGNMENT:
216     case GL_VERTEX_ARRAY:
217     case GL_VERTEX_ARRAY_BUFFER_BINDING:
218     case GL_VERTEX_ARRAY_SIZE:
219     case GL_VERTEX_ARRAY_STRIDE:
220     case GL_VERTEX_ARRAY_TYPE:
221     case GL_SPOT_CUTOFF:
222     case GL_TEXTURE_MIN_FILTER:
223     case GL_TEXTURE_MAG_FILTER:
224     case GL_TEXTURE_WRAP_S:
225     case GL_TEXTURE_WRAP_T:
226     case GL_GENERATE_MIPMAP:
227     case GL_GENERATE_MIPMAP_HINT:
228     case GL_RENDERBUFFER_WIDTH_OES:
229     case GL_RENDERBUFFER_HEIGHT_OES:
230     case GL_RENDERBUFFER_INTERNAL_FORMAT_OES:
231     case GL_RENDERBUFFER_RED_SIZE_OES:
232     case GL_RENDERBUFFER_GREEN_SIZE_OES:
233     case GL_RENDERBUFFER_BLUE_SIZE_OES:
234     case GL_RENDERBUFFER_ALPHA_SIZE_OES:
235     case GL_RENDERBUFFER_DEPTH_SIZE_OES:
236     case GL_RENDERBUFFER_STENCIL_SIZE_OES:
237     case GL_RENDERBUFFER_BINDING:
238     case GL_FRAMEBUFFER_BINDING:
239     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_OES:
240     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_OES:
241     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_OES:
242     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_OES:
243     case GL_FENCE_STATUS_NV:
244     case GL_FENCE_CONDITION_NV:
245     case GL_TEXTURE_WIDTH_QCOM:
246     case GL_TEXTURE_HEIGHT_QCOM:
247     case GL_TEXTURE_DEPTH_QCOM:
248     case GL_TEXTURE_INTERNAL_FORMAT_QCOM:
249     case GL_TEXTURE_FORMAT_QCOM:
250     case GL_TEXTURE_TYPE_QCOM:
251     case GL_TEXTURE_IMAGE_VALID_QCOM:
252     case GL_TEXTURE_NUM_LEVELS_QCOM:
253     case GL_TEXTURE_TARGET_QCOM:
254     case GL_TEXTURE_OBJECT_VALID_QCOM:
255     case GL_BLEND_EQUATION_RGB_OES:
256     case GL_BLEND_EQUATION_ALPHA_OES:
257     case GL_BLEND_DST_RGB_OES:
258     case GL_BLEND_SRC_RGB_OES:
259     case GL_BLEND_DST_ALPHA_OES:
260     case GL_BLEND_SRC_ALPHA_OES:
261     case GL_MAX_LIGHTS:
262     case GL_SHADER_TYPE:
263     case GL_DELETE_STATUS:
264     case GL_COMPILE_STATUS:
265     case GL_INFO_LOG_LENGTH:
266     case GL_SHADER_SOURCE_LENGTH:
267     case GL_CURRENT_PROGRAM:
268     case GL_LINK_STATUS:
269     case GL_VALIDATE_STATUS:
270     case GL_ATTACHED_SHADERS:
271     case GL_ACTIVE_UNIFORMS:
272     case GL_ACTIVE_ATTRIBUTES:
273     case GL_SUBPIXEL_BITS:
274     case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
275     case GL_NUM_SHADER_BINARY_FORMATS:
276     case GL_SHADER_COMPILER:
277     case GL_MAX_VERTEX_ATTRIBS:
278     case GL_MAX_VERTEX_UNIFORM_VECTORS:
279     case GL_MAX_VARYING_VECTORS:
280     case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
281     case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
282     case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
283     case GL_MAX_RENDERBUFFER_SIZE:
284     case GL_MAX_TEXTURE_IMAGE_UNITS:
285     case GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES:
286     case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
287     case GL_LINE_WIDTH:
288         s = 1;
289         break;
290     case GL_ALIASED_LINE_WIDTH_RANGE:
291     case GL_ALIASED_POINT_SIZE_RANGE:
292     case GL_DEPTH_RANGE:
293     case GL_MAX_VIEWPORT_DIMS:
294     case GL_SMOOTH_POINT_SIZE_RANGE:
295     case GL_SMOOTH_LINE_WIDTH_RANGE:
296         s= 2;
297         break;
298     case GL_SPOT_DIRECTION:
299     case GL_POINT_DISTANCE_ATTENUATION:
300     case GL_CURRENT_NORMAL:
301         s =  3;
302         break;
303     case GL_CURRENT_VERTEX_ATTRIB:
304     case GL_CURRENT_TEXTURE_COORDS:
305     case GL_CURRENT_COLOR:
306     case GL_FOG_COLOR:
307     case GL_AMBIENT:
308     case GL_DIFFUSE:
309     case GL_SPECULAR:
310     case GL_EMISSION:
311     case GL_POSITION:
312     case GL_LIGHT_MODEL_AMBIENT:
313     case GL_TEXTURE_ENV_COLOR:
314     case GL_SCISSOR_BOX:
315     case GL_VIEWPORT:
316     case GL_TEXTURE_CROP_RECT_OES:
317     case GL_COLOR_CLEAR_VALUE:
318     case GL_COLOR_WRITEMASK:
319     case GL_AMBIENT_AND_DIFFUSE:
320     case GL_BLEND_COLOR:
321         s =  4;
322         break;
323     case GL_MODELVIEW_MATRIX:
324     case GL_PROJECTION_MATRIX:
325     case GL_TEXTURE_MATRIX:
326         s = 16;
327     break;
328     default:
329         ERR("glUtilsParamSize: unknow param 0x%08x\n", param);
330         s = 1; // assume 1
331     }
332     return s;
333 }
334 
glUtilsPackPointerData(unsigned char * dst,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)335 void glUtilsPackPointerData(unsigned char *dst, unsigned char *src,
336                      int size, GLenum type, unsigned int stride,
337                      unsigned int datalen)
338 {
339     unsigned int  vsize = size * glSizeof(type);
340     if (stride == 0) stride = vsize;
341 
342     if (stride == vsize) {
343         memcpy(dst, src, datalen);
344     } else {
345         for (unsigned int i = 0; i < datalen; i += vsize) {
346             memcpy(dst, src, vsize);
347             dst += vsize;
348             src += stride;
349         }
350     }
351 }
352 
glUtilsWritePackPointerData(void * _stream,unsigned char * src,int size,GLenum type,unsigned int stride,unsigned int datalen)353 void glUtilsWritePackPointerData(void* _stream, unsigned char *src,
354                                  int size, GLenum type, unsigned int stride,
355                                  unsigned int datalen)
356 {
357     IOStream* stream = reinterpret_cast<IOStream*>(_stream);
358 
359     unsigned int  vsize = size * glSizeof(type);
360     if (stride == 0) stride = vsize;
361 
362     if (stride == vsize) {
363         stream->writeFully(src, datalen);
364     } else {
365         for (unsigned int i = 0; i < datalen; i += vsize) {
366             stream->writeFully(src, (size_t)vsize);
367             src += stride;
368         }
369     }
370 }
371 
glUtilsPixelBitSize(GLenum format,GLenum type)372 int glUtilsPixelBitSize(GLenum format, GLenum type)
373 {
374     int components = 0;
375     int componentsize = 0;
376     int pixelsize = 0;
377     switch(type) {
378     case GL_BYTE:
379     case GL_UNSIGNED_BYTE:
380         componentsize = 8;
381         break;
382     case GL_SHORT:
383     case GL_UNSIGNED_SHORT:
384     case GL_UNSIGNED_SHORT_5_6_5:
385     case GL_UNSIGNED_SHORT_4_4_4_4:
386     case GL_UNSIGNED_SHORT_5_5_5_1:
387     case GL_RGB565_OES:
388     case GL_RGB5_A1_OES:
389     case GL_RGBA4_OES:
390         pixelsize = 16;
391         break;
392     case GL_INT:
393     case GL_UNSIGNED_INT:
394     case GL_FLOAT:
395     case GL_FIXED:
396     case GL_UNSIGNED_INT_24_8_OES:
397         pixelsize = 32;
398         break;
399     default:
400         ERR("glUtilsPixelBitSize: unknown pixel type - assuming pixel data 0\n");
401         componentsize = 0;
402     }
403 
404     if (pixelsize == 0) {
405         switch(format) {
406 #if 0
407         case GL_RED:
408         case GL_GREEN:
409         case GL_BLUE:
410 #endif
411         case GL_ALPHA:
412         case GL_LUMINANCE:
413         case GL_DEPTH_COMPONENT:
414         case GL_DEPTH_STENCIL_OES:
415             components = 1;
416             break;
417         case GL_LUMINANCE_ALPHA:
418             components = 2;
419             break;
420         case GL_RGB:
421 #if 0
422         case GL_BGR:
423 #endif
424             components = 3;
425             break;
426         case GL_RGBA:
427         case GL_BGRA_EXT:
428             components = 4;
429             break;
430         default:
431             ERR("glUtilsPixelBitSize: unknown pixel format...\n");
432             components = 0;
433         }
434         pixelsize = components * componentsize;
435     }
436 
437     return pixelsize;
438 }
439 
440 // pack a list of strings into one.
glUtilsPackStrings(char * ptr,char ** strings,GLint * length,GLsizei count)441 void glUtilsPackStrings(char *ptr,  char **strings,  GLint *length, GLsizei count)
442 {
443     char *p = ptr;
444     *p = '\0';
445     for (int i = 0; i < count; i++) {
446         int l=0;
447         if (strings[i]!=NULL) {
448             if (length == NULL || length[i] < 0) {
449                 l = strlen(strings[i]);
450                 strcat(p, strings[i]);
451             } else {
452                 l = length[i];
453                 strncat(p, strings[i], l);
454             }
455         }
456         p += l;
457     }
458 }
459 
460 // claculate the length of a list of strings
glUtilsCalcShaderSourceLen(char ** strings,GLint * length,GLsizei count)461 int glUtilsCalcShaderSourceLen( char **strings,  GLint *length, GLsizei count)
462 {
463     int len = 0;
464     for (int i = 0; i < count; i++) {
465         int l;
466         if (length == NULL || length[i] < 0) {
467             l = strings[i]!=NULL ? strlen(strings[i]) : 0;
468         } else {
469             l = length[i];
470         }
471         len += l;
472     }
473     return len;
474 
475 }
476