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