gl2.cpp revision 761eaed2ffc5022606de252997630dbcd9e805d9
1/*
2 ** Copyright 2007, 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
17#include <ctype.h>
18#include <string.h>
19#include <errno.h>
20
21#include <sys/ioctl.h>
22
23#include <GLES2/gl2.h>
24#include <GLES2/gl2ext.h>
25
26#include <cutils/log.h>
27#include <cutils/properties.h>
28
29#include "hooks.h"
30#include "egl_impl.h"
31
32using namespace android;
33
34// ----------------------------------------------------------------------------
35// Actual GL entry-points
36// ----------------------------------------------------------------------------
37
38#undef API_ENTRY
39#undef CALL_GL_API
40#undef CALL_GL_API_RETURN
41
42#define DEBUG_CALL_GL_API 0
43
44#if USE_FAST_TLS_KEY
45
46    #ifdef HAVE_ARM_TLS_REGISTER
47        #define GET_TLS(reg) \
48            "mrc p15, 0, " #reg ", c13, c0, 3 \n"
49    #else
50        #define GET_TLS(reg) \
51            "mov   " #reg ", #0xFFFF0FFF      \n"  \
52            "ldr   " #reg ", [" #reg ", #-15] \n"
53    #endif
54
55    #define API_ENTRY(_api) __attribute__((naked)) _api
56
57    #define CALL_GL_API(_api, ...)                              \
58         asm volatile(                                          \
59            GET_TLS(r12)                                        \
60            "ldr   r12, [r12, %[tls]] \n"                       \
61            "cmp   r12, #0            \n"                       \
62            "ldrne pc,  [r12, %[api]] \n"                       \
63            "bx    lr                 \n"                       \
64            :                                                   \
65            : [tls] "J"(TLS_SLOT_OPENGL_API*4),                 \
66              [api] "J"(__builtin_offsetof(gl_hooks_t, gl._api))    \
67            :                                                   \
68            );
69
70    #define CALL_GL_API_RETURN(_api, ...) \
71        CALL_GL_API(_api, __VA_ARGS__) \
72        return 0; // placate gcc's warnings. never reached.
73
74#else
75
76    #define API_ENTRY(_api) _api
77
78#if DEBUG_CALL_GL_API
79
80    #define CALL_GL_API(_api, ...)                                       \
81        gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;  \
82        _c->_api(__VA_ARGS__); \
83        GLenum status = GL_NO_ERROR; \
84        while ((status = glGetError()) != GL_NO_ERROR) { \
85            LOGD("[" #_api "] 0x%x", status); \
86        }
87
88#else
89
90    #define CALL_GL_API(_api, ...)                                       \
91        gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;  \
92        _c->_api(__VA_ARGS__);
93
94#endif
95
96    #define CALL_GL_API_RETURN(_api, ...)                                \
97        gl_hooks_t::gl_t const * const _c = &getGlThreadSpecific()->gl;  \
98        return _c->_api(__VA_ARGS__)
99
100#endif
101
102
103extern "C" {
104#include "gl2_api.in"
105#include "gl2ext_api.in"
106}
107
108#undef API_ENTRY
109#undef CALL_GL_API
110#undef CALL_GL_API_RETURN
111
112
113/*
114 * These GL calls are special because they need to EGL to retrieve some
115 * informations before they can execute.
116 */
117
118extern "C" void __glEGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image);
119extern "C" void __glEGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image);
120
121
122void glEGLImageTargetTexture2DOES(GLenum target, GLeglImageOES image)
123{
124    GLeglImageOES implImage =
125        (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
126    __glEGLImageTargetTexture2DOES(target, implImage);
127}
128
129void glEGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
130{
131    GLeglImageOES implImage =
132        (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image);
133    __glEGLImageTargetRenderbufferStorageOES(target, implImage);
134}
135
136