1 /**************************************************************************
2  *
3  * Copyright 2009 Younes Manton.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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 VMWARE AND/OR ITS SUPPLIERS 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.
25  *
26  **************************************************************************/
27 
28 #ifndef vl_compositor_h
29 #define vl_compositor_h
30 
31 #include "pipe/p_state.h"
32 #include "pipe/p_video_codec.h"
33 #include "pipe/p_video_state.h"
34 
35 #include "util/u_rect.h"
36 
37 #include "vl_types.h"
38 #include "vl_csc.h"
39 
40 struct pipe_context;
41 
42 /**
43  * composing and displaying of image data
44  */
45 
46 #define VL_COMPOSITOR_MAX_LAYERS 16
47 #define VL_COMPOSITOR_MIN_DIRTY (0)
48 #define VL_COMPOSITOR_MAX_DIRTY (1 << 15)
49 
50 /* deinterlace allgorithem */
51 enum vl_compositor_deinterlace
52 {
53    VL_COMPOSITOR_NONE,
54    VL_COMPOSITOR_WEAVE,
55    VL_COMPOSITOR_BOB_TOP,
56    VL_COMPOSITOR_BOB_BOTTOM,
57    VL_COMPOSITOR_MOTION_ADAPTIVE
58 };
59 
60 /* clockwise degree */
61 enum vl_compositor_rotation
62 {
63    VL_COMPOSITOR_ROTATE_0,
64    VL_COMPOSITOR_ROTATE_90,
65    VL_COMPOSITOR_ROTATE_180,
66    VL_COMPOSITOR_ROTATE_270
67 };
68 
69 struct vl_compositor_layer
70 {
71    bool clearing;
72 
73    bool viewport_valid;
74    struct pipe_viewport_state viewport;
75 
76    void *fs;
77    void *cs;
78    void *samplers[3];
79    void *blend;
80 
81    struct pipe_sampler_view *sampler_views[3];
82    struct {
83       struct vertex2f tl, br;
84    } src, dst;
85    struct vertex2f zw;
86    struct vertex4f colors[4];
87    enum vl_compositor_rotation rotate;
88 };
89 
90 struct vl_compositor_state
91 {
92    struct pipe_context *pipe;
93 
94    bool scissor_valid;
95    struct pipe_scissor_state scissor;
96    struct pipe_resource *shader_params;
97 
98    union pipe_color_union clear_color;
99 
100    unsigned used_layers:VL_COMPOSITOR_MAX_LAYERS;
101    struct vl_compositor_layer layers[VL_COMPOSITOR_MAX_LAYERS];
102    bool interlaced;
103 };
104 
105 struct vl_compositor
106 {
107    struct pipe_context *pipe;
108 
109    struct pipe_framebuffer_state fb_state;
110    struct pipe_vertex_buffer vertex_buf;
111 
112    void *sampler_linear;
113    void *sampler_nearest;
114    void *blend_clear, *blend_add;
115    void *rast;
116    void *dsa;
117    void *vertex_elems_state;
118 
119    void *vs;
120    void *fs_video_buffer;
121    void *fs_weave_rgb;
122    void *fs_rgba;
123    void *cs_video_buffer;
124    void *cs_weave_rgb;
125    void *cs_rgba;
126 
127    bool pipe_cs_composit_supported;
128    bool pipe_gfx_supported;
129 
130    enum vl_compositor_deinterlace deinterlace;
131 
132    struct {
133       struct {
134          void *y;
135          void *uv;
136       } weave;
137       struct {
138          void *y;
139          void *uv;
140       } bob;
141    } fs_yuv;
142 
143    struct {
144       struct {
145          void *y;
146          void *uv;
147       } weave;
148       struct {
149          void *y;
150          void *uv;
151       } bob;
152    } cs_yuv;
153 
154    struct {
155       void *rgb;
156       void *yuv;
157    } fs_palette;
158 
159    struct {
160       void *y;
161       void *uv;
162    } fs_rgb_yuv;
163 };
164 
165 /**
166  * initialize this compositor
167  */
168 bool
169 vl_compositor_init(struct vl_compositor *compositor, struct pipe_context *pipe);
170 
171 /**
172  * init state bag
173  */
174 bool
175 vl_compositor_init_state(struct vl_compositor_state *state, struct pipe_context *pipe);
176 
177 /**
178  * set yuv -> rgba conversion matrix
179  */
180 bool
181 vl_compositor_set_csc_matrix(struct vl_compositor_state *settings,
182                              const vl_csc_matrix *matrix,
183                              float luma_min, float luma_max);
184 
185 /**
186  * reset dirty area, so it's cleared with the clear colour
187  */
188 void
189 vl_compositor_reset_dirty_area(struct u_rect *dirty);
190 
191 /**
192  * set the clear color
193  */
194 void
195 vl_compositor_set_clear_color(struct vl_compositor_state *settings, union pipe_color_union *color);
196 
197 /**
198  * get the clear color
199  */
200 void
201 vl_compositor_get_clear_color(struct vl_compositor_state *settings, union pipe_color_union *color);
202 
203 /**
204  * set the destination clipping
205  */
206 void
207 vl_compositor_set_dst_clip(struct vl_compositor_state *settings, struct u_rect *dst_clip);
208 
209 /**
210  * set overlay samplers
211  */
212 /*@{*/
213 
214 /**
215  * reset all currently set layers
216  */
217 void
218 vl_compositor_clear_layers(struct vl_compositor_state *state);
219 
220 /**
221  * set the blender used to render a layer
222  */
223 void
224 vl_compositor_set_layer_blend(struct vl_compositor_state *state,
225                               unsigned layer, void *blend, bool is_clearing);
226 
227 /**
228  * set the layer destination area
229  */
230 void
231 vl_compositor_set_layer_dst_area(struct vl_compositor_state *settings,
232                                  unsigned layer, struct u_rect *dst_area);
233 
234 /**
235  * set a video buffer as a layer to render
236  */
237 void
238 vl_compositor_set_buffer_layer(struct vl_compositor_state *state,
239                                struct vl_compositor *compositor,
240                                unsigned layer,
241                                struct pipe_video_buffer *buffer,
242                                struct u_rect *src_rect,
243                                struct u_rect *dst_rect,
244                                enum vl_compositor_deinterlace deinterlace);
245 
246 /**
247  * set a paletted sampler as a layer to render
248  */
249 void
250 vl_compositor_set_palette_layer(struct vl_compositor_state *state,
251                                 struct vl_compositor *compositor,
252                                 unsigned layer,
253                                 struct pipe_sampler_view *indexes,
254                                 struct pipe_sampler_view *palette,
255                                 struct u_rect *src_rect,
256                                 struct u_rect *dst_rect,
257                                 bool include_color_conversion);
258 
259 /**
260  * set a rgba sampler as a layer to render
261  */
262 void
263 vl_compositor_set_rgba_layer(struct vl_compositor_state *state,
264                              struct vl_compositor *compositor,
265                              unsigned layer,
266                              struct pipe_sampler_view *rgba,
267                              struct u_rect *src_rect,
268                              struct u_rect *dst_rect,
269                              struct vertex4f *colors);
270 
271 /**
272  * set the layer rotation
273  */
274 void
275 vl_compositor_set_layer_rotation(struct vl_compositor_state *state,
276                                  unsigned layer,
277                                  enum vl_compositor_rotation rotate);
278 
279 /**
280  * deinterlace yuv buffer with full abilities
281  */
282 void
283 vl_compositor_yuv_deint_full(struct vl_compositor_state *state,
284                              struct vl_compositor *compositor,
285                              struct pipe_video_buffer *src,
286                              struct pipe_video_buffer *dst,
287                              struct u_rect *src_rect,
288                              struct u_rect *dst_rect,
289                              enum vl_compositor_deinterlace deinterlace);
290 
291 /**
292 + * convert rgb to yuv
293 + */
294 void
295 vl_compositor_convert_rgb_to_yuv(struct vl_compositor_state *state,
296                                  struct vl_compositor *compositor,
297                                  unsigned layer,
298                                  struct pipe_resource *src_res,
299                                  struct pipe_video_buffer *dst,
300                                  struct u_rect *src_rect,
301                                  struct u_rect *dst_rect);
302 
303 /*@}*/
304 
305 /**
306  * render the layers to the frontbuffer
307  */
308 void
309 vl_compositor_render(struct vl_compositor_state *state,
310                      struct vl_compositor       *compositor,
311                      struct pipe_surface        *dst_surface,
312                      struct u_rect              *dirty_area,
313                      bool                        clear_dirty);
314 
315 /**
316  * destroy this compositor
317  */
318 void
319 vl_compositor_cleanup(struct vl_compositor *compositor);
320 
321 /**
322  * destroy this state bag
323  */
324 void
325 vl_compositor_cleanup_state(struct vl_compositor_state *state);
326 
327 #endif /* vl_compositor_h */
328