Do not redraw background on every frame

This commit is contained in:
XANTRONIX Development 2024-01-05 16:27:57 -05:00
parent 8fb4f1632b
commit f1f7e423a2

View file

@ -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)