]> git.cworth.org Git - apitrace/blob - glretrace_cgl.cpp
Mac OS X: Fix getProcessName() to avoid assertion failure in truncate
[apitrace] / glretrace_cgl.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
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
27 #include <string.h>
28
29 #include "glproc.hpp"
30 #include "retrace.hpp"
31 #include "glretrace.hpp"
32
33
34 using namespace glretrace;
35
36
37 typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
38 typedef std::map<unsigned long long, glws::Context *> ContextMap;
39 static DrawableMap drawable_map;
40 static ContextMap context_map;
41 static glws::Context *sharedContext = NULL;
42
43
44 static glws::Drawable *
45 getDrawable(unsigned long drawable_id) {
46     if (drawable_id == 0) {
47         return NULL;
48     }
49
50     /* XXX: Support multiple drawables. */
51     drawable_id = 1;
52
53     DrawableMap::const_iterator it;
54     it = drawable_map.find(drawable_id);
55     if (it == drawable_map.end()) {
56         return (drawable_map[drawable_id] = glws::createDrawable(visual));
57     }
58
59     return it->second;
60 }
61
62
63 static glws::Context *
64 getContext(unsigned long long ctx) {
65     if (ctx == 0) {
66         return NULL;
67     }
68
69     ContextMap::const_iterator it;
70     it = context_map.find(ctx);
71     if (it == context_map.end()) {
72         glws::Context *context;
73         context_map[ctx] = context = glws::createContext(visual, sharedContext);
74         if (!sharedContext) {
75             sharedContext = context;
76         }
77         return context;
78     }
79
80     return it->second;
81 }
82
83
84 static void retrace_CGLSetCurrentContext(trace::Call &call) {
85     unsigned long long ctx = call.arg(0).toUIntPtr();
86
87     glws::Drawable *new_drawable = getDrawable(ctx);
88     glws::Context *new_context = getContext(ctx);
89
90     bool result = glws::makeCurrent(new_drawable, new_context);
91
92     if (new_drawable && new_context && result) {
93         drawable = new_drawable;
94         context = new_context;
95     } else {
96         drawable = NULL;
97         context = NULL;
98     }
99 }
100
101
102 static void retrace_CGLFlushDrawable(trace::Call &call) {
103     if (drawable && context) {
104         if (double_buffer) {
105             drawable->swapBuffers();
106         } else {
107             glFlush();
108         }
109
110         frame_complete(call);
111     }
112 }
113
114
115 const retrace::Entry glretrace::cgl_callbacks[] = {
116     {"CGLSetCurrentContext", &retrace_CGLSetCurrentContext},
117     {"CGLGetCurrentContext", &retrace::ignore},
118     {"CGLEnable", &retrace::ignore},
119     {"CGLDisable", &retrace::ignore},
120     {"CGLSetParameter", &retrace::ignore},
121     {"CGLGetParameter", &retrace::ignore},
122     {"CGLFlushDrawable", &retrace_CGLFlushDrawable},
123     {NULL, NULL},
124 };
125