| Ticket UUID: | fcd6717d8b9dee3eb75c2046f1e9fa37207cf437 | |||
| Title: | Aqua: consider removing XImage pixelpower attribute | |||
| Type: | RFE | Version: | core-8-6-branch | |
| Submitter: | chrstphrchvz | Created on: | 2020-08-07 09:14:20 | |
| Subsystem: | 82. X11 Emulation | Assigned To: | marc_culler | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Closed | Last Modified: | 2020-08-25 11:51:14 | |
| Resolution: | Accepted | Closed By: | marc_culler | |
| Closed on: | 2020-08-25 11:51:14 | |||
| Description: |
I've been somewhat confused by the comments in Tk Aqua's XGetImage(), etc. that claim these functions are "unused". I think this may only be a reflection of core Tk, and not reflect that these are still used by existing programs and extensions. My belief is that these functions should remain compatible with existing usage, and avoid introducing API/ABI-breaking changes or useless features; the addition of the pixelpower attribute is such a change that I believe should be reverted. As part of my CALayer-drawing experiment, I have tried using TkImg to capture a window, which entails repairing XGetImage() and TkMacOSXBitmapRepFromDrawableRect(). I have success doing this for a non-Retina display. But TkImg and other existing XGetImage() users expect to get an XImage of the requested width and height, or NULL otherwise. They know nothing about pixelpower, and I don't believe that they suddenly should. For compatibility, if a capture needs to made of a window on a Retina display, then it should return a scaled-down capture with the requested width and height. If programs need to be able to retrieve an unscaled capture from a Retina display, then I believe a new API for doing so should be added instead. One approach I would consider is extending ::tk::mac::iconBitmap. Another reason pixelpower may be mistaken: it only accommodates scaling factors of 2**n (n∈ℤ⁰⁺). But Apple has already released devices (e.g. iPhone X with "Super Retina display") with a scaling factor of 3, so it is possible there will one day be similar non-power-of-two scaling factor in use on the Mac. | |||
| User Comments: |
marc_culler (claiming to be Marc Culler) added on 2020-08-25 11:51:14:
The XImage.pixelpower field is no longer used on core-8-6-branch, although it is still in the structure, and has been completely removed in trunk. The problems with XGetImage and XCopyArea remain unsolved. But there are other tickets about that. So I think I can close this one. marc_culler (claiming to be Marc Culler) added on 2020-08-23 02:40:15: I have a macbook Air with a Retina display and a monitor that I use for my desktop computer, so I'd be happy to test if you supplied a test script. I'd also be interested in having the script you are using with TkImg. I probably misread the code, but it looked like the Retinaciousness was being stored in a global variable, so it would not be sensitive to a window being moved from one display to another. More importantly, though, what do you see as the issue with simply asking an NSWindow for its backingStoreScaleFactor whenever that is relevant? chrstphrchvz added on 2020-08-22 23:52:00: Marc, once again I appreciate your patience with this issue, and apologize for my impatience by contrast. Yes, I meant "opaque backing store" in the sense that programs aren't supposed be aware it exists, let alone interact with it directly. I would suggest giving up on this issue, rather than attempting to get cacheDisplayInRect: or any of Apple's other non-replacements working (which would be even more quixotic in my opinion). In my attempt to repair XCopyArea() and XGetImage(), I made sure that the scaling factor adjusted properly when I changed the display from a Retina to a non-Retina resolution and back while Tk was still running. So I did not suspect an issue with multiple displays with different scaling factors, as I would expect that the scaling factor (which Tk reads on-demand and didn't cache) would be determined only by which display a toplevel's NSWindow is currently on, and automatically adjust when a toplevel is dragged between displays. But I am unable to test that configuration with the Macs I have, and am unwilling to acquire any hardware to test. I would be willing to provide a test script and instructions to someone else who wants to test it. marc_culler (claiming to be Marc Culler) added on 2020-08-22 20:07:48: I have pushed a new branch, nopixelpower, which includes many but not all of the changes in the patch plus a significant amount of reorganization. I tried to approach the changes as a minimalist. Things are removed. Nothing is added. But the pixelpower field is not removed from the XImage structure, in order to avoid compounding one incompatibility with another; I do intend to remove it before merging this with the trunk branch, however. The function TkMacOSXBitmapRepFromDrawableRect is replaced by CreateCGIMageFromDrawableRect, which is a static function in TkImage.c, the new home of XGetImage and XCopyArea. No attempt was made to fix its behavior when passed a window. I did not include the code for introspection about retina displays as I was not convinced that it would be that much more convenient than just asking the window for its backingScaleFactor. Also I was worried about whether it would work on a system with two displays only one of which is a Retina display. While I admire your (Christopher's) gumption in attempting to rework the entire display structure of mac Tk more in the image of X11, I personally view that as a Quixotic effort. So I am going to look for other ways to fix XGetImage and XCopyArea. It took me a long time to figure out what you meant by an "opaque backing store" since, especially when discussing images, I understand the word "opaque" to mean something else. But, indeed, Apple's unwavering determination to make the backing store of an NSWindow opaque in the sense of computer programming is what makes the effort look Quixotic to me. The idea that the only way to get an image from a window is to redraw the window in a different graphics context is silly. But they seem unlikely to ever abandon it. So I think I will investigate whether there is some way to make that work, even though I expect it will be extremely slow if it is possible at all. marc_culler (claiming to be Marc Culler) added on 2020-08-21 22:05:26: I finally got around to researching why the pixelpower was introduced in the first place. The problem it was solving had to do with blending images that have an alpha channel. The code in question was in tkImgPhInstance.c. That file contains a static function BlendComplexColor which does an integer version of the Porter-Duff algorithm. That function is used by the display proc for a photoImage. When displaying a photoimage, a background image is copied from the window and the photoimage is blended with it. Then the resulting blend is copied back to the screen. But if this was done on a retina display then the photoimage would get blended with a scrambled version of 1/4 of the background, since the background has twice the resolution of the photoimage and the integer Porter-Duff algorithm would iterate over pixels. Some years later a new #define variable TKPUTIMAGE_CAN_BLEND was created. It signals that the platform specific implementation of TkPutImage knows how to do the blending, so BlendComplexAlpha should not be used. On macOS the blending is handled in TkPutImage by the NSImage class. The variable is currently #defined only on macOS. (And I see that the definition should be moved out of tkImgPhotoInstance.c and placed in tkMacOSXPort.h instead.) Anyway, I think that means that there is no longer any need for the pixelpower field in the XImage structure (which messed up the XImage structure and created a backwards incompatibility, but only for macOS). I agree that we should get rid of it. I am not so sure how to deal with the change to the XImage structure. But I am pretty sure that pixelpower is now superfluous, and will soon be wrong when we have 3x3 super-Retina displays. kevin_walzer added on 2020-08-08 20:27:12: Marc, didn't you add pixlelpower to accommodate correct scaling of images on Retina displays? As I recall there were some odd bugs in the Tk canvas demo with images not rendering correctly on Retina displays, and this addition fixed the issue. chrstphrchvz added on 2020-08-07 16:58:18: I have attached the patch, but now I wonder if I can do no good here by removing pixelpower. The ABI indeed was broken some time ago in 8.6.6, and reverting it means breaking it all over again. If something like a binary distribution of an extension was compiled with Tk Aqua 8.6.6 through 8.6.10, then running it will now have problems like XPutPixel() (aka ImagePutPixel()) getting called instead of XGetPixel() (aka ImageGetPixel()). chrstphrchvz added on 2020-08-07 15:29:38: Thanks for explaining the issues with iconBitmap. I had the idea of extending it merely because it was something already exposing access to NSImage. However I suspected it had issues similar to what you describe: for example, TkpGetNativeAppBitmap() (tkMacOSXBitmap.c) looks like it might be returning pixmaps which aren't strictly what Tk calls "bitmaps" (1-bit-per-pixel pixmaps) to GetBitmap() (tkBitmap.c). I should be able to provide a patch to remove pixelpower. I'm inclined to simultaneously move the [win backingScaleFactor] wrapping from XGetImage() to a separate function. I currently find that functionality useful for repairing TkMacOSXBitmapRepFromDrawableRect(), although Apple advises using convertRectToBacking:/convertRectFromBacking: instead. Something about Objective-C that I don't understand is why trying implement the function as:
int TkMacOSXNSWindowBackingScaleFactor(NSWindow *win) {
#ifdef __clang__
static BOOL hasRetina = [NSWindow instancesRespondToSelector:@selector(backingScaleFactor)];
if (hasRetina) {
return [win backingScaleFactor] == 2.0 ? 2 : 1;
}
#endif
return 1;
}
refuses to compile because the result of [NSWindow instancesRespondToSelector:@selector(backingScaleFactor)] isn't known at compile time—even though I specifically intend for hasRetina to be initialized at runtime when the function is first called. Using a global enum variable instead will work. marc_culler (claiming to be Marc Culler) added on 2020-08-07 13:33:11: Hi Christopher, Can you provide a patch that gets rid of pixelpower. I am strongly against extending iconBitmap. That should not exist. NSImages are images, not bitmaps. I intend to replace iconBitmap by the new nsimage type (in mac_styles) in 8.7. I would like to see that happen in 8.6 too, although that runs into the "no new features in 8.6" issue and needs some discussion. If you want to do this, please consider extending nsimage, not iconBitmap. | |||
Attachments:
- fcd6717d8b.diff [download] added by chrstphrchvz on 2020-08-09 12:07:38. [details]
