1 /* Copyright (C) 1998, 1999 artofcode LLC.  All rights reserved.
2 
3   This program is free software; you can redistribute it and/or modify it
4   under the terms of the GNU General Public License as published by the
5   Free Software Foundation; either version 2 of the License, or (at your
6   option) any later version.
7 
8   This program is distributed in the hope that it will be useful, but
9   WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   General Public License for more details.
12 
13   You should have received a copy of the GNU General Public License along
14   with this program; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA, 02111-1307.
16 
17 */
18 
19 /*$Id: gxpageq.h,v 1.2.6.1.2.1 2003/01/17 00:49:04 giles Exp $ */
20 /* Page queue implementation */
21 
22 /* Initial version 2/1/98 by John Desrosiers (soho@crl.com) */
23 /* 7/17/98 L. Peter Deutsch (ghost@aladdin.com) edited to conform to
24    Ghostscript coding standards */
25 /* 8/7/98 ghost@aladdin.com fixed bugs in #define st_... statements */
26 /* 11/3/98 ghost@aladdin.com further updates for coding standards,
27    moves definition of page queue structure to gxpageq.c */
28 /* 12/1/98 soho@crl.com - Upgraded gx_page_queue_action_t comments */
29 
30 #ifndef gxpageq_INCLUDED
31 # define gxpageq_INCLUDED
32 
33 #include "gsmemory.h"
34 #include "gxband.h"
35 #include "gxsync.h"
36 
37 /* --------------- Data type definitions --------------------- */
38 
39 /*	Action codes for each page queue entry. Each page that the interpreter
40 	emits to the page queue can actually be broken down into a sequence of
41 	one or more page queue entries. The general form for a given page's
42 	sequence of page queue entries can be expressed as:
43 		[PARTIAL_PAGE]... [COPY_PAGE [PARTIAL_PAGE]...]... FULL_PAGE
44 	where elements in square brackets are optional, and ellipses show
45 	repetition. NOTE that a single ACTION_TERMINATE (followed by nothing) can
46 	also show up at any point in the page queue in lieu of page descriptions.
47 
48 
49 	PARTIAL_PAGE: The interpreter emits a partial page when the bandlist is
50 	too small to contain a page's full representation. Partial pages will
51 	be emitted in out-of-memory situations *only* after the interpreter
52 	has determined that no further page queue entries are in the page
53 	queue, indicating that no further memory can be reclaimed by merely
54 	waiting for queued pages to render and free their associated bandlist.
55 
56 	Note that num_copies is undefined for partial pages: the actual
57 	number of pages to render will only be known when ...COPY_PAGE
58 	or FULL_PAGE is emitted.
59 
60 	Partial pages are never imaged.
61 
62 
63 	FULL_PAGE: The interpreter emits a full page when a page description
64 	is complete (e.g. showpage), or trashed (e.g. setpagedevice). The
65 	page's complete description consists of the FULL_PAGE plus all
66 	PARTIAL_PAGEs that immediately precede it in the page queue (and
67 	possibly preceding COPY_PAGEs) all the way back to the previous
68 	FULL_PAGE (or up to the beginning of queue entries).
69 
70 	In the case of a trashed page, the page count will be 0. The page
71 	queue may choose to not render the 0-count FULL_PAGE queue entry
72 	for efficiency. If they have not been rendered, the page queue
73 	may choose to also discard (and/or not render) any PARTIAL_PAGEs
74 	leading up to the trashed page. The page queue must however take
75 	care to not discard any entries leading up to a COPY_PAGE with
76 	a non-0 page count that may precede the FULL_PAGE, since COPY_PAGE
77 	must be rendered in that case. In any event, a 0-count page will
78 	not be imaged.
79 
80 	In the case of a complete page, the page count will be 0 or greater.
81 	The 0-count page is equivalent to a trashed page -- see above. The
82 	renderer must ensure that all PARTIAL_PAGEs and COPY_PAGEs leading
83 	up to the FULL_PAGE are rendered sequentially before rendering
84 	and imaging the FULL_PAGE.
85 
86 
87 	COPY_PAGE: is similar to FULL_PAGE above, except that COPY_PAGE must
88 	keep the rendered results, instead of clearing them. COPY_PAGE
89 	differs from a partial page in that the page must be imaged, as well
90 	as rasterized. This is to support PostScript language "copypage"
91 	semantics.
92 
93 	Note that a 0 page count here does not absolve the renderer from
94 	rendering the page queue entries (unless all subsequent COPY_PAGEs
95 	the the FULL_PAGE for this page also have a 0 page count), since
96 	the results of COPY_PAGE must be available for subsequent pages.
97 
98 
99 	TERMINATE: This entry can appear at any time in the page queue. It
100 	will be the last entry to ever appear in the queue. The semantics
101 	of this entry require all prior non-zero-count COPY_PAGEs and
102 	FULL_PAGEs to be imaged. Any trailing PARTIAL_PAGEs may optionally
103 	be rendered, but should not be imaged.
104  */
105 typedef enum {
106     GX_PAGE_QUEUE_ACTION_PARTIAL_PAGE,
107     GX_PAGE_QUEUE_ACTION_FULL_PAGE,
108     GX_PAGE_QUEUE_ACTION_COPY_PAGE,
109     GX_PAGE_QUEUE_ACTION_TERMINATE
110 } gx_page_queue_action_t;
111 
112 /*
113  * Define the structure used to manage a page queue.
114  * A page queue is a monitor-locked FIFO which holds completed command
115  * list files ready for rendering.
116  */
117 #ifndef gx_page_queue_DEFINED
118 # define gx_page_queue_DEFINED
119 typedef struct gx_page_queue_s gx_page_queue_t;
120 #endif
121 
122 /*
123  * Define a page queue entry object.
124  */
125 typedef struct gx_page_queue_entry_s gx_page_queue_entry_t;
126 struct gx_page_queue_entry_s {
127     gx_band_page_info_t page_info;
128     gx_page_queue_action_t action;	/* action code */
129     int num_copies;		/* number of copies to render, only defined */
130                                 /* if action == ...FULL_PAGE or ...COPY_PAGE */
131     gx_page_queue_entry_t *next;		/* link to next in queue */
132     gx_page_queue_t *queue;	/* link to queue the entry is in */
133 };
134 
135 #define private_st_gx_page_queue_entry()\
136   gs_private_st_ptrs2(st_gx_page_queue_entry, gx_page_queue_entry_t,\
137     "gx_page_queue_entry",\
138     gx_page_queue_entry_enum_ptrs, gx_page_queue_entry_reloc_ptrs,\
139     next, queue)
140 
141 /* -------------- Public Procedure Declaraions --------------------- */
142 
143 /* Allocate a page queue. */
144 gx_page_queue_t *gx_page_queue_alloc(P1(gs_memory_t *mem));
145 
146 /*
147  * Allocate and initialize a page queue entry.
148  * All page queue entries must be allocated by this routine.
149  */
150 /* rets ptr to allocated object, 0 if VM error */
151 gx_page_queue_entry_t *
152 gx_page_queue_entry_alloc(P1(
153     gx_page_queue_t * queue	/* queue that entry is being alloc'd for */
154     ));
155 
156 /*
157  * Free a page queue entry.
158  * All page queue entries must be destroyed by this routine.
159  */
160 void gx_page_queue_entry_free(P1(
161     gx_page_queue_entry_t * entry	/* entry to free up */
162     ));
163 
164 /*
165  * Free the page_info resources held by the pageq entry.  Used to free
166  * pages' clist, typically after rendering.  Note that this routine is NOT
167  * called implicitly by gx_page_queue_entry_free, since page clist may be
168  * managed separately from page queue entries.  However, unless you are
169  * managing clist separately, you must call this routine before freeing the
170  * pageq entry itself (via gx_page_queue_entry_free), or you will leak
171  * memory (lots).
172  */
173 void gx_page_queue_entry_free_page_info(P1(
174     gx_page_queue_entry_t * entry	/* entry to free up */
175     ));
176 
177 /*
178  * Initialize a page queue; this must be done before it can be used.
179  * This routine allocates & inits various necessary structures and will
180  * fail if insufficient memory is available.
181  */
182 /* -ve error code, or 0 */
183 int gx_page_queue_init(P2(
184     gx_page_queue_t * queue,	/* page queue to init */
185     gs_memory_t * memory	/* allocator for dynamic memory */
186     ));
187 
188 /*
189  * Destroy a page queue which was initialized by gx_page_queue_init.
190  * Any page queue entries in the queue are released and destroyed;
191  * dynamic allocations are released.
192  */
193 void gx_page_queue_dnit(P1(
194     gx_page_queue_t * queue	/* page queue to dnit */
195     ));
196 
197 /*
198  * If there are any pages in queue, wait until one of them finishes
199  * rendering.  Typically called by writer's out-of-memory error handlers
200  * that want to wait until some memory has been freed.
201  */
202 /* rets 0 if no pages were waiting for rendering, 1 if actually waited */
203 int gx_page_queue_wait_one_page(P1(
204     gx_page_queue_t * queue	/* queue to wait on */
205     ));
206 
207 /*
208  * Wait until all (if any) pages in queue have finished rendering. Typically
209  * called by writer operations which need to drain the page queue before
210  * continuing.
211  */
212 void gx_page_queue_wait_until_empty(P1(
213     gx_page_queue_t * queue		/* page queue to wait on */
214     ));
215 
216 /*
217  * Add a pageq queue entry to the end of the page queue. If an unsatisfied
218  * reader thread has an outstanding gx_page_queue_start_deque(), wake it up.
219  */
220 void gx_page_queue_enqueue(P1(
221     gx_page_queue_entry_t * entry	/* entry to add */
222     ));
223 
224 /*
225  * Allocate & construct a pageq entry, then add to the end of the pageq as
226  * in gx_page_queue_enqueue. If unable to allocate a new pageq entry, uses
227  * the pre-allocated reserve entry held in the pageq. When using the reserve
228  * pageq entry, wait until enough pages have been rendered to allocate a new
229  * reserve for next time -- this should always succeed & returns eFatal if not.
230  * Unless the reserve was used, does not wait for any rendering to complete.
231  * Typically called by writer when it has a (partial) page ready for rendering.
232  */
233 /* rets 0 ok, gs_error_Fatal if error */
234 int gx_page_queue_add_page(P4(
235     gx_page_queue_t * queue,		/* page queue to add to */
236     gx_page_queue_action_t action,		/* action code to queue */
237     const gx_band_page_info_t * page_info,	/* bandinfo incl. bandlist */
238     int page_count		/* # of copies to print if final "print," */
239 				   /* 0 if partial page, -1 if cancel */
240     ));
241 
242 /*
243  * Retrieve the least-recently added queue entry from the pageq. If no
244  * entry is available, waits on a signal from gx_page_queue_enqueue. Must
245  * eventually be followed by a call to gx_page_queue_finish_dequeue for the
246  * same pageq entry.
247  * Even though the pageq is actually removed from the pageq, a mark is made in
248  * the pageq to indicate that the pageq is not "empty" until the
249  * gx_page_queue_finish_dequeue; this is for the benefit of
250  * gx_page_queue_wait_???, since the completing the current page's rendering
251  * may free more memory.
252  * Typically called by renderer thread loop, which looks like:
253     do {
254 	gx_page_queue_start_deqeueue(...);
255 	render_retrieved_entry(...);
256 	gx_page_queue_finish_dequeue(...);
257     } while (some condition);
258  */
259 gx_page_queue_entry_t *		/* removed entry */
260 gx_page_queue_start_dequeue(P1(
261     gx_page_queue_t * queue	/* page queue to retrieve from */
262     ));
263 
264 /*
265  * Free the pageq entry and its associated band list data, then signal any
266  * waiting threads.  Typically used to indicate completion of rendering the
267  * pageq entry.  Note that this is different from gx_page_queue_entry_free,
268  * which does not free the band list data (a separate call of
269  * gx_page_queue_entry_free_page_info is required).
270  */
271 void gx_page_queue_finish_dequeue(P1(
272     gx_page_queue_entry_t * entry  /* entry that was retrieved to delete */
273     ));
274 
275 #endif /*!defined(gxpageq_INCLUDED) */
276