Tk Source Code

View Ticket
Login
Ticket UUID: f9eddb541a3710dc3dfd3867666b91a3f4d7f8a6
Title: Improve TkSetRegion(); implement XSetClipRectangles() on Windows and improve it
Type: Patch Version: 8.6.13
Submitter: chrstphrchvz Created on: 2023-07-16 12:02:35
Subsystem: 82. X11 Emulation Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2023-07-17 20:40:26
Resolution: Fixed Closed By: nab
    Closed on: 2023-07-17 20:40:26
Description:

Here is a series of patches to consider:

  1. Introduce a convenience function TkpCopyRegion(dst, src). Win32 and HIShape have dedicated copy APIs which are trivial to wrap, and XUnionRegion() in Xlib is optimized to detect copying (https://gitlab.freedesktop.org/xorg/lib/libx11/-/blob/libX11-1.8.6/src/Region.c?ref_type=tags#L1144) (although I am currently not proposing a use for TkpCopyRegion() on X11). This function is not strictly necessary; alternative approaches are possible.
  2. For safety, have AllocClipMask() initialize the TkpClipMask.
  3. Stylistic adjustment to FreeClipMask() to make the next change clearer.
  4. TkSetRegion() on Windows and Aqua has been storing a reference to the provided region in the GC without copying or taking ownership of the region, hence the warning in the comment: unlike Xlib’s XSetRegion(), it is not safe to destroy the region while the GC still holds a reference to it. This has not been an issue for core Tk, which has relied on the pattern of doing TkCreateRegion(), TkSetRegion(), then XSetClipMask(…, …, None) before TkDestroyRegion(). I propose eliminating this safety issue by copying the provided region to a dedicated region for the TkpClipMask.
  5. Implement XSetClipRectangles() on Windows by relocating the existing Aqua implementation (which does not contain any Mac-specific code). Because it uses TkSetRegion(), the above issue needed to be resolved first, otherwise any code using it on Windows would trigger use-after-free behavior. And were it not for an extra retain (which I suggested removing in [2cb602807b]), use-after-free would also occur on Aqua.
  6. Better yet, have XSetClipRectangles() set clip_mask->value.region directly, rather than creating a temporary region and relying on TkSetRegion() to copy it. Something to do later would be to have code in core Tk try to use XSetClipRectangles() instead of TkSetRegion(), since the latter now requires copying a region.

User Comments: nab added on 2023-07-17 20:40:26:
tested on windows (Tk compiled thanks to clang.exe) and it works very fine.

many thanks.
++

fvogel added on 2023-07-17 20:01:59:

Merged in core-8-6-branch and upwards. Many thanks to Christopher, once more!

Also: I have committed your proposal for a "smoke test" and have checked it builds on all the three platforms, plus it works: the labelframe text gets correctly clipped. This is indeed a smoke test, and it also provides an example of use of XSetClipRectangles() live in the Tk code, so I thought this patch would be useful in these two respects.


nab added on 2023-07-17 17:13:56:
cool.
thanks François.

++

fvogel added on 2023-07-17 17:11:53:
Like: in a few hours.

fvogel added on 2023-07-17 17:10:07:
Soon.

nab added on 2023-07-17 16:27:43:
Hi,
do you plan to merge this branch trunk soon or in a week?

best regards,
Nicolas

jan.nijtmans added on 2023-07-17 07:08:43:

I'm OK putting this in 8.6


chrstphrchvz added on 2023-07-17 06:31:01:

Well, this is not for a new feature. This is for fixing a cross-platform inconsistency. Extensions such as TkTable currently have any usage of XSetClipRectangles() disabled on Windows. They would be unaffected by this function suddenly becoming available, and would have to be patched and rebuilt to use this function. Is it not okay to make this function available for Windows in 8.6.14, when it was made available for Aqua in 8.6.11?


fvogel added on 2023-07-16 17:31:24:
Aside from the testing, some questions I have: can this be put in 8.6? or is 8.7 a better place? New features normally go in 8.7, that's one answer. Another answer could stem from the question: does the patch have an impact on binary compatibility as seen from extensions?

chrstphrchvz added on 2023-07-16 17:18:43:

I tested XSetClipRectangles() on Windows and Aqua using the revised text widget as of [11905189f8]. If there should at least be “smoke test” coverage of this function, then it would probably be easiest to use it for non-Ttk labelframe widgets with overflowing text; see latest attached patch.


fvogel added on 2023-07-16 13:43:45:

Always a pleasure to deal with your well formatted reports, step by step reasoning and perfect code. Very clear all way along. Many many thanks!

All patches now committed in a dedicated branch bug-f9eddb541a.

The code builds now on all the three platforms. What else remains to be done in your opinion? XSetCLipRectangles() is not used by Tk at the moment, therefore no testing is possible right?


chrstphrchvz added on 2023-07-16 12:14:54:

Something to do later would be to have code in core Tk try to use XSetClipRectangles() instead of TkSetRegion(), since the latter now requires copying a region.

Never mind, for rectangular regions there is probably not much overhead compared to copying just a rectangle.


Attachments: