1 /**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 **************************************************************************/
8
9 #include "radeon_video.h"
10
11 #include "radeon_vce.h"
12 #include "radeonsi/si_pipe.h"
13 #include "util/u_memory.h"
14 #include "util/u_video.h"
15 #include "vl/vl_defines.h"
16 #include "vl/vl_video_buffer.h"
17
18 #include <unistd.h>
19
20 /* generate an stream handle */
si_vid_alloc_stream_handle()21 unsigned si_vid_alloc_stream_handle()
22 {
23 static unsigned counter = 0;
24 unsigned stream_handle = 0;
25 unsigned pid = getpid();
26 int i;
27
28 for (i = 0; i < 32; ++i)
29 stream_handle |= ((pid >> i) & 1) << (31 - i);
30
31 stream_handle ^= ++counter;
32 return stream_handle;
33 }
34
35 /* create a buffer in the winsys */
si_vid_create_buffer(struct pipe_screen * screen,struct rvid_buffer * buffer,unsigned size,unsigned usage)36 bool si_vid_create_buffer(struct pipe_screen *screen, struct rvid_buffer *buffer, unsigned size,
37 unsigned usage)
38 {
39 memset(buffer, 0, sizeof(*buffer));
40 buffer->usage = usage;
41
42 /* Hardware buffer placement restrictions require the kernel to be
43 * able to move buffers around individually, so request a
44 * non-sub-allocated buffer.
45 */
46 buffer->res = si_resource(pipe_buffer_create(screen, PIPE_BIND_CUSTOM, usage, size));
47
48 return buffer->res != NULL;
49 }
50
51 /* create a tmz buffer in the winsys */
si_vid_create_tmz_buffer(struct pipe_screen * screen,struct rvid_buffer * buffer,unsigned size,unsigned usage)52 bool si_vid_create_tmz_buffer(struct pipe_screen *screen, struct rvid_buffer *buffer, unsigned size,
53 unsigned usage)
54 {
55 memset(buffer, 0, sizeof(*buffer));
56 buffer->usage = usage;
57 buffer->res = si_resource(pipe_buffer_create(screen, PIPE_BIND_CUSTOM | PIPE_BIND_PROTECTED,
58 usage, size));
59 return buffer->res != NULL;
60 }
61
62
63 /* destroy a buffer */
si_vid_destroy_buffer(struct rvid_buffer * buffer)64 void si_vid_destroy_buffer(struct rvid_buffer *buffer)
65 {
66 si_resource_reference(&buffer->res, NULL);
67 }
68
69 /* reallocate a buffer, preserving its content */
si_vid_resize_buffer(struct pipe_context * context,struct radeon_cmdbuf * cs,struct rvid_buffer * new_buf,unsigned new_size,struct rvid_buf_offset_info * buf_ofst_info)70 bool si_vid_resize_buffer(struct pipe_context *context, struct radeon_cmdbuf *cs,
71 struct rvid_buffer *new_buf, unsigned new_size,
72 struct rvid_buf_offset_info *buf_ofst_info)
73 {
74 struct si_context *sctx = (struct si_context *)context;
75 struct si_screen *sscreen = (struct si_screen *)context->screen;
76 struct radeon_winsys *ws = sscreen->ws;
77 unsigned bytes = MIN2(new_buf->res->buf->size, new_size);
78 struct rvid_buffer old_buf = *new_buf;
79 void *src = NULL, *dst = NULL;
80
81 if (!si_vid_create_buffer(context->screen, new_buf, new_size, new_buf->usage))
82 goto error;
83
84 if (old_buf.usage == PIPE_USAGE_STAGING) {
85 src = ws->buffer_map(ws, old_buf.res->buf, cs, PIPE_MAP_READ | RADEON_MAP_TEMPORARY);
86 if (!src)
87 goto error;
88
89 dst = ws->buffer_map(ws, new_buf->res->buf, cs, PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);
90 if (!dst)
91 goto error;
92
93 if (buf_ofst_info) {
94 memset(dst, 0, new_size);
95 for(int i =0; i < buf_ofst_info->num_units; i++) {
96 memcpy(dst, src, buf_ofst_info->old_offset);
97 dst += buf_ofst_info->new_offset;
98 src += buf_ofst_info->old_offset;
99 }
100 } else {
101 memcpy(dst, src, bytes);
102 if (new_size > bytes) {
103 new_size -= bytes;
104 dst += bytes;
105 memset(dst, 0, new_size);
106 }
107 }
108 ws->buffer_unmap(ws, new_buf->res->buf);
109 ws->buffer_unmap(ws, old_buf.res->buf);
110 } else {
111 si_barrier_before_simple_buffer_op(sctx, 0, &new_buf->res->b.b, &old_buf.res->b.b);
112 if (buf_ofst_info) {
113 uint64_t dst_offset = 0, src_offset = 0;
114 for (int i = 0; i < buf_ofst_info->num_units; i++) {
115 si_copy_buffer(sctx, &new_buf->res->b.b, &old_buf.res->b.b,
116 dst_offset, src_offset, buf_ofst_info->old_offset);
117 dst_offset += buf_ofst_info->new_offset;
118 src_offset += buf_ofst_info->old_offset;
119 }
120 } else {
121 bytes = MIN2(new_buf->res->b.b.width0, old_buf.res->b.b.width0);
122 si_copy_buffer(sctx, &new_buf->res->b.b, &old_buf.res->b.b, 0, 0, bytes);
123 }
124 context->flush(context, NULL, 0);
125 }
126
127 si_vid_destroy_buffer(&old_buf);
128 return true;
129
130 error:
131 if (src)
132 ws->buffer_unmap(ws, old_buf.res->buf);
133 si_vid_destroy_buffer(new_buf);
134 *new_buf = old_buf;
135 return false;
136 }
137
138 /* clear the buffer with zeros */
si_vid_clear_buffer(struct pipe_context * context,struct rvid_buffer * buffer)139 void si_vid_clear_buffer(struct pipe_context *context, struct rvid_buffer *buffer)
140 {
141 struct si_context *sctx = (struct si_context *)context;
142 uint32_t zero = 0;
143
144 sctx->b.clear_buffer(&sctx->b, &buffer->res->b.b, 0, buffer->res->b.b.width0, &zero, 4);
145 context->flush(context, NULL, 0);
146 }
147