From f1f7e423a241c408f5adf1b2f6cf9a9e5577469b Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Fri, 5 Jan 2024 16:27:57 -0500 Subject: [PATCH] Do not redraw background on every frame --- py/hexagram/cluster.pyx | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/py/hexagram/cluster.pyx b/py/hexagram/cluster.pyx index 6a64cad..6a5948e 100644 --- a/py/hexagram/cluster.pyx +++ b/py/hexagram/cluster.pyx @@ -114,8 +114,8 @@ class Clock(TextGauge): ) class Cluster(): - __slots__ = 'gauges', 'speedo', 'fuel', 'tacho', 'thermo', 'odo', \ - 'ambient', 'clock', 'shift_indicator', 'status', + __slots__ = '_redraw', 'bg', 'gauges', 'speedo', 'fuel', 'tacho', 'thermo', \ + 'odo', 'ambient', 'clock', 'shift_indicator', 'status', WIDTH = 1280 HEIGHT = 480 @@ -127,6 +127,11 @@ class Cluster(): SECTION_HEIGHT_BOTTOM = 52 def __init__(self, rpm_min: float, rpm_redline: float, rpm_max: float): + self._redraw = True + self.bg = cairo.ImageSurface(cairo.FORMAT_ARGB32, + self.WIDTH, + self.HEIGHT) + self.gauges: List[Gauge] = list() self.speedo = Speedo(self.HEIGHT / 2, @@ -172,14 +177,14 @@ class Cluster(): self.gauges.append(self.shift_indicator) self.gauges.append(self.status) - def draw_bg(self, cr: cairo.Context): + def _update_bg(self): + cr = cairo.Context(self.bg) cr.set_source_rgb(0.1, 0.1, 0.1) cr.rectangle(0, 0, self.WIDTH, self.HEIGHT) cr.fill() - cr.rectangle(0, 0, self.WIDTH, self.HEIGHT) - pattern = HexagonPattern() + cr.rectangle(0, 0, self.WIDTH, self.HEIGHT) pattern.fill(cr) # Top dark area @@ -207,6 +212,18 @@ class Cluster(): for gauge in self.gauges: gauge.draw_bg(cr) + def draw_bg(self, cr: cairo.Context): + if self._redraw: + self._update_bg() + self._redraw = False + + op = cr.get_operator() + cr.set_source_surface(self.bg, 0, 0) + cr.set_operator(cairo.OPERATOR_SOURCE) + cr.rectangle(0, 0, self.WIDTH, self.HEIGHT) + cr.fill() + cr.set_operator(op) + def draw_fg(self, cr: cairo.Context): for gauge in self.gauges: gauge.draw_fg(cr)