1/*
2 * Copyright © 2000, 2008 Keith Packard
3 *             2004 Eric Anholt
4 *             2005 Zack Rusin
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Copyright holders make no
13 * representations about the suitability of this software for any purpose.  It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
21 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
22 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25
26/** @file
27 * UXA - the unified memory acceleration architecture.
28 *
29 * This is the header containing the public API of UXA for uxa drivers.
30 */
31
32#ifndef UXA_H
33#define UXA_H
34
35#include "scrnintstr.h"
36#include "pixmapstr.h"
37#include "windowstr.h"
38#include "gcstruct.h"
39#include "picturestr.h"
40#include "fb.h"
41
42#define UXA_VERSION_MAJOR   1
43#define UXA_VERSION_MINOR   0
44#define UXA_VERSION_RELEASE 0
45
46typedef enum {
47	UXA_ACCESS_RO,
48	UXA_ACCESS_RW
49} uxa_access_t;
50
51/**
52 * The UxaDriver structure is allocated through uxa_driver_alloc(), and then
53 * fllled in by drivers.
54 */
55typedef struct _UxaDriver {
56    /**
57     * uxa_major and uxa_minor should be set by the driver to the version of
58     * UXA which the driver was compiled for (or configures itself at runtime
59     * to support).  This allows UXA to extend the structure for new features
60     * without breaking ABI for drivers compiled against older versions.
61     */
62    int uxa_major, uxa_minor;
63
64    /**
65     * The flags field is bitfield of boolean values controlling UXA's behavior.
66     *
67     * The flags include UXA_TWO_BITBLT_DIRECTIONS.
68     */
69    int flags;
70
71    /** @name solid
72     * @{
73     */
74    /**
75     * prepare_solid() sets up the driver for doing a solid fill.
76     * @param pPixmap Destination pixmap
77     * @param alu raster operation
78     * @param planemask write mask for the fill
79     * @param fg "foreground" color for the fill
80     *
81     * This call should set up the driver for doing a series of solid fills
82     * through the solid() call.  The alu raster op is one of the GX*
83     * graphics functions listed in X.h, and typically maps to a similar
84     * single-byte "ROP" setting in all hardware.  The planemask controls
85     * which bits of the destination should be affected, and will only represent
86     * the bits up to the depth of pPixmap.  The fg is the pixel value of the
87     * foreground color referred to in ROP descriptions.
88     *
89     * Note that many drivers will need to store some of the data in the driver
90     * private record, for sending to the hardware with each drawing command.
91     *
92     * The prepare_solid() call is required of all drivers, but it may fail for any
93     * reason.  Failure results in a fallback to software rendering.
94     */
95    Bool        (*prepare_solid) (PixmapPtr      pPixmap,
96                                 int            alu,
97                                 Pixel          planemask,
98                                 Pixel          fg);
99
100    /**
101     * solid() performs a solid fill set up in the last prepare_solid() call.
102     *
103     * @param pPixmap destination pixmap
104     * @param x1 left coordinate
105     * @param y1 top coordinate
106     * @param x2 right coordinate
107     * @param y2 bottom coordinate
108     *
109     * Performs the fill set up by the last prepare_solid() call, covering the
110     * area from (x1,y1) to (x2,y2) in pPixmap.  Note that the coordinates are
111     * in the coordinate space of the destination pixmap, so the driver will
112     * need to set up the hardware's offset and pitch for the destination
113     * coordinates according to the pixmap's offset and pitch within
114     * framebuffer.
115     *
116     * This call is required if prepare_solid() ever succeeds.
117     */
118    void        (*solid) (PixmapPtr      pPixmap, int x1, int y1, int x2, int y2);
119
120    /**
121     * done_solid() finishes a set of solid fills.
122     *
123     * @param pPixmap destination pixmap.
124     *
125     * The done_solid() call is called at the end of a series of consecutive
126     * solid() calls following a successful prepare_solid().  This allows drivers
127     * to finish up emitting drawing commands that were buffered, or clean up
128     * state from prepare_solid().
129     *
130     * This call is required if prepare_solid() ever succeeds.
131     */
132    void        (*done_solid) (PixmapPtr      pPixmap);
133    /** @} */
134
135    /** @name copy
136     * @{
137     */
138    /**
139     * prepare_copy() sets up the driver for doing a copy within video
140     * memory.
141     *
142     * @param pSrcPixmap source pixmap
143     * @param pDstPixmap destination pixmap
144     * @param dx X copy direction
145     * @param dy Y copy direction
146     * @param alu raster operation
147     * @param planemask write mask for the fill
148     *
149     * This call should set up the driver for doing a series of copies from the
150     * the pSrcPixmap to the pDstPixmap.  The dx flag will be positive if the
151     * hardware should do the copy from the left to the right, and dy will be
152     * positive if the copy should be done from the top to the bottom.  This
153     * is to deal with self-overlapping copies when pSrcPixmap == pDstPixmap.
154     * If your hardware can only support blits that are (left to right, top to
155     * bottom) or (right to left, bottom to top), then you should set
156     * #UXA_TWO_BITBLT_DIRECTIONS, and UXA will break down copy operations to
157     * ones that meet those requirements.  The alu raster op is one of the GX*
158     * graphics functions listed in X.h, and typically maps to a similar
159     * single-byte "ROP" setting in all hardware.  The planemask controls which
160     * bits of the destination should be affected, and will only represent the
161     * bits up to the depth of pPixmap.
162     *
163     * Note that many drivers will need to store some of the data in the driver
164     * private record, for sending to the hardware with each drawing command.
165     *
166     * The prepare_copy() call is required of all drivers, but it may fail for any
167     * reason.  Failure results in a fallback to software rendering.
168     */
169    Bool        (*prepare_copy) (PixmapPtr       pSrcPixmap,
170                                PixmapPtr       pDstPixmap,
171                                int             dx,
172                                int             dy,
173                                int             alu,
174                                Pixel           planemask);
175
176    /**
177     * copy() performs a copy set up in the last prepare_copy call.
178     *
179     * @param pDstPixmap destination pixmap
180     * @param srcX source X coordinate
181     * @param srcY source Y coordinate
182     * @param dstX destination X coordinate
183     * @param dstY destination Y coordinate
184     * @param width width of the rectangle to be copied
185     * @param height height of the rectangle to be copied.
186     *
187     * Performs the copy set up by the last prepare_copy() call, copying the
188     * rectangle from (srcX, srcY) to (srcX + width, srcY + width) in the source
189     * pixmap to the same-sized rectangle at (dstX, dstY) in the destination
190     * pixmap.  Those rectangles may overlap in memory, if
191     * pSrcPixmap == pDstPixmap.  Note that this call does not receive the
192     * pSrcPixmap as an argument -- if it's needed in this function, it should
193     * be stored in the driver private during prepare_copy().  As with solid(),
194     * the coordinates are in the coordinate space of each pixmap, so the driver
195     * will need to set up source and destination pitches and offsets from those
196     * pixmaps, probably using uxaGetPixmapOffset() and uxa_get_pixmap_pitch().
197     *
198     * This call is required if prepare_copy ever succeeds.
199     */
200    void        (*copy) (PixmapPtr       pDstPixmap,
201                         int    srcX,
202                         int    srcY,
203                         int    dstX,
204                         int    dstY,
205                         int    width,
206                         int    height);
207
208    /**
209     * done_copy() finishes a set of copies.
210     *
211     * @param pPixmap destination pixmap.
212     *
213     * The done_copy() call is called at the end of a series of consecutive
214     * copy() calls following a successful prepare_copy().  This allows drivers
215     * to finish up emitting drawing commands that were buffered, or clean up
216     * state from prepare_copy().
217     *
218     * This call is required if prepare_copy() ever succeeds.
219     */
220    void        (*done_copy) (PixmapPtr       pDstPixmap);
221    /** @} */
222
223    /** @name composite
224     * @{
225     */
226    /**
227     * check_composite() checks to see if a composite operation could be
228     * accelerated.
229     *
230     * @param op Render operation
231     * @param pSrcPicture source Picture
232     * @param pMaskPicture mask picture
233     * @param pDstPicture destination Picture
234     *
235     * The check_composite() call checks if the driver could handle acceleration
236     * of op with the given source, mask, and destination pictures.  This allows
237     * drivers to check source and destination formats, supported operations,
238     * transformations, and component alpha state, and send operations it can't
239     * support to software rendering early on.
240     *
241     * See prepare_composite() for more details on likely issues that drivers
242     * will have in accelerating composite operations.
243     *
244     * The check_composite() call is recommended if prepare_composite() is
245     * implemented, but is not required.
246     */
247    Bool        (*check_composite) (int          op,
248                                   PicturePtr   pSrcPicture,
249                                   PicturePtr   pMaskPicture,
250                                   PicturePtr   pDstPicture);
251
252    /**
253     * prepare_composite() sets up the driver for doing a composite operation
254     * described in the Render extension protocol spec.
255     *
256     * @param op Render operation
257     * @param pSrcPicture source Picture
258     * @param pMaskPicture mask picture
259     * @param pDstPicture destination Picture
260     * @param pSrc source pixmap
261     * @param pMask mask pixmap
262     * @param pDst destination pixmap
263     *
264     * This call should set up the driver for doing a series of composite
265     * operations, as described in the Render protocol spec, with the given
266     * pSrcPicture, pMaskPicture, and pDstPicture.  The pSrc, pMask, and
267     * pDst are the pixmaps containing the pixel data, and should be used for
268     * setting the offset and pitch used for the coordinate spaces for each of
269     * the Pictures.
270     *
271     * Notes on interpreting Picture structures:
272     * - The Picture structures will always have a valid pDrawable.
273     * - The Picture structures will never have alphaMap set.
274     * - The mask Picture (and therefore pMask) may be NULL, in which case the
275     *   operation is simply src OP dst instead of src IN mask OP dst, and
276     *   mask coordinates should be ignored.
277     * - pMarkPicture may have componentAlpha set, which greatly changes
278     *   the behavior of the composite operation.  componentAlpha has no effect
279     *   when set on pSrcPicture or pDstPicture.
280     * - The source and mask Pictures may have a transformation set
281     *   (Picture->transform != NULL), which means that the source coordinates
282     *   should be transformed by that transformation, resulting in scaling,
283     *   rotation, etc.  The PictureTransformPoint() call can transform
284     *   coordinates for you.  Transforms have no effect on Pictures when used
285     *   as a destination.
286     * - The source and mask pictures may have a filter set.  PictFilterNearest
287     *   and PictFilterBilinear are defined in the Render protocol, but others
288     *   may be encountered, and must be handled correctly (usually by
289     *   prepare_composite failing, and falling back to software).  Filters have
290     *   no effect on Pictures when used as a destination.
291     * - The source and mask Pictures may have repeating set, which must be
292     *   respected.  Many chipsets will be unable to support repeating on
293     *   pixmaps that have a width or height that is not a power of two.
294     *
295     * If your hardware can't support source pictures (textures) with
296     * non-power-of-two pitches, you should set #UXA_OFFSCREEN_ALIGN_POT.
297     *
298     * Note that many drivers will need to store some of the data in the driver
299     * private record, for sending to the hardware with each drawing command.
300     *
301     * The prepare_composite() call is not required.  However, it is highly
302     * recommended for performance of antialiased font rendering and performance
303     * of cairo applications.  Failure results in a fallback to software
304     * rendering.
305     */
306    Bool        (*prepare_composite) (int                op,
307                                     PicturePtr         pSrcPicture,
308                                     PicturePtr         pMaskPicture,
309                                     PicturePtr         pDstPicture,
310                                     PixmapPtr          pSrc,
311                                     PixmapPtr          pMask,
312                                     PixmapPtr          pDst);
313
314    /**
315     * composite() performs a composite operation set up in the last
316     * prepare_composite() call.
317     *
318     * @param pDstPixmap destination pixmap
319     * @param srcX source X coordinate
320     * @param srcY source Y coordinate
321     * @param maskX source X coordinate
322     * @param maskY source Y coordinate
323     * @param dstX destination X coordinate
324     * @param dstY destination Y coordinate
325     * @param width destination rectangle width
326     * @param height destination rectangle height
327     *
328     * Performs the composite operation set up by the last prepare_composite()
329     * call, to the rectangle from (dstX, dstY) to (dstX + width, dstY + height)
330     * in the destination Pixmap.  Note that if a transformation was set on
331     * the source or mask Pictures, the source rectangles may not be the same
332     * size as the destination rectangles and filtering.  Getting the coordinate
333     * transformation right at the subpixel level can be tricky, and rendercheck
334     * can test this for you.
335     *
336     * This call is required if prepare_composite() ever succeeds.
337     */
338    void        (*composite) (PixmapPtr         pDst,
339                              int       srcX,
340                              int        srcY,
341                              int        maskX,
342                              int        maskY,
343                              int        dstX,
344                              int        dstY,
345                              int        width,
346                              int        height);
347
348    /**
349     * done_composite() finishes a set of composite operations.
350     *
351     * @param pPixmap destination pixmap.
352     *
353     * The done_composite() call is called at the end of a series of consecutive
354     * composite() calls following a successful prepare_composite().  This allows
355     * drivers to finish up emitting drawing commands that were buffered, or
356     * clean up state from prepare_composite().
357     *
358     * This call is required if prepare_composite() ever succeeds.
359     */
360    void        (*done_composite) (PixmapPtr         pDst);
361    /** @} */
362
363    /**
364     * put_image() loads a rectangle of data from src into pDst.
365     *
366     * @param pDst destination pixmap
367     * @param x destination X coordinate.
368     * @param y destination Y coordinate
369     * @param width width of the rectangle to be copied
370     * @param height height of the rectangle to be copied
371     * @param src pointer to the beginning of the source data
372     * @param src_pitch pitch (in bytes) of the lines of source data.
373     *
374     * put_image() copies data in system memory beginning at src (with
375     * pitch src_pitch) into the destination pixmap from (x, y) to
376     * (x + width, y + height).  This is typically done with hostdata uploads,
377     * where the CPU sets up a blit command on the hardware with instructions
378     * that the blit data will be fed through some sort of aperture on the card.
379     *
380     * put_image() is most important for the performance of uxa_glyphs()
381     * (antialiased font drawing) by allowing pipelining of data uploads,
382     * avoiding a sync of the card after each glyph.
383     *
384     * @return TRUE if the driver successfully uploaded the data.  FALSE
385     * indicates that UXA should fall back to doing the upload in software.
386     *
387     * put_image() is not required, but is recommended if composite
388     * acceleration is supported.
389     */
390    Bool        (*put_image) (PixmapPtr            pDst,
391				   int                  x,
392				   int                  y,
393				   int                  w,
394				   int                  h,
395                                   char                 *src,
396                                   int                  src_pitch);
397
398    /**
399     * get_image() loads a rectangle of data from pSrc into dst
400     *
401     * @param pSrc source pixmap
402     * @param x source X coordinate.
403     * @param y source Y coordinate
404     * @param width width of the rectangle to be copied
405     * @param height height of the rectangle to be copied
406     * @param dst pointer to the beginning of the destination data
407     * @param dst_pitch pitch (in bytes) of the lines of destination data.
408     *
409     * get_image() copies data from offscreen memory in pSrc from
410     * (x, y) to (x + width, y + height), to system memory starting at
411     * dst (with pitch dst_pitch).  This would usually be done
412     * using scatter-gather DMA, supported by a DRM call, or by blitting to AGP
413     * and then synchronously reading from AGP.  Because the implementation
414     * might be synchronous, UXA leaves it up to the driver to call
415     * uxa_mark_sync() if get_image() was asynchronous.  This is in
416     * contrast to most other acceleration calls in UXA.
417     *
418     * @return TRUE if the driver successfully downloaded the data.  FALSE
419     * indicates that UXA should fall back to doing the download in software.
420     *
421     * get_image() is not required, but is highly recommended.
422     */
423    Bool (*get_image)(PixmapPtr pSrc,
424                               int x,  int y,
425                               int w,  int h,
426                               char *dst,  int dst_pitch);
427
428    /** @{ */
429    /**
430     * prepare_access() is called before CPU access to an offscreen pixmap.
431     *
432     * @param pPix the pixmap being accessed
433     * @param index the index of the pixmap being accessed.
434     *
435     * prepare_access() will be called before CPU access to an offscreen pixmap.
436     * This can be used to set up hardware surfaces for byteswapping or
437     * untiling, or to adjust the pixmap's devPrivate.ptr for the purpose of
438     * making CPU access use a different aperture.
439     *
440     * The index is one of #UXA_PREPARE_DEST, #UXA_PREPARE_SRC, or
441     * #UXA_PREPARE_MASK, indicating which pixmap is in question.  Since only up
442     * to three pixmaps will have prepare_access() called on them per operation,
443     * drivers can have a small, statically-allocated space to maintain state
444     * for prepare_access() and finish_access() in.  Note that the same pixmap may
445     * have prepare_access() called on it more than once, for uxample when doing
446     * a copy within the same pixmap (so it gets prepare_access as()
447     * #UXA_PREPARE_DEST and then as #UXA_PREPARE_SRC).
448     *
449     * prepare_access() may fail.  An uxample might be the case of hardware that
450     * can set up 1 or 2 surfaces for CPU access, but not 3.  If prepare_access()
451     * fails, UXA will migrate the pixmap to system memory.
452     * get_image() must be implemented and must not fail if a driver
453     * wishes to fail in prepare_access().  prepare_access() must not fail when
454     * pPix is the visible screen, because the visible screen can not be
455     * migrated.
456     *
457     * @return TRUE if prepare_access() successfully prepared the pixmap for CPU
458     * drawing.
459     * @return FALSE if prepare_access() is unsuccessful and UXA should use
460     * get_image() to migate the pixmap out.
461     */
462    Bool	(*prepare_access)(PixmapPtr pPix, uxa_access_t access);
463
464    /**
465     * finish_access() is called after CPU access to an offscreen pixmap.
466     *
467     * @param pPix the pixmap being accessed
468     * @param index the index of the pixmap being accessed.
469     *
470     * finish_access() will be called after finishing CPU access of an offscreen
471     * pixmap set up by prepare_access().  Note that the finish_access() will not be
472     * called if prepare_access() failed.
473     */
474    void	(*finish_access)(PixmapPtr pPix);
475
476    /**
477     * PixmapIsOffscreen() is an optional driver replacement to
478     * uxa_pixmap_is_offscreen(). Set to NULL if you want the standard behaviour
479     * of uxa_pixmap_is_offscreen().
480     *
481     * @param pPix the pixmap
482     * @return TRUE if the given drawable is in framebuffer memory.
483     *
484     * uxa_pixmap_is_offscreen() is used to determine if a pixmap is in offscreen
485     * memory, meaning that acceleration could probably be done to it, and that it
486     * will need to be wrapped by prepare_access()/finish_access() when accessing it
487     * with the CPU.
488     *
489     *
490     */
491    Bool	(*pixmap_is_offscreen)(PixmapPtr pPix);
492
493    /** @} */
494} uxa_driver_t;
495
496/** @name UXA driver flags
497 * @{
498 */
499/**
500 * UXA_TWO_BITBLT_DIRECTIONS indicates to UXA that the driver can only
501 * support copies that are (left-to-right, top-to-bottom) or
502 * (right-to-left, bottom-to-top).
503 */
504#define UXA_TWO_BITBLT_DIRECTIONS	(1 << 2)
505
506/** @} */
507
508/** @name UXA CreatePixmap hint flags
509 * @{
510 */
511/**
512 * Flag to hint that the first operation on the pixmap will be a
513 * prepare_access.
514 */
515#define UXA_CREATE_PIXMAP_FOR_MAP	0x20000000
516/** @} */
517
518uxa_driver_t *
519uxa_driver_alloc(void);
520
521Bool
522uxa_driver_init(ScreenPtr screen, uxa_driver_t *uxa_driver);
523
524void
525uxa_driver_fini(ScreenPtr pScreen);
526
527CARD32
528uxa_get_pixmap_first_pixel (PixmapPtr pPixmap);
529
530void
531uxa_set_fallback_debug (ScreenPtr screen, Bool enable);
532
533/**
534 * Returns TRUE if the given planemask covers all the significant bits in the
535 * pixel values for pDrawable.
536 */
537#define UXA_PM_IS_SOLID(_pDrawable, _pm) \
538	(((_pm) & FbFullMask((_pDrawable)->depth)) == \
539	 FbFullMask((_pDrawable)->depth))
540
541#endif /* UXA_H */
542