gfx: Properly set graphics thread delay #6
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR properly sets the delay for the graphics drawing thread (task).
The original implementation is incorrect because when calculating the number of ticks to sleep the graphics task for, the task begins sleeping after some ticks which should be included in the calculated number have already passed.
For example, let
t
be the time in ms at which the frame starts to get rendered andd
be a delay (in our case, the frame duration). We expect that the next frame begin rendering att'=t+d
. However, consider processing timep
. The current implementation begins rendering the next frame att'=t+p+d
, since usingvTaskDelay
delays the current task relative to when that function is called (at timet+p
) (see FreeRTOS API documentation). This time misalignment compounds for each frame, since the computation delay occurs for each frame. So if the animation has 1000 frames and extraneous computation time takes 2 ticks (for the TidByt's hardware configuration, 1 tick = 1ms), then the animation is played for 2 seconds longer than it should, an order of magnitude (!!!) in CPU time! If the computation time takes even longer (say, 4 or 5 ticks), this results in many seconds worth of time misalignment.This fix uses
xTaskDelayUntil
, which is the recommended API call to schedule task delays that end on an absolute time. This allows us to exploit the RTOS' scheduler so that the next frame begins drawing att'=t+d
.