py: cam: cam_qt: mmap the fbs only once

Instead of doing an mmap and munmap every time a Request is complete,
mmap all the buffers once at the start of the program.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Tomi Valkeinen 2022-05-30 17:27:16 +03:00 committed by Laurent Pinchart
parent 958d9187aa
commit dfcf638a0a

View file

@ -52,6 +52,16 @@ class QtRenderer:
self.windows = windows self.windows = windows
buf_mmap_map = {}
for ctx in self.contexts:
for stream in ctx.streams:
for buf in ctx.allocator.buffers(stream):
mfb = libcamera.utils.MappedFrameBuffer(buf).mmap()
buf_mmap_map[buf] = mfb
self.buf_mmap_map = buf_mmap_map
def run(self): def run(self):
camnotif = QtCore.QSocketNotifier(self.cm.event_fd, QtCore.QSocketNotifier.Read) camnotif = QtCore.QSocketNotifier(self.cm.event_fd, QtCore.QSocketNotifier.Read)
camnotif.activated.connect(lambda _: self.readcam()) camnotif.activated.connect(lambda _: self.readcam())
@ -81,7 +91,9 @@ class QtRenderer:
for stream, fb in buffers.items(): for stream, fb in buffers.items():
wnd = next(wnd for wnd in self.windows if wnd.stream == stream) wnd = next(wnd for wnd in self.windows if wnd.stream == stream)
wnd.handle_request(stream, fb) mfb = self.buf_mmap_map[fb]
wnd.handle_request(stream, mfb)
self.state.request_processed(ctx, req) self.state.request_processed(ctx, req)
@ -145,8 +157,7 @@ class MainWindow(QtWidgets.QWidget):
controlsLayout.addStretch() controlsLayout.addStretch()
def buf_to_qpixmap(self, stream, fb): def buf_to_qpixmap(self, stream, mfb):
with libcamera.utils.MappedFrameBuffer(fb) as mfb:
cfg = stream.configuration cfg = stream.configuration
if cfg.pixel_format == libcam.formats.MJPEG: if cfg.pixel_format == libcam.formats.MJPEG:
@ -161,10 +172,10 @@ class MainWindow(QtWidgets.QWidget):
return pix return pix
def handle_request(self, stream, fb): def handle_request(self, stream, mfb):
ctx = self.ctx ctx = self.ctx
pix = self.buf_to_qpixmap(stream, fb) pix = self.buf_to_qpixmap(stream, mfb)
self.label.setPixmap(pix) self.label.setPixmap(pix)
self.frameLabel.setText('Queued: {}\nDone: {}\nFps: {:.2f}' self.frameLabel.setText('Queued: {}\nDone: {}\nFps: {:.2f}'