~ubuntu-branches/ubuntu/intrepid/xserver-xgl/intrepid

« back to all changes in this revision

Viewing changes to render/filter.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2006-02-13 14:21:43 UTC
  • Revision ID: james.westby@ubuntu.com-20060213142143-mad6z9xzem7hzxz9
Tags: upstream-7.0.0
ImportĀ upstreamĀ versionĀ 7.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: filter.c,v 1.11.4.1 2006/01/18 07:21:43 airlied Exp $
 
3
 *
 
4
 * Copyright Ā© 2002 Keith Packard
 
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 Keith Packard not be used in
 
11
 * advertising or publicity pertaining to distribution of the software without
 
12
 * specific, written prior permission.  Keith Packard makes 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
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 
17
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 
18
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 
19
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 
20
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 
21
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 
22
 * PERFORMANCE OF THIS SOFTWARE.
 
23
 */
 
24
 
 
25
#ifdef HAVE_DIX_CONFIG_H
 
26
#include <dix-config.h>
 
27
#endif
 
28
 
 
29
#include "misc.h"
 
30
#include "scrnintstr.h"
 
31
#include "os.h"
 
32
#include "regionstr.h"
 
33
#include "validate.h"
 
34
#include "windowstr.h"
 
35
#include "input.h"
 
36
#include "resource.h"
 
37
#include "colormapst.h"
 
38
#include "cursorstr.h"
 
39
#include "dixstruct.h"
 
40
#include "gcstruct.h"
 
41
#include "servermd.h"
 
42
#include "picturestr.h"
 
43
 
 
44
static char **filterNames;
 
45
static int  nfilterNames;
 
46
 
 
47
/*
 
48
 * standard but not required filters don't have constant indices
 
49
 */
 
50
 
 
51
int
 
52
PictureGetFilterId (char *filter, int len, Bool makeit)
 
53
{
 
54
    int     i;
 
55
    char    *name;
 
56
    char    **names;
 
57
 
 
58
    if (len < 0)
 
59
        len = strlen (filter);
 
60
    for (i = 0; i < nfilterNames; i++)
 
61
        if (!CompareISOLatin1Lowered ((unsigned char *) filterNames[i], -1, (unsigned char *) filter, len))
 
62
            return i;
 
63
    if (!makeit)
 
64
        return -1;
 
65
    name = xalloc (len + 1);
 
66
    if (!name)
 
67
        return -1;
 
68
    memcpy (name, filter, len);
 
69
    name[len] = '\0';
 
70
    if (filterNames)
 
71
        names = xrealloc (filterNames, (nfilterNames + 1) * sizeof (char *));
 
72
    else
 
73
        names = xalloc (sizeof (char *));
 
74
    if (!names)
 
75
    {
 
76
        xfree (name);
 
77
        return -1;
 
78
    }
 
79
    filterNames = names;
 
80
    i = nfilterNames++;
 
81
    filterNames[i] = name;
 
82
    return i;
 
83
}
 
84
 
 
85
static Bool
 
86
PictureSetDefaultIds (void)
 
87
{
 
88
    /* careful here -- this list must match the #define values */
 
89
 
 
90
    if (PictureGetFilterId (FilterNearest, -1, TRUE) != PictFilterNearest)
 
91
        return FALSE;
 
92
    if (PictureGetFilterId (FilterBilinear, -1, TRUE) != PictFilterBilinear)
 
93
        return FALSE;
 
94
 
 
95
    if (PictureGetFilterId (FilterFast, -1, TRUE) != PictFilterFast)
 
96
        return FALSE;
 
97
    if (PictureGetFilterId (FilterGood, -1, TRUE) != PictFilterGood)
 
98
        return FALSE;
 
99
    if (PictureGetFilterId (FilterBest, -1, TRUE) != PictFilterBest)
 
100
        return FALSE;
 
101
 
 
102
    if (PictureGetFilterId (FilterConvolution, -1, TRUE) != PictFilterConvolution)
 
103
        return FALSE;
 
104
    return TRUE;
 
105
}
 
106
 
 
107
char *
 
108
PictureGetFilterName (int id)
 
109
{
 
110
    if (0 <= id && id < nfilterNames)
 
111
        return filterNames[id];
 
112
    else
 
113
        return 0;
 
114
}
 
115
 
 
116
static void
 
117
PictureFreeFilterIds (void)
 
118
{
 
119
    int     i;
 
120
 
 
121
    for (i = 0; i < nfilterNames; i++)
 
122
        xfree (filterNames[i]);
 
123
    xfree (filterNames);
 
124
    nfilterNames = 0;
 
125
    filterNames = 0;
 
126
}
 
127
 
 
128
int
 
129
PictureAddFilter (ScreenPtr                         pScreen,
 
130
                  char                              *filter,
 
131
                  PictFilterValidateParamsProcPtr   ValidateParams)
 
132
{
 
133
    PictureScreenPtr    ps = GetPictureScreen(pScreen);
 
134
    int                 id = PictureGetFilterId (filter, -1,  TRUE);
 
135
    int                 i;
 
136
    PictFilterPtr       filters;
 
137
 
 
138
    if (id < 0)
 
139
        return -1;
 
140
    /*
 
141
     * It's an error to attempt to reregister a filter
 
142
     */
 
143
    for (i = 0; i < ps->nfilters; i++)
 
144
        if (ps->filters[i].id == id)
 
145
            return -1;
 
146
    if (ps->filters)
 
147
        filters = xrealloc (ps->filters, (ps->nfilters + 1) * sizeof (PictFilterRec));
 
148
    else
 
149
        filters = xalloc (sizeof (PictFilterRec));
 
150
    if (!filters)
 
151
        return -1;
 
152
    ps->filters = filters;
 
153
    i = ps->nfilters++;
 
154
    ps->filters[i].name = PictureGetFilterName (id);
 
155
    ps->filters[i].id = id;
 
156
    ps->filters[i].ValidateParams = ValidateParams;
 
157
    return id;
 
158
}
 
159
 
 
160
Bool
 
161
PictureSetFilterAlias (ScreenPtr pScreen, char *filter, char *alias)
 
162
{
 
163
    PictureScreenPtr    ps = GetPictureScreen(pScreen);
 
164
    int                 filter_id = PictureGetFilterId (filter, -1, FALSE);
 
165
    int                 alias_id = PictureGetFilterId (alias, -1, TRUE);
 
166
    int                 i;
 
167
 
 
168
    if (filter_id < 0 || alias_id < 0)
 
169
        return FALSE;
 
170
    for (i = 0; i < ps->nfilterAliases; i++)
 
171
        if (ps->filterAliases[i].alias_id == alias_id)
 
172
            break;
 
173
    if (i == ps->nfilterAliases)
 
174
    {
 
175
        PictFilterAliasPtr  aliases;
 
176
 
 
177
        if (ps->filterAliases)
 
178
            aliases = xrealloc (ps->filterAliases,
 
179
                                (ps->nfilterAliases + 1) *
 
180
                                sizeof (PictFilterAliasRec));
 
181
        else
 
182
            aliases = xalloc (sizeof (PictFilterAliasRec));
 
183
        if (!aliases)
 
184
            return FALSE;
 
185
        ps->filterAliases = aliases;
 
186
        ps->filterAliases[i].alias = PictureGetFilterName (alias_id);
 
187
        ps->filterAliases[i].alias_id = alias_id;
 
188
        ps->nfilterAliases++;
 
189
    }
 
190
    ps->filterAliases[i].filter_id = filter_id;
 
191
    return TRUE;
 
192
}
 
193
 
 
194
PictFilterPtr
 
195
PictureFindFilter (ScreenPtr pScreen, char *name, int len)
 
196
{
 
197
    PictureScreenPtr    ps = GetPictureScreen(pScreen);
 
198
    int                 id = PictureGetFilterId (name, len, FALSE);
 
199
    int                 i;
 
200
 
 
201
    if (id < 0)
 
202
        return 0;
 
203
    /* Check for an alias, allow them to recurse */
 
204
    for (i = 0; i < ps->nfilterAliases; i++)
 
205
        if (ps->filterAliases[i].alias_id == id)
 
206
        {
 
207
            id = ps->filterAliases[i].filter_id;
 
208
            i = 0;
 
209
        }
 
210
    /* find the filter */
 
211
    for (i = 0; i < ps->nfilters; i++)
 
212
        if (ps->filters[i].id == id)
 
213
            return &ps->filters[i];
 
214
    return 0;
 
215
}
 
216
 
 
217
static Bool
 
218
convolutionFilterValidateParams (PicturePtr pPicture,
 
219
                                 int       filter,
 
220
                                 xFixed    *params,
 
221
                                 int       nparams)
 
222
{
 
223
    if (nparams < 3)
 
224
        return FALSE;
 
225
 
 
226
    if (xFixedFrac (params[0]) || xFixedFrac (params[1]))
 
227
        return FALSE;
 
228
 
 
229
    nparams -= 2;
 
230
    if ((xFixedToInt (params[0]) * xFixedToInt (params[1])) > nparams)
 
231
        return FALSE;
 
232
 
 
233
    return TRUE;
 
234
}
 
235
 
 
236
 
 
237
Bool
 
238
PictureSetDefaultFilters (ScreenPtr pScreen)
 
239
{
 
240
    if (!filterNames)
 
241
        if (!PictureSetDefaultIds ())
 
242
            return FALSE;
 
243
    if (PictureAddFilter (pScreen, FilterNearest, 0) < 0)
 
244
        return FALSE;
 
245
    if (PictureAddFilter (pScreen, FilterBilinear, 0) < 0)
 
246
        return FALSE;
 
247
 
 
248
    if (!PictureSetFilterAlias (pScreen, FilterNearest, FilterFast))
 
249
        return FALSE;
 
250
    if (!PictureSetFilterAlias (pScreen, FilterBilinear, FilterGood))
 
251
        return FALSE;
 
252
    if (!PictureSetFilterAlias (pScreen, FilterBilinear, FilterBest))
 
253
        return FALSE;
 
254
 
 
255
    if (PictureAddFilter (pScreen, FilterConvolution, convolutionFilterValidateParams) < 0)
 
256
        return FALSE;
 
257
 
 
258
    return TRUE;
 
259
}
 
260
 
 
261
void
 
262
PictureResetFilters (ScreenPtr pScreen)
 
263
{
 
264
    PictureScreenPtr    ps = GetPictureScreen(pScreen);
 
265
 
 
266
    xfree (ps->filters);
 
267
    xfree (ps->filterAliases);
 
268
    PictureFreeFilterIds ();
 
269
}
 
270
 
 
271
int
 
272
SetPictureFilter (PicturePtr pPicture, char *name, int len, xFixed *params, int nparams)
 
273
{
 
274
    ScreenPtr           pScreen;
 
275
    PictureScreenPtr    ps;
 
276
    PictFilterPtr       pFilter;
 
277
    xFixed              *new_params;
 
278
    int                 i, result;
 
279
 
 
280
    if (!pPicture->pDrawable)
 
281
       return Success;
 
282
 
 
283
    pScreen = pPicture->pDrawable->pScreen;
 
284
    ps = GetPictureScreen(pScreen);
 
285
    pFilter = PictureFindFilter (pScreen, name, len);
 
286
 
 
287
    if (!pFilter)
 
288
        return BadName;
 
289
    if (pFilter->ValidateParams)
 
290
    {
 
291
        if (!(*pFilter->ValidateParams) (pPicture, pFilter->id, params, nparams))
 
292
            return BadMatch;
 
293
    }
 
294
    else if (nparams)
 
295
        return BadMatch;
 
296
 
 
297
    if (nparams != pPicture->filter_nparams)
 
298
    {
 
299
        new_params = xalloc (nparams * sizeof (xFixed));
 
300
        if (!new_params)
 
301
            return BadAlloc;
 
302
        xfree (pPicture->filter_params);
 
303
        pPicture->filter_params = new_params;
 
304
        pPicture->filter_nparams = nparams;
 
305
    }
 
306
    for (i = 0; i < nparams; i++)
 
307
        pPicture->filter_params[i] = params[i];
 
308
    pPicture->filter = pFilter->id;
 
309
 
 
310
    result = (*ps->ChangePictureFilter) (pPicture, pPicture->filter,
 
311
                                         params, nparams);
 
312
    return result;
 
313
    return Success;
 
314
}