Subject: Re: bug#63187: 30.0.50; Tail of longer lines painted after end of nearby lines on macOS

From: Aaron Jensen <[email-masked]>
Date: Sat, 24 Jun 2023 17:43:04 -0400

On Sat, Jun 24, 2023 at 5:29 PM Alan Third <[email-masked]> wrote:
>
> On Sat, Jun 24, 2023 at 12:05:45PM -0400, Aaron Jensen wrote:
> > > But if that's the case, why would removing the asynchronous call to
> > > getContext fix so many problems?
> >
> > It's possible we have two very different problems that only appear related:
> > 1. The one i'm seeing, which is sort of ghosting of other lines into
> > the whitespace of nearby lines. The getContext call removal did not
> > fix this for me, I saw it happen once.
> > 2. The one Kai is seeing, that is exacerbated by decreasing the
> > polling interval, but seems to be helped by removing the getContext
> > call.
>
> Yes, it might be two different things.
>
> > > Something perhaps worth trying... Since removing the asynchronous call
> > > to getContext fixes the problems, perhaps we need to think about the
> > > "lazy" way we get the next buffer when the current one is displayed.
> > > At the moment it just forgets about it until we want to draw to the
> > > screen, at which point we call getContext and it creates the buffer if
> > > necessary and copies the old one to the new one.
> > >
> > > Maybe we should get the new buffer and do the copy when we set the
> > > current buffer for display...
> > >
> > > IIRC I avoided that because there isn't always time for the buffer to
> > > have been sent to VRAM and unlocked before the *next* call to display,
> > > so I wanted to leave it as long as possible between display and
> > > getting the next buffer, but maybe this is just the wrong way to do
> > > it.
> > >
> > > So I suppose putting a call to getContext right after "currentSurface
> > > == NULL" in display might be a quick and dirty way to test that.
> >
> > My problem is that at this point it happens so infrequently and I have
> > no idea if that's because of the patches I'm trying or some other
> > environmental thing or just luck. I'm going to try running without the
> > async getContext and without the setNeedsDisplay for a while and see
> > if it happens. Perhaps the setNeedsDisplay is somehow causing an issue
> > and that's why changing it from source to dest seemed to help but it
> > didn't alleviate it.
>
> OK
>
> > If I see it again, I'll add the sync getContext call in, though I'll
> > admit I do not understand your paragraph above starting with IIRC. Are
> > you suspecting a potential problem with reading from the surface that
> > is in the process of being copied to vram?
>
> Maybe... I'm really not sure what might be going on.
>
> My suspicion is that if we try to swap between the buffers too fast,
> something is going wrong between the process of flushing the drawing
> to the pixel buffer and copying the pixel buffer to the next one.

Do we have any way to know when the flush is done? In other words, can
we only return the surface to the pool after that? Or is that already
done?

> So, we have two buffers, A and B. We draw to A, but before we're done
> the system calls display. We send off the incomplete buffer A to VRAM,
> and then take a copy of that incomplete buffer for B. At some point
> the system decides to flush the graphics context to the buffer, but
> it's flushing to A, and we've *already* copied A to B.

Can we avoid sending incomplete buffers? What is "done"? I don't know
much about graphics programming but I imagine we don't want to send
incomplete buffers ever, we want to finish painting the whole buffer,
then send it. I think I'm also missing understanding on what it means
to flush the graphics context to the buffer. Is that the drawing that
we're doing (like rendering text/etc?) I feel like I may need a
whiteboard session or something to get my head around this so that I
can be of any assistance other than asking dumb questions :)

> This would possibly explain why Kai lowering the polling interval
> induces the issue, as it may increase the frequency at which the
> screen is updated beyond the point where we're able to keep up.
>
> To be honest though, I feel it should all be pretty linear and this
> idea implies things are happening out-of-order. So I'm not convinced
> I'm right.
>
>
> Who knows. Maybe all we need to do is make sure we don't try to draw
> to the screen while emacs is drawing to the buffer... Something like
> this:
>
> modified src/nsterm.m @@ -10622,7 +10622,7 @@ - (void) display
>  {
>    NSTRACE_WHEN (NSTRACE_GROUP_FOCUS, "[EmacsLayer display]");
>
> -  if (context)
> +  if (context && context != [NSGraphicsContext currentContext])
>      {
>        [self releaseContext];
>
>
> ...
>
> Actually...
>
> That change should probably be made anyway. If the NS run loop kicks
> in between an ns_focus call and an ns_unfocus call, it could call
> display and our display function will happily destroy the existing
> context without creating a new one, so any *subsequent* drawing
> operations, up until ns_unfocus, will be lost.

OK, I'm adding this to my current build.

Is this in line with the type of issue I'm seeing where scrolling
works but the ghosting either replicates (or scrolls with it?) In
other words, what would you expect to see in this scenario? Would it
just stop painting entirely?

>
> I'm not sure if that's a legitimate concern... 😕
>
>
> I've got too many ideas about how to fix it and no way to actually try
> them out, never mind the difficulty of inducing the issue if I did.

I'm happy to try things if you don't mind a 1-2 week feedback cycle
(since lately that's about how long it takes for me to see an issue...