1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
30 #include "retrace.hpp"
31 #include "glretrace.hpp"
34 using namespace glretrace;
37 typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
38 typedef std::map<unsigned long long, Context *> ContextMap;
39 static DrawableMap drawable_map;
40 static ContextMap context_map;
41 static Context *sharedContext = NULL;
44 static glws::Drawable *
45 getDrawable(unsigned long drawable_id) {
46 if (drawable_id == 0) {
50 /* XXX: Support multiple drawables. */
53 DrawableMap::const_iterator it;
54 it = drawable_map.find(drawable_id);
55 if (it == drawable_map.end()) {
56 return (drawable_map[drawable_id] = glretrace::createDrawable());
64 getContext(unsigned long long ctx) {
69 ContextMap::const_iterator it;
70 it = context_map.find(ctx);
71 if (it == context_map.end()) {
73 context_map[ctx] = context = glretrace::createContext(sharedContext);
75 sharedContext = context;
83 static void retrace_CGLCreateContext(trace::Call &call) {
84 unsigned long long share = call.arg(1).toUIntPtr();
85 Context *sharedContext = getContext(share);
87 const trace::Array *ctx_ptr = dynamic_cast<const trace::Array *>(&call.arg(2));
88 unsigned long long ctx = ctx_ptr->values[0]->toUIntPtr();
90 Context *context = glretrace::createContext(sharedContext);
91 context_map[ctx] = context;
95 static void retrace_CGLDestroyContext(trace::Call &call) {
96 unsigned long long ctx = call.arg(0).toUIntPtr();
98 ContextMap::iterator it;
99 it = context_map.find(ctx);
100 if (it == context_map.end()) {
106 context_map.erase(it);
110 static void retrace_CGLSetCurrentContext(trace::Call &call) {
111 unsigned long long ctx = call.arg(0).toUIntPtr();
113 glws::Drawable *new_drawable = getDrawable(ctx);
114 Context *new_context = getContext(ctx);
116 glretrace::makeCurrent(call, new_drawable, new_context);
120 static void retrace_CGLFlushDrawable(trace::Call &call) {
121 if (currentDrawable && currentContext) {
122 if (retrace::doubleBuffer) {
123 currentDrawable->swapBuffers();
128 frame_complete(call);
133 const retrace::Entry glretrace::cgl_callbacks[] = {
134 {"CGLCreateContext", &retrace_CGLCreateContext},
135 {"CGLDestroyContext", &retrace_CGLDestroyContext},
136 {"CGLSetCurrentContext", &retrace_CGLSetCurrentContext},
137 {"CGLGetCurrentContext", &retrace::ignore},
138 {"CGLEnable", &retrace::ignore},
139 {"CGLDisable", &retrace::ignore},
140 {"CGLSetParameter", &retrace::ignore},
141 {"CGLGetParameter", &retrace::ignore},
142 {"CGLFlushDrawable", &retrace_CGLFlushDrawable},