Tk Source Code

View Ticket
Login
Ticket UUID: fab5fed65edd296c8ce7039503afa2cd62efa435
Title: OS X - lots of textDisp failures (spurious "borders" and indices in tk_textRedraw)
Type: Bug Version: core-8-6-branch
Submitter: fvogel Created on: 2017-01-14 14:15:26
Subsystem: 18. [text] Assigned To: fvogel
Priority: 6 Severity: Minor
Status: Closed Last Modified: 2017-11-07 20:58:13
Resolution: Fixed Closed By: fvogel
    Closed on: 2017-11-07 20:58:13
Description:

OS X - lots of textDisp failures.

All of them have in common spurious "borders" and indices in tk_textRedraw. There are 30 such tests.

Detailed tests results on OS X for core-8-6-branch:

==== textDisp-4.8 UpdateDisplayInfo, filling in extra vertical space FAILED
==== Contents of test case:

    .t delete 1.0 end
    .t insert end "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17"
    .t yview 16.0
    update
    .t delete 5.0 14.0
    update
    set x [list [.t index @0,0] $tk_textRelayout $tk_textRedraw]

---- Result was:
1.0 {5.0 4.0 3.0 2.0 1.0} {borders 1.0 2.0 3.0 4.0 5.0 8.0 eof}
---- Result should have been (exact matching):
1.0 {5.0 4.0 3.0 2.0 1.0} {1.0 2.0 3.0 4.0 5.0 eof}
==== textDisp-4.8 FAILED


==== textDisp-4.10 UpdateDisplayInfo, filling in extra vertical space FAILED
==== Contents of test case:

    .t delete 1.0 end
    .t insert end "1\n2\n3\n4\n5\nLine 6 is such a long line that it wraps around.\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17"
    .t yview end
    update
    .t delete 13.0 end
    update
    list [.t index @0,0] $tk_textRelayout $tk_textRedraw

---- Result was:
5.0 {12.0 7.0 6.40 6.20 6.0 5.0} {borders 5.0 6.0 6.20 6.40 7.0 12.0}
---- Result should have been (exact matching):
5.0 {12.0 7.0 6.40 6.20 6.0 5.0} {5.0 6.0 6.20 6.40 7.0 12.0}
==== textDisp-4.10 FAILED



==== textDisp-4.11 UpdateDisplayInfo, filling in extra vertical space FAILED
==== Contents of test case:

    .t delete 1.0 end
    .t insert end "1\n2\n3\n4\n5\nLine 6 is such a long line that it wraps around, not once but really quite a few times.\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17"
    .t yview end
    update
    .t delete 14.0 end
    update
    list [.t index @0,0] $tk_textRelayout $tk_textRedraw

---- Result was:
6.40 {13.0 7.0 6.80 6.60 6.40} {borders 6.40 6.60 6.80 7.0 8.0 13.0}
---- Result should have been (exact matching):
6.40 {13.0 7.0 6.80 6.60 6.40} {6.40 6.60 6.80 7.0 13.0}
==== textDisp-4.11 FAILED


==== textDisp-4.13 UpdateDisplayInfo, special handling for top/bottom lines FAILED
==== Contents of test case:

    .t tag add x 1.0 end
    .t yview 1.0
    update
    .t yview scroll 3 units
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{11.0 12.0 13.0} {borders 4.0 9.0 10.0 11.0 12.0 13.0}
---- Result should have been (exact matching):
{11.0 12.0 13.0} {4.0 10.0 11.0 12.0 13.0}
==== textDisp-4.13 FAILED



==== textDisp-4.14 UpdateDisplayInfo, special handling for top/bottom lines FAILED
==== Contents of test case:

    .t tag remove x 1.0 end
    .t yview 1.0
    update
    .t yview scroll 3 units
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{11.0 12.0 13.0} {borders 10.0 11.0 12.0 13.0}
---- Result should have been (exact matching):
{11.0 12.0 13.0} {11.0 12.0 13.0}
==== textDisp-4.14 FAILED



==== textDisp-4.15 UpdateDisplayInfo, special handling for top/bottom lines FAILED
==== Contents of test case:

    .t tag add x 1.0 end
    .t yview 4.0
    update
    .t yview scroll -2 units
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 3.0} {borders 2.0 3.0 4.0 5.0 11.0}
---- Result should have been (exact matching):
{2.0 3.0} {2.0 3.0 4.0 11.0}
==== textDisp-4.15 FAILED



==== textDisp-4.16 UpdateDisplayInfo, special handling for top/bottom lines FAILED
==== Contents of test case:

    .t tag remove x 1.0 end
    .t yview 4.0
    update
    .t yview scroll -2 units
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 3.0} {borders 2.0 3.0 4.0}
---- Result should have been (exact matching):
{2.0 3.0} {2.0 3.0}
==== textDisp-4.16 FAILED


==== textDisp-6.1 scrolling in DisplayText, scroll up FAILED
==== Contents of test case:

    .t delete 1.0 end
    .t insert 1.0 "Line 1"
    foreach i {2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
        .t insert end "\nLine $i"
    }
    update
    .t delete 2.0 3.0
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 10.0} {borders 2.0 9.0 10.0}
---- Result should have been (exact matching):
{2.0 10.0} {2.0 10.0}
==== textDisp-6.1 FAILED



==== textDisp-6.2 scrolling in DisplayText, scroll down FAILED
==== Contents of test case:

    .t delete 1.0 end
    .t insert 1.0 "Line 1"
    foreach i {2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
        .t insert end "\nLine $i"
    }
    update
    .t insert 2.0 "New Line 2\n"
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 3.0} {borders 2.0 3.0 4.0}
---- Result should have been (exact matching):
{2.0 3.0} {2.0 3.0}
==== textDisp-6.2 FAILED



==== textDisp-6.3 scrolling in DisplayText, multiple scrolls FAILED
==== Contents of test case:

    .t configure -wrap char
    .t delete 1.0 end
    .t insert 1.0 "Line 1"
    foreach i {2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
        .t insert end "\nLine $i"
    }
    update
    .t insert 2.end "is so long that it wraps"
    .t insert 4.end "is so long that it wraps"
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 2.20 4.0 4.20} {borders 2.0 2.20 3.0 4.0 4.20 5.0}
---- Result should have been (exact matching):
{2.0 2.20 4.0 4.20} {2.0 2.20 4.0 4.20}
==== textDisp-6.3 FAILED



==== textDisp-6.4 scrolling in DisplayText, scrolls interfere FAILED
==== Contents of test case:

    .t configure -wrap char
    .t delete 1.0 end
    .t insert 1.0 "Line 1"
    foreach i {2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
        .t insert end "\nLine $i"
    }
    update
    .t insert 2.end "is so long that it wraps around, not once but three times"
    .t insert 4.end "is so long that it wraps"
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 2.20 2.40 2.60 4.0 4.20} {borders 2.0 2.20 2.40 2.60 3.0 4.0 4.20 6.0}
---- Result should have been (exact matching):
{2.0 2.20 2.40 2.60 4.0 4.20} {2.0 2.20 2.40 2.60 4.0 4.20 6.0}
==== textDisp-6.4 FAILED


==== textDisp-8.7 TkTextChanged FAILED
==== Contents of test case:

    .t delete 1.0 end
    .t insert 1.0 "Line 1 is so long that it wraps around, two times"
    foreach i {2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
        .t insert end "\nLine $i"
    }
    update
    .t delete 1.2 1.end
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{1.0 9.0 10.0} {borders 1.0 8.0 9.0 10.0}
---- Result should have been (exact matching):
{1.0 9.0 10.0} {1.0 9.0 10.0}
==== textDisp-8.7 FAILED


==== textDisp-8.9 TkTextChanged FAILED
==== Contents of test case:

    .t delete 1.0 end
    .t insert 1.0 "Line 1 is so long that it wraps around, two times"
    foreach i {2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
        .t insert end "\nLine $i"
    }
    update
    .t delete 2.0 3.0
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 8.0} {borders 2.0 7.0 8.0}
---- Result should have been (exact matching):
{2.0 8.0} {2.0 8.0}
==== textDisp-8.9 FAILED



==== textDisp-8.10 TkTextChanged FAILED
==== Contents of test case:

    .t configure -wrap char
    .t delete 1.0 end
    .t insert 1.0 "Line 1\nLine 2 is long enough to wrap\nLine 3 is also long enough to wrap\nLine 4"
    .t tag add big 2.19
    update
    .t delete 2.19
    update
    set tk_textRedraw

---- Result was:
borders 2.0 2.20 4.0 eof
---- Result should have been (exact matching):
2.0 2.20 eof
==== textDisp-8.10 FAILED


==== textDisp-9.1 TkTextRedrawTag FAILED
==== Contents of test case:

    .t configure -wrap char
    .t delete 1.0 end
    .t insert 1.0 "Line 1\nLine 2 is long enough to wrap around\nLine 3\nLine 4"
    update
    .t tag add big 2.2 2.4
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 2.18} {borders 2.0 2.18 3.0}
---- Result should have been (exact matching):
{2.0 2.18} {2.0 2.18}
==== textDisp-9.1 FAILED


==== textDisp-9.3 TkTextRedrawTag FAILED
==== Contents of test case:

    .t configure -wrap char
    .t delete 1.0 end
    .t insert 1.0 "Line 1\nLine 2 is long enough to wrap around\nLine 3\nLine 4"
    update
    .t tag add big 2.2 2.4
    update
    .t tag remove big 1.0 end
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 2.20} {borders 2.0 2.20 4.0 eof}
---- Result should have been (exact matching):
{2.0 2.20} {2.0 2.20 eof}
==== textDisp-9.3 FAILED



==== textDisp-9.4 TkTextRedrawTag FAILED
==== Contents of test case:

    .t configure -wrap char
    .t delete 1.0 end
    .t insert 1.0 "Line 1\nLine 2 is long enough to wrap around\nLine 3\nLine 4"
    update
    .t tag add big 2.2 2.20
    update
    .t tag remove big 1.0 end
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 2.20} {borders 2.0 2.20 eof}
---- Result should have been (exact matching):
{2.0 2.20} {2.0 2.20 eof}
==== textDisp-9.4 FAILED



==== textDisp-9.5 TkTextRedrawTag FAILED
==== Contents of test case:

    .t configure -wrap char
    .t delete 1.0 end
    .t insert 1.0 "Line 1\nLine 2 is long enough to wrap around\nLine 3\nLine 4"
    update
    .t tag add big 2.2 2.end
    update
    .t tag remove big 1.0 end
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 2.20} {borders 2.0 2.20 eof}
---- Result should have been (exact matching):
{2.0 2.20} {2.0 2.20 eof}
==== textDisp-9.5 FAILED


==== textDisp-9.7 TkTextRedrawTag FAILED
==== Contents of test case:

    .t configure -wrap char
    .t delete 1.0 end
    .t insert 1.0 "Line 1\nLine 2 is long enough to wrap\nLine 3 is also long enough to wrap\nLine 4"
    .t tag add big 2.19
    update
    .t tag remove big 2.19
    update
    set tk_textRedraw

---- Result was:
borders 2.0 2.20 4.0 eof
---- Result should have been (exact matching):
2.0 2.20 eof
==== textDisp-9.7 FAILED


==== textDisp-9.12 TkTextRedrawTag FAILED
==== Contents of test case:

    .t configure -wrap char
    .t delete 1.0 end
    for {set i 1} {$i < 5} {incr i} {
      .t insert end "Line $i+++Line $i\n"
    }
    .t tag configure hidden -elide true
    .t tag add hidden 2.6 3.6
    update
    .t tag add hidden 3.11 4.6
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
2.0 {borders 2.0 5.0 eof}
---- Result should have been (exact matching):
2.0 {2.0 eof}
==== textDisp-9.12 FAILED



==== textDisp-9.13 TkTextRedrawTag FAILED
==== Contents of test case:

    .t configure -wrap none
    .t delete 1.0 end
    for {set i 1} {$i < 10} {incr i} {
        .t insert end "Line $i - This is Line [format %c [expr 64+$i]]\n"
    }
    .t tag add hidden 2.8 2.17
    .t tag add hidden 6.8 7.17
    .t tag configure hidden -background red
    .t tag configure hidden -elide true
    update
    .t tag configure hidden -elide false
    update
    list $tk_textRelayout $tk_textRedraw

---- Result was:
{2.0 6.0 7.0} {borders 2.0 6.0 7.0 8.0}
---- Result should have been (exact matching):
{2.0 6.0 7.0} {2.0 6.0 7.0}
==== textDisp-9.13 FAILED


==== textDisp-11.2 TkTextSetYView FAILED
==== Contents of test case:

    .t yview 30.0
    update
    .t yview 32.0
    update
    list [.t index @0,0] $tk_textRedraw

---- Result was:
32.0 {borders 39.0 40.0 41.0}
---- Result should have been (exact matching):
32.0 {40.0 41.0}
==== textDisp-11.2 FAILED



==== textDisp-11.3 TkTextSetYView FAILED
==== Contents of test case:

    .t yview 30.0
    update
    .t yview 28.0
    update
    list [.t index @0,0] $tk_textRedraw

---- Result was:
28.0 {borders 28.0 29.0 30.0}
---- Result should have been (exact matching):
28.0 {28.0 29.0}
==== textDisp-11.3 FAILED



==== textDisp-11.4 TkTextSetYView FAILED
==== Contents of test case:

    .t yview 30.0
    update
    .t yview 31.4
    update
    list [.t index @0,0] $tk_textRedraw

---- Result was:
31.0 {borders 39.0 40.0}
---- Result should have been (exact matching):
31.0 40.0
==== textDisp-11.4 FAILED


==== textDisp-11.6 TkTextSetYView FAILED
==== Contents of test case:

    .t yview 30.0
    update
    set tk_textRedraw {}
    .t yview -pickplace 28.0
    update
    list [.t index @0,0] $tk_textRedraw

---- Result was:
28.0 {borders 28.0 29.0 30.0}
---- Result should have been (exact matching):
28.0 {28.0 29.0}
==== textDisp-11.6 FAILED



==== textDisp-11.7 TkTextSetYView FAILED
==== Contents of test case:

    .t yview 30.0
    update ; update
    set tk_textRedraw {}
    .t yview -pickplace 26.0
    update
    list [.t index @0,0] $tk_textRedraw

---- Result was:
21.0 {borders 21.0 22.0 23.0 24.0 25.0 26.0 27.0 28.0 29.0}
---- Result should have been (exact matching):
21.0 {21.0 22.0 23.0 24.0 25.0 26.0 27.0 28.0 29.0}
==== textDisp-11.7 FAILED



==== textDisp-11.8 TkTextSetYView FAILED
==== Contents of test case:

    .t yview 30.0
    update
    set tk_textRedraw {}
    .t yview -pickplace 41.0
    update
    list [.t index @0,0] $tk_textRedraw

---- Result was:
32.0 {borders 39.0 40.0 41.0}
---- Result should have been (exact matching):
32.0 {40.0 41.0}
==== textDisp-11.8 FAILED



==== textDisp-11.9 TkTextSetYView FAILED
==== Contents of test case:

    .t yview 30.0
    update
    set tk_textRedraw {}
    .t yview -pickplace 43.0
    update
    list [.t index @0,0] $tk_textRedraw

---- Result was:
38.0 {borders 40.0 41.0 42.0 43.0 44.0 45.0 46.0 47.0 48.0}
---- Result should have been (exact matching):
38.0 {40.0 41.0 42.0 43.0 44.0 45.0 46.0 47.0 48.0}
==== textDisp-11.9 FAILED


==== textDisp-11.12 TkTextSetYView, wrapped line is off-screen FAILED
==== Contents of test case:

    .t insert 10.0 "Long line with enough text to wrap\n"
    .t yview 1.0
    update
    set tk_textRedraw {}
    .t see 10.30
    update
    list [.t index @0,0] $tk_textRedraw

---- Result was:
2.0 {borders 10.0 10.20}
---- Result should have been (exact matching):
2.0 10.20
==== textDisp-11.12 FAILED



==== textDisp-11.13 TkTestSetYView, partially visible last line FAILED
==== Contents of test case:

    catch {destroy .top}
    toplevel .top
    wm geometry .top +0+0
    text .top.t -width 20 -height 5
    pack .top.t
    .top.t insert end "Line 1"
    for {set i 2} {$i <= 100} {incr i} {
        .top.t insert end "\nLine $i"
    }
    update
    scan [wm geometry .top] "%dx%d" w2 h2
    wm geometry .top ${w2}x[expr $h2-2]
    update
    .top.t yview 1.0
    update
    set tk_textRedraw {}
    .top.t see 5.0
    update
    # Note, with smooth scrolling, the results of this test
    # have changed, and the old '2.0 {5.0 6.0}' is quite wrong.
    list [.top.t index @0,0] $tk_textRedraw

---- Result was:
1.0 {4.0 5.0}
---- Result should have been (exact matching):
1.0 5.0
==== textDisp-11.13 FAILED


User Comments: fvogel added on 2017-11-07 20:58:13:
Merged to core-8-6-branch and trunk. Many thanks to Marc Culler!

fvogel added on 2017-11-01 19:28:29:

This does no longer happen in the bugfix branch (OSX_redraw_artifacts)


marc_culler (claiming to be Marc Culler) added on 2017-10-17 18:30:52:
Oh, and the long post was not here, it was on ticket 92e614e612.  So I will now
post the correction there and leave this as an advertisement for a long
discussion of the causes of graphics artifacts on macOS.

marc_culler added on 2017-10-17 15:33:32:
One small correction to my long post, pointed out by Francois:

I should have said "The drawRgn is the intersection of the visRgn with the
window's *clipping* rectangle."

The clipping rectangle is usually equal to the window's bounding rectangle but
a smaller clipping rectangle can be assigned by calling TkpClipDrawableToRect.

fvogel added on 2017-10-15 18:18:53:
More accurately: exactly the same tests fail now again, but their results are not the same failed results as in this original report.

I'm attaching tests results as of now (tkTextDisp_failures_15Oct2017.txt)

fvogel added on 2017-10-15 14:07:23:

Yes, that's the patch proposed by Marc, that got committed. See branch OSX_redraw_artifacts for full details of the work toward fixing these redrawing issues.

Indeed a lot of redrawing artifacts are gone with the latest state of this branch, that is at [53177176].

The bad news is that I see the 30 tests reported here fail again identically. This is puzzling to me because you (Marc) report that these tests still pass for you with your patches.


stevenaaus added on 2017-10-14 02:28:34:
Thanks so much Marc. This fixes so many rendering issues. 
I tested it on Gregor's revised tktext widget (wish 8.7).

I suppose this is the whole working patch:

diff -urN tk_prev/macosx/tkMacOSXDraw.c tk/macosx/tkMacOSXDraw.c                                                               
--- tk_prev/macosx/tkMacOSXDraw.c       2017-08-17 20:44:01.000000000 +1000                                                    
+++ tk/macosx/tkMacOSXDraw.c    2017-10-14 11:47:03.000000000 +1000                                                            
@@ -1559,6 +1559,7 @@                                                                                                          
                                                                                                                               
            /* Convert to Tk coordinates. */                                                                                   
            TkMacOSXSetWithNativeRegion(damageRgn, dmgRgn);                                                                    
+                   TkMacOSXOffsetRegion(damageRgn, -macDraw->xOff, -macDraw->yOff);                                           
            if (extraRgn) {                                                                                                    
                CFRelease(extraRgn);                                                                                           
            }                                                                                                                  
diff -urN tk_prev/macosx/tkMacOSXWindowEvent.c tk/macosx/tkMacOSXWindowEvent.c                                                 
--- tk_prev/macosx/tkMacOSXWindowEvent.c        2017-08-17 20:44:01.000000000 +1000                                            
+++ tk/macosx/tkMacOSXWindowEvent.c     2017-10-14 11:45:10.000000000 +1000
@@ -357,8 +357,8 @@
     event.xany.window = Tk_WindowId(winPtr);
     event.xany.display = Tk_Display(winPtr);
     event.type = Expose;
-    event.xexpose.x = damageBounds.origin.x;
-    event.xexpose.y = damageBounds.origin.y;
+    event.xexpose.x = damageBounds.origin.x - bounds.origin.x;
+    event.xexpose.y = damageBounds.origin.y - bounds.origin.y;
     event.xexpose.width = damageBounds.size.width;
     event.xexpose.height = damageBounds.size.height;
     event.xexpose.count = 0;

kevin_walzer added on 2017-10-13 15:31:01:
Patch from Marc Culler committed to trunk (32fa275d) and core-8-6-branch (0ccc37e8).

marc_culler (claiming to be Marc Culler) added on 2017-10-13 11:43:58:
Hi François,

I am reopening this ticket to propose an alternate fix for this problem.

As you describe here, you had discovered that the mac port of Tk was failing
many tests of the Text widget because it was generating expose events which
were offset from their correct position on the screen. When they were offset
horizontally they would generate spurious updates of the borders and when they
were offset vertically they would cause undamaged lines to be redrawn.  Your
fix, in addition to deleting a hack that I had made long ago to try to deal
with this issue, was to change two lines in the GenerateUpdates function. 
You made this change:

-    event.xexpose.x = damageBounds.origin.x - bounds.origin.x;
-    event.xexpose.y = damageBounds.origin.y - bounds.origin.y;
+    event.xexpose.x = damageBounds.origin.x;
+    event.xexpose.y = damageBounds.origin.y;


This made the tests pass.  Unfortunately, however, it also produced new
artifacts, as we learned later on. The cause of these artifacts was that the
expose events were still located in the wrong place, although not in a way
which caused the tests to fail.  For example, in the Rube Goldberg demo
there was a rectangle at the top of the Canvas (which happened to be exactly
the size of the Label above the Canvas) that would never be updated and would
fill up with garbage.  The was caused by the expose event being displaced
vertically by the y offset of the Canvas, i.e. the height of the label.  In
the peer text demo, when a new peer was created a black box would appear in
the window, indicating that that a damaged part of the screen was not being
updated by the expose event.

While you were on the right track with your analysis, it turns out that the
correction to the offsets of the damage rectangle needed to be applied a little
differently.  I finally noticed that there was a step missing in the
TkScrollWindow function in tkMacOSXDraw.c, where the damage rectangle is
being converted from NSView coordinates (origin at lower left) to Tk
coordinates.

When I reversed your 2-line change and made the following change instead:

            /* Convert to Tk coordinates. */
            TkMacOSXSetWithNativeRegion(damageRgn, dmgRgn);
+           TkMacOSXOffsetRegion(damageRgn, -macDraw->xOff, -macDraw->yOff);

then the artifacts disappeared from the Rube Goldberg demo and from the peer
Text demo, yet the Text widget tests still passed.

After the fact, it is pretty obvious that the offset above needs to be applied
to correctly convert the coordinates.  However the omission of that line was a
very old error that is present in the ancient Tk 8.5.9 which Apple ships with
its computers. No one had noticed it for all of these years.

fvogel added on 2017-01-18 22:42:13:
I also wanted to thank Brad Lanam who gave me access to an OS X computer remotely from the place I live to his place (on the other side of the world, or almost). Without his help I couldn't have done anything regarding this ticket. Thanks, Brad!

fvogel added on 2017-01-18 22:26:08:
Many thanks for your review and confirmation.

This is now merged to core-8-6-branch and trunk.

marc_culler (claiming to be Marc Culler) added on 2017-01-18 20:29:26:
I have looked over these changes and tested this code on my machine. I think all of these changes are correct, and finding the duplicate invalidation was a good catch.

Thank you for finding and fixing these, François!  (And sorry about that hack!)

- Marc

fvogel added on 2017-01-17 19:01:04:
Thanks.

Nevertheless before merging I would like to get feedback from Tk developers on OS X. Preferably from Marc Culler, who last changed the code I modified. Kevin Walzer welcome otherwise or in addition.

jan.nijtmans added on 2017-01-17 08:54:41:
That's great news! Well done, François!

fvogel added on 2017-01-17 07:56:55:
I have now run the complete test suite on OS X (except the menu test file that hangs) in both the present bugfix branch and the current core-8-6-branch. The 30 test reported as failing in the present ticket do so now pass, and no new other test fails.

fvogel added on 2017-01-15 22:15:31:

Moreover, the analysis of this ticket showed that on OS X the damaged region is invalidated twice:

- once through the processing of the Expose event (on OS X the Appkit is not used to draw the widget, Tk is used instead, see comments in tkMacOSXWindowEvent.c around line 770)
- a second time because DisplayText() calls TextInvalidateRegion() after TkScrollWindow()

The following patch could therefore be applied, as a small optimization:

Index: generic/tkTextDisp.c
==================================================================
--- generic/tkTextDisp.c
+++ generic/tkTextDisp.c
@@ -4297,12 +4297,13 @@

        damageRgn = TkCreateRegion();
        if (TkScrollWindow(textPtr->tkwin, dInfoPtr->scrollGC, dInfoPtr->x,
                oldY, dInfoPtr->maxX-dInfoPtr->x, height, 0, y-oldY,
                damageRgn)) {
+#ifndef MAC_OSX_TK
            TextInvalidateRegion(textPtr, damageRgn);
-
+#endif
        }
        numCopies++;
        TkDestroyRegion(damageRgn);
     }


fvogel added on 2017-01-15 22:04:42:

On OS X, TkScrollWindow(), after scrolling and computing the damage region, queues Expose events for the damage region.

These Expose events are processed by TextEventProc(). Processing bypasses the event queue, because of the call chain TkScrollWindow()->generateExposeEvents->GenerateUpdates()->Tk_HandleEvent()

Note that this Tk_HandleEvent() was introduced by [35a5490f].

TextEventProc() simply calls TkTextRedrawRegion() with the rectangle received in the event.

Then TkTextRedrawRegion() calls TextInvalidateRegion() and schedules a DisplayText() for execution at idle time.

Strangely enough, the rectangle received in the Expose event is not exactly the same as the rectangle invalidated later directly from DisplayText().

Looking at the computation of the damage rectangle in generateExposeEvents and GenerateUpdates(): In GenerateUpdates(), an Expose event is first created for the window containing the text widget (since this window intersects the damaged region), then recursively for all children of this window (if they intersect the damage region). In the specific case of textDisp-6.1, there are two children: a frame and the text widget we're interested in. An Expose event is generated for the text widget in the damage region.

The x and y coordinates sent in the Expose event for the text widget children are computed as follows:

    event.xexpose.x = damageBounds.origin.x - bounds.origin.x;
    event.xexpose.y = damageBounds.origin.y - bounds.origin.y;

but I believe this is wrong, they should be:

    event.xexpose.x = damageBounds.origin.x;
    event.xexpose.y = damageBounds.origin.y;

Indeed changing this fixes the second part of the problem reported in the present ticket: "borders" is correctly no longer present in the tk_textRedraw list. This is [dd0005dc8a].

All 30 tests originally failing in this ticket are now passing, and no new test of textDisp.text is failing.


fvogel added on 2017-01-15 21:53:04:

Updated test script inspired by textDisp-6.1, showing the spurious "borders" in the output (this is at [114ef627]):

package require Tk
frame .f
text .t -font {Courier 12} -width 20 -height 10
pack .f .t -side left
if {[tk windowingsystem] eq "aqua"} {
    .t configure -font {Arial 16} 
} else {
    .t configure -font {Arial 12} 
}
.t debug on
.t insert 1.0 "Line 1"
foreach i {2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
.t insert end "\nLine $i"
}
update
.t delete 2.0 3.0  ; # this is what triggers what we want to measure
puts [list $tk_textRelayout $tk_textRedraw]
<verbatim>
</p>


fvogel added on 2017-01-14 14:33:11:

Detailed analysis based on the test script:

tk_textRedraw is a list used in debug mode of the text widget.

It is reset to {} at the beginning of DisplayText().

In DisplayText(), "borders" is added to the tk_textRedraw list if (dInfoPtr->flags & REDRAW_BORDERS)

The REDRAW_BORDERS flag is SET:

1. in TextInvalidateRegion(), if the clipbox of the region it receives intersects the surrounding area of the text displayed by the text widget
2. in TkTextRelayoutWindow(), at each call

The REDRAW_BORDERS flag is RESET (cleared) after border redrawing in DisplayText().

Debugging shows that when running .t delete 2.0 3.0, TkTextRelayoutWindow() is not called.

The failure must therefore be due to the call to TextInvalidateRegion().

The algorithm in DisplayText() cleverly tries to see if it's possible to bring some parts of the screen up-to-date by scrolling (copying from other parts of the screen). In textDisp-6.1 it is indeed possible.

TkScrollWindow() is therefore called to move all lines after the deleted one upwards.

Be it on Win or OS X, the x, y, width, height, dx, dy values are the same (provided the metrics of the used font are the same on the two OSes, which is the case in my test script, that's the reason for the test on [tk windowingsystem])

On both systems, TkScrollWindow() returns true (which means there is a damaged region), therefore TextInvalidateRegion() is called.

The first thing TextInvalidateRegion() does is to compute the clip box in which the region it receives is in. TkClipBox() is called for that purpose.

The interesting thing is that TkClipBox() returns slightly different rects (here: y coordinates and heights) on OS X compared to Win. With my above test case:

Win: TextInvalidateRegion: received region has clipbox rect: x = 1 y = 163 w = 180 h = 18

OS X: TextInvalidateRegion: received region has clipbox rect: x = 1 y = 162 w = 180 h = 20

If, on OS X, y and h are artificially adjusted (just as a debug try) to match the values produced by Win, then the results obtained for textDisp-6.1 no longer contain "borders", as expected (but the spurious index "9.0" is still present).

Consequence: either a) the region returned by TkScrollWindow() is not the same on the two OSes, or b) TkClipBox() from the same region gives two different clip boxes on the two OSes.

I tend to suspect a) because in TextInvalidateRegion() identification of the invalidated lines uses TkRectInRegion(), which also produces different results on Win and OS X (which explains the "9.0" in the obtained results of textDisp-6.1 on OS X). If not a), then c) both TkClipBox() and TkRectInRegion() must behave differently on the two OSes.

Investigation of hypothesis a): the region returned by TkScrollWindow() is not the same on Win and OS X

On Win, TkScrollWindow() is just ScrollWindowEx().

On OS X, TkScrollWindow() is implemented in Objective-C, and in its source code there is the following suspicious snippet:

	    /* Expand the rectangles slightly to avoid degeneracies. */
	    srcRect.origin.y -= 1;
	    srcRect.size.height += 2;
	    dstRect.origin.y += 1;
	    dstRect.size.height -= 2;

This came in with [15a7b752]. Which degeneracies are we talking about here, and how does this prevent them?

Removing the above hack largely makes tests results for textDisp.test now OK as far as lines redrawing is concerned (i.e. : there are no longer spurious lines in the tk_textRedraw list). This is partial fix [114ef62767].

But "borders" is still present in the tk_textRedraw list.


fvogel added on 2017-01-14 14:18:44:

Let's focus on a simple one for the time being: textDisp-6.1

textDisp-6.1 checks that the relayout and redraw lists after deletion of a complete text line are consistent with the performed deletion operation .t delete 2.0 3.0

This test PASSES under Win and Linux, but FAILS under OS X.

The other failing occurrences in the results output by textDisp.test are similar.

Simple test script inspired by textDisp-6.1:

package require Tk
pack [text .t -width 20 -height 10 -highlightthickness 0 -borderwidth 0]
if {[tk windowingsystem] eq "aqua"} {
    .t configure -font {Arial 16} 
} else {
    .t configure -font {Arial 12} 
}
.t debug on
.t insert 1.0 "Line 1"
foreach i {2 3 4 5 6 7 8 9 10 11 12 13 14 15} {
.t insert end "\nLine $i"
}
update
.t delete 2.0 3.0  ; # this is what triggers what we want to measure


Attachments: