-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathegl_utils.lua
277 lines (195 loc) · 5.86 KB
/
egl_utils.lua
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
local ffi = require "ffi"
local bit = require "bit"
local lshift = bit.lshift
local DMX = require "DisplayManX"
local EGL = require "egl"
local vgu = require "vgu"
EGL.Lib = ffi.load("EGL");
local EglDisplay = {}
local EglDisplay_mt = {
__index = EglDisplay,
}
EglDisplay.new = function(api, dispid)
local dpy
if not dispid then
dpy = EGL.Lib.eglGetDisplay(EGL.EGL_DEFAULT_DISPLAY);
else
return nil
end
local obj = {
Handle = dpy;
}
setmetatable(obj, EglDisplay_mt);
obj:Initialize();
--print("BINDING TO API: ", api);
if api then
-- api = api or EGL.EGL_OPENVG_API
assert(obj:BindToAPI(api), "Could not bind to API");
end
assert(obj:ChooseConfig(), "Could not choose config");
assert(obj:CreateContext(), "Could not create context");
return obj
end
EglDisplay.free = function(self)
local res = EGL.Lib.eglTerminate(self.Handle);
end
--[[
Various Attributes
--]]
EglDisplay.ClientAPIs = function(self)
return ffi.string(EGL.Lib.eglQueryString(self.Handle, EGL.EGL_CLIENT_APIS));
end
EglDisplay.CurrentAPI = function(self)
return EGL.Lib.eglQueryAPI();
end
EglDisplay.Vendor = function(self)
return ffi.string(EGL.Lib.eglQueryString(self.Handle, EGL.EGL_VENDOR));
end
EglDisplay.Extensions = function(self)
return ffi.string(EGL.Lib.eglQueryString(self.Handle, EGL.EGL_EXTENSIONS));
end
--[[
Meat and potatoes
--]]
EglDisplay.Initialize = function(self)
local pmajor = ffi.new("EGLint[1]");
local pminor = ffi.new("EGLint[1]");
local res = EGL.Lib.eglInitialize(self.Handle, nil, nil);
assert(res ~= EGL.EGL_FALSE);
return self, pmajor[0], pminor[0];
end
EglDisplay.BindToAPI = function(self, api)
api = api or EGL.EGL_OPENVG_API
local res = EGL.Lib.eglBindAPI(api);
if res == EGL_FALSE then
return nil
end
return self
end
EglDisplay.ChooseConfig = function(self, attribute_list)
attribute_list = attribute_list or ffi.new("EGLint[11]",
EGL.EGL_RED_SIZE, 8,
EGL.EGL_GREEN_SIZE, 8,
EGL.EGL_BLUE_SIZE, 8,
EGL.EGL_ALPHA_SIZE, 8,
EGL.EGL_SURFACE_TYPE, EGL.EGL_WINDOW_BIT,
EGL.EGL_NONE);
local pconfig = ffi.new("EGLConfig[10]");
local pnum_config = ffi.new("EGLint[1]");
local res = EGL.Lib.eglChooseConfig(self.Handle, attribute_list, pconfig, 10, pnum_config);
assert(res == EGL.EGL_TRUE);
local num_config = pnum_config[0]
print("EglDisplay.ChooseConfig(): num: ", num_config);
self.Config = pconfig[0];
return self.Config;
end
EglDisplay.CreateContext = function(self, config)
config = config or self.Config
local ctx = EGL.Lib.eglCreateContext(self.Handle, config, EGL.EGL_NO_CONTEXT, nil);
self.Context = ctx;
return ctx;
end
EglDisplay.CreateWindowSurface = function(self, nativewindow, config)
config = config or self.Config
local srf = EGL.Lib.eglCreateWindowSurface(self.Handle, config, nativewindow, nil);
self.Surface = srf
return srf;
end
EglDisplay.CreateWindow = function(self, width, height, config)
config = config or self.config
return EGLWindow.new(self, width , height)
end
EglDisplay.MakeCurrent = function(self, surface, context)
surface = surface or self.Surface;
context = context or self.Context;
-- connect the context to the surface
local res = EGL.Lib.eglMakeCurrent(self.Handle, surface, surface, context);
return res
end
EglDisplay.SwapBuffers = function(self, surface)
surface = surface or self.Surface;
local res = EGL.Lib.eglSwapBuffers(self.Handle, surface);
return res;
end
--[[
Representation of a window
--]]
--[[
Create a native window. This is essentially
the lowest level window 'handle'. EGL then
uses this handle to create a managed 'window'.
--]]
local function createNativeView(dmxdisplay, width, height, x, y, level)
x = x or 0
y = y or 0
level = level or 0
local dst_rect = VC_RECT_T(x,y,width, height);
local src_rect = VC_RECT_T(0,0, lshift(width, 16), lshift(height,16));
--local alpha = VC_DISPMANX_ALPHA_T(ffi.C.DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS,255,0);
--local view = dmxdisplay:CreateElement(dst_rect, nil, src_rect, 0, DISPMANX_PROTECTION_NONE, alpha);
--local view = dmxdisplay:CreateElement(dst_rect, nil, src_rect);
local view = dmxdisplay:CreateView(width, height, x,y, level);
assert(view, "FAILURE: Did not create element");
-- create an EGL window surface
local nativewindow = ffi.new("EGL_DISPMANX_WINDOW_T");
nativewindow.element = view.Surface.Handle;
nativewindow.width = width;
nativewindow.height = height;
return nativewindow, view;
end
local EGLWindow = {}
local EGLWindow_mt = {
__index = EGLWindow,
}
EGLWindow.new = function(width, height, x, y, config, api)
x = x or 0
y = y or 0
config = config or {background = {153, 153, 153}};
local obj = {
X = x;
Y = y;
Width = width;
Height = height;
}
-- Create the display object
obj.Display = EglDisplay.new(api);
-- create nativewindow
local dmxdisplay = DMX.DMXDisplay();
obj.NativeWindow, obj.NativeView = createNativeView(dmxdisplay, width, height, x, y);
-- create window surface
obj.Surface = obj.Display:CreateWindowSurface(obj.NativeWindow);
obj.Display:MakeCurrent();
setmetatable(obj, EGLWindow_mt);
if config.background then
obj:SetBackgroundColor(config.background[1], config.background[2], config.background[3], config.background[3]);
end
obj:Clear();
obj:SwapBuffers();
return obj
end
EGLWindow.Clear = function(self)
glClear(GL_COLOR_BUFFER_BIT);
end
EGLWindow.SetBackgroundColor = function(self, red, green, blue, alpha)
alpha = alpha or 1.0;
glClearColor(red/256, green/256, blue/256, alpha);
end
EGLWindow.SwapBuffers = function(self)
self.Display:SwapBuffers();
end
EGLWindow.MoveTo = function(self, x, y)
self.NativeView:MoveTo(x,y);
self.X = x;
self.Y = y;
--self:SwapBuffers();
end
EGL.Display = EglDisplay
EGL.Window = EGLWindow;
return EGL
--[[
return {
Lib = EGL.Lib,
Display = EglDisplay,
Window = EGLWindow,
}
--]]