José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 1 | ########################################################################## |
| 2 | # |
| 3 | # Copyright 2011 VMware, Inc. |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | # of this software and associated documentation files (the "Software"), to deal |
| 8 | # in the Software without restriction, including without limitation the rights |
| 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | # copies of the Software, and to permit persons to whom the Software is |
| 11 | # furnished to do so, subject to the following conditions: |
| 12 | # |
| 13 | # The above copyright notice and this permission notice shall be included in |
| 14 | # all copies or substantial portions of the Software. |
| 15 | # |
| 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | # THE SOFTWARE. |
| 23 | # |
| 24 | ##########################################################################/ |
| 25 | |
| 26 | |
José Fonseca | 1b6c875 | 2012-04-15 14:33:00 +0100 | [diff] [blame] | 27 | """CGL tracing generator.""" |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 28 | |
| 29 | |
José Fonseca | 452d325 | 2012-04-14 15:55:40 +0100 | [diff] [blame] | 30 | from gltrace import GlTracer |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 31 | from specs.stdapi import Module, API |
José Fonseca | bd86a22 | 2011-09-27 09:21:38 +0100 | [diff] [blame] | 32 | from specs.glapi import glapi |
| 33 | from specs.cglapi import cglapi |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 34 | |
| 35 | |
| 36 | class CglTracer(GlTracer): |
| 37 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 38 | def isFunctionPublic(self, function): |
José Fonseca | 1b6c875 | 2012-04-15 14:33:00 +0100 | [diff] [blame] | 39 | # all OpenGL symbols are visible on MacOSX |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 40 | return True |
| 41 | |
Imre Deak | a4c698d | 2012-05-23 10:56:06 +0300 | [diff] [blame] | 42 | def traceFunctionImplBody(self, function): |
Imre Deak | a4c698d | 2012-05-23 10:56:06 +0300 | [diff] [blame] | 43 | if function.name == 'CGLReleaseContext': |
| 44 | # Unlike other GL APIs like EGL or GLX, CGL will make the context |
| 45 | # not current if it's the current context. |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 46 | print(' if (_CGLGetContextRetainCount(ctx) == 1) {') |
| 47 | print(' if (gltrace::releaseContext((uintptr_t)ctx)) {') |
| 48 | print(' if (_CGLGetCurrentContext() == ctx) {') |
| 49 | print(' gltrace::clearContext();') |
| 50 | print(' }') |
| 51 | print(' }') |
| 52 | print(' }') |
Imre Deak | a4c698d | 2012-05-23 10:56:06 +0300 | [diff] [blame] | 53 | |
| 54 | if function.name == 'CGLDestroyContext': |
| 55 | # The same rule applies here about the as for CGLReleaseContext. |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 56 | print(' if (gltrace::releaseContext((uintptr_t)ctx)) {') |
| 57 | print(' if (_CGLGetCurrentContext() == ctx) {') |
| 58 | print(' gltrace::clearContext();') |
| 59 | print(' }') |
| 60 | print(' }') |
Imre Deak | a4c698d | 2012-05-23 10:56:06 +0300 | [diff] [blame] | 61 | |
José Fonseca | 53eb318 | 2012-07-09 10:47:16 +0100 | [diff] [blame] | 62 | GlTracer.traceFunctionImplBody(self, function) |
| 63 | |
| 64 | if function.name == 'CGLCreateContext': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 65 | print(' if (_result == kCGLNoError) {') |
Robert Tarasov | 6ccf5bb | 2020-01-10 12:31:25 -0800 | [diff] [blame] | 66 | print(' gltrace::createContext((uintptr_t)*ctx, (uintptr_t)share);') |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 67 | print(' }') |
José Fonseca | 53eb318 | 2012-07-09 10:47:16 +0100 | [diff] [blame] | 68 | |
| 69 | if function.name == 'CGLSetCurrentContext': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 70 | print(' if (_result == kCGLNoError) {') |
| 71 | print(' if (ctx != NULL) {') |
| 72 | print(' gltrace::setContext((uintptr_t)ctx);') |
| 73 | print(' } else {') |
| 74 | print(' gltrace::clearContext();') |
| 75 | print(' }') |
| 76 | print(' }') |
José Fonseca | 53eb318 | 2012-07-09 10:47:16 +0100 | [diff] [blame] | 77 | |
| 78 | if function.name == 'CGLRetainContext': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 79 | print(' gltrace::retainContext((uintptr_t)ctx);') |
José Fonseca | 53eb318 | 2012-07-09 10:47:16 +0100 | [diff] [blame] | 80 | |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 81 | |
| 82 | if __name__ == '__main__': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 83 | print() |
| 84 | print('#include <stdlib.h>') |
| 85 | print('#include <string.h>') |
| 86 | print() |
| 87 | print('#include "trace_writer_local.hpp"') |
| 88 | print() |
| 89 | print('// To validate our prototypes') |
| 90 | print('#define GL_GLEXT_PROTOTYPES') |
| 91 | print() |
| 92 | print('#include "glproc.hpp"') |
| 93 | print('#include "glsize.hpp"') |
| 94 | print() |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 95 | |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 96 | module = Module() |
| 97 | module.mergeModule(cglapi) |
| 98 | module.mergeModule(glapi) |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 99 | api = API() |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 100 | api.addModule(module) |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 101 | tracer = CglTracer() |
José Fonseca | 1b6c875 | 2012-04-15 14:33:00 +0100 | [diff] [blame] | 102 | tracer.traceApi(api) |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 103 | |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 104 | print(r''' |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 105 | |
José Fonseca | 4364860 | 2011-05-15 12:56:59 +0100 | [diff] [blame] | 106 | PUBLIC |
| 107 | void * gll_noop = 0; |
| 108 | |
José Fonseca | aa5ba93 | 2014-06-24 11:33:59 +0100 | [diff] [blame] | 109 | |
| 110 | __attribute__((constructor)) |
| 111 | static void |
| 112 | _init(void) { |
| 113 | /* |
José Fonseca | a3e4614 | 2014-12-04 22:08:24 +0000 | [diff] [blame] | 114 | * XXX: Setting DYLD_IMAGE_SUFFIX to anything (even an empty string) |
| 115 | * changes dlopen behavior causing our trick of symlinking a temporary file |
| 116 | * to the real OpenGL framework to stop working, leading to infinite |
| 117 | * recursion. |
| 118 | */ |
| 119 | if (getenv("DYLD_IMAGE_SUFFIX")) { |
| 120 | os::log("error: tracing with DYLD_IMAGE_SUFFIX not supported.\n"); |
| 121 | os::abort(); |
| 122 | } |
| 123 | |
| 124 | /* |
José Fonseca | aa5ba93 | 2014-06-24 11:33:59 +0100 | [diff] [blame] | 125 | * XXX: Temporary workaround for |
| 126 | * https://github.com/apitrace/apitrace/issues/278#issuecomment-46889575 |
| 127 | * until we have a better way of intercepting applications that |
| 128 | * dlopen("libGL.dylib") directly. |
| 129 | */ |
| 130 | setenv("SDL_OPENGL_LIBRARY", "/System/Library/Frameworks/OpenGL.framework/OpenGL", 1); |
| 131 | } |
| 132 | |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 133 | ''') |