forked from marcan/blitzloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpi.py
More file actions
173 lines (137 loc) · 5.8 KB
/
rpi.py
File metadata and controls
173 lines (137 loc) · 5.8 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2017 Hector Martin "marcan" <hector@marcansoft.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os, time
from blitzloop.backend.common import BaseDisplay
import ctypes, ctypes.util
from ctypes import cdll, c_int, Structure, byref
# The shit we have to do to get stuff to work on the Raspberry Pi...
os.environ["PYOPENGL_PLATFORM"] = "egl" # Get PyOpenGL to use EGL/GLES
# monkeypatch ctypes to pick the right libraries... sigh.
def find_library(name):
if name in ("GLESv2", "EGL"):
return "/opt/vc/lib/lib%s.so" % name
elif name == "GL":
raise Exception()
else:
print(name)
return old_find_library(name)
old_find_library = ctypes.util.find_library
ctypes.util.find_library = find_library
# Raspberry Pi libs are missing dependencies, we need GLES before EGL...
cdll.LoadLibrary("/opt/vc/lib/libGLESv2.so")
import blitzloop.backend.gles_fixes
import OpenGL.GLES2 as gl
import OpenGL.EGL as egl
from OpenGL import arrays
class DISPMANX_MODEINFO_T(Structure):
_fields_ = [("width", c_int),
("height", c_int),
("transform", c_int),
("input_format", c_int),
("display_num", c_int),
]
class EGL_DISPMANX_WINDOW_T(Structure):
_fields_ = [("element", c_int),
("width", c_int),
("height", c_int)]
class VC_RECT_T(Structure):
_fields_ = [("x", c_int),
("y", c_int),
("width", c_int),
("height", c_int)]
class VC_DISPMANX_ALPHA_T(Structure):
_fields_ = [("flags", c_int),
("opacity", c_int),
("mask", c_int)]
libbcm_host = cdll.LoadLibrary("libbcm_host.so")
class Display(BaseDisplay):
def __init__(self, width=640, height=480, fullscreen=False, aspect=None):
self.gl = gl
libbcm_host.bcm_host_init()
display = libbcm_host.vc_dispmanx_display_open(0)
mode = DISPMANX_MODEINFO_T()
libbcm_host.vc_dispmanx_display_get_info(display, byref(mode))
print("Display mode: %dx%d" % (mode.width, mode.height))
self.disp = egl.eglGetDisplay(egl.EGL_DEFAULT_DISPLAY)
attribList = arrays.GLintArray.asArray([
egl.EGL_RENDERABLE_TYPE, egl.EGL_OPENGL_ES2_BIT,
egl.EGL_SURFACE_TYPE, egl.EGL_WINDOW_BIT,
#egl.EGL_COLOR_BUFFER_TYPE, egl.EGL_RGB_BUFFER,
egl.EGL_RED_SIZE, 8,
egl.EGL_GREEN_SIZE, 8,
egl.EGL_BLUE_SIZE, 8,
egl.EGL_ALPHA_SIZE, 8,
egl.EGL_NONE
])
ctxAttrib = arrays.GLintArray.asArray([
egl.EGL_CONTEXT_CLIENT_VERSION, 2,
egl.EGL_NONE
])
egl.eglInitialize(self.disp, None, None)
config = egl.EGLConfig()
num_configs = ctypes.c_long()
egl.eglChooseConfig(self.disp, attribList, byref(config), 1, byref(num_configs))
ret = ctypes.c_int()
egl.eglBindAPI(egl.EGL_OPENGL_ES_API)
update = libbcm_host.vc_dispmanx_update_start(0)
rectDst = VC_RECT_T()
rectDst.x = rectDst.y = 0
rectDst.width = mode.width
rectDst.height = mode.height
rectSrc = VC_RECT_T()
rectSrc.x = rectDst.y = 0
rectSrc.width = mode.width << 16
rectSrc.height = mode.height << 16
alpha = VC_DISPMANX_ALPHA_T()
alpha.flags = 1 << 16 # premultiplied alpha
alpha.opacity = 255
alpha.mask = 0
self.nativeWindow = EGL_DISPMANX_WINDOW_T()
self.nativeWindow.width = mode.width
self.nativeWindow.height = mode.height
layer = 0
self.nativeWindow.element = libbcm_host.vc_dispmanx_element_add(
update, display, layer, byref(rectDst), 0, byref(rectSrc),
0, byref(alpha), 0, 0)
libbcm_host.vc_dispmanx_update_submit_sync(update)
libbcm_host.vc_dispmanx_display_close(display)
self.surface = egl.eglCreateWindowSurface(self.disp, config, byref(self.nativeWindow), None)
self.context = egl.eglCreateContext(self.disp, config, egl.EGL_NO_CONTEXT, ctxAttrib)
assert egl.eglMakeCurrent(self.disp, self.surface, self.surface, self.context)
egl.eglSwapInterval(self.disp, 1)
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
for i in range(5):
gl.glClearColor(0, 0, 0, 1.0)
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
egl.eglSwapBuffers(self.disp, self.surface)
self.win_width = self.width = mode.width
self.win_height = self.height = mode.height
gl.glViewport(0, 0, self.win_width, self.win_height)
BaseDisplay.__init__(self, mode.width, mode.height, True, aspect)
# Transparent layer
self.clear_color = self.TRANSPARENT
self._initialize()
def swap_buffers(self):
egl.eglSwapBuffers(self.disp, self.surface)
if __name__ == "__main__":
d = Display()
while True:
for color in (1.,0.,0.,1.), (0.,1.,0.,1.), (0.,0.,1.,1.):
gl.glClearColor(*color)
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
egl.eglSwapBuffers(d.disp, d.surface)
time.sleep(0.5)