1 /**************************************************************************
3 * Copyright 2011 Christian König.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
26 **************************************************************************/
28 #include "vl_video_buffer.h"
29 #include <util/u_format.h>
30 #include <util/u_inlines.h>
31 #include <util/u_sampler.h>
32 #include <pipe/p_screen.h>
33 #include <pipe/p_context.h>
36 bool vl_video_buffer_init(struct vl_video_buffer
*buffer
,
37 struct pipe_context
*pipe
,
38 unsigned width
, unsigned height
, unsigned depth
,
39 enum pipe_video_chroma_format chroma_format
,
41 const enum pipe_format resource_format
[VL_MAX_PLANES
],
44 struct pipe_resource templ
;
47 assert(buffer
&& pipe
);
48 assert(num_planes
> 0 && num_planes
<= VL_MAX_PLANES
);
50 memset(buffer
, 0, sizeof(struct vl_video_buffer
));
52 buffer
->num_planes
= num_planes
;
54 memset(&templ
, 0, sizeof(templ
));
55 templ
.target
= PIPE_TEXTURE_2D
;
56 templ
.format
= resource_format
[0];
58 templ
.height0
= height
;
61 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
64 buffer
->resources
[0] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
65 if (!buffer
->resources
[0])
68 if (num_planes
== 1) {
69 assert(chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_444
);
73 templ
.format
= resource_format
[1];
74 if (chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
80 } else if (chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
87 buffer
->resources
[1] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
88 if (!buffer
->resources
[1])
94 templ
.format
= resource_format
[2];
95 buffer
->resources
[2] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
96 if (!buffer
->resources
[2])
102 for (i
= 0; i
< VL_MAX_PLANES
; ++i
)
103 pipe_resource_reference(&buffer
->resources
[i
], NULL
);
109 adjust_swizzle(struct pipe_sampler_view
*sv_templ
)
111 if (util_format_get_nr_components(sv_templ
->format
) == 1) {
112 sv_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
113 sv_templ
->swizzle_g
= PIPE_SWIZZLE_RED
;
114 sv_templ
->swizzle_b
= PIPE_SWIZZLE_RED
;
115 sv_templ
->swizzle_a
= PIPE_SWIZZLE_RED
;
119 vl_sampler_views
*vl_video_buffer_sampler_views(struct vl_video_buffer
*buffer
)
121 struct pipe_sampler_view sv_templ
;
122 struct pipe_context
*pipe
;
129 for (i
= 0; i
< buffer
->num_planes
; ++i
) {
130 if (!buffer
->sampler_views
[i
]) {
131 memset(&sv_templ
, 0, sizeof(sv_templ
));
132 u_sampler_view_default_template(&sv_templ
, buffer
->resources
[i
], buffer
->resources
[i
]->format
);
133 adjust_swizzle(&sv_templ
);
134 buffer
->sampler_views
[i
] = pipe
->create_sampler_view(pipe
, buffer
->resources
[i
], &sv_templ
);
135 if (!buffer
->sampler_views
[i
])
140 return &buffer
->sampler_views
;
143 for (i
= 0; i
< buffer
->num_planes
; ++i
)
144 pipe_sampler_view_reference(&buffer
->sampler_views
[i
], NULL
);
149 vl_surfaces
*vl_video_buffer_surfaces(struct vl_video_buffer
*buffer
)
151 struct pipe_surface surf_templ
;
152 struct pipe_context
*pipe
;
159 for (i
= 0; i
< buffer
->num_planes
; ++i
) {
160 if (!buffer
->surfaces
[i
]) {
161 memset(&surf_templ
, 0, sizeof(surf_templ
));
162 surf_templ
.format
= buffer
->resources
[i
]->format
;
163 surf_templ
.usage
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
164 buffer
->surfaces
[i
] = pipe
->create_surface(pipe
, buffer
->resources
[i
], &surf_templ
);
165 if (!buffer
->surfaces
[i
])
170 return &buffer
->surfaces
;
173 for (i
= 0; i
< buffer
->num_planes
; ++i
)
174 pipe_surface_reference(&buffer
->surfaces
[i
], NULL
);
179 void vl_video_buffer_cleanup(struct vl_video_buffer
*buffer
)
185 for (i
= 0; i
< VL_MAX_PLANES
; ++i
) {
186 pipe_surface_reference(&buffer
->surfaces
[i
], NULL
);
187 pipe_sampler_view_reference(&buffer
->sampler_views
[i
], NULL
);
188 pipe_resource_reference(&buffer
->resources
[i
], NULL
);