summaryrefslogtreecommitdiff
path: root/retrace/glws_waffle.cpp
blob: 3fcbe32fddf787c4a70d92257627b7ca29e7dca4 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#define WAFFLE_API_EXPERIMENTAL
#define WAFFLE_API_VERSION 0x0103

#include "os.hpp"
#include "waffle.h"
#include "glws.hpp"

namespace glws {

struct waffle_display *dpy;

class WaffleVisual : public Visual
{
public:
    struct waffle_config *config;

    WaffleVisual(Profile prof, waffle_config *cfg) :
        Visual(prof),
        config (cfg)
    {}

    ~WaffleVisual() {
        if (this->config != NULL) {
            waffle_config_destroy(this->config);
            this->config = NULL;
        }
    }
};

class WaffleDrawable : public Drawable
{
public:
    struct waffle_window *window;

    WaffleDrawable(const Visual *vis, int w, int h, bool pbuffer,
        struct waffle_window *win) :
        Drawable (vis, w, h, pbuffer),
        window (win)
    {}

    void
    resize(int w, int h) {
        if (w == width && h == height) {
            return;
        }
        waffle_window_resize(window, w, h);
        Drawable::resize(w, h);
    }

    void
    show(void) {
        if (visible) {
            return;
        }
        waffle_window_show(window);
        Drawable::show();
    }

    void
    swapBuffers(void) {
        waffle_window_swap_buffers(window);
    }
};

class WaffleContext : public Context
{
public:
    struct waffle_context *context;

    WaffleContext(const Visual *vis, struct waffle_context *ctx) :
        Context(vis),
        context(ctx)
    {}

    ~WaffleContext() {
        if( context != NULL ) {
            waffle_context_destroy(context);
            context = NULL;
        }
    }
};

/*
 With waffle there is not too many events to look for..
 */
bool
processEvents(void) {
    return true;
}

void
init(void) {
    int i;
    int waffle_init_attrib_list[3];

    i = 0;
    waffle_init_attrib_list[i++] = WAFFLE_PLATFORM;
    waffle_init_attrib_list[i++] =
#if defined(__ANDROID__)
        WAFFLE_PLATFORM_ANDROID
#elif defined(__APPLE__)
        WAFFLE_PLATFORM_CGL
#elif defined(HAVE_X11)
#  if 1
        WAFFLE_PLATFORM_GLX
#  else
        WAFFLE_PLATFORM_X11_EGL
#  endif
#else
#  warning Unsupported platform
        WAFFLE_NONE
#endif
    ;
    waffle_init_attrib_list[i++] = WAFFLE_NONE;

    waffle_init(waffle_init_attrib_list);

    dpy = waffle_display_connect(NULL);
    if (!dpy) {
        os::log("%s: waffle_display_connect(NULL) == NULL\n", __FILE__);
        os::abort();
    }
}

void
cleanup(void) {
    waffle_display_disconnect(dpy);
}

Visual *
createVisual(bool doubleBuffer, Profile profile) {
    struct waffle_config *cfg;
    int config_attrib_list[64], i(0), waffle_profile;

    switch (profile) {
    case PROFILE_ES1:
        waffle_profile = WAFFLE_CONTEXT_OPENGL_ES1;
        break;
    case PROFILE_ES2:
        waffle_profile = WAFFLE_CONTEXT_OPENGL_ES2;
        break;
    default:
        os::log("%s: Unsupported context profile\n", __FILE__);
        os::abort();
        return NULL;
    }

    if(!waffle_display_supports_context_api(dpy, waffle_profile)) {
        os::log("%s: !waffle_display_supports_context_api\n",
            __FILE__);

        os::abort();
        return NULL;
    }

    config_attrib_list[i++] = WAFFLE_CONTEXT_API;
    config_attrib_list[i++] = waffle_profile;
    config_attrib_list[i++] = WAFFLE_RED_SIZE;
    config_attrib_list[i++] = 8;
    config_attrib_list[i++] = WAFFLE_GREEN_SIZE;
    config_attrib_list[i++] = 8;
    config_attrib_list[i++] = WAFFLE_BLUE_SIZE;
    config_attrib_list[i++] = 8;
    config_attrib_list[i++] = WAFFLE_DEPTH_SIZE;
    config_attrib_list[i++] = 8;
    config_attrib_list[i++] = WAFFLE_ALPHA_SIZE;
    config_attrib_list[i++] = 8;
    config_attrib_list[i++] = WAFFLE_STENCIL_SIZE;
    config_attrib_list[i++] = 8;
    config_attrib_list[i++] = WAFFLE_DOUBLE_BUFFERED;
    config_attrib_list[i++] = doubleBuffer;
    config_attrib_list[i++] = 0;

    cfg = waffle_config_choose(dpy, config_attrib_list);
    if (!cfg)
    {
        os::log("Error in %s waffle_config_choose(dpy, " \
            "config_attrib_list)\n", __FILE__);
        os::abort();
        return NULL;
    }
    return new WaffleVisual(profile, cfg);
}

Drawable *
createDrawable(const Visual *visual, int width, int height, bool pbuffer)
{
    struct waffle_window *window;
    const WaffleVisual *waffleVisual =
        static_cast<const WaffleVisual *>(visual);

    window = waffle_window_create(waffleVisual->config, width, height);
    return new WaffleDrawable(visual, width, height, pbuffer, window);
}

Context *
createContext(const Visual *visual, Context *shareContext,
              bool debug)
{
    struct waffle_context *context;
    const WaffleVisual *waffleVisual =
        static_cast<const WaffleVisual *>(visual);

    if (shareContext != NULL) {
        os::log("Error in %s createContext, shareContext != NULL\n", __FILE__);
        os::abort();
        return NULL;
    }

    context = waffle_context_create(waffleVisual->config, NULL);
    if (!context) {
        os::log("Error in %s waffle_context_create(config, NULL)\n",
               __FILE__);

        os::abort();
        return NULL;
    }
    return new WaffleContext(visual, context);
}

bool
makeCurrent(Drawable *drawable, Context *context)
{
    if (!drawable || !context) {
        return waffle_make_current(dpy, NULL, NULL);
    } else {
        WaffleDrawable *waffleDrawable =
            static_cast<WaffleDrawable *>(drawable);

        WaffleContext *waffleContext =
            static_cast<WaffleContext *>(context);

        return waffle_make_current(dpy, waffleDrawable->window,
                                   waffleContext->context);
    }
}

} /* namespace glws */