iris: Make iris_has_color_unresolved more generic
[mesa.git] / src / gallium / drivers / radeon / radeon_video.c
1 /**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "radeon_video.h"
29
30 #include "radeon_vce.h"
31 #include "radeonsi/si_pipe.h"
32 #include "util/u_memory.h"
33 #include "util/u_video.h"
34 #include "vl/vl_defines.h"
35 #include "vl/vl_video_buffer.h"
36
37 #include <unistd.h>
38
39 /* generate an stream handle */
40 unsigned si_vid_alloc_stream_handle()
41 {
42 static unsigned counter = 0;
43 unsigned stream_handle = 0;
44 unsigned pid = getpid();
45 int i;
46
47 for (i = 0; i < 32; ++i)
48 stream_handle |= ((pid >> i) & 1) << (31 - i);
49
50 stream_handle ^= ++counter;
51 return stream_handle;
52 }
53
54 /* create a buffer in the winsys */
55 bool si_vid_create_buffer(struct pipe_screen *screen, struct rvid_buffer *buffer, unsigned size,
56 unsigned usage)
57 {
58 memset(buffer, 0, sizeof(*buffer));
59 buffer->usage = usage;
60
61 /* Hardware buffer placement restrictions require the kernel to be
62 * able to move buffers around individually, so request a
63 * non-sub-allocated buffer.
64 */
65 buffer->res = si_resource(pipe_buffer_create(screen, PIPE_BIND_SHARED, usage, size));
66
67 return buffer->res != NULL;
68 }
69
70 /* destroy a buffer */
71 void si_vid_destroy_buffer(struct rvid_buffer *buffer)
72 {
73 si_resource_reference(&buffer->res, NULL);
74 }
75
76 /* reallocate a buffer, preserving its content */
77 bool si_vid_resize_buffer(struct pipe_screen *screen, struct radeon_cmdbuf *cs,
78 struct rvid_buffer *new_buf, unsigned new_size)
79 {
80 struct si_screen *sscreen = (struct si_screen *)screen;
81 struct radeon_winsys *ws = sscreen->ws;
82 unsigned bytes = MIN2(new_buf->res->buf->size, new_size);
83 struct rvid_buffer old_buf = *new_buf;
84 void *src = NULL, *dst = NULL;
85
86 if (!si_vid_create_buffer(screen, new_buf, new_size, new_buf->usage))
87 goto error;
88
89 src = ws->buffer_map(old_buf.res->buf, cs, PIPE_TRANSFER_READ | RADEON_TRANSFER_TEMPORARY);
90 if (!src)
91 goto error;
92
93 dst = ws->buffer_map(new_buf->res->buf, cs, PIPE_TRANSFER_WRITE | RADEON_TRANSFER_TEMPORARY);
94 if (!dst)
95 goto error;
96
97 memcpy(dst, src, bytes);
98 if (new_size > bytes) {
99 new_size -= bytes;
100 dst += bytes;
101 memset(dst, 0, new_size);
102 }
103 ws->buffer_unmap(new_buf->res->buf);
104 ws->buffer_unmap(old_buf.res->buf);
105 si_vid_destroy_buffer(&old_buf);
106 return true;
107
108 error:
109 if (src)
110 ws->buffer_unmap(old_buf.res->buf);
111 si_vid_destroy_buffer(new_buf);
112 *new_buf = old_buf;
113 return false;
114 }
115
116 /* clear the buffer with zeros */
117 void si_vid_clear_buffer(struct pipe_context *context, struct rvid_buffer *buffer)
118 {
119 struct si_context *sctx = (struct si_context *)context;
120
121 si_sdma_clear_buffer(sctx, &buffer->res->b.b, 0, buffer->res->b.b.width0, 0);
122 context->flush(context, NULL, 0);
123 }