15b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/**************************************************************************
25b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
35b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * Copyright 2011 Advanced Micro Devices, Inc.
45b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * All Rights Reserved.
55b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
65b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * Permission is hereby granted, free of charge, to any person obtaining a
75b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * copy of this software and associated documentation files (the
85b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * "Software"), to deal in the Software without restriction, including
95b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * without limitation the rights to use, copy, modify, merge, publish,
105b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * distribute, sub license, and/or sell copies of the Software, and to
115b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * permit persons to whom the Software is furnished to do so, subject to
125b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * the following conditions:
135b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
145b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * The above copyright notice and this permission notice (including the
155b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * next paragraph) shall be included in all copies or substantial portions
165b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * of the Software.
175b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
185b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
195b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
205b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
215b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
225b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
235b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
245b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
255b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
265b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König **************************************************************************/
275b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
285b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/*
295b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * Authors:
305b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *      Christian König <christian.koenig@amd.com>
315b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König *
325b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König */
335b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
34786af2f963925df2c2a6fb60b29a83e8340f03c7Andreas Hartmetz#include "si_pipe.h"
357bcfb0bc8ffed0db244cf0b3d4e200add590a02aChristian König#include "radeon/radeon_video.h"
365b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König#include "radeon/radeon_uvd.h"
379ff0cf903dc4ed806961d1e099d1308322092892Christian König#include "radeon/radeon_vce.h"
385b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
395b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/**
405b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * creates an video buffer with an UVD compatible memory layout
415b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König */
4245578def716f17e4588c6567a5fb3b6dc9569aecAndreas Hartmetzstruct pipe_video_buffer *si_video_buffer_create(struct pipe_context *pipe,
4345578def716f17e4588c6567a5fb3b6dc9569aecAndreas Hartmetz						 const struct pipe_video_buffer *tmpl)
445b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König{
45238aeabce0e5cfd850279a68fe0c816adc175294Andreas Hartmetz	struct si_context *ctx = (struct si_context *)pipe;
46363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák	struct r600_texture *resources[VL_NUM_COMPONENTS] = {};
47a582b22c6382f24d921e9fe8a24917100c1396f1Marek Olšák	struct radeon_surf *surfaces[VL_NUM_COMPONENTS] = {};
485b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	struct pb_buffer **pbs[VL_NUM_COMPONENTS] = {};
495b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	const enum pipe_format *resource_formats;
505b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	struct pipe_video_buffer template;
515b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	struct pipe_resource templ;
527d2f2a0c890b1993532a45c8c392c28950ddc06eChristian König	unsigned i, array_size;
535b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
545b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	assert(pipe);
555b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
565b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	/* first create the needed resources as "normal" textures */
575b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	resource_formats = vl_video_buffer_formats(pipe->screen, tmpl->buffer_format);
585b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	if (!resource_formats)
595b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		return NULL;
605b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
617d2f2a0c890b1993532a45c8c392c28950ddc06eChristian König	array_size = tmpl->interlaced ? 2 : 1;
625b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	template = *tmpl;
635b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	template.width = align(tmpl->width, VL_MACROBLOCK_WIDTH);
647d2f2a0c890b1993532a45c8c392c28950ddc06eChristian König	template.height = align(tmpl->height / array_size, VL_MACROBLOCK_HEIGHT);
655b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
66c32114460dbb7f33885c181a0d7dee07b15b8751Marek Olšák	vl_video_buffer_template(&templ, &template, resource_formats[0], 1, array_size, PIPE_USAGE_DEFAULT, 0);
67270fab5164d04bd8ce5f322c1a4d6ca39734d39aGrigori Goronzy	/* TODO: get tiling working */
68270fab5164d04bd8ce5f322c1a4d6ca39734d39aGrigori Goronzy	templ.bind = PIPE_BIND_LINEAR;
69363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák	resources[0] = (struct r600_texture *)
705b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		pipe->screen->resource_create(pipe->screen, &templ);
715b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	if (!resources[0])
725b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		goto error;
735b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
745b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	if (resource_formats[1] != PIPE_FORMAT_NONE) {
75c32114460dbb7f33885c181a0d7dee07b15b8751Marek Olšák		vl_video_buffer_template(&templ, &template, resource_formats[1], 1, array_size, PIPE_USAGE_DEFAULT, 1);
76270fab5164d04bd8ce5f322c1a4d6ca39734d39aGrigori Goronzy		templ.bind = PIPE_BIND_LINEAR;
77363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák		resources[1] = (struct r600_texture *)
785b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			pipe->screen->resource_create(pipe->screen, &templ);
795b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		if (!resources[1])
805b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			goto error;
815b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	}
825b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
835b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	if (resource_formats[2] != PIPE_FORMAT_NONE) {
84c32114460dbb7f33885c181a0d7dee07b15b8751Marek Olšák		vl_video_buffer_template(&templ, &template, resource_formats[2], 1, array_size, PIPE_USAGE_DEFAULT, 2);
85270fab5164d04bd8ce5f322c1a4d6ca39734d39aGrigori Goronzy		templ.bind = PIPE_BIND_LINEAR;
86363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák		resources[2] = (struct r600_texture *)
875b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			pipe->screen->resource_create(pipe->screen, &templ);
885b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		if (!resources[2])
895b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			goto error;
905b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	}
915b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
925b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
935b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		if (!resources[i])
945b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			continue;
955b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
965b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		surfaces[i] = & resources[i]->surface;
975b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		pbs[i] = &resources[i]->resource.buf;
985b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	}
995b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
100c868974396e95d900c7754bce38c0c950f6e3ab6Nicolai Hähnle	rvid_join_surfaces(ctx->b.ws, pbs, surfaces);
1015b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1025b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	for (i = 0; i < VL_NUM_COMPONENTS; ++i) {
1035b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König		if (!resources[i])
1045b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König			continue;
1055b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
106cf811faeff1eaa1aef817ae45314cc3419c44222Marek Olšák		/* reset the address */
10783012b50854c795294b4b9e8b2766bb5258dafeaChristian König		resources[i]->resource.gpu_address = ctx->b.ws->buffer_get_virtual_address(
108cf811faeff1eaa1aef817ae45314cc3419c44222Marek Olšák			resources[i]->resource.buf);
1095b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	}
1105b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1117d2f2a0c890b1993532a45c8c392c28950ddc06eChristian König	template.height *= array_size;
1125b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	return vl_video_buffer_create_ex2(pipe, &template, (struct pipe_resource **)resources);
1135b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1145b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian Königerror:
1155b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	for (i = 0; i < VL_NUM_COMPONENTS; ++i)
11636cf5a57c2b53b50778482f7341b7afcdc434dafMarek Olšák		r600_texture_reference(&resources[i], NULL);
1175b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1185b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	return NULL;
1195b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König}
1205b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1215b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/* set the decoding target buffer offsets */
122cf811faeff1eaa1aef817ae45314cc3419c44222Marek Olšákstatic struct pb_buffer* si_uvd_set_dtb(struct ruvd_msg *msg, struct vl_video_buffer *buf)
1235b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König{
124363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák	struct r600_texture *luma = (struct r600_texture *)buf->resources[0];
125363b2805f7af8e9f20700eceddb107f0e1cdfa6cMarek Olšák	struct r600_texture *chroma = (struct r600_texture *)buf->resources[1];
1265b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1272c2c54b819d8e0bd940f56501a2c3699d7cc2397Christian König	msg->body.decode.dt_field_mode = buf->base.interlaced;
1285b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1295b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König	ruvd_set_dt_surfaces(msg, &luma->surface, &chroma->surface);
1305b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
131cf811faeff1eaa1aef817ae45314cc3419c44222Marek Olšák	return luma->resource.buf;
1325b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König}
1335b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König
1349ff0cf903dc4ed806961d1e099d1308322092892Christian König/* get the radeon resources for VCE */
1359ff0cf903dc4ed806961d1e099d1308322092892Christian Königstatic void si_vce_get_buffer(struct pipe_resource *resource,
136cf811faeff1eaa1aef817ae45314cc3419c44222Marek Olšák			      struct pb_buffer **handle,
137a582b22c6382f24d921e9fe8a24917100c1396f1Marek Olšák			      struct radeon_surf **surface)
1389ff0cf903dc4ed806961d1e099d1308322092892Christian König{
1399ff0cf903dc4ed806961d1e099d1308322092892Christian König	struct r600_texture *res = (struct r600_texture *)resource;
1409ff0cf903dc4ed806961d1e099d1308322092892Christian König
1419ff0cf903dc4ed806961d1e099d1308322092892Christian König	if (handle)
142cf811faeff1eaa1aef817ae45314cc3419c44222Marek Olšák		*handle = res->resource.buf;
1439ff0cf903dc4ed806961d1e099d1308322092892Christian König
1449ff0cf903dc4ed806961d1e099d1308322092892Christian König	if (surface)
1459ff0cf903dc4ed806961d1e099d1308322092892Christian König		*surface = &res->surface;
1469ff0cf903dc4ed806961d1e099d1308322092892Christian König}
1479ff0cf903dc4ed806961d1e099d1308322092892Christian König
1485b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König/**
1495b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König * creates an UVD compatible decoder
1505b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König */
15145578def716f17e4588c6567a5fb3b6dc9569aecAndreas Hartmetzstruct pipe_video_codec *si_uvd_create_decoder(struct pipe_context *context,
15245578def716f17e4588c6567a5fb3b6dc9569aecAndreas Hartmetz					       const struct pipe_video_codec *templ)
1535b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König{
1549ff0cf903dc4ed806961d1e099d1308322092892Christian König	struct si_context *ctx = (struct si_context *)context;
1559ff0cf903dc4ed806961d1e099d1308322092892Christian König
1569ff0cf903dc4ed806961d1e099d1308322092892Christian König        if (templ->entrypoint == PIPE_VIDEO_ENTRYPOINT_ENCODE)
1579ff0cf903dc4ed806961d1e099d1308322092892Christian König                return rvce_create_encoder(context, templ, ctx->b.ws, si_vce_get_buffer);
1589ff0cf903dc4ed806961d1e099d1308322092892Christian König
1591b79764f495c769f3d56a4229085e313f23c9e3eAndreas Hartmetz	return ruvd_create_decoder(context, templ, si_uvd_set_dtb);
1605b2855bfe79bfc3995969f2bf775d89b1bc1808aChristian König}
161