def _update_run(self):
# This method runs in the background _update_thread
while True:
# Only update the screen if do_draw's finished the last update;
# this effectively serves to "drop frames" if the system's too
# busy
if self._draw_pending.wait(self.screen_update_delay):
# The wait period above enforces the maximum update rate; if
# a draw is still pending, wait on the stop event instead
if self._stop.wait(self.screen_update_delay):
break
else:
# Only update if the screen's modification timestamp indicates
# that the data has changed since last time
ts = self._screen_client.timestamp
if ts > self._draw_timestamp:
with self._size_lock:
img = self._board_scaled.copy()
pixels = GdkPixbuf.Pixbuf.new_from_bytes(
GLib.Bytes.new(self._screen_client.rgb_array.tostring()),
colorspace=GdkPixbuf.Colorspace.RGB, has_alpha=False,
bits_per_sample=8, width=8, height=8, rowstride=8 * 3)
pixel_rect = Gdk.Rectangle()
pixel_rect.x = int(126 * self._ratio)
pixel_rect.y = int(155 * self._ratio)
pixel_rect.width = int(512 * self._ratio)
pixel_rect.height = pixel_rect.width
pixels.composite(
img,
pixel_rect.x, pixel_rect.y,
pixel_rect.width, pixel_rect.height,
pixel_rect.x, pixel_rect.y,
# Why 8.1? With 8.0 (which is what it should be),
# registration errors crop up at the far right (try
# it and see); no idea why 8.1 is required to
# correct them, but I'm too knackered to argue with
# Gdk any more...
pixel_rect.width / 8.1, pixel_rect.height / 8.1,
GdkPixbuf.InterpType.NEAREST, 255)
self._grid_scaled.composite(
img,
pixel_rect.x, pixel_rect.y,
pixel_rect.width, pixel_rect.height,
pixel_rect.x, pixel_rect.y,
1, 1,
GdkPixbuf.InterpType.NEAREST, 255)
self._draw_image = img
self._draw_timestamp = ts
self._draw_pending.set()
# Schedule a redraw when the app is next idle; like Gtk
# methods, Gdk methods must only be called from the main
# thread (otherwise the app locks up)
try:
GLib.idle_add(self.props.window.invalidate_rect, None, False)
except AttributeError:
# Our Gdk window has been destroyed; don't whinge, just
# exit the thread as we're obviously shutting down
break
评论列表
文章目录