summaryrefslogtreecommitdiff
path: root/wrappers/cgltrace.py
blob: 6c72f6966ca1a184f9004ee2fd5375be91887d7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
##########################################################################
#
# Copyright 2011 VMware, Inc.
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##########################################################################/


"""CGL tracing generator."""


from gltrace import GlTracer
from specs.stdapi import Module, API
from specs.glapi import glapi
from specs.cglapi import cglapi


class CglTracer(GlTracer):

    def isFunctionPublic(self, function):
        # all OpenGL symbols are visible on MacOSX
        return True

    def traceFunctionImplBody(self, function):
        if function.name == 'CGLReleaseContext':
            # Unlike other GL APIs like EGL or GLX, CGL will make the context
            # not current if it's the current context.
            print '    if (_CGLGetContextRetainCount(ctx) == 1) {'
            print '        if (gltrace::releaseContext((uintptr_t)ctx)) {'
            print '            if (_CGLGetCurrentContext() == ctx) {'
            print '                gltrace::clearContext();'
            print '            }'
            print '        }'
            print '    }'

        if function.name == 'CGLDestroyContext':
            # The same rule applies here about the  as for CGLReleaseContext.
            print '    if (gltrace::releaseContext((uintptr_t)ctx)) {'
            print '        if (_CGLGetCurrentContext() == ctx) {'
            print '            gltrace::clearContext();'
            print '        }'
            print '    }'

        GlTracer.traceFunctionImplBody(self, function)

        if function.name == 'CGLCreateContext':
            print '    if (_result == kCGLNoError) {'
            print '        gltrace::createContext((uintptr_t)*ctx);'
            print '    }'

        if function.name == 'CGLSetCurrentContext':
            print '    if (_result == kCGLNoError) {'
            print '        if (ctx != NULL) {'
            print '            gltrace::setContext((uintptr_t)ctx);'
            print '        } else {'
            print '            gltrace::clearContext();'
            print '        }'
            print '    }'

        if function.name == 'CGLRetainContext':
            print '    gltrace::retainContext((uintptr_t)ctx);'


if __name__ == '__main__':
    print
    print '#include <stdlib.h>'
    print '#include <string.h>'
    print
    print '#include "trace_writer_local.hpp"'
    print
    print '// To validate our prototypes'
    print '#define GL_GLEXT_PROTOTYPES'
    print
    print '#include "glproc.hpp"'
    print '#include "glsize.hpp"'
    print

    module = Module()
    module.mergeModule(cglapi)
    module.mergeModule(glapi)
    api = API()
    api.addModule(module)
    tracer = CglTracer()
    tracer.traceApi(api)

    print r'''

PUBLIC
void * gll_noop = 0;


__attribute__((constructor))
static void
_init(void) {
    /*
     * XXX: Setting DYLD_IMAGE_SUFFIX to anything (even an empty string)
     * changes dlopen behavior causing our trick of symlinking a temporary file
     * to the real OpenGL framework to stop working, leading to infinite
     * recursion.
     */
    if (getenv("DYLD_IMAGE_SUFFIX")) {
       os::log("error: tracing with DYLD_IMAGE_SUFFIX not supported.\n");
       os::abort();
    }

    /*
     * XXX: Temporary workaround for
     * https://github.com/apitrace/apitrace/issues/278#issuecomment-46889575
     * until we have a better way of intercepting applications that
     * dlopen("libGL.dylib") directly.
     */
    setenv("SDL_OPENGL_LIBRARY", "/System/Library/Frameworks/OpenGL.framework/OpenGL", 1);
}

'''