• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2022 Advanced Micro Devices, Inc.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
17  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19  * OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * Authors: AMD
22  *
23  */
24 
25 #include "background.h"
26 #include "common.h"
27 #include "vpe_priv.h"
28 
vpe_create_bg_segments(struct vpe_priv * vpe_priv,struct vpe_rect * gaps,uint16_t gaps_cnt,enum vpe_cmd_ops ops)29 void vpe_create_bg_segments(
30     struct vpe_priv *vpe_priv, struct vpe_rect *gaps, uint16_t gaps_cnt, enum vpe_cmd_ops ops)
31 {
32     uint16_t            gap_index;
33     struct vpe_cmd_info cmd_info    = {0};
34     struct scaler_data *scaler_data = &(cmd_info.inputs[0].scaler_data);
35     struct stream_ctx  *stream_ctx = &(vpe_priv->stream_ctx[0]);
36     int32_t             vp_x       = stream_ctx->stream.scaling_info.src_rect.x;
37     int32_t             vp_y       = stream_ctx->stream.scaling_info.src_rect.y;
38     uint16_t            src_h_div  = vpe_is_yuv420(stream_ctx->stream.surface_info.format) ? 2 : 1;
39     uint16_t            src_v_div  = vpe_is_yuv420(stream_ctx->stream.surface_info.format) ? 2 : 1;
40     uint16_t            dst_h_div  = vpe_is_yuv420(vpe_priv->output_ctx.surface.format) ? 2 : 1;
41     uint16_t            dst_v_div  = vpe_is_yuv420(vpe_priv->output_ctx.surface.format) ? 2 : 1;
42 
43     for (gap_index = 0; gap_index < gaps_cnt; gap_index++) {
44 
45         /* format */
46         scaler_data->format             = stream_ctx->stream.surface_info.format;
47         scaler_data->lb_params.alpha_en = stream_ctx->per_pixel_alpha;
48 
49         /* recout */
50 
51         scaler_data->recout.x      = 0;
52         scaler_data->recout.y      = 0;
53         scaler_data->recout.height = VPE_MIN_VIEWPORT_SIZE;
54         scaler_data->recout.width  = VPE_MIN_VIEWPORT_SIZE;
55 
56         /* ratios */
57         scaler_data->ratios.horz = vpe_fixpt_one;
58         scaler_data->ratios.vert = vpe_fixpt_one;
59 
60         if (vpe_is_yuv420(scaler_data->format)) {
61             scaler_data->ratios.horz_c = vpe_fixpt_from_fraction(1, 2);
62             scaler_data->ratios.vert_c = vpe_fixpt_from_fraction(1, 2);
63         }
64         else {
65             scaler_data->ratios.horz_c = vpe_fixpt_one;
66             scaler_data->ratios.vert_c = vpe_fixpt_one;
67         }
68 
69         /* Active region */
70         scaler_data->h_active = gaps[gap_index].width;
71         scaler_data->v_active = gaps[gap_index].height;
72 
73         /* viewport */
74 
75         scaler_data->viewport.x      = vp_x;
76         scaler_data->viewport.y      = vp_y;
77         scaler_data->viewport.width  = VPE_MIN_VIEWPORT_SIZE;
78         scaler_data->viewport.height = VPE_MIN_VIEWPORT_SIZE;
79 
80         scaler_data->viewport_c.x      = scaler_data->viewport.x / src_h_div;
81         scaler_data->viewport_c.y      = scaler_data->viewport.y / src_v_div;
82         scaler_data->viewport_c.width  = scaler_data->viewport.width / src_h_div;
83         scaler_data->viewport_c.height = scaler_data->viewport.height / src_v_div;
84 
85         /* destination viewport */
86         scaler_data->dst_viewport = gaps[gap_index];
87 
88         scaler_data->dst_viewport_c.x      = scaler_data->dst_viewport.x / dst_h_div;
89         scaler_data->dst_viewport_c.y      = scaler_data->dst_viewport.y / dst_v_div;
90         scaler_data->dst_viewport_c.width  = scaler_data->dst_viewport.width / dst_h_div;
91         scaler_data->dst_viewport_c.height = scaler_data->dst_viewport.height / dst_v_div;
92 
93         /* taps and inits */
94         scaler_data->taps.h_taps = scaler_data->taps.v_taps = 4;
95         scaler_data->taps.h_taps_c = scaler_data->taps.v_taps_c = 2;
96 
97         scaler_data->inits.h = vpe_fixpt_div_int(
98             vpe_fixpt_add_int(scaler_data->ratios.horz, (int)(scaler_data->taps.h_taps + 1)), 2);
99         scaler_data->inits.v = vpe_fixpt_div_int(
100             vpe_fixpt_add_int(scaler_data->ratios.vert, (int)(scaler_data->taps.v_taps + 1)), 2);
101         scaler_data->inits.h_c = vpe_fixpt_div_int(
102             vpe_fixpt_add_int(scaler_data->ratios.horz_c, (int)(scaler_data->taps.h_taps_c + 1)),
103             2);
104         scaler_data->inits.v_c = vpe_fixpt_div_int(
105             vpe_fixpt_add_int(scaler_data->ratios.vert_c, (int)(scaler_data->taps.v_taps_c + 1)),
106             2);
107 
108         VPE_ASSERT(gaps_cnt - gap_index - 1 <= (uint16_t)0xF);
109 
110         // background takes stream_idx 0 as its input
111         cmd_info.inputs[0].stream_idx      = 0;
112         cmd_info.num_outputs               = 1;
113         cmd_info.outputs[0].dst_viewport   = scaler_data->dst_viewport;
114         cmd_info.outputs[0].dst_viewport_c = scaler_data->dst_viewport_c;
115 
116         cmd_info.num_inputs = 1;
117         cmd_info.ops        = ops;
118         cmd_info.cd         = (uint8_t)(gaps_cnt - gap_index - 1);
119         cmd_info.tm_enabled = false; // currently only support frontend tm
120         vpe_vector_push(vpe_priv->vpe_cmd_vector, &cmd_info);
121     }
122 }
123 
vpe_full_bg_gaps(struct vpe_rect * gaps,const struct vpe_rect * target_rect,uint16_t max_gaps)124 void vpe_full_bg_gaps(struct vpe_rect *gaps, const struct vpe_rect *target_rect, uint16_t max_gaps)
125 {
126     uint16_t gap_index;
127     int32_t  last_covered;
128     uint32_t gap_width, gap_remainder;
129 
130     if (max_gaps == 0) {
131         VPE_ASSERT(0);
132         return;
133     }
134     last_covered  = target_rect->x;
135     gap_width     = target_rect->width / max_gaps;
136     gap_remainder = target_rect->width % max_gaps;
137 
138     for (gap_index = 0; gap_index < max_gaps; gap_index++) {
139         gaps[gap_index].x     = last_covered;
140         gaps[gap_index].y     = target_rect->y;
141         gaps[gap_index].width = gap_width;
142         if (gap_index >= max_gaps - gap_remainder) {
143             gaps[gap_index].width += 1;
144         }
145         gaps[gap_index].height = target_rect->height;
146         last_covered           = last_covered + (int32_t)gaps[gap_index].width;
147     }
148 }
149 
150 /* calculates the gaps in target_rect which are not covered by the first stream
151    and returns the number of gaps */
vpe_find_bg_gaps(struct vpe_priv * vpe_priv,const struct vpe_rect * target_rect,struct vpe_rect * gaps,uint16_t max_gaps)152 uint16_t vpe_find_bg_gaps(struct vpe_priv *vpe_priv, const struct vpe_rect *target_rect,
153     struct vpe_rect *gaps, uint16_t max_gaps)
154 {
155     uint16_t            num_gaps = 0;
156     uint16_t            num_segs;
157     struct vpe_rect    *dst_viewport_rect;
158     bool                full_bg       = false;
159     const uint32_t      max_seg_width = vpe_priv->pub.caps->plane_caps.max_viewport_width;
160     const uint16_t      num_multiple  = vpe_priv->vpe_num_instance ? vpe_priv->vpe_num_instance : 1;
161     struct stream_ctx*  ctx           = &vpe_priv->stream_ctx[0];
162 
163     num_segs          = ctx->num_segments;
164     dst_viewport_rect = &(ctx->segment_ctx[0].scaler_data.dst_viewport);
165 
166     if (ctx->stream_type == VPE_STREAM_TYPE_BG_GEN) {
167         goto full_bg;
168     }
169 
170     if (target_rect->x < dst_viewport_rect->x) {
171 
172         if (target_rect->width <= max_seg_width) {
173             goto full_bg;
174         }
175         gaps[0].x      = target_rect->x;
176         gaps[0].y      = target_rect->y;
177         gaps[0].width  = (uint32_t)(dst_viewport_rect->x - target_rect->x);
178         gaps[0].height = target_rect->height;
179         num_gaps++;
180         if (gaps[0].width > max_seg_width || (num_gaps % num_multiple > 0)) {
181             if (!vpe_priv->resource.split_bg_gap(
182                     gaps, target_rect, max_seg_width, max_gaps, &num_gaps, num_multiple)) {
183                 goto full_bg;
184             }
185         }
186     }
187     dst_viewport_rect =
188         &(ctx->segment_ctx[num_segs - 1].scaler_data.dst_viewport);
189 
190     if (target_rect->x + (int32_t)target_rect->width >
191         dst_viewport_rect->x + (int32_t)dst_viewport_rect->width) {
192 
193         if (num_gaps == max_gaps) {
194             goto full_bg;
195         }
196 
197         gaps[num_gaps].x = dst_viewport_rect->x + (int32_t)dst_viewport_rect->width;
198         gaps[num_gaps].y = target_rect->y;
199         gaps[num_gaps].width =
200             (uint32_t)(target_rect->x + (int32_t)target_rect->width -
201                        (dst_viewport_rect->x + (int32_t)dst_viewport_rect->width));
202         gaps[num_gaps].height = target_rect->height;
203         num_gaps++;
204         if (gaps[num_gaps - 1].width > max_seg_width || (num_gaps % num_multiple > 0)) {
205             if (!vpe_priv->resource.split_bg_gap(
206                     gaps, target_rect, max_seg_width, max_gaps, &num_gaps, num_multiple)) {
207                 goto full_bg;
208             }
209         }
210     }
211     return num_gaps;
212 
213 full_bg:
214     vpe_full_bg_gaps(gaps, target_rect, max_gaps);
215     return max_gaps;
216 }
217