1 /**************************************************************************
3 * Copyright 2013 Advanced Micro Devices, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "radeon_video.h"
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"
39 /* generate an stream handle */
40 unsigned si_vid_alloc_stream_handle()
42 static unsigned counter
= 0;
43 unsigned stream_handle
= 0;
44 unsigned pid
= getpid();
47 for (i
= 0; i
< 32; ++i
)
48 stream_handle
|= ((pid
>> i
) & 1) << (31 - i
);
50 stream_handle
^= ++counter
;
54 /* create a buffer in the winsys */
55 bool si_vid_create_buffer(struct pipe_screen
*screen
, struct rvid_buffer
*buffer
, unsigned size
,
58 memset(buffer
, 0, sizeof(*buffer
));
59 buffer
->usage
= usage
;
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.
65 buffer
->res
= si_resource(pipe_buffer_create(screen
, PIPE_BIND_SHARED
, usage
, size
));
67 return buffer
->res
!= NULL
;
70 /* destroy a buffer */
71 void si_vid_destroy_buffer(struct rvid_buffer
*buffer
)
73 si_resource_reference(&buffer
->res
, NULL
);
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
)
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
;
86 if (!si_vid_create_buffer(screen
, new_buf
, new_size
, new_buf
->usage
))
89 src
= ws
->buffer_map(old_buf
.res
->buf
, cs
, PIPE_TRANSFER_READ
| RADEON_TRANSFER_TEMPORARY
);
93 dst
= ws
->buffer_map(new_buf
->res
->buf
, cs
, PIPE_TRANSFER_WRITE
| RADEON_TRANSFER_TEMPORARY
);
97 memcpy(dst
, src
, bytes
);
98 if (new_size
> bytes
) {
101 memset(dst
, 0, new_size
);
103 ws
->buffer_unmap(new_buf
->res
->buf
);
104 ws
->buffer_unmap(old_buf
.res
->buf
);
105 si_vid_destroy_buffer(&old_buf
);
110 ws
->buffer_unmap(old_buf
.res
->buf
);
111 si_vid_destroy_buffer(new_buf
);
116 /* clear the buffer with zeros */
117 void si_vid_clear_buffer(struct pipe_context
*context
, struct rvid_buffer
*buffer
)
119 struct si_context
*sctx
= (struct si_context
*)context
;
121 si_sdma_clear_buffer(sctx
, &buffer
->res
->b
.b
, 0, buffer
->res
->b
.b
.width0
, 0);
122 context
->flush(context
, NULL
, 0);