Filter Functions - Getting years of development in cups-filters into Printer Applications
TL;DR: Another step towards cups-filters 2.0: Filter functions
Hi,
we are currently in the middle of the development of the new concept for printer and scanner drivers, the Printer Applications. Thanks to Michael Sweet for introducing this concept and for creating the base library for Printer Applications, PAPPL:
https://github.com/michaelrsweet/pappl/
Also thanks for all the contributors, especially our GSoC students.
A first example of a PAPPL-based Printer Application got posted:
https://github.com/michaelrsweet/hp-printer-app/
Looking into it I have started thinking about how to create Printer Applications to port classic printer drivers (consisting of filters and PPD files) and PostScript PPDs (special case of classic printer drivers) into Printer Applications.
See also the former threads here:
https://lists.linuxfoundation.org/pipermail/printing-architecture/2020/thread.html
Printer Applications: Retro-fitting classic drivers:
https://lists.linuxfoundation.org/pipermail/printing-architecture/2020/003823.html
PostScript printers without PPD files?:
https://lists.linuxfoundation.org/pipermail/printing-architecture/2020/003826.html
Canceling jobs in Printer Applications:
https://lists.linuxfoundation.org/pipermail/printing-architecture/2020/003831.html
The Printer Application for PostScript printers has to output PostScript, with code snippets from the PPD file embedded and the PostScript generated primarily from PDF as applications usually send jobs in PDF and going through an interim Raster state would make everything very awkward and also cause problems on older PostScript printers.
So the Printer Application has to accept PDF as main input format and turn it into PostScript, then embed the code snippets from the PPD.
On this process we have worked and optimized for years, in the cups-filters project, especially flatten filled in forms and annotations in PDF input files, work around many PostScript interpreter bugs in printers, assure correct page geometry for the output, printing only selected pages, reverse order, number-up, ... So the best is to use the filters of cups-filters or at least their code also in Printer Applications.
But Printer Applications have a different filter concept than CUPS, having one filter turning the input data in printer-ready output, this done by a function in the code of the Printer Application, in contrast to CUPS which calculates a chain of filters needed for the job and calls these as external executables.
Therefore I have started working on a new filter concept for cups-filters 2.0 (coming soon), the filter functions. The plan is to turn every filter into a filter function, available in libcupsfilters. The functions have a common interface and a common data structure to supply job and printer properties to them. Additional functions will be created to call a given chain of filter functions or to find out a suitable filter chain for a given job.
To continue support for CUPS wrappers will be supplied which allow the call of each filter by the usual external executable in /usr/lib/cups/filters/.
You find a working example in the GitHub of cups-filters;
https://github.com/OpenPrinting/cups-filters/
The filter functions reside in the libcupsfilters library. All definitions regarding them you see in the cupsfilters/filter.h file:
https://github.com/OpenPrinting/cups-filters/blob/master/cupsfilters/filter.h
The filter functions have the following properties:
- They take an input stream and produce an output stream, specified by file descriptors
- They take all printer and job properties in a data structure (filter_data_t), in case of chaining filters each filter is supposed to get the same structure
- Job and printer properties can be supplied as IPP attributes but also with option lists and PPDs (for retro-fits), dpending on what is more suitable
- They are all called with the same scheme (filter_function_t), so one can simply put together chains of these functions by supplying an array fo filter functions to a filter chain execution function (not yet implemented).
- Logging is done through a callback function (filter_logfunc_t), so it can be easily adapted to the environment.
As a working example and as part for a PostScript printer support Printer Application I have converted the pstops filter of CUPS into the filter function pstops(). See cupsfilters/filter.h, cupsfilters/pstops.c, an cupsfilters/filter.c:
https://github.com/OpenPrinting/cups-filters/blob/master/cupsfilters/filter.h
https://github.com/OpenPrinting/cups-filters/blob/master/cupsfilters/filter.c
https://github.com/OpenPrinting/cups-filters/blob/master/cupsfilters/pstops.c
The behavior of the function is exactly the same as the original filter, I only changed its interfaces for input, output, and logging.
I also created a logging function for CUPS logging (cups_logfunc()), so that if the filter function is used for a CUPS filter one easily gets the logging needed by CUPS,
Making a CUPS filter from a single filter function is easy. See the wrapper which I have made for pstops(), filter/pstops.c:
https://github.com/OpenPrinting/cups-filters/blob/master/filter/pstops.c
Note that this filter does not get compiled and installed by default, so that the standard installation of cups-filters does not clash with CUPS. To try it out you need to run
./configure --enable-pstops
make
Note that this pstops filter needs the current libcupsfilters, as its "dirty work" happens there.
I have also modified the pdftops filter which used to call pstops of CUPS as an external executable, Now it calls the pstops() filter function instead, which made handling of options much easier.
My plans are to get all the filters of cups-filters turned into filter functions, to make cups-filters an easy-to-use filter repository also for Printer Applications.
Non-PPD-only filter functions (probably all except pstops and foomatic-rip) should also work with only IPP-based job and printer properties.
In addition I want to add more conditional compiling and ./configure options to make special lightweight builds of cups-filters for Printer Application Snaps, for example leaving out all PPD-dependent (which uses libppd), all QPDF-dependent, Raster-only Printer Application, ... and naturally also leaving out cups-browsed.
Any contributions are highly welcome.
Till
Hi Till and Mike,
thank you for your work on printer applications!
I have concerns about printing performance with filter functions instead of the current filtering, which uses separate executables.
Since PWG 2018 in Sunnyvale IIUC the idea for printer applications is to provide IPP everywhere support for devices which don't support IPP Everywhere by default and users will not need to buy a new device just because PPD support will be removed.
I would say most users of printer applications will be people/groups, which don't want to or cannot (due high expense of replacing hundreds of devices) replace their devices, so they will print via printer applications.
Then groups like printing enterprises usually print many jobs at the time, so any delay due waiting for output of filtering function can have bigger impact on printing process.
Isn't it possible to stay with external executables in printer applications? Or the performance will be even better than the current filtering?
Thank you in advance and have a nice day,
Zdenek
Quoted 166 lines · Till Kamppeter
TL;DR: Another step towards cups-filters 2.0: Filter functions
Hi,
we are currently in the middle of the development of the new concept for printer and scanner drivers, the Printer Applications. Thanks to Michael Sweet for introducing this concept and for creating the base library for Printer Applications, PAPPL:
https://github.com/michaelrsweet/pappl/
Also thanks for all the contributors, especially our GSoC students.
A first example of a PAPPL-based Printer Application got posted:
https://github.com/michaelrsweet/hp-printer-app/
Looking into it I have started thinking about how to create Printer Applications to port classic printer drivers (consisting of filters and PPD files) and PostScript PPDs (special case of classic printer drivers) into Printer Applications.
See also the former threads here:
https://lists.linuxfoundation.org/pipermail/printing-architecture/2020/thread.html
Printer Applications: Retro-fitting classic drivers:
https://lists.linuxfoundation.org/pipermail/printing-architecture/2020/003823.htmlPostScript printers without PPD files?:
https://lists.linuxfoundation.org/pipermail/printing-architecture/2020/003826.htmlCanceling jobs in Printer Applications:
https://lists.linuxfoundation.org/pipermail/printing-architecture/2020/003831.htmlThe Printer Application for PostScript printers has to output PostScript, with code snippets from the PPD file embedded and the PostScript generated primarily from PDF as applications usually send jobs in PDF and going through an interim Raster state would make everything very awkward and also cause problems on older PostScript printers.
So the Printer Application has to accept PDF as main input format and turn it into PostScript, then embed the code snippets from the PPD.
On this process we have worked and optimized for years, in the cups-filters project, especially flatten filled in forms and annotations in PDF input files, work around many PostScript interpreter bugs in printers, assure correct page geometry for the output, printing only selected pages, reverse order, number-up, ... So the best is to use the filters of cups-filters or at least their code also in Printer Applications.
But Printer Applications have a different filter concept than CUPS, having one filter turning the input data in printer-ready output, this done by a function in the code of the Printer Application, in contrast to CUPS which calculates a chain of filters needed for the job and calls these as external executables.
Therefore I have started working on a new filter concept for cups-filters 2.0 (coming soon), the filter functions. The plan is to turn every filter into a filter function, available in libcupsfilters. The functions have a common interface and a common data structure to supply job and printer properties to them. Additional functions will be created to call a given chain of filter functions or to find out a suitable filter chain for a given job.
To continue support for CUPS wrappers will be supplied which allow the call of each filter by the usual external executable in
/usr/lib/cups/filters/.You find a working example in the GitHub of cups-filters;
https://github.com/OpenPrinting/cups-filters/
The filter functions reside in the libcupsfilters library. All definitions regarding them you see in the
cupsfilters/filter.hfile:https://github.com/OpenPrinting/cups-filters/blob/master/cupsfilters/filter.h
The filter functions have the following properties:
- They take an input stream and produce an output stream, specified by file descriptors
- They take all printer and job properties in a data structure (
filter_data_t), in case of chaining filters each filter is supposed to get the same structure- Job and printer properties can be supplied as IPP attributes but also with option lists and PPDs (for retro-fits), dpending on what is more suitable
- They are all called with the same scheme (
filter_function_t), so one can simply put together chains of these functions by supplying an array fo filter functions to a filter chain execution function (not yet implemented).- Logging is done through a callback function (
filter_logfunc_t), so it can be easily adapted to the environment.As a working example and as part for a PostScript printer support Printer Application I have converted the pstops filter of CUPS into the filter function
pstops(). Seecupsfilters/filter.h,cupsfilters/pstops.c, ancupsfilters/filter.c:https://github.com/OpenPrinting/cups-filters/blob/master/cupsfilters/filter.h
https://github.com/OpenPrinting/cups-filters/blob/master/cupsfilters/filter.c
https://github.com/OpenPrinting/cups-filters/blob/master/cupsfilters/pstops.c
The behavior of the function is exactly the same as the original filter, I only changed its interfaces for input, output, and logging.
I also created a logging function for CUPS logging (
cups_logfunc()), so that if the filter function is used for a CUPS filter one easily gets the logging needed by CUPS,Making a CUPS filter from a single filter function is easy. See the wrapper which I have made for
pstops(),filter/pstops.c:https://github.com/OpenPrinting/cups-filters/blob/master/filter/pstops.c
Note that this filter does not get compiled and installed by default, so that the standard installation of cups-filters does not clash with CUPS. To try it out you need to run
./configure --enable-pstops
makeNote that this pstops filter needs the current libcupsfilters, as its "dirty work" happens there.
I have also modified the pdftops filter which used to call pstops of CUPS as an external executable, Now it calls the
pstops()filter function instead, which made handling of options much easier.My plans are to get all the filters of cups-filters turned into filter functions, to make cups-filters an easy-to-use filter repository also for Printer Applications.
Non-PPD-only filter functions (probably all except pstops and foomatic-rip) should also work with only IPP-based job and printer properties.
In addition I want to add more conditional compiling and ./configure options to make special lightweight builds of cups-filters for Printer Application Snaps, for example leaving out all PPD-dependent (which uses libppd), all QPDF-dependent, Raster-only Printer Application, ... and naturally also leaving out cups-browsed.
Any contributions are highly welcome.
Till
_______________________________________________
Printing-architecture mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/printing-architecture
Signature · 3 lines
Zdenek Dohnal Software Engineer Red Hat Czech - Brno TPB-C
Quoted 7 lines · Zdenek Dohnal
Hi Till and Mike,
thank you for your work on printer applications!
I have concerns about printing performance with filter functions instead of the current filtering, which uses separate executables.
[...]
Quoted 4 lines
Isn't it possible to stay with external executables in printer applications? Or the performance will be even better than the current filtering?
You probably mean that filters as external executable have parallelized, so that while the first filter, pdftopdf for flattening the filled forms and applying N-up, is still working, the second filter, pdftops already starts turning the PDF into PostScript, and the third filter, pstops (in current cups-filters started by pdftops) already starts inserting option PostScript code from the PPD as soon as the first PostScript bits come out of Ghostscript.
In the filter function scenario this parallelization will still take place. a main process will fork a sub-process for each individual filter and these processes are piped together to get one filter's output to the next filter's input. So the individual filters run in separate processes on separate CPU cores in parallel.
What is optimized or improved is that the filters and the caller are now the same executable. Options do not need to be converted into one long string (5th command line argument) or into environment variables and by the sub process back into the needed internal data structures. CUPS option lists and even IPP job and printer attributes structures can get passed on to the filter function directly. Also a PPD file (in case of retro-fitting Printer Applications) does not need to be loaded from the disk by each individual filter and parsed into the CUPS PPD data structure, but instead, the data structure is generated once by the caller (it has to do it anyway to generate the get-printer-attributes IPP response), and directly passed on to the filters. Also any overhead of launching an external program with its command line is eliminated.
In addition, the filter functions are also more easy to use for scanning, in Scanner Applications.
Even CUPS performance can be improved: Instead of having a separate wrapper executable for each filter function, making cups-filters looking exactly the same as before for CUPS, one can create a universal filter executable which takes the input and output data from the environment (supplied by CUPS), calculates the chain of filter functions and calls them in forked processes, again loading and parsing PPD and options only once. Assuming that CUPS only handles the formats for driverless printing CUPS will work with this one single filter and a single MIME conversion rule for all supported input and output formats.
Till
The conversion of the CUPS filters into filter functions is on the way. I have converted pstops and pdftops, Vikrant is currently converting his pclmtoraster filter and Jai is working on rastertops.
To make sure that filter functions work as intended I want to post here some guidelines for contributors.
Please keep the following points in mind when converting:
1. The "filter_data_t *data" argument of a filter function provides info about the printer, the job, and the logging function. It does not provide info about how an individual filter is used. If chaining filters for one job, each filter gets a pointer into the same data record here.
2. The "void *parameters" argument is individual to each filter call and it is not pre-structured by the API. One can put here what is useful for each filter, and in a typical job it can happen that one filter function is called twice in the chain, but with different parameters settings. As an example I plan to replace the gstoraster, gstopxl, and gstopdf CUPS filters by one ghostscript() filter function which takes the output format (Raster, PCL-XL, PDF) as parameter.
3. Filter functions should never output to stdout or stderr in hard-coded way and not take data from stdin in hard-coded way. They always have to take the job data from their inputfd and put out the resulting data to their outputfd, actual streaming is preferred to boost performance and parallelization of chained filters.
4. Logging should never go right to stderr but always into the logging function. Control terms (like "PAGE: 1 1") should not get removed and they should also go to the logging function, but with the FILTER_LOGLEVEL_CONTROL log level, so that the filter can be retro-fitted to CUPS. Actual log meesages for CUPS need to get their prefix removed and sent to the log function with a log level equivalent to the removed prefix. See the cups_logfunc() function in cupsfilters/filter.c
5. After the filter's files are moved from filter/ into the cupsfilters/ directory and converted, in their old place in filter/ a new file with filter's name is put but containing the minimum code for calling the filter function as CUPS filter, see filter/pstops.c and filter/pdftops.c.
6. For PPD handling, do not use libcups as the PPD handling functions in libcups will go away in a later CUPS version. Use libppd of cups-filters, with "#include <ppd/ppd.h>". I have also moved the functions of libcups' ppd-private.h into the public API.
7. A Printer Application linking libcupsfilters can run several jobs in parallel, as separate threads or forked sub processes. This means that the filter functions must work in parallel, being thread-safe. So please check whether this is OK with global variables and/or namespace {...} which you are using.
The planned filter chaining filter function will take as its parameters an array of a structure where each element contains a filter function plus the parameters for the filter function's call in this place. The filter's data record will be the same for the chaining filter function and all filter functions called by it. The chaining function will pass the job data through all the filter functions listed in the array, in order, and output the result.
I have also already planned a function with which taking option settings from job/printer IPP attributes can easily get done and which can be easily added to already converted filters, so do not worry for now when your conversion is not using the supplied IPP attributes.
I will soon post when new resources/tools/functions for filter functions are implemented.
Till
Quoted 1 line · Till Kamppeter
TL;DR: Another step towards cups-filters 2.0: Filter functions
[...]
Quoted 1 line
Therefore I have started working on a new filter concept for cups-filters 2.0 (coming soon), the filter functions. The plan is to turn every filter into a filter function, available in libcupsfilters. The functions have a common interface and a common data structure to supply job and printer properties to them. Additional functions will be created to call a given chain of filter functions or to find out a suitable filter chain for a given job.
[...]