##
Preparation phase

As in any realtime plugin, the process_buffer() method in Blend Algebra/Blend Program gets a set of frames from the tracks the plugin is attached to. Then the following events take place. It is checked if the configuration (the parameters) got changed. There is one parameter which requires special treatment, the user function name.

In order to prevent resource consuming recompilation of the functions on each new frame, the plugin maintains the successfully compiled and attached functions in cache. If at some keyframe the function name gets changed, the plugin searches if this function is already known and cached. In addition to important function related objects such as entry points, there is a timestamp representing when this function was last checked to be up to date.

If the current function name is empty, it means a function is not used. Nothing else has to be done; all tracks are fetched and passed along in the processing pipeline unchanged. If the function is not empty and seen for the first time, or its timestamp is older than the global timestamp, it is checked as follows.

1. File lock is placed on the function source file to prevent concurrent modification of object files in case of several simultaneous compilations.
2. Compilation script BlendAlgebraCompile.pl/BlendProgramCompile.pl is started. The script checks if the resulting shared object file exists and is newer than the source and recompiles it if not.
3. The plugin checks if the shared object timestamp became newer than the timestamp of this function in cache (if any). If the cached version of the function in memory is up to date, it stays there. If not, the outdated function is detached from the plugin, the updated one is reattached, and its entry points are fetched and put into cache. The function's timestamp in cache is set to the current time since the function has just been checked.

While recompiling or dynamic linking, various things may go wrong.

1. If in the unlikely scenario where the given function file does not exist, the program does nothing, same as for an empty function. No error message is shown in this case in order to prevent a possible deadlock.
2. If recompilation was unsuccessful because of a syntax error, the error message is shown. More detailed diagnostics from the compiler can be seen in the terminal window in which CINELERRA-GG was started.
3. If compilation succeeded, but dynamic linking did not, the error message is shown. In case of any error, the failed function is marked with the current timestamp in cache so that the error messages appear only once before the global timestamp gets updated.

Updating global timestamp forces all cached functions to be checked for recompilation when first accessed. The global timestamp is updated when the following events occur: changing function name, pressing Edit... or Refresh button, or exiting the Attach... dialog with the OK button.

If the current active function is up to date and attached, the plugin fetches video frames from all of the affected tracks with the important parameters like frame width and height. Then the INIT phase of the function is executed (once for each frame). Several important parameters are requested as defined by the function. They are:

- Working color space needed inside the function. If it is not the same as the color space of the project, then color space conversions have to be done.
- The required number of tracks the function works on. If less than the required number of tracks is available, an error message is shown and the function is not executed.
- Whether the function supports parallelizing or not. If the function does not claim parallelizing support, it will be executed sequentially even if the PARALLE_REQUEST checkbox is ON in the plugin GUI.