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 */
si_vid_alloc_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 */
si_vid_create_buffer(struct pipe_screen * screen,struct rvid_buffer * buffer,unsigned size,unsigned usage)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 /* create a tmz buffer in the winsys */
si_vid_create_tmz_buffer(struct pipe_screen * screen,struct rvid_buffer * buffer,unsigned size,unsigned usage)71 bool si_vid_create_tmz_buffer(struct pipe_screen *screen, struct rvid_buffer *buffer, unsigned size,
72 unsigned usage)
73 {
74 memset(buffer, 0, sizeof(*buffer));
75 buffer->usage = usage;
76 buffer->res = si_resource(pipe_buffer_create(screen, PIPE_BIND_SHARED | PIPE_BIND_PROTECTED,
77 usage, size));
78 return buffer->res != NULL;
79 }
80
81
82 /* destroy a buffer */
si_vid_destroy_buffer(struct rvid_buffer * buffer)83 void si_vid_destroy_buffer(struct rvid_buffer *buffer)
84 {
85 si_resource_reference(&buffer->res, NULL);
86 }
87
88 /* reallocate a buffer, preserving its content */
si_vid_resize_buffer(struct pipe_screen * screen,struct radeon_cmdbuf * cs,struct rvid_buffer * new_buf,unsigned new_size)89 bool si_vid_resize_buffer(struct pipe_screen *screen, struct radeon_cmdbuf *cs,
90 struct rvid_buffer *new_buf, unsigned new_size)
91 {
92 struct si_screen *sscreen = (struct si_screen *)screen;
93 struct radeon_winsys *ws = sscreen->ws;
94 unsigned bytes = MIN2(new_buf->res->buf->size, new_size);
95 struct rvid_buffer old_buf = *new_buf;
96 void *src = NULL, *dst = NULL;
97
98 if (!si_vid_create_buffer(screen, new_buf, new_size, new_buf->usage))
99 goto error;
100
101 src = ws->buffer_map(old_buf.res->buf, cs, PIPE_MAP_READ | RADEON_MAP_TEMPORARY);
102 if (!src)
103 goto error;
104
105 dst = ws->buffer_map(new_buf->res->buf, cs, PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
106 if (!dst)
107 goto error;
108
109 memcpy(dst, src, bytes);
110 if (new_size > bytes) {
111 new_size -= bytes;
112 dst += bytes;
113 memset(dst, 0, new_size);
114 }
115 ws->buffer_unmap(new_buf->res->buf);
116 ws->buffer_unmap(old_buf.res->buf);
117 si_vid_destroy_buffer(&old_buf);
118 return true;
119
120 error:
121 if (src)
122 ws->buffer_unmap(old_buf.res->buf);
123 si_vid_destroy_buffer(new_buf);
124 *new_buf = old_buf;
125 return false;
126 }
127
128 /* clear the buffer with zeros */
si_vid_clear_buffer(struct pipe_context * context,struct rvid_buffer * buffer)129 void si_vid_clear_buffer(struct pipe_context *context, struct rvid_buffer *buffer)
130 {
131 struct si_context *sctx = (struct si_context *)context;
132
133 si_sdma_clear_buffer(sctx, &buffer->res->b.b, 0, buffer->res->b.b.width0, 0);
134 context->flush(context, NULL, 0);
135 }
136