File: Device.cpp

package info (click to toggle)
storm-lang 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,984 kB
  • sloc: ansic: 261,420; cpp: 140,270; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (189 lines) | stat: -rw-r--r-- 4,444 bytes parent folder | download | duplicates (3)
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
#include "stdafx.h"
#include "Device.h"
#include "App.h"
#include "Core/Convert.h"
#include "D2D/Device.h"
#include "Cairo/Device.h"
#include "Cairo/GLDevice.h"
#include "Skia/Device.h"
#include "Exception.h"
#include "Window.h"
#include "RenderMgr.h"
#include "Workaround/Apply.h"

namespace gui {

#if defined(GUI_WIN32)

	Device *Device::create(Engine &e) {
		return applyEnvWorkarounds(new D2DDevice(e));
	}

#elif defined(GUI_GTK)

	GtkWidget *Device::drawWidget(Engine &e, Handle handle) {
		Window *w = app(e)->findWindow(handle);
		if (!w)
			return handle.widget();
		else
			return w->drawWidget();
	}

	template <class T>
	Device *create(Engine &e) {
		return new T(e);
	}

	struct CreateDevice {
		const char *name;
		Device *(*create)(Engine &e);
	};

	// These are ordered by priority:
	static CreateDevice devices[] = {
#ifdef GUI_ENABLE_SKIA
		{ "skia", &create<SkiaDevice> },
#endif
#ifdef GUI_ENABLE_CAIRO_GL
		{ "gl", &create<CairoGLDevice> },
#endif
		{ "gtk", &create<CairoGtkDevice> },
		{ "software", &create<CairoSwDevice> },
		{ "sw", &create<CairoSwDevice> },
	};

	Device *Device::create(Engine &e) {
		const char *preference = getenv(ENV_RENDER_BACKEND);
		CreateDevice *selected = null;
		if (!preference) {
			selected = &devices[0];
		} else {
			for (size_t i = 0; i < ARRAY_COUNT(devices); i++) {
				if (strcmp(preference, devices[i].name) == 0) {
					selected = &devices[i];
					break;
				}
			}

			if (!selected) {
				StrBuf *msg = new (e) StrBuf();
				*msg << S("The supplied value of ") S(ENV_RENDER_BACKEND) S(" is not supported.\n");
				*msg << S("This build provides the following options: ");
				for (size_t i = 0; i < ARRAY_COUNT(devices); i++) {
					if (i > 0)
						*msg << S(", ");
					*msg << toWChar(e, devices[i].name)->v;
				}

				throw new (e) GuiError(msg->toS());
			}
		}

		Device *device = (*selected->create)(e);
		return applyEnvWorkarounds(device);
	}

#else
#error "Unknown UI toolkit."
#endif

#ifdef GUI_GTK

	static void reportError(Engine &e, GError *error) {
		StrBuf *msg = new (e) StrBuf();
		*msg << S("Initialization of OpenGL failed: ") << toWChar(e, error->message)->v << S("\n");
		*msg << S("Try setting the environment variable ") << S(ENV_RENDER_BACKEND) << S(" to \"gtk\" or \"software\".");
		g_clear_error(&error);

		throw new (e) GuiError(msg->toS());
	}

	GLDevice::GLDevice(Engine &e) : Device(), e(e) {}

	GLDevice::~GLDevice() {
		for (Map::iterator i = context.begin(); i != context.end(); ++i) {
			i->second->owner = null;
		}
	}

	Surface *GLDevice::createSurface(Handle window) {
		GtkWidget *widget = drawWidget(e, window);
		GdkWindow *w = gtk_widget_get_window(widget);
		if (!w)
			return null;

		Context *c = null;

		// Note: We don't own references in the map.
		Map::iterator found = context.find(w);
		if (found == context.end()) {
			c = createContext(w, createContext(w));

			// Figure out if we need any workarounds...
			gdk_gl_context_make_current(c->context);
			c->workarounds = glWorkarounds();

			context.insert(std::make_pair(w, c));
		} else {
			c = found->second;
			c->ref();
		}

		try {
			Surface *r = createSurface(widget, c);
			if (c->workarounds)
				r = c->workarounds->apply(r);
			return r;
		} catch (...) {
			c->unref();
			throw;
		}
	}

	GdkGLContext *GLDevice::createContext(GdkWindow *window) {
		GError *error = NULL;
		GdkGLContext *context = gdk_window_create_gl_context(window, &error);
		if (error) {
			g_object_unref(context);
			reportError(e, error);
		}

		gdk_gl_context_set_use_es(context, true);
		gdk_gl_context_set_required_version(context, 2, 0);

		gdk_gl_context_realize(context, &error);
		if (error) {
			g_object_unref(context);
			reportError(e, error);
		}

		return context;
	}

	GLDevice::Context *GLDevice::createContext(GdkWindow *window, GdkGLContext *context) {
		return new Context(this, window, context);
	}

	GLDevice::Context::Context(GLDevice *owner, GdkWindow *window, GdkGLContext *context)
		: window(window), context(context), id(0), refs(1), owner(owner), workarounds(null) {}

	GLDevice::Context::~Context() {
		// Remove us from our owner's map fo contexts. The owner might have been destroyed before
		// us, so keep that in mind.
		if (owner) {
			owner->context.erase(window);

			// Free the ID as well.
			if (id)
				renderMgr(owner->e)->freeId(id);
		}

		gdk_gl_context_clear_current();
		g_object_unref(context);

		delete workarounds;
	}

#endif

}