Skip to content

Commit

Permalink
#4201 minor fixes and cleanups
Browse files Browse the repository at this point in the history
* always use an 'XPRA_' prefix for env vars,
* more readable output,
* get the window size as early as possible,
* use the correct cairo format for rgb updates ('rgb24' only means no alpha, 'rgb_format' tells us the number of pixel components)
  • Loading branch information
totaam committed Jul 31, 2024
1 parent b1b2b26 commit 8a42844
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
39 changes: 23 additions & 16 deletions tests/xpra/codecs/replay_screen_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
winfo = "window.info"


def load_window_dir(dirpath: str) -> list[dict]:
def read_json(filename: str):
with open(filename, "rb") as f:
serialized = f.read()
return json.loads(serialized)


def load_window_dir(dirpath: str) -> tuple[dict, list[dict]]:
data = {}
windowinfo_file = os.path.join(dirpath, winfo)
if not os.path.exists(windowinfo_file):
raise ValueError(f"directory {dirpath!r} does not look like a replay directory, {winfo!r} not found")
windowinfo = read_json(windowinfo_file)

for dirname in os.listdir(dirpath):
if dirname == winfo:
Expand All @@ -37,7 +44,7 @@ def load_window_dir(dirpath: str) -> list[dict]:
updates = []
for ts in sorted(data.keys()):
updates.append(data[ts])
return updates
return windowinfo, updates


def load_update_dir(dirpath: str) -> dict:
Expand All @@ -47,22 +54,22 @@ def load_update_dir(dirpath: str) -> dict:
continue
flush = int(entry.split(".", 1)[0])
filename = os.path.join(dirpath, entry)
with open(filename, "rb") as f:
serialized = f.read()
update_data = json.loads(serialized)
update_data = read_json(filename)
update_data["directory"] = dirpath
data[flush] = update_data
return dict(reversed(sorted(data.items())))


class ReplayApp:

def __init__(self, replay_data: list[dict]):
def __init__(self, window_info: dict, replay_data: list[dict]):
self.replay_data = replay_data
self.frame = 0
self.update = 0
self.frame_data = {}
self.backing = None
self.window_info = window_info
self.window_size = window_info.get("dimensions", (200, 200))
self.drawing_area = Gtk.DrawingArea()
self.drawing_area.connect("draw", self.draw_cb)
self.replay_window = self.init_replay_window()
Expand All @@ -71,7 +78,7 @@ def __init__(self, replay_data: list[dict]):

def init_replay_window(self) -> Gtk.Window:
replay_window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
replay_window.set_size_request(200, 200)
replay_window.set_size_request(*self.window_size)
replay_window.add(self.drawing_area)
replay_window.connect("delete_event", Gtk.main_quit)
replay_window.show_all()
Expand Down Expand Up @@ -156,12 +163,10 @@ def draw_update(self):
options = update.get("options", {})
if not options.get("paint", True):
return
window_size = options.get("window-size")
if window_size:
ww, wh = window_size
self.replay_window.resize(*window_size)
if self.backing is None:
self.backing = cairo.ImageSurface(cairo.Format.ARGB32, ww, wh)

ww, wh = self.window_size
if self.backing is None:
self.backing = cairo.ImageSurface(cairo.Format.ARGB32, ww, wh)

encoding = update.get("encoding")
filename = update.get("file", f"{self.update}.{encoding}")
Expand Down Expand Up @@ -193,7 +198,8 @@ def draw_update(self):
if options.get("lz4"):
from xpra.net.lz4.lz4 import decompress
rgb_data = decompress(file_data)
if encoding == "rgb24":
rgb_format = options.get("rgb_format", "BGRX")
if len(rgb_format) == 3:
from xpra.codecs.argb.argb import rgb_to_bgrx
rgb_data = rgb_to_bgrx(rgb_data)
else:
Expand All @@ -202,7 +208,8 @@ def draw_update(self):
img = img.convert("RGBA")
rgb_data = img.tobytes("raw", img.mode)
ba = bytearray(rgb_data)
surface = cairo.ImageSurface.create_for_data(ba, cairo.FORMAT_ARGB32, w, h, stride or w*4)
cfmt = cairo.FORMAT_ARGB32 if encoding != "rgb24" else cairo.FORMAT_RGB24
surface = cairo.ImageSurface.create_for_data(ba, cfmt, w, h, stride or w*4)
# paint in on the backing:
ctx = cairo.Context(self.backing)
ctx.set_source_surface(surface, x, y)
Expand Down Expand Up @@ -239,7 +246,7 @@ def main(argv: list[str]) -> int:
print("usage: %s directory" % (argv[0]))
return 1
dirpath = argv[1]
app = ReplayApp(load_window_dir(dirpath))
app = ReplayApp(*load_window_dir(dirpath))
app.run()
return 0

Expand Down
6 changes: 3 additions & 3 deletions xpra/server/window/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
FORCE_PILLOW = envbool("XPRA_FORCE_PILLOW", False)
HARDCODED_ENCODING: str = os.environ.get("XPRA_HARDCODED_ENCODING", "")

SCREEN_UPDATES_DIRECTORY = os.environ.get("SCREEN_UPDATES_DIRECTORY", "")
SCREEN_UPDATES_DIRECTORY = os.environ.get("XPRA_SCREEN_UPDATES_DIRECTORY", "")

MAX_SEQUENCE = 2**64

Expand Down Expand Up @@ -2484,7 +2484,7 @@ def save_update(self, packet: tuple, damage_time: float) -> None:
if not os.path.exists(wid_dir):
os.mkdir(wid_dir)
windowinfo_file = os.path.join(wid_dir, "window.info")
with open(windowinfo_file, "wb") as f:
with open(windowinfo_file, "w") as f:
f.write(json.dumps(self.get_info(), indent="\t"))
dirname = os.path.join(wid_dir, str(round(damage_time*1000)))
if self.screen_updates_directory != dirname:
Expand Down Expand Up @@ -2516,7 +2516,7 @@ def save_update(self, packet: tuple, damage_time: float) -> None:
update_file = os.path.join(self.screen_updates_directory, update_filename)
with open(update_file, "wb") as f:
f.write(data)
compresslog.warn(f"saved {coding}: {len(data)} bytes to {update_file}")
compresslog.warn(f"saved {coding:6}: {len(data):8} bytes to {update_file!r}")
index_info["file"] = update_filename

index_file = os.path.join(self.screen_updates_directory, f"{index}.info")
Expand Down

0 comments on commit 8a42844

Please sign in to comment.