elasticman
Posts: 4
Joined: Wed Nov 20, 2019 9:40 am

Possible to measure time of HDMI output generation based on framebuffer copy?

Wed Nov 20, 2019 9:49 am

As the title may not be fully clear, here's a complete description of the problem.

I've written a small library for the linux framebuffer using mmap and some (higher) drawing routines, effectlively copying specific bytes to specific offsets in the mapped memory.

To measure the output latency I need to determine the moment that follows my

Code: Select all

memcpy 
command (copied n bytes into the framebuffer) to eventually generate output on the Raspberry's HDMI port.

According to the image below, the next step that follows the framebuffer memory copy is the Hardware Video Scaler (HVS), again followed by the FIFO and HDMI encoder.

Image
Source: https://blog.benjdoherty.com/2019/05/21 ... pberry-Pi/

I now want to determine the time it takes after the framebuffer memcpy as close as possible. In other words, how long it takes for the graphics processsing unit and all other steps to produce the HDMI output from by framebuffer input (moments marked as t1 and t2 in the image below).


Image

Is this even possible? Would you (re)write some kernel code to achieve that or is it possible from userland (or even within some C code)?

----------
Meta data

Raspberry PI 4 latest
OS: Raspbian Buster Lite Programming
language: C

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7527
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Possible to measure time of HDMI output generation based on framebuffer copy?

Wed Nov 20, 2019 11:20 am

Which particular line of the image are you concerned about? HDMI is a serial link, therefore there is a temporal difference between when the lines are transmitted.

https://www.raspberrypi.org/forums/view ... 3&t=257218 has just been discussing a very similar concept.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

elasticman
Posts: 4
Joined: Wed Nov 20, 2019 9:40 am

Re: Possible to measure time of HDMI output generation based on framebuffer copy?

Wed Nov 20, 2019 11:35 am

The device in your linked thread is indeed similar to the one I'm building.

As I'm only alternatingly showing full black respectively white images there's no need to select a specific line as there shouldn't be any difference.

From the linked thread:
You can arrange for the Pi to update the screen from eg black to white in sync on a VSYNC, and then time until the photoresistor detects a change. You probably want to be looking at either the libDRM API, or DispmanX. Both can provide you with a vsync callback or a flip/update confirmation callback from which you can start your timer, and then you can monitor a GPIO for the the output of your photoresistor.
Is it possible to use any of these callbacks within the framebuffer environment? Or would I need to use dispmanx as a whole?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7527
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Possible to measure time of HDMI output generation based on framebuffer copy?

Wed Nov 20, 2019 11:42 am

elasticman wrote:
Wed Nov 20, 2019 11:35 am
Is it possible to use any of these callbacks within the framebuffer environment? Or would I need to use dispmanx as a whole?
The framebuffer is a single "live" buffer that is being rendered to the screen.
You could use DispmanX to get vsync callbacks, and use those to trigger your memcpy/memset call. You will need to be pretty swift about it as IIRC the callback is signalling the start of the vsync period, so the scanout of the first line won't be too much after that.

Please time how long your memory operation takes. Using mmap to access the framebuffer is frequently more time consuming than one expects.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

elasticman
Posts: 4
Joined: Wed Nov 20, 2019 9:40 am

Re: Possible to measure time of HDMI output generation based on framebuffer copy?

Wed Nov 20, 2019 11:57 am

You could use DispmanX to get vsync callbacks, and use those to trigger your memcpy/memset call. You will need to be pretty swift about it as IIRC the callback is signalling the start of the vsync period, so the scanout of the first line won't be too much after that.

Please time how long your memory operation takes. Using mmap to access the framebuffer is frequently more time consuming than one expects.
So my procedure would be something like (pseudo code):

Code: Select all

register_vsync_start_callback(start_func);
register_vsync_update_callback(update_func);

bool isStarted = false;
bool isComplete = false;

while(1) {
    memset(framebuffer, 0x00); // full black image
    // Wait for start in interrupt
    while(!isStarted);
    while(!isComplete);
    
    print("GPU latency is " + time_end - time_start);
}


start_func() {
   memset(framebuffer, 0xFF...) // full white image
   time_start = time_now();
   isStarted = true;
}

update_func() {
  time_end = time_now();
  isComplete = true;
}
I'll measure the time for the memcpy ASAP and report it back (however, don't have access to the device until evening)

Edit:
I've found

Code: Select all

/* Update callback. */
typedef void (*DISPMANX_CALLBACK_FUNC_T)(DISPMANX_UPDATE_HANDLE_T u, void * arg);

/* Progress callback */
typedef void (*DISPMANX_PROGRESS_CALLBACK_FUNC_T)(DISPMANX_UPDATE_HANDLE_T u,
                                                  uint32_t line,
                                                  void * arg);
in https://github.com/raspberrypi/userland ... nx_types.h

Both require a type DISPMANX_UPDATE_HANDLE_T which I guess needs to be created independently and despite the fact I'm directly accessing the framebuffer ?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7527
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Possible to measure time of HDMI output generation based on framebuffer copy?

Wed Nov 20, 2019 12:26 pm

You haven't made any Dispmanx updates, therefore you won't get callbacks for them.

https://github.com/raspberrypi/userland ... anx.h#L136

Code: Select all

vc_dispmanx_vsync_callback( DISPMANX_DISPLAY_HANDLE_T display, DISPMANX_CALLBACK_FUNC_T cb_func, void *cb_arg );
The vsync callbacks do have an update argment, but it will be 0/NULL.

Code: Select all

void vsync_func(DISPMANX_UPDATE_HANDLE_T u, void * arg)
{
  //u will be 0/NULL for vsync callbacks
  //use arg if you want to pass state around nicely.
  printf("VSYNC!\n");
  start_time = time_now();
}

int main()
{
  DISPMANX_DISPLAY_HANDLE_T *display;
  display = vc_dispmanx_display_open(DISPMANX_ID_HDMI0);
  vc_dispmanx_vsync_callback(display, vsync_func, cookie);
  

}
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

elasticman
Posts: 4
Joined: Wed Nov 20, 2019 9:40 am

Re: Possible to measure time of HDMI output generation based on framebuffer copy?

Fri Nov 22, 2019 11:34 am

Do you have an educated guess about the time spans we're probably talking for outputting the framebuffer's content to the HDMI cable? Are we talking about < 1ms or in the higher millisecs range?

If it's less than a millisecond, it's probably not worth the struggle to measure.

Return to “Graphics programming”