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: Sun, 25 Jun 2023 18:33:09 -0400

On Sun, Jun 25, 2023 at 5:18 PM Alan Third <[email-masked]> wrote:
>
> On Sun, Jun 25, 2023 at 03:07:39PM -0400, Aaron Jensen wrote:
> > On Sun, Jun 25, 2023 at 2:17 PM Alan Third <[email-masked]> wrote:
> > > No, you can use [NSGraphicsContext flushGraphics] and CGContextFlush
> > > to force it.
> >
> > OK, that makes sense, but it's hard to find definitive documentation
> > of that (and ChatGPT seems to think that it's not true, but that's
> > likely a hallucination):
> >
> > =====START GPT=====
> > [NSGraphicsContext flushGraphics] (in Cocoa) and CGContextFlush (in
> > Core Graphics) are used to bypass this batching process and force any
> > pending drawing commands to be executed immediately. After calling
> > these methods, you can be sure that all your previously issued drawing
> > commands have been sent to the GPU.
> >
> > However, it's important to understand that while these functions
> > ensure that the commands are sent to the GPU, they don't guarantee
> > that the GPU has finished executing them. The actual rendering work is
> > still done asynchronously by the GPU.
> >
> > So if you need to read back the results of your rendering (e.g., from
> > an IOSurface), there might still be a brief period where the rendering
> > hasn't completed yet, even after calling flushGraphics or
> > CGContextFlush. If you need to ensure that all GPU rendering work is
> > complete before you proceed, you would typically need to use a more
> > low-level API (like Metal or OpenGL) that provides explicit
> > synchronization capabilities.
> >
> > ...
> >
> > Regarding your question about the [NSGraphicsContext flushGraphics]
> > and CGContextFlush, here are the descriptions directly from Apple's
> > documentation:
> >
> > [NSGraphicsContext flushGraphics]: "Forces any buffered drawing
> > commands to be sent to the destination."
> >
> > CGContextFlush: "Forces all drawing operations to be completed in the
> > specified context."
> >
> > It's important to understand these function calls ensure that drawing
> > commands are dispatched, not that they are completed. This is an
> > inference based on understanding of how graphics pipelines generally
> > work. For more detailed behavior and how these calls interact with
> > your specific use-case, you should refer to Apple's documentation and
> > guides, or consider reaching out to Apple's developer support.
> > =====END GPT=====
>
> The GPU isn't involved in this part. We're drawing to an IOSurface,
> which is a buffer in system memory. So when we draw and flush the
> graphics it's all done by the CPU in system memory.
>
> Then we put the IOSurfaceRef in the contents variable of the layer and
> at that point the GPU uses DMA to copy the buffer from system memory
> to GPU memory. While this is happening the IOSurface is locked so we
> know we shouldn't use it.
>
> Once it's in GPU memory, the GPU blits it to the screenbuffer or
> something, but by that time the IOSurface should be unlocked and we
> can start (safely) working on it again.

OK, that's clarifying, thank you.

> > > Set CACHE_MAX_SIZE to 1.
> > >
> > > But on my machine this resulted in unacceptable rendering flaws on
> > > almost all animated gifs as partially drawn buffers were sent to VRAM.
> >
> > What did you see?
> > I tried with this:
> >
> > https://share.cleanshot.com/vJTClHW9
> >
> > And I didn't notice anything drawing related, but I have an M1 with
> > integrated memory.
>
> Lots of white space. I thought that it was being caught either just
> before or as the image was being drawn into the buffer, so some
> portion of the image area was blank.

Interesting. Would:

+  if (context && context != [NSGraphicsContext currentContext])

have impacted that in any way?

Aaron