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 #include <GLES/gl.h>
20
21 #include <assert.h>
22 #include <inttypes.h>
23 #include <stdio.h>
24
25 // setGles2Gles(true) when mounting GLES translator on top of another
26 // GLES library;
27 void setGles2Gles(bool isGles2gles);
28 bool isGles2Gles();
29
30 // is/setCoreProfile sets the global variable for
31 // when core profile is in use.
32 void setCoreProfile(bool isCore);
33 bool isCoreProfile();
34
35 typedef enum {
36 GLES_1_1 = 1,
37 GLES_2_0 = 2,
38 GLES_3_0 = 3,
39 GLES_3_1 = 4,
40 MAX_GLES_VERSION //Must be last
41 } GLESVersion;
42
43 bool isPowerOf2(int num);
44
45 // <EGL/egl.h> defines many types as 'void*' while they're really
46 // implemented as unsigned integers. These convenience template functions
47 // help casting between them safely without generating compiler warnings.
SafePointerFromUInt(unsigned int handle)48 inline void* SafePointerFromUInt(unsigned int handle) {
49 return (void*)(uintptr_t)(handle);
50 }
51
SafeUIntFromPointerFileLine(const void * ptr,const char * file,int line)52 inline unsigned int SafeUIntFromPointerFileLine(const void* ptr,
53 const char* file, int line) {
54 #if 1
55 // Ignore the assert below to avoid crashing when running older
56 // system images, which might have buggy encoder libraries. Print
57 // an error message though.
58 if ((uintptr_t)(ptr) != (unsigned int)(uintptr_t)(ptr)) {
59 fprintf(stderr, "(%s:%d) EmuGL:WARNING: bad generic pointer %p\n",
60 file, line, ptr);
61 }
62 #else
63 // Assertion error if the pointer contains a value that does not fit
64 // in an unsigned integer!
65 assert((uintptr_t)(ptr) == (unsigned int)(uintptr_t)(ptr));
66 #endif
67 return (unsigned int)(uintptr_t)(ptr);
68 }
69
70 #define SafeUIntFromPointer(ptr) \
71 SafeUIntFromPointerFileLine((ptr), __FILE__, __LINE__)
72
73 struct FramebufferChannelBits {
74 int red;
75 int green;
76 int blue;
77 int alpha;
78 int depth;
79 int stencil;
80 };
81
82 FramebufferChannelBits glFormatToChannelBits(GLenum colorFormat,
83 GLenum depthFormat,
84 GLenum stencilFormat);
85
86 GLenum baseFormatOfInternalFormat(GLint internalformat);
87 GLenum accurateTypeOfInternalFormat(GLint internalformat);
88
89 #endif
90