Path: blob/21.2-virgl/src/gallium/drivers/radeon/radeon_video.c
4570 views
/**************************************************************************1*2* Copyright 2013 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "radeon_video.h"2829#include "radeon_vce.h"30#include "radeonsi/si_pipe.h"31#include "util/u_memory.h"32#include "util/u_video.h"33#include "vl/vl_defines.h"34#include "vl/vl_video_buffer.h"3536#include <unistd.h>3738/* generate an stream handle */39unsigned si_vid_alloc_stream_handle()40{41static unsigned counter = 0;42unsigned stream_handle = 0;43unsigned pid = getpid();44int i;4546for (i = 0; i < 32; ++i)47stream_handle |= ((pid >> i) & 1) << (31 - i);4849stream_handle ^= ++counter;50return stream_handle;51}5253/* create a buffer in the winsys */54bool si_vid_create_buffer(struct pipe_screen *screen, struct rvid_buffer *buffer, unsigned size,55unsigned usage)56{57memset(buffer, 0, sizeof(*buffer));58buffer->usage = usage;5960/* Hardware buffer placement restrictions require the kernel to be61* able to move buffers around individually, so request a62* non-sub-allocated buffer.63*/64buffer->res = si_resource(pipe_buffer_create(screen, PIPE_BIND_SHARED, usage, size));6566return buffer->res != NULL;67}6869/* create a tmz buffer in the winsys */70bool si_vid_create_tmz_buffer(struct pipe_screen *screen, struct rvid_buffer *buffer, unsigned size,71unsigned usage)72{73memset(buffer, 0, sizeof(*buffer));74buffer->usage = usage;75buffer->res = si_resource(pipe_buffer_create(screen, PIPE_BIND_SHARED | PIPE_BIND_PROTECTED,76usage, size));77return buffer->res != NULL;78}798081/* destroy a buffer */82void si_vid_destroy_buffer(struct rvid_buffer *buffer)83{84si_resource_reference(&buffer->res, NULL);85}8687/* reallocate a buffer, preserving its content */88bool si_vid_resize_buffer(struct pipe_screen *screen, struct radeon_cmdbuf *cs,89struct rvid_buffer *new_buf, unsigned new_size)90{91struct si_screen *sscreen = (struct si_screen *)screen;92struct radeon_winsys *ws = sscreen->ws;93unsigned bytes = MIN2(new_buf->res->buf->size, new_size);94struct rvid_buffer old_buf = *new_buf;95void *src = NULL, *dst = NULL;9697if (!si_vid_create_buffer(screen, new_buf, new_size, new_buf->usage))98goto error;99100src = ws->buffer_map(ws, old_buf.res->buf, cs, PIPE_MAP_READ | RADEON_MAP_TEMPORARY);101if (!src)102goto error;103104dst = ws->buffer_map(ws, new_buf->res->buf, cs, PIPE_MAP_WRITE | RADEON_MAP_TEMPORARY);105if (!dst)106goto error;107108memcpy(dst, src, bytes);109if (new_size > bytes) {110new_size -= bytes;111dst += bytes;112memset(dst, 0, new_size);113}114ws->buffer_unmap(ws, new_buf->res->buf);115ws->buffer_unmap(ws, old_buf.res->buf);116si_vid_destroy_buffer(&old_buf);117return true;118119error:120if (src)121ws->buffer_unmap(ws, old_buf.res->buf);122si_vid_destroy_buffer(new_buf);123*new_buf = old_buf;124return false;125}126127/* clear the buffer with zeros */128void si_vid_clear_buffer(struct pipe_context *context, struct rvid_buffer *buffer)129{130struct si_context *sctx = (struct si_context *)context;131uint32_t zero = 0;132133sctx->b.clear_buffer(&sctx->b, &buffer->res->b.b, 0, buffer->res->b.b.width0, &zero, 4);134context->flush(context, NULL, 0);135}136137138