Merge branch 'devel/upgrade' into tizen
[platform/upstream/libXrender.git] / src / Filter.c
1 /*
2  *
3  * Copyright © 2002 Keith Packard
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of Keith Packard not be used in
10  * advertising or publicity pertaining to distribution of the software without
11  * specific, written prior permission.  Keith Packard makes no
12  * representations about the suitability of this software for any purpose.  It
13  * is provided "as is" without express or implied warranty.
14  *
15  * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17  * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21  * PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include "Xrenderint.h"
28 #include <limits.h>
29
30 XFilters *
31 XRenderQueryFilters (Display *dpy, Drawable drawable)
32 {
33     XRenderExtDisplayInfo               *info = XRenderFindDisplay (dpy);
34     XRenderInfo                 *xri;
35     xRenderQueryFiltersReq      *req;
36     xRenderQueryFiltersReply    rep;
37     XFilters                    *filters;
38     char                        *name;
39     char                        len;
40     int                         i;
41     unsigned long               nbytes, nbytesAlias, nbytesName;
42
43     if (!RenderHasExtension (info))
44         return NULL;
45
46     if (!XRenderQueryFormats (dpy))
47         return NULL;
48
49     xri = info->info;
50     if (xri->minor_version < 6)
51         return NULL;
52
53     LockDisplay (dpy);
54     GetReq (RenderQueryFilters, req);
55     req->reqType = info->codes->major_opcode;
56     req->renderReqType = X_RenderQueryFilters;
57     req->drawable = drawable;
58     if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
59     {
60         UnlockDisplay (dpy);
61         SyncHandle ();
62         return NULL;
63     }
64
65     /*
66      * Limit each component of combined size to 1/4 the max, which is far
67      * more than they should ever possibly need.
68      */
69     if ((rep.length < (INT_MAX >> 2)) &&
70         (rep.numFilters < ((INT_MAX / 4) / sizeof (char *))) &&
71         (rep.numAliases < ((INT_MAX / 4) / sizeof (short)))) {
72         /*
73          * Compute total number of bytes for filter names
74          */
75         nbytes = (unsigned long)rep.length << 2;
76         nbytesAlias = rep.numAliases * 2;
77         if (rep.numAliases & 1)
78             nbytesAlias += 2;
79         nbytesName = nbytes - nbytesAlias;
80
81         /*
82          * Allocate one giant block for the whole data structure
83          */
84         filters = Xmalloc (sizeof (XFilters) +
85                            (rep.numFilters * sizeof (char *)) +
86                            (rep.numAliases * sizeof (short)) +
87                            nbytesName);
88     } else
89         filters = NULL;
90
91     if (!filters)
92     {
93         _XEatDataWords(dpy, rep.length);
94         UnlockDisplay (dpy);
95         SyncHandle ();
96         return NULL;
97     }
98
99     /*
100      * Layout:
101      *  XFilters
102      *  numFilters  char * pointers to filter names
103      *  numAliases  short alias values
104      *  nbytesName  char strings
105      */
106
107     filters->nfilter = rep.numFilters;
108     filters->nalias = rep.numAliases;
109     filters->filter = (char **) (filters + 1);
110     filters->alias = (short *) (filters->filter + rep.numFilters);
111     name = (char *) (filters->alias + rep.numAliases);
112
113     /*
114      * Read the filter aliases
115      */
116     _XRead16Pad (dpy, filters->alias, 2 * rep.numAliases);
117
118     /*
119      * Read the filter names
120      */
121     for (i = 0; i < rep.numFilters; i++)
122     {
123         int     l;
124         _XRead (dpy, &len, 1);
125         l = len & 0xff;
126         filters->filter[i] = name;
127         _XRead (dpy, name, l);
128         name[l] = '\0';
129         name += l + 1;
130     }
131     i = name - (char *) (filters->alias + rep.numAliases);
132
133     if (i & 3)
134         _XEatData (dpy, 4 - (i & 3));
135
136     UnlockDisplay (dpy);
137     SyncHandle ();
138     return filters;
139 }
140
141 void
142 XRenderSetPictureFilter  (Display   *dpy,
143                           Picture   picture,
144                           const char *filter,
145                           XFixed    *params,
146                           int       nparams)
147 {
148     XRenderExtDisplayInfo               *info = XRenderFindDisplay (dpy);
149     xRenderSetPictureFilterReq  *req;
150     int                         nbytes = strlen (filter);
151
152     RenderSimpleCheckExtension (dpy, info);
153     LockDisplay(dpy);
154     GetReq(RenderSetPictureFilter, req);
155     req->reqType = info->codes->major_opcode;
156     req->renderReqType = X_RenderSetPictureFilter;
157     req->picture = picture;
158     req->nbytes = nbytes;
159     req->length += ((nbytes + 3) >> 2) + nparams;
160     Data (dpy, filter, nbytes);
161     Data (dpy, (_Xconst char *)params, nparams << 2);
162     UnlockDisplay(dpy);
163     SyncHandle();
164 }