cpu_backend.c 11.7 KB
Newer Older
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
/*
 * Copyright © 2013 Siarhei Siamashka <siarhei.siamashka@gmail.com>
 *
 * 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.
 */

#include <stdlib.h>
#include <string.h>

#include "cpuinfo.h"
#include "cpu_backend.h"

30
#if defined(__arm__) || defined(__aarch64__)
31

32
33
34
35
36
37
38
#ifdef __GNUC__
#define always_inline inline __attribute__((always_inline))
#else
#define always_inline inline
#endif

void memcpy_armv5te(void *dst, const void *src, int size);
39
40
void writeback_scratch_to_mem_neon(int size, void *dst, const void *src);
void aligned_fetch_fbmem_to_scratch_neon(int size, void *dst, const void *src);
41
void aligned_fetch_fbmem_to_scratch_vfp(int size, void *dst, const void *src);
42
void aligned_fetch_fbmem_to_scratch_arm(int size, void *dst, const void *src);
43
44
45
46
47
48

static always_inline void
writeback_scratch_to_mem_arm(int size, void *dst, const void *src)
{
    memcpy_armv5te(dst, src, size);
}
49

50
51
52
53
54
55
56
57
58
59
#ifdef __aarch64__
static always_inline void
writeback_scratch_to_mem_memcpy(int size, void *dst, const void *src)
{
    memcpy(dst, src, size);
}

#define writeback_scratch_to_mem_neon writeback_scratch_to_mem_memcpy
#endif

60
61
62
63
64
65
66
67
68
69
70
#define SCRATCHSIZE 2048

/*
 * This is a function similar to memmove, which tries to minimize uncached read
 * penalty for the source buffer (for example if the source is a framebuffer).
 *
 * Note: because this implementation fetches data as 32 byte aligned chunks
 * valgrind is going to scream about read accesses outside the source buffer.
 * (even if an aligned 32 byte chunk contains only a single byte belonging
 * to the source buffer, the whole chunk is going to be read).
 */
71
72
73
74
static always_inline void
twopass_memmove(void *dst_, const void *src_, size_t size,
                void (*aligned_fetch_fbmem_to_scratch)(int, void *, const void *),
                void (*writeback_scratch_to_mem)(int, void *, const void *))
75
76
77
78
79
80
81
82
83
84
{
    uint8_t tmpbuf[SCRATCHSIZE + 32 + 31];
    uint8_t *scratchbuf = (uint8_t *)((uintptr_t)(&tmpbuf[0] + 31) & ~31);
    uint8_t *dst = (uint8_t *)dst_;
    const uint8_t *src = (const uint8_t *)src_;
    uintptr_t alignshift = (uintptr_t)src & 31;
    uintptr_t extrasize = (alignshift == 0) ? 0 : 32;

    if (src > dst) {
        while (size >= SCRATCHSIZE) {
85
86
87
            aligned_fetch_fbmem_to_scratch(SCRATCHSIZE + extrasize,
                                           scratchbuf, src - alignshift);
            writeback_scratch_to_mem(SCRATCHSIZE, dst, scratchbuf + alignshift);
88
89
90
91
92
            size -= SCRATCHSIZE;
            dst += SCRATCHSIZE;
            src += SCRATCHSIZE;
        }
        if (size > 0) {
93
94
95
            aligned_fetch_fbmem_to_scratch(size + extrasize,
                                           scratchbuf, src - alignshift);
            writeback_scratch_to_mem(size, dst, scratchbuf + alignshift);
96
97
98
99
100
101
102
103
        }
    }
    else {
        uintptr_t remainder = size % SCRATCHSIZE;
        dst += size - remainder;
        src += size - remainder;
        size -= remainder;
        if (remainder) {
104
105
106
            aligned_fetch_fbmem_to_scratch(remainder + extrasize,
                                           scratchbuf, src - alignshift);
            writeback_scratch_to_mem(remainder, dst, scratchbuf + alignshift);
107
108
109
110
111
        }
        while (size > 0) {
            dst -= SCRATCHSIZE;
            src -= SCRATCHSIZE;
            size -= SCRATCHSIZE;
112
113
114
            aligned_fetch_fbmem_to_scratch(SCRATCHSIZE + extrasize,
                                           scratchbuf, src - alignshift);
            writeback_scratch_to_mem(SCRATCHSIZE, dst, scratchbuf + alignshift);
115
116
117
118
119
        }
    }
}

static void
120
121
122
123
124
125
126
twopass_memmove_neon(void *dst, const void *src, size_t size)
{
    twopass_memmove(dst, src, size,
                    aligned_fetch_fbmem_to_scratch_neon,
                    writeback_scratch_to_mem_neon);
}

127
128
#ifdef __arm__

129
130
131
132
133
134
135
136
static void
twopass_memmove_vfp(void *dst, const void *src, size_t size)
{
    twopass_memmove(dst, src, size,
                    aligned_fetch_fbmem_to_scratch_vfp,
                    writeback_scratch_to_mem_arm);
}

137
138
139
140
141
142
143
144
static void
twopass_memmove_arm(void *dst, const void *src, size_t size)
{
    twopass_memmove(dst, src, size,
                    aligned_fetch_fbmem_to_scratch_arm,
                    writeback_scratch_to_mem_arm);
}

145
146
#endif

147
148
149
150
151
152
153
154
static void
twopass_blt_8bpp(int        width,
                 int        height,
                 uint8_t   *dst_bytes,
                 uintptr_t  dst_stride,
                 uint8_t   *src_bytes,
                 uintptr_t  src_stride,
                 void (*twopass_memmove)(void *, const void *, size_t))
155
156
157
158
159
160
161
162
163
164
165
166
{
    if (src_bytes < dst_bytes + width &&
        src_bytes + src_stride * height > dst_bytes)
    {
        src_bytes += src_stride * height - src_stride;
        dst_bytes += dst_stride * height - dst_stride;
        dst_stride = -dst_stride;
        src_stride = -src_stride;
        if (src_bytes + width > dst_bytes)
        {
            while (--height >= 0)
            {
167
                twopass_memmove(dst_bytes, src_bytes, width);
168
169
170
171
172
173
174
175
                dst_bytes += dst_stride;
                src_bytes += src_stride;
            }
            return;
        }
    }
    while (--height >= 0)
    {
176
        twopass_memmove(dst_bytes, src_bytes, width);
177
178
179
180
181
        dst_bytes += dst_stride;
        src_bytes += src_stride;
    }
}

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
static always_inline int
overlapped_blt(void     *self,
               uint32_t *src_bits,
               uint32_t *dst_bits,
               int       src_stride,
               int       dst_stride,
               int       src_bpp,
               int       dst_bpp,
               int       src_x,
               int       src_y,
               int       dst_x,
               int       dst_y,
               int       width,
               int       height,
               void (*twopass_memmove)(void *, const void *, size_t))
{
    uint8_t *dst_bytes = (uint8_t *)dst_bits;
    uint8_t *src_bytes = (uint8_t *)src_bits;
    cpu_backend_t *ctx = (cpu_backend_t *)self;
    int bpp = src_bpp >> 3;
    int uncached_source = (src_bytes >= ctx->uncached_area_begin) &&
                          (src_bytes < ctx->uncached_area_end);
    if (!uncached_source)
        return 0;

    if (src_bpp != dst_bpp || src_bpp & 7 || src_stride < 0 || dst_stride < 0)
        return 0;

    twopass_blt_8bpp((uintptr_t) width * bpp,
                     height,
                     dst_bytes + (uintptr_t) dst_y * dst_stride * 4 +
                                 (uintptr_t) dst_x * bpp,
                     (uintptr_t) dst_stride * 4,
                     src_bytes + (uintptr_t) src_y * src_stride * 4 +
                                 (uintptr_t) src_x * bpp,
                     (uintptr_t) src_stride * 4,
                     twopass_memmove);
    return 1;
}

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
static int
overlapped_blt_neon(void     *self,
                    uint32_t *src_bits,
                    uint32_t *dst_bits,
                    int       src_stride,
                    int       dst_stride,
                    int       src_bpp,
                    int       dst_bpp,
                    int       src_x,
                    int       src_y,
                    int       dst_x,
                    int       dst_y,
                    int       width,
                    int       height)
{
237
238
239
240
241
    return overlapped_blt(self, src_bits, dst_bits, src_stride, dst_stride,
                          src_bpp, dst_bpp, src_x, src_y, dst_x, dst_y,
                          width, height,
                          twopass_memmove_neon);
}
242

243
244
#ifdef __arm__

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
static int
overlapped_blt_vfp(void     *self,
                   uint32_t *src_bits,
                   uint32_t *dst_bits,
                   int       src_stride,
                   int       dst_stride,
                   int       src_bpp,
                   int       dst_bpp,
                   int       src_x,
                   int       src_y,
                   int       dst_x,
                   int       dst_y,
                   int       width,
                   int       height)
{
    return overlapped_blt(self, src_bits, dst_bits, src_stride, dst_stride,
                          src_bpp, dst_bpp, src_x, src_y, dst_x, dst_y,
                          width, height,
                          twopass_memmove_vfp);
264
265
}

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
static int
overlapped_blt_arm(void     *self,
                   uint32_t *src_bits,
                   uint32_t *dst_bits,
                   int       src_stride,
                   int       dst_stride,
                   int       src_bpp,
                   int       dst_bpp,
                   int       src_x,
                   int       src_y,
                   int       dst_x,
                   int       dst_y,
                   int       width,
                   int       height)
{
    return overlapped_blt(self, src_bits, dst_bits, src_stride, dst_stride,
                          src_bpp, dst_bpp, src_x, src_y, dst_x, dst_y,
                          width, height,
                          twopass_memmove_arm);
}

287
288
#endif

289
290
#endif

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* An empty, always failing implementation */
static int
overlapped_blt_noop(void     *self,
                    uint32_t *src_bits,
                    uint32_t *dst_bits,
                    int       src_stride,
                    int       dst_stride,
                    int       src_bpp,
                    int       dst_bpp,
                    int       src_x,
                    int       src_y,
                    int       dst_x,
                    int       dst_y,
                    int       width,
                    int       height)
{
    return 0;
}

cpu_backend_t *cpu_backend_init(uint8_t *uncached_buffer,
                                size_t   uncached_buffer_size)
{
    cpu_backend_t *ctx = calloc(sizeof(cpu_backend_t), 1);
    if (!ctx)
        return NULL;

    ctx->uncached_area_begin = uncached_buffer;
    ctx->uncached_area_end   = uncached_buffer + uncached_buffer_size;

    ctx->blt2d.self = ctx;
    ctx->blt2d.overlapped_blt = overlapped_blt_noop;

    ctx->cpuinfo = cpuinfo_init();

#ifdef __arm__
326
327
328
329
330
    if (ctx->cpuinfo->has_arm_neon &&
        ctx->cpuinfo->arm_implementer == 0x41 &&
        ctx->cpuinfo->arm_part == 0xC08)
    {
        /* NEON works better on Cortex-A8 */
331
332
        ctx->blt2d.overlapped_blt = overlapped_blt_neon;
    }
333
    else if (ctx->cpuinfo->has_arm_wmmx) {
334
335
336
        /* ARM LDM/STM works better than VFP/WMMX on Marvell PJ4 */
        ctx->blt2d.overlapped_blt = overlapped_blt_arm;
    }
337
338
339
340
    else if (ctx->cpuinfo->has_arm_vfp && ctx->cpuinfo->has_arm_edsp) {
        /* VFP works better on Cortex-A9, Cortex-A15 and maybe everything else */
        ctx->blt2d.overlapped_blt = overlapped_blt_vfp;
    }
341
342
#endif

343
344
345
346
#ifdef __aarch64__
    ctx->blt2d.overlapped_blt = overlapped_blt_neon;
#endif

347
348
349
350
351
352
353
354
355
356
    return ctx;
}

void cpu_backend_close(cpu_backend_t *ctx)
{
    if (ctx->cpuinfo)
        cpuinfo_close(ctx->cpuinfo);

    free(ctx);
}