Skip to content

Commit 1052435

Browse files
authored
ui: speed up mici/AugmentedRoadView by optimizing _calc_frame_matrix caching (#36669)
speed up AugmentedRoadView by optimizing _calc_frame_matrix caching
1 parent ce59642 commit 1052435

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

selfdrive/ui/mici/onroad/augmented_road_view.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ def __init__(self, bookmark_callback=None, stream_type: VisionStreamType = Visio
138138
self.view_from_calib = view_frame_from_device_frame.copy()
139139
self.view_from_wide_calib = view_frame_from_device_frame.copy()
140140

141-
self._last_calib_time: float = 0
142-
self._last_rect_dims = (0.0, 0.0)
143-
self._last_stream_type = stream_type
141+
self._matrix_cache_key = (0, 0, 0, 0, stream_type)
144142
self._cached_matrix: np.ndarray | None = None
145143
self._content_rect = rl.Rectangle()
146144
self._last_click_time = 0.0
@@ -284,10 +282,19 @@ def _update_calibration(self):
284282
self.view_from_wide_calib = view_frame_from_device_frame @ wide_from_device @ device_from_calib
285283

286284
def _calc_frame_matrix(self, rect: rl.Rectangle) -> np.ndarray:
285+
v_ego_quantized = round(ui_state.sm['carState'].vEgo, 1)
286+
cache_key = (
287+
ui_state.sm.recv_frame['liveCalibration'],
288+
int(self._content_rect.width),
289+
int(self._content_rect.height),
290+
self.stream_type,
291+
v_ego_quantized
292+
)
293+
294+
if cache_key == self._matrix_cache_key and self._cached_matrix is not None:
295+
return self._cached_matrix
296+
287297
# Get camera configuration
288-
# TODO: cache with vEgo?
289-
calib_time = ui_state.sm.recv_frame['liveCalibration']
290-
current_dims = (self._content_rect.width, self._content_rect.height)
291298
device_camera = self.device_camera or DEFAULT_DEVICE_CAMERA
292299
is_wide_camera = self.stream_type == WIDE_CAM
293300
intrinsic = device_camera.ecam.intrinsics if is_wide_camera else device_camera.fcam.intrinsics
@@ -323,9 +330,7 @@ def _calc_frame_matrix(self, rect: rl.Rectangle) -> np.ndarray:
323330
x_offset, y_offset = 0, 0
324331

325332
# Cache the computed transformation matrix to avoid recalculations
326-
self._last_calib_time = calib_time
327-
self._last_rect_dims = current_dims
328-
self._last_stream_type = self.stream_type
333+
self._matrix_cache_key = cache_key
329334
self._cached_matrix = np.array([
330335
[zoom * 2 * cx / w, 0, -x_offset / w * 2],
331336
[0, zoom * 2 * cy / h, -y_offset / h * 2],

0 commit comments

Comments
 (0)