aboutsummaryrefslogtreecommitdiff
path: root/pixman/pixman-android.c
blob: e316118fcdb0a8323348c317cb52716eb75eacc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright © 2013 The Android Open Source Project
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
/*
 * Copyright © 2000 SuSE, Inc.
 * Copyright © 2007 Red Hat, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of SuSE not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  SuSE makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author:  Keith Packard, SuSE, Inc.
 */
/*
 * Copyright © 2009 ARM Ltd, Movial Creative Technologies Oy
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of ARM Ltd not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  ARM Ltd makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 *
 * Author:  Ian Rickards (ian.rickards@arm.com)
 * Author:  Jonathan Morton (jonathan.morton@movial.com)
 * Author:  Markku Vire (markku.vire@movial.com)
 *
 */

#include "config.h"
#include "pixman-android.h"
#include "pixman-private.h"

void pixman_scaled_bilinear_scanline_8888_8888_SRC_asm_neon(uint32_t *dst,
        const uint32_t *top, const uint32_t *bottom, int wt, int wb,
        pixman_fixed_t x, pixman_fixed_t ux, int width);

static inline void scaled_bilinear_scanline_neon_8888_8888_SRC(uint32_t * dst,
        const uint32_t * mask, const uint32_t * src_top,
        const uint32_t * src_bottom, int32_t w, int wt, int wb,
        pixman_fixed_t vx, pixman_fixed_t unit_x, pixman_fixed_t max_vx,
        pixman_bool_t zero_src) {
    pixman_scaled_bilinear_scanline_8888_8888_SRC_asm_neon(dst, src_top,
            src_bottom, wt, wb, vx, unit_x, w);
}

static inline int pixman_fixed_to_bilinear_weight(pixman_fixed_t x) {
    return (x >> (16 - BILINEAR_INTERPOLATION_BITS))
            & ((1 << BILINEAR_INTERPOLATION_BITS) - 1);
}

/*
 * For each scanline fetched from source image with PAD repeat:
 * - calculate how many pixels need to be padded on the left side
 * - calculate how many pixels need to be padded on the right side
 * - update width to only count pixels which are fetched from the image
 * All this information is returned via 'width', 'left_pad', 'right_pad'
 * arguments. The code is assuming that 'unit_x' is positive.
 *
 * Note: 64-bit math is used in order to avoid potential overflows, which
 *       is probably excessive in many cases. This particular function
 *       may need its own correctness test and performance tuning.
 */
static force_inline void pad_repeat_get_scanline_bounds(
        int32_t source_image_width, pixman_fixed_t vx, pixman_fixed_t unit_x,
        int32_t * width, int32_t * left_pad, int32_t * right_pad) {
    int64_t max_vx = (int64_t) source_image_width << 16;
    int64_t tmp;
    if (vx < 0) {
        tmp = ((int64_t) unit_x - 1 - vx) / unit_x;
        if (tmp > *width) {
            *left_pad = *width;
            *width = 0;
        } else {
            *left_pad = (int32_t) tmp;
            *width -= (int32_t) tmp;
        }
    } else {
        *left_pad = 0;
    }
    tmp = ((int64_t) unit_x - 1 - vx + max_vx) / unit_x - *left_pad;
    if (tmp < 0) {
        *right_pad = *width;
        *width = 0;
    } else if (tmp >= *width) {
        *right_pad = 0;
    } else {
        *right_pad = *width - (int32_t) tmp;
        *width = (int32_t) tmp;
    }
}

static force_inline void bilinear_pad_repeat_get_scanline_bounds(
        int32_t source_image_width, pixman_fixed_t vx, pixman_fixed_t unit_x,
        int32_t * left_pad, int32_t * left_tz, int32_t * width,
        int32_t * right_tz, int32_t * right_pad) {
    int width1 = *width, left_pad1, right_pad1;
    int width2 = *width, left_pad2, right_pad2;

    pad_repeat_get_scanline_bounds(source_image_width, vx, unit_x, &width1,
            &left_pad1, &right_pad1);
    pad_repeat_get_scanline_bounds(source_image_width, vx + pixman_fixed_1,
            unit_x, &width2, &left_pad2, &right_pad2);

    *left_pad = left_pad2;
    *left_tz = left_pad1 - left_pad2;
    *right_tz = right_pad2 - right_pad1;
    *right_pad = right_pad1;
    *width -= *left_pad + *left_tz + *right_tz + *right_pad;
}

void android_bilinear_filter(android_simple_image* src_image,
        android_simple_image* dst_image, float scale) {
    int32_t src_width = src_image->width;
    int32_t src_height = src_image->height;
    pixman_fixed_t fixed_scale = pixman_double_to_fixed(scale);
    pixman_transform_t transform;
    pixman_transform_init_scale(&transform, fixed_scale, fixed_scale);
    pixman_vector_t v;
    int32_t left_pad, left_tz, right_tz, right_pad;
    int32_t src_x = 0;
    int32_t src_y = 0;
    pixman_fixed_t unit_x, unit_y;
    int32_t width = dst_image->width;
    int32_t height = dst_image->height;
    uint32_t dst_line = 0;
    uint32_t* src_first_line;
    int32_t dest_y = 0;
    int32_t dest_x = 0;
    uint32_t* dst;
    int y1, y2;
    pixman_fixed_t vx, vy;
    uint32_t solid_mask;
    const uint32_t* mask = &solid_mask;
    v.vector[0] = ((pixman_fixed_t) (((src_x) << 16)))
            + (((pixman_fixed_t) (((1) << 16)))) / 2;
    v.vector[1] = ((pixman_fixed_t) (((src_y) << 16)))
            + (((pixman_fixed_t) (((1) << 16)))) / 2;
    v.vector[2] = (((pixman_fixed_t) (((1) << 16))));
    if (!pixman_transform_point_3d(&transform, &v)) {
        return;
    }
    unit_x = transform.matrix[0][0];
    unit_y = transform.matrix[1][1];
    v.vector[0] -= (((pixman_fixed_t) (((1) << 16)))) / 2;
    v.vector[1] -= (((pixman_fixed_t) (((1) << 16)))) / 2;
    vy = v.vector[1];
    bilinear_pad_repeat_get_scanline_bounds(src_width, v.vector[0], unit_x,
            &left_pad, &left_tz, &width, &right_tz, &right_pad);
    v.vector[0] += left_pad * unit_x;
    while (--height >= 0) {
        int weight1, weight2;
        dst_image->get_scanline(dst_image, (void**)(&dst), dst_line);
        dst_line++;
        vx = v.vector[0];
        y1 = ((int) (((vy) >> 16)));
        weight2 = pixman_fixed_to_bilinear_weight(vy);
        if (weight2) {
            y2 = y1 + 1;
            weight1 = (1 << 7) - weight2;
        } else {
            y2 = y1;
            weight1 = weight2 = (1 << 7) / 2;
        }
        vy += unit_y;
        uint32_t buf1[2];
        uint32_t buf2[2];
        uint32_t* src1;
        uint32_t* src2;
        if (y1 < 0) {
            weight1 = 0;
            y1 = 0;
        }
        if (y1 >= src_height) {
            weight1 = 0;
            y1 = src_height - 1;
        }
        if (y2 < 0) {
            weight2 = 0;
            y2 = 0;
        }
        if (y2 >= src_height) {
            weight2 = 0;
            y2 = src_height - 1;
        }
        src_image->get_scanline(src_image, (void**)(&src1), y1);
        src_image->get_scanline(src_image, (void**)(&src2), y2);
        if (left_pad > 0) {
            buf1[0] = buf1[1] = 0;
            buf2[0] = buf2[1] = 0;
            scaled_bilinear_scanline_neon_8888_8888_SRC(dst, mask, buf1, buf2,
                    left_pad, weight1, weight2, 0, 0, 0, 1);
            dst += left_pad;
        }
        if (left_tz > 0) {
            buf1[0] = 0;
            buf1[1] = src1[0];
            buf2[0] = 0;
            buf2[1] = src2[0];
            scaled_bilinear_scanline_neon_8888_8888_SRC(dst, mask, buf1, buf2,
                    left_tz, weight1, weight2,
                    ((vx)
                            & ((((pixman_fixed_t) (((1) << 16))))
                                    - ((pixman_fixed_t) (1)))), unit_x, 0, 0);
            dst += left_tz;
            vx += left_tz * unit_x;
        }
        if (width > 0) {
            scaled_bilinear_scanline_neon_8888_8888_SRC(dst, mask, src1, src2,
                    width, weight1, weight2, vx, unit_x, 0, 0);
            dst += width;
            vx += width * unit_x;
        }
        if (right_tz > 0) {
            buf1[0] = src1[src_width - 1];
            buf1[1] = 0;
            buf2[0] = src2[src_width - 1];
            buf2[1] = 0;
            scaled_bilinear_scanline_neon_8888_8888_SRC(dst, mask, buf1, buf2,
                    right_tz, weight1, weight2,
                    ((vx)
                            & ((((pixman_fixed_t) (((1) << 16))))
                                    - ((pixman_fixed_t) (1)))), unit_x, 0, 0);
            dst += right_tz;
        }
        if (right_pad > 0) {
            buf1[0] = buf1[1] = 0;
            buf2[0] = buf2[1] = 0;
            scaled_bilinear_scanline_neon_8888_8888_SRC(dst, mask, buf1, buf2,
                    right_pad, weight1, weight2, 0, 0, 0, 1);
        }
    }
}