summaryrefslogtreecommitdiff
path: root/src/surface_output.c
blob: 77344c2e6c1e80fda2823db6417e5de722be0f98 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * NVIDIA TEGRA 2 VDPAU backend driver
 *
 * Copyright (c) 2016 Dmitry Osipenko <digetx@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "vdpau_tegra.h"

VdpStatus vdp_output_surface_query_capabilities(
                                            VdpDevice device,
                                            VdpRGBAFormat surface_rgba_format,
                                            VdpBool *is_supported,
                                            uint32_t *max_width,
                                            uint32_t *max_height)
{
    return vdp_bitmap_surface_query_capabilities(device,
                                                 surface_rgba_format,
                                                 is_supported,
                                                 max_width,
                                                 max_height);
}

VdpStatus vdp_output_surface_query_get_put_bits_native_capabilities(
                                        VdpDevice device,
                                        VdpRGBAFormat surface_rgba_format,
                                        VdpBool *is_supported)
{
    tegra_device *dev = get_device(device);

    if (dev == NULL) {
        return VDP_STATUS_INVALID_HANDLE;
    }

    *is_supported = VDP_FALSE;

    put_device(dev);

    return VDP_STATUS_OK;
}

VdpStatus vdp_output_surface_query_put_bits_indexed_capabilities(
                                        VdpDevice device,
                                        VdpRGBAFormat surface_rgba_format,
                                        VdpIndexedFormat bits_indexed_format,
                                        VdpColorTableFormat color_table_format,
                                        VdpBool *is_supported)
{
    tegra_device *dev = get_device(device);

    if (dev == NULL) {
        return VDP_STATUS_INVALID_HANDLE;
    }

    *is_supported = VDP_FALSE;

    put_device(dev);

    return VDP_STATUS_OK;
}

VdpStatus vdp_output_surface_query_put_bits_y_cb_cr_capabilities(
                                        VdpDevice device,
                                        VdpRGBAFormat surface_rgba_format,
                                        VdpYCbCrFormat bits_ycbcr_format,
                                        VdpBool *is_supported)
{
    tegra_device *dev = get_device(device);

    if (dev == NULL) {
        return VDP_STATUS_INVALID_HANDLE;
    }

    *is_supported = VDP_FALSE;

    put_device(dev);

    return VDP_STATUS_OK;
}

VdpStatus vdp_output_surface_create(VdpDevice device,
                                    VdpRGBAFormat rgba_format,
                                    uint32_t width, uint32_t height,
                                    VdpOutputSurface *surface)
{
    tegra_device *dev = get_device(device);

    if (dev == NULL) {
        return VDP_STATUS_INVALID_HANDLE;
    }

    switch (rgba_format) {
    case VDP_RGBA_FORMAT_R8G8B8A8:
    case VDP_RGBA_FORMAT_B8G8R8A8:
        break;
    default:
        put_device(dev);
        return VDP_STATUS_INVALID_RGBA_FORMAT;
    }

    *surface = create_surface(dev, width, height, rgba_format, 1, 0);

    if (*surface == VDP_INVALID_HANDLE) {
        put_device(dev);
        return VDP_STATUS_RESOURCES;
    }

    put_device(dev);

    return VDP_STATUS_OK;
}

VdpStatus vdp_output_surface_destroy(VdpOutputSurface surface)
{
    return vdp_bitmap_surface_destroy(surface);
}

VdpStatus vdp_output_surface_get_parameters(VdpOutputSurface surface,
                                            VdpRGBAFormat *rgba_format,
                                            uint32_t *width, uint32_t *height)
{
    VdpBool stub;

    return vdp_bitmap_surface_get_parameters(surface, rgba_format,
                                             width, height, &stub);
}

VdpStatus vdp_output_surface_get_bits_native(VdpOutputSurface surface,
                                             VdpRect const *source_rect,
                                             void *const *destination_data,
                                             uint32_t const *destination_pitches)
{
    tegra_surface *surf = get_surface(surface);

    if (surf == NULL) {
        return VDP_STATUS_INVALID_HANDLE;
    }

    put_surface(surf);

    return VDP_STATUS_NO_IMPLEMENTATION;
}

VdpStatus vdp_output_surface_put_bits_native(VdpOutputSurface surface,
                                             void const *const *source_data,
                                             uint32_t const *source_pitches,
                                             VdpRect const *destination_rect)
{
    return vdp_bitmap_surface_put_bits_native(surface, source_data,
                                              source_pitches, destination_rect);
}

VdpStatus vdp_output_surface_put_bits_indexed(
                                        VdpOutputSurface surface,
                                        VdpIndexedFormat source_indexed_format,
                                        void const *const *source_data,
                                        uint32_t const *source_pitch,
                                        VdpRect const *destination_rect,
                                        VdpColorTableFormat color_table_format,
                                        void const *color_table)
{
    tegra_surface *surf = get_surface(surface);

    if (surf == NULL) {
        return VDP_STATUS_INVALID_HANDLE;
    }

    put_surface(surf);

    return VDP_STATUS_NO_IMPLEMENTATION;
}

VdpStatus vdp_output_surface_put_bits_y_cb_cr(
                                        VdpOutputSurface surface,
                                        VdpYCbCrFormat source_ycbcr_format,
                                        void const *const *source_data,
                                        uint32_t const *source_pitches,
                                        VdpRect const *destination_rect,
                                        VdpCSCMatrix const *csc_matrix)
{
    tegra_surface *surf = get_surface(surface);

    if (surf == NULL) {
        return VDP_STATUS_INVALID_HANDLE;
    }

    put_surface(surf);

    return VDP_STATUS_NO_IMPLEMENTATION;
}

VdpStatus vdp_output_surface_render_bitmap_surface(
                            VdpOutputSurface destination_surface,
                            VdpRect const *destination_rect,
                            VdpBitmapSurface source_surface,
                            VdpRect const *source_rect,
                            VdpColor const *colors,
                            VdpOutputSurfaceRenderBlendState const *blend_state,
                            uint32_t flags)
{
    tegra_surface *dst_surf = get_surface(destination_surface);
    tegra_surface *src_surf = get_surface(source_surface);
    pixman_image_t *src_pix;
    pixman_image_t *dst_pix;
    pixman_format_code_t pfmt;
    pixman_transform_t transform;
    void *dst_data;
    uint32_t src_width, src_height;
    uint32_t src_x0, src_y0;
    uint32_t dst_width, dst_height;
    uint32_t dst_x0, dst_y0;
    int need_scale = 0;
    int need_rotate = 0;
    int ret;

    if (dst_surf == NULL || src_surf == NULL) {
        put_surface(dst_surf);
        put_surface(src_surf);
        return VDP_STATUS_INVALID_HANDLE;
    }

    assert(dst_surf->idle_hack ||
           dst_surf->status == VDP_PRESENTATION_QUEUE_STATUS_IDLE);

    pthread_mutex_lock(&src_surf->lock);

    ret = shared_surface_transfer_video(dst_surf);
    if (ret) {
        pthread_mutex_unlock(&src_surf->lock);
        put_surface(dst_surf);
        put_surface(src_surf);
        return VDP_STATUS_RESOURCES;
    }

    dst_pix  = dst_surf->pix;
    dst_data = pixman_image_get_data(dst_pix);

    pfmt = pixman_image_get_format(dst_pix);
    ret = pixman_format_supported_destination(pfmt);

    assert(ret != 0);

    if (destination_rect != NULL) {
        dst_width = destination_rect->x1 - destination_rect->x0;
        dst_height = destination_rect->y1 - destination_rect->y0;
        dst_x0 = destination_rect->x0;
        dst_y0 = destination_rect->y0;
    } else {
        dst_width = pixman_image_get_width(dst_pix);
        dst_height = pixman_image_get_height(dst_pix);
        dst_x0 = 0;
        dst_y0 = 0;
    }

    if (source_surface == VDP_INVALID_HANDLE) {
        ret = host1x_gr2d_clear_rect(dst_surf->dev->stream,
                                     dst_surf->pixbuf,
                                     0xFFFFFFFF,
                                     dst_x0, dst_y0,
                                     dst_width, dst_height);
        if (ret == 0) {
            pthread_mutex_unlock(&src_surf->lock);
            put_surface(dst_surf);
            put_surface(src_surf);
            return VDP_STATUS_OK;
        }

        ret = pixman_fill(dst_data,
                          pixman_image_get_stride(dst_pix) / 4,
                          PIXMAN_FORMAT_BPP(pfmt),
                          dst_x0, dst_y0,
                          dst_width, dst_height,
                          0xFFFFFFFF);

        assert(ret != 0);

        pthread_mutex_unlock(&src_surf->lock);

        put_surface(dst_surf);
        put_surface(src_surf);

        return VDP_STATUS_OK;
    }

    if (blend_state != NULL) {
        if (blend_state->struct_version != VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION) {
            pthread_mutex_unlock(&src_surf->lock);
            put_surface(dst_surf);
            put_surface(src_surf);
            return VDP_STATUS_INVALID_STRUCT_VERSION;
        }
    }

    src_pix = src_surf->pix;

    pfmt = pixman_image_get_format(src_pix);
    ret = pixman_format_supported_destination(pfmt);

    assert(ret != 0);

    if (source_rect != NULL) {
        src_width = source_rect->x1 - source_rect->x0;
        src_height = source_rect->y1 - source_rect->y0;
        src_x0 = source_rect->x0;
        src_y0 = source_rect->y0;
    } else {
        src_width = pixman_image_get_width(src_pix);
        src_height = pixman_image_get_height(src_pix);
        src_x0 = 0;
        src_y0 = 0;
    }

    assert(!(flags & ~3ul));

    switch (flags & 3) {
    case VDP_OUTPUT_SURFACE_RENDER_ROTATE_90:
    case VDP_OUTPUT_SURFACE_RENDER_ROTATE_180:
    case VDP_OUTPUT_SURFACE_RENDER_ROTATE_270:
        need_rotate = 1;
        break;
    default:
        break;
    }

    if (dst_width != src_width || dst_height != src_height) {
        need_scale = 1;
    }

    if (!need_rotate) {
        host1x_gr2d_surface_blit(dst_surf->dev->stream,
                                 src_surf->pixbuf,
                                 dst_surf->pixbuf,
                                 &csc_rgb_default,
                                 src_x0, src_y0,
                                 src_width, src_height,
                                 dst_x0, dst_y0,
                                 dst_width, dst_height);
        pthread_mutex_unlock(&src_surf->lock);
        put_surface(dst_surf);
        put_surface(src_surf);
        return VDP_STATUS_OK;
    }

    if (need_scale || need_rotate) {
        pixman_transform_init_identity(&transform);

        switch (flags & 3) {
        case VDP_OUTPUT_SURFACE_RENDER_ROTATE_90:
            ret = pixman_transform_rotate(&transform, NULL, 0.0, 1.0);
            break;
        case VDP_OUTPUT_SURFACE_RENDER_ROTATE_180:
            ret = pixman_transform_rotate(&transform, NULL, -1.0, 0.0);
            break;
        case VDP_OUTPUT_SURFACE_RENDER_ROTATE_270:
            ret = pixman_transform_rotate(&transform, NULL, 0.0, -1.0);
            break;
        default:
            ret = 1;
            break;
        }

        assert(ret != 0);

        if (need_scale) {
            double scalew = (double)src_width / (double)dst_width;
            double scaleh = (double)src_height / (double)dst_height;

            ret = pixman_transform_scale(&transform, NULL,
                                         pixman_double_to_fixed(scalew),
                                         pixman_double_to_fixed(scaleh));

            assert(ret != 0);
        }

        ret = pixman_image_set_transform(src_pix, &transform);

        assert(ret != 0);
    }

    pixman_image_composite(PIXMAN_OP_SRC,
                           src_pix,
                           NULL,
                           dst_pix,
                           src_x0, src_y0,
                           0, 0,
                           dst_x0, dst_y0,
                           dst_width, dst_height);

    if (need_scale || need_rotate) {
        ret = pixman_image_set_transform(src_pix, NULL);
        assert(ret != 0);
    }

    pthread_mutex_unlock(&src_surf->lock);

    put_surface(dst_surf);
    put_surface(src_surf);

    return VDP_STATUS_OK;
}

VdpStatus vdp_output_surface_render_output_surface(
                            VdpOutputSurface destination_surface,
                            VdpRect const *destination_rect,
                            VdpOutputSurface source_surface,
                            VdpRect const *source_rect,
                            VdpColor const *colors,
                            VdpOutputSurfaceRenderBlendState const *blend_state,
                            uint32_t flags)
{
    return vdp_output_surface_render_bitmap_surface(destination_surface,
                                                    destination_rect,
                                                    source_surface,
                                                    source_rect,
                                                    colors,
                                                    blend_state,
                                                    flags);
}