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 **************************************************************************/
30 #include <pipe/p_screen.h>
31 #include <pipe/p_context.h>
32 #include <pipe/p_state.h>
34 #include <util/u_format.h>
35 #include <util/u_inlines.h>
36 #include <util/u_sampler.h>
37 #include <util/u_memory.h>
39 #include "vl_video_buffer.h"
41 const enum pipe_format const_resource_formats_YV12
[3] = {
47 const enum pipe_format const_resource_formats_NV12
[3] = {
49 PIPE_FORMAT_R8G8_UNORM
,
53 const enum pipe_format
*
54 vl_video_buffer_formats(struct pipe_screen
*screen
, enum pipe_format format
)
57 case PIPE_FORMAT_YV12
:
58 return const_resource_formats_YV12
;
60 case PIPE_FORMAT_NV12
:
61 return const_resource_formats_NV12
;
69 vl_video_buffer_is_format_supported(struct pipe_screen
*screen
,
70 enum pipe_format format
,
71 enum pipe_video_profile profile
)
73 const enum pipe_format
*resource_formats
;
76 resource_formats
= vl_video_buffer_formats(screen
, format
);
77 if (!resource_formats
)
80 for(i
= 0; i
< VL_MAX_PLANES
; ++i
) {
81 if (!resource_formats
[i
])
84 if (!screen
->is_format_supported(screen
, resource_formats
[i
], PIPE_TEXTURE_2D
, 0, PIPE_USAGE_STATIC
))
92 vl_video_buffer_max_size(struct pipe_screen
*screen
)
94 uint32_t max_2d_texture_level
;
96 max_2d_texture_level
= screen
->get_param(screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
);
98 return 1 << (max_2d_texture_level
-1);
102 vl_video_buffer_destroy(struct pipe_video_buffer
*buffer
)
104 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
109 for (i
= 0; i
< VL_MAX_PLANES
; ++i
) {
110 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
111 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
112 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
113 pipe_resource_reference(&buf
->resources
[i
], NULL
);
119 static struct pipe_sampler_view
**
120 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer
*buffer
)
122 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
123 struct pipe_sampler_view sv_templ
;
124 struct pipe_context
*pipe
;
129 pipe
= buf
->base
.context
;
131 for (i
= 0; i
< buf
->num_planes
; ++i
) {
132 if (!buf
->sampler_view_planes
[i
]) {
133 memset(&sv_templ
, 0, sizeof(sv_templ
));
134 u_sampler_view_default_template(&sv_templ
, buf
->resources
[i
], buf
->resources
[i
]->format
);
136 if (util_format_get_nr_components(buf
->resources
[i
]->format
) == 1)
137 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= sv_templ
.swizzle_a
= PIPE_SWIZZLE_RED
;
139 buf
->sampler_view_planes
[i
] = pipe
->create_sampler_view(pipe
, buf
->resources
[i
], &sv_templ
);
140 if (!buf
->sampler_view_planes
[i
])
145 return buf
->sampler_view_planes
;
148 for (i
= 0; i
< buf
->num_planes
; ++i
)
149 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
154 static struct pipe_sampler_view
**
155 vl_video_buffer_sampler_view_components(struct pipe_video_buffer
*buffer
)
157 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
158 struct pipe_sampler_view sv_templ
;
159 struct pipe_context
*pipe
;
160 unsigned i
, j
, component
;
164 pipe
= buf
->base
.context
;
166 for (component
= 0, i
= 0; i
< buf
->num_planes
; ++i
) {
167 unsigned nr_components
= util_format_get_nr_components(buf
->resources
[i
]->format
);
169 for (j
= 0; j
< nr_components
; ++j
, ++component
) {
170 assert(component
< VL_MAX_PLANES
);
172 if (!buf
->sampler_view_components
[component
]) {
173 memset(&sv_templ
, 0, sizeof(sv_templ
));
174 u_sampler_view_default_template(&sv_templ
, buf
->resources
[i
], buf
->resources
[i
]->format
);
175 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= PIPE_SWIZZLE_RED
+ j
;
176 sv_templ
.swizzle_a
= PIPE_SWIZZLE_ONE
;
177 buf
->sampler_view_components
[component
] = pipe
->create_sampler_view(pipe
, buf
->resources
[i
], &sv_templ
);
178 if (!buf
->sampler_view_components
[component
])
184 return buf
->sampler_view_components
;
187 for (i
= 0; i
< VL_MAX_PLANES
; ++i
)
188 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
193 static struct pipe_surface
**
194 vl_video_buffer_surfaces(struct pipe_video_buffer
*buffer
)
196 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
197 struct pipe_surface surf_templ
;
198 struct pipe_context
*pipe
;
203 pipe
= buf
->base
.context
;
205 for (i
= 0; i
< buf
->num_planes
; ++i
) {
206 if (!buf
->surfaces
[i
]) {
207 memset(&surf_templ
, 0, sizeof(surf_templ
));
208 surf_templ
.format
= buf
->resources
[i
]->format
;
209 surf_templ
.usage
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
210 buf
->surfaces
[i
] = pipe
->create_surface(pipe
, buf
->resources
[i
], &surf_templ
);
211 if (!buf
->surfaces
[i
])
216 return buf
->surfaces
;
219 for (i
= 0; i
< buf
->num_planes
; ++i
)
220 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
225 struct pipe_video_buffer
*
226 vl_video_buffer_create(struct pipe_context
*pipe
,
227 enum pipe_format buffer_format
,
228 enum pipe_video_chroma_format chroma_format
,
229 unsigned width
, unsigned height
)
231 const enum pipe_format
*resource_formats
;
232 struct pipe_video_buffer
*result
;
233 unsigned buffer_width
, buffer_height
;
237 assert(width
> 0 && height
> 0);
239 pot_buffers
= !pipe
->screen
->get_video_param
242 PIPE_VIDEO_PROFILE_UNKNOWN
,
243 PIPE_VIDEO_CAP_NPOT_TEXTURES
246 resource_formats
= vl_video_buffer_formats(pipe
->screen
, buffer_format
);
247 if (!resource_formats
)
250 buffer_width
= pot_buffers
? util_next_power_of_two(width
) : align(width
, MACROBLOCK_WIDTH
);
251 buffer_height
= pot_buffers
? util_next_power_of_two(height
) : align(height
, MACROBLOCK_HEIGHT
);
253 result
= vl_video_buffer_create_ex
255 pipe
, buffer_width
, buffer_height
, 1,
256 chroma_format
, resource_formats
, PIPE_USAGE_STATIC
259 result
->buffer_format
= buffer_format
;
264 struct pipe_video_buffer
*
265 vl_video_buffer_create_ex(struct pipe_context
*pipe
,
266 unsigned width
, unsigned height
, unsigned depth
,
267 enum pipe_video_chroma_format chroma_format
,
268 const enum pipe_format resource_formats
[VL_MAX_PLANES
],
271 struct vl_video_buffer
*buffer
;
272 struct pipe_resource templ
;
277 buffer
= CALLOC_STRUCT(vl_video_buffer
);
279 buffer
->base
.context
= pipe
;
280 buffer
->base
.destroy
= vl_video_buffer_destroy
;
281 buffer
->base
.get_sampler_view_planes
= vl_video_buffer_sampler_view_planes
;
282 buffer
->base
.get_sampler_view_components
= vl_video_buffer_sampler_view_components
;
283 buffer
->base
.get_surfaces
= vl_video_buffer_surfaces
;
284 buffer
->base
.chroma_format
= chroma_format
;
285 buffer
->base
.width
= width
;
286 buffer
->base
.height
= height
;
287 buffer
->num_planes
= 1;
289 memset(&templ
, 0, sizeof(templ
));
290 templ
.target
= depth
> 1 ? PIPE_TEXTURE_3D
: PIPE_TEXTURE_2D
;
291 templ
.format
= resource_formats
[0];
292 templ
.width0
= width
;
293 templ
.height0
= height
;
294 templ
.depth0
= depth
;
295 templ
.array_size
= 1;
296 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
299 buffer
->resources
[0] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
300 if (!buffer
->resources
[0])
303 if (resource_formats
[1] == PIPE_FORMAT_NONE
) {
304 assert(chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_444
);
305 assert(resource_formats
[2] == PIPE_FORMAT_NONE
);
306 return &buffer
->base
;
308 buffer
->num_planes
= 2;
310 templ
.format
= resource_formats
[1];
311 if (chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
314 } else if (chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
318 buffer
->resources
[1] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
319 if (!buffer
->resources
[1])
322 if (resource_formats
[2] == PIPE_FORMAT_NONE
)
323 return &buffer
->base
;
325 buffer
->num_planes
= 3;
327 templ
.format
= resource_formats
[2];
328 buffer
->resources
[2] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
329 if (!buffer
->resources
[2])
332 return &buffer
->base
;
335 for (i
= 0; i
< VL_MAX_PLANES
; ++i
)
336 pipe_resource_reference(&buffer
->resources
[i
], NULL
);