aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/hgl/GLRendererRoster.cpp
blob: 1712a871c985e87ee5799a53f2d6b6ae8f3b7a9c (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
/*
 * Copyright 2006-2012 Haiku, Inc. All Rights Reserved.
 * Distributed under the terms of the MIT License.
 *
 * Authors:
 *		Philippe Houdoin <philippe.houdoin@free.fr>
 *		Alexander von Gluck IV <kallisti5@unixzen.com>
 */


#include <driver_settings.h>
#include <image.h>

#include <kernel/image.h>
#include <system/safemode_defs.h>

#include <Directory.h>
#include <FindDirectory.h>
#include <Path.h>
#include <String.h>
#include "GLDispatcher.h"
#include "GLRendererRoster.h"

#include <new>
#include <string.h>


extern "C" status_t _kern_get_safemode_option(const char* parameter,
	char* buffer, size_t* _bufferSize);


GLRendererRoster::GLRendererRoster(BGLView* view, ulong options)
	:
	fNextID(0),
	fView(view),
	fOptions(options),
	fSafeMode(false),
	fABISubDirectory(NULL)
{
	char parameter[32];
	size_t parameterLength = sizeof(parameter);

	if (_kern_get_safemode_option(B_SAFEMODE_SAFE_MODE,
		parameter, &parameterLength) == B_OK) {
		if (!strcasecmp(parameter, "enabled") || !strcasecmp(parameter, "on")
			|| !strcasecmp(parameter, "true") || !strcasecmp(parameter, "yes")
			|| !strcasecmp(parameter, "enable") || !strcmp(parameter, "1"))
			fSafeMode = true;
	}

	if (_kern_get_safemode_option(B_SAFEMODE_DISABLE_USER_ADD_ONS,
		parameter, &parameterLength) == B_OK) {
		if (!strcasecmp(parameter, "enabled") || !strcasecmp(parameter, "on")
			|| !strcasecmp(parameter, "true") || !strcasecmp(parameter, "yes")
			|| !strcasecmp(parameter, "enable") || !strcmp(parameter, "1"))
			fSafeMode = true;
	}

	// We might run in compatibility mode on a system with a different ABI. The
	// renderers matching our ABI can usually be found in respective
	// subdirectories of the opengl add-ons directories.
	system_info info;
	if (get_system_info(&info) == B_OK
		&& (info.abi & B_HAIKU_ABI_MAJOR)
			!= (B_HAIKU_ABI & B_HAIKU_ABI_MAJOR)) {
			switch (B_HAIKU_ABI & B_HAIKU_ABI_MAJOR) {
				case B_HAIKU_ABI_GCC_2:
					fABISubDirectory = "gcc2";
					break;
				case B_HAIKU_ABI_GCC_4:
					fABISubDirectory = "gcc4";
					break;
			}
	}

	AddDefaultPaths();
}


GLRendererRoster::~GLRendererRoster()
{

}


BGLRenderer*
GLRendererRoster::GetRenderer(int32 id)
{
	RendererMap::const_iterator iterator = fRenderers.find(id);
	if (iterator == fRenderers.end())
		return NULL;

	struct renderer_item item = iterator->second;
	return item.renderer;
}


void
GLRendererRoster::AddDefaultPaths()
{
	// add user directories first, so that they can override system renderers
	const directory_which paths[] = {
		B_USER_NONPACKAGED_ADDONS_DIRECTORY,
		B_USER_ADDONS_DIRECTORY,
		B_SYSTEM_ADDONS_DIRECTORY,
	};

	for (uint32 i = fSafeMode ? 4 : 0;
		i < sizeof(paths) / sizeof(paths[0]); i++) {
		BPath path;
		status_t status = find_directory(paths[i], &path, true);
		if (status == B_OK && path.Append("opengl") == B_OK)
			AddPath(path.Path());
	}
}


status_t
GLRendererRoster::AddPath(const char* path)
{
	BDirectory directory(path);
	status_t status = directory.InitCheck();
	if (status < B_OK)
		return status;

	// if a subdirectory for our ABI exists, use that instead
	if (fABISubDirectory != NULL) {
		BEntry entry(&directory, fABISubDirectory);
		if (entry.IsDirectory()) {
			status = directory.SetTo(&entry);
			if (status != B_OK)
				return status;
		}
	}

	node_ref nodeRef;
	status = directory.GetNodeRef(&nodeRef);
	if (status < B_OK)
		return status;

	int32 count = 0;
	int32 files = 0;

	entry_ref ref;
	BEntry entry;
	while (directory.GetNextRef(&ref) == B_OK) {
		entry.SetTo(&ref);
		if (entry.InitCheck() == B_OK && !entry.IsFile())
			continue;

		if (CreateRenderer(ref) == B_OK)
			count++;

		files++;
	}

	if (files != 0 && count == 0)
		return B_BAD_VALUE;

	return B_OK;
}


status_t
GLRendererRoster::AddRenderer(BGLRenderer* renderer,
	image_id image, const entry_ref* ref, ino_t node)
{
	renderer_item item;
	item.renderer = renderer;
	item.image = image;
	item.node = node;
	if (ref != NULL)
		item.ref = *ref;

	try {
		fRenderers[fNextID] = item;
	} catch (...) {
		return B_NO_MEMORY;
	}

	renderer->fOwningRoster = this;
	renderer->fID = fNextID++;
	return B_OK;
}


status_t
GLRendererRoster::CreateRenderer(const entry_ref& ref)
{
	BEntry entry(&ref);
	node_ref nodeRef;
	status_t status = entry.GetNodeRef(&nodeRef);
	if (status < B_OK)
		return status;

	BPath path(&ref);
	image_id image = load_add_on(path.Path());
	if (image < B_OK)
		return image;

	BGLRenderer* (*instantiate_renderer)
		(BGLView* view, ulong options, BGLDispatcher* dispatcher);

	status = get_image_symbol(image, "instantiate_gl_renderer",
		B_SYMBOL_TYPE_TEXT, (void**)&instantiate_renderer);
	if (status == B_OK) {
		BGLRenderer* renderer
			= instantiate_renderer(fView, fOptions, new BGLDispatcher());
		if (!renderer) {
			unload_add_on(image);
			return B_UNSUPPORTED;
		}

		if (AddRenderer(renderer, image, &ref, nodeRef.node) != B_OK) {
			renderer->Release();
			// this will delete the renderer
			unload_add_on(image);
		}
		return B_OK;
	}
	unload_add_on(image);

	return status;
}