Skip to content

Commit e1b4880

Browse files
committed
Workaround for DRM displays wheb XB24 not available
Signed-off-by: David Plowman <[email protected]>
1 parent 7d9f5cd commit e1b4880

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

picamera2/previews/drm_preview.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,23 @@ class DrmPreview(NullPreview):
6363
def __init__(self, x=0, y=0, width=640, height=480, transform=None):
6464
self.init_drm(x, y, width, height, transform)
6565
self.stop_count = 0
66-
self.fb = pykms.DumbFramebuffer(self.card, width, height, "XB24")
67-
self.mem = mmap.mmap(self.fb.fd(0), width * height * 3, mmap.MAP_SHARED, mmap.PROT_WRITE)
68-
self.fd = self.fb.fd(0)
66+
67+
# Allocate a buffer for MJPEG decode. If "XB24" appears unsupported, try "XR24".
68+
self.fb = None
69+
try:
70+
self.fb = pykms.DumbFramebuffer(self.card, width, height, "XB24")
71+
except Exception:
72+
pass
73+
if not self.fb:
74+
try:
75+
self.fb = pykms.DumbFramebuffer(self.card, width, height, "XR24")
76+
except Exception:
77+
pass
78+
# Even if we don't have a buffer, only fail later if it turns out we need it.
79+
if self.fb:
80+
self.mem = mmap.mmap(self.fb.fd(0), width * height * 3, mmap.MAP_SHARED, mmap.PROT_WRITE)
81+
self.fd = self.fb.fd(0)
82+
6983
super().__init__(width=width, height=height)
7084

7185
def render_request(self, completed_request):
@@ -182,6 +196,9 @@ def render_drm(self, picam2, completed_request):
182196

183197
if pixel_format == "MJPEG":
184198
img = completed_request.make_array(self.display_stream_name).tobytes()
199+
if not self.fb:
200+
# This is point at which this buffer really needs to exist!
201+
raise RuntimeError("Failed to allocate buffer for MJPEG frame")
185202
self.mem.seek(0)
186203
self.mem.write(img)
187204
fd = self.fd

0 commit comments

Comments
 (0)