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 const_resource_formats_YUVA
[3] = {
54 PIPE_FORMAT_R8G8B8A8_UNORM
,
59 const enum pipe_format const_resource_formats_VUYA
[3] = {
60 PIPE_FORMAT_B8G8R8A8_UNORM
,
65 const unsigned const_resource_plane_order_YUV
[3] = {
71 const unsigned const_resource_plane_order_YVU
[3] = {
77 const enum pipe_format
*
78 vl_video_buffer_formats(struct pipe_screen
*screen
, enum pipe_format format
)
81 case PIPE_FORMAT_YV12
:
82 return const_resource_formats_YV12
;
84 case PIPE_FORMAT_NV12
:
85 return const_resource_formats_NV12
;
87 case PIPE_FORMAT_R8G8B8A8_UNORM
:
88 return const_resource_formats_YUVA
;
90 case PIPE_FORMAT_B8G8R8A8_UNORM
:
91 return const_resource_formats_VUYA
;
99 vl_video_buffer_plane_order(enum pipe_format format
)
102 case PIPE_FORMAT_YV12
:
103 return const_resource_plane_order_YVU
;
105 case PIPE_FORMAT_NV12
:
106 case PIPE_FORMAT_R8G8B8A8_UNORM
:
107 case PIPE_FORMAT_B8G8R8A8_UNORM
:
108 return const_resource_plane_order_YUV
;
116 vl_video_buffer_is_format_supported(struct pipe_screen
*screen
,
117 enum pipe_format format
,
118 enum pipe_video_profile profile
)
120 const enum pipe_format
*resource_formats
;
123 resource_formats
= vl_video_buffer_formats(screen
, format
);
124 if (!resource_formats
)
127 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
128 if (!resource_formats
[i
])
131 if (!screen
->is_format_supported(screen
, resource_formats
[i
], PIPE_TEXTURE_2D
, 0, PIPE_USAGE_STATIC
))
139 vl_video_buffer_max_size(struct pipe_screen
*screen
)
141 uint32_t max_2d_texture_level
;
143 max_2d_texture_level
= screen
->get_param(screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
);
145 return 1 << (max_2d_texture_level
-1);
149 vl_video_buffer_set_associated_data(struct pipe_video_buffer
*vbuf
,
150 struct pipe_video_decoder
*vdec
,
151 void *associated_data
,
152 void (*destroy_associated_data
)(void *))
154 vbuf
->decoder
= vdec
;
156 if (vbuf
->associated_data
== associated_data
)
159 if (vbuf
->associated_data
)
160 vbuf
->destroy_associated_data(vbuf
->associated_data
);
162 vbuf
->associated_data
= associated_data
;
163 vbuf
->destroy_associated_data
= destroy_associated_data
;
167 vl_video_buffer_get_associated_data(struct pipe_video_buffer
*vbuf
,
168 struct pipe_video_decoder
*vdec
)
170 if (vbuf
->decoder
== vdec
)
171 return vbuf
->associated_data
;
177 vl_vide_buffer_template(struct pipe_resource
*templ
,
178 const struct pipe_video_buffer
*tmpl
,
179 enum pipe_format resource_format
,
180 unsigned depth
, unsigned usage
, unsigned plane
)
182 memset(templ
, 0, sizeof(*templ
));
183 templ
->target
= depth
> 1 ? PIPE_TEXTURE_3D
: PIPE_TEXTURE_2D
;
184 templ
->format
= resource_format
;
185 templ
->width0
= tmpl
->width
;
186 templ
->height0
= tmpl
->height
;
187 templ
->depth0
= depth
;
188 templ
->array_size
= 1;
189 templ
->bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
190 templ
->usage
= usage
;
193 if (tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
196 } else if (tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
203 vl_video_buffer_destroy(struct pipe_video_buffer
*buffer
)
205 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
210 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
211 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
212 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
213 pipe_resource_reference(&buf
->resources
[i
], NULL
);
216 for (i
= 0; i
< VL_NUM_COMPONENTS
* 2; ++i
)
217 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
219 vl_video_buffer_set_associated_data(buffer
, NULL
, NULL
, NULL
);
224 static struct pipe_sampler_view
**
225 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer
*buffer
)
227 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
228 struct pipe_sampler_view sv_templ
;
229 struct pipe_context
*pipe
;
234 pipe
= buf
->base
.context
;
236 for (i
= 0; i
< buf
->num_planes
; ++i
) {
237 if (!buf
->sampler_view_planes
[i
]) {
238 memset(&sv_templ
, 0, sizeof(sv_templ
));
239 u_sampler_view_default_template(&sv_templ
, buf
->resources
[i
], buf
->resources
[i
]->format
);
241 if (util_format_get_nr_components(buf
->resources
[i
]->format
) == 1)
242 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= sv_templ
.swizzle_a
= PIPE_SWIZZLE_RED
;
244 buf
->sampler_view_planes
[i
] = pipe
->create_sampler_view(pipe
, buf
->resources
[i
], &sv_templ
);
245 if (!buf
->sampler_view_planes
[i
])
250 return buf
->sampler_view_planes
;
253 for (i
= 0; i
< buf
->num_planes
; ++i
)
254 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
259 static struct pipe_sampler_view
**
260 vl_video_buffer_sampler_view_components(struct pipe_video_buffer
*buffer
)
262 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
263 struct pipe_sampler_view sv_templ
;
264 struct pipe_context
*pipe
;
265 const unsigned *plane_order
;
266 unsigned i
, j
, component
;
270 pipe
= buf
->base
.context
;
272 plane_order
= vl_video_buffer_plane_order(buf
->base
.buffer_format
);
274 for (component
= 0, i
= 0; i
< buf
->num_planes
; ++i
) {
275 struct pipe_resource
*res
= buf
->resources
[plane_order
[i
]];
276 unsigned nr_components
= util_format_get_nr_components(res
->format
);
278 for (j
= 0; j
< nr_components
; ++j
, ++component
) {
279 assert(component
< VL_NUM_COMPONENTS
);
281 if (!buf
->sampler_view_components
[component
]) {
282 memset(&sv_templ
, 0, sizeof(sv_templ
));
283 u_sampler_view_default_template(&sv_templ
, res
, res
->format
);
284 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= PIPE_SWIZZLE_RED
+ j
;
285 sv_templ
.swizzle_a
= PIPE_SWIZZLE_ONE
;
286 buf
->sampler_view_components
[component
] = pipe
->create_sampler_view(pipe
, res
, &sv_templ
);
287 if (!buf
->sampler_view_components
[component
])
293 return buf
->sampler_view_components
;
296 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
)
297 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
302 static struct pipe_surface
**
303 vl_video_buffer_surfaces(struct pipe_video_buffer
*buffer
)
305 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
306 struct pipe_surface surf_templ
;
307 struct pipe_context
*pipe
;
308 unsigned i
, j
, depth
, surf
;
312 pipe
= buf
->base
.context
;
314 depth
= buffer
->interlaced
? 2 : 1;
315 for (i
= 0, surf
= 0; i
< depth
; ++i
) {
316 for (j
= 0; j
< VL_NUM_COMPONENTS
; ++j
, ++surf
) {
317 assert(surf
< (VL_NUM_COMPONENTS
* 2));
319 if (!buf
->resources
[j
]) {
320 pipe_surface_reference(&buf
->surfaces
[surf
], NULL
);
324 if (!buf
->surfaces
[surf
]) {
325 memset(&surf_templ
, 0, sizeof(surf_templ
));
326 surf_templ
.format
= buf
->resources
[j
]->format
;
327 surf_templ
.usage
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
328 surf_templ
.u
.tex
.first_layer
= surf_templ
.u
.tex
.last_layer
= i
;
329 buf
->surfaces
[surf
] = pipe
->create_surface(pipe
, buf
->resources
[j
], &surf_templ
);
330 if (!buf
->surfaces
[surf
])
336 return buf
->surfaces
;
339 for (i
= 0; i
< (VL_NUM_COMPONENTS
* 2); ++i
)
340 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
345 struct pipe_video_buffer
*
346 vl_video_buffer_create(struct pipe_context
*pipe
,
347 const struct pipe_video_buffer
*tmpl
)
349 const enum pipe_format
*resource_formats
;
350 struct pipe_video_buffer templat
, *result
;
354 assert(tmpl
->width
> 0 && tmpl
->height
> 0);
356 pot_buffers
= !pipe
->screen
->get_video_param
359 PIPE_VIDEO_PROFILE_UNKNOWN
,
360 PIPE_VIDEO_CAP_NPOT_TEXTURES
363 resource_formats
= vl_video_buffer_formats(pipe
->screen
, tmpl
->buffer_format
);
364 if (!resource_formats
)
368 templat
.width
= pot_buffers
? util_next_power_of_two(tmpl
->width
)
369 : align(tmpl
->width
, VL_MACROBLOCK_WIDTH
);
370 templat
.height
= pot_buffers
? util_next_power_of_two(tmpl
->height
)
371 : align(tmpl
->height
, VL_MACROBLOCK_HEIGHT
);
373 if (tmpl
->interlaced
)
376 result
= vl_video_buffer_create_ex
378 pipe
, &templat
, resource_formats
,
379 tmpl
->interlaced
? 2 : 1, PIPE_USAGE_STATIC
383 if (result
&& tmpl
->interlaced
)
389 struct pipe_video_buffer
*
390 vl_video_buffer_create_ex(struct pipe_context
*pipe
,
391 const struct pipe_video_buffer
*tmpl
,
392 const enum pipe_format resource_formats
[VL_NUM_COMPONENTS
],
393 unsigned depth
, unsigned usage
)
395 struct pipe_resource res_tmpl
;
396 struct pipe_resource
*resources
[VL_NUM_COMPONENTS
];
401 memset(resources
, 0, sizeof resources
);
403 vl_vide_buffer_template(&res_tmpl
, tmpl
, resource_formats
[0], depth
, usage
, 0);
404 resources
[0] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
408 if (resource_formats
[1] == PIPE_FORMAT_NONE
) {
409 assert(tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_444
);
410 assert(resource_formats
[2] == PIPE_FORMAT_NONE
);
411 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
414 vl_vide_buffer_template(&res_tmpl
, tmpl
, resource_formats
[1], depth
, usage
, 1);
415 resources
[1] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
419 if (resource_formats
[2] == PIPE_FORMAT_NONE
)
420 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
422 vl_vide_buffer_template(&res_tmpl
, tmpl
, resource_formats
[2], depth
, usage
, 2);
423 resources
[2] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
427 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
430 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
)
431 pipe_resource_reference(&resources
[i
], NULL
);
436 struct pipe_video_buffer
*
437 vl_video_buffer_create_ex2(struct pipe_context
*pipe
,
438 const struct pipe_video_buffer
*tmpl
,
439 struct pipe_resource
*resources
[VL_NUM_COMPONENTS
])
441 struct vl_video_buffer
*buffer
;
444 buffer
= CALLOC_STRUCT(vl_video_buffer
);
446 buffer
->base
= *tmpl
;
447 buffer
->base
.context
= pipe
;
448 buffer
->base
.destroy
= vl_video_buffer_destroy
;
449 buffer
->base
.get_sampler_view_planes
= vl_video_buffer_sampler_view_planes
;
450 buffer
->base
.get_sampler_view_components
= vl_video_buffer_sampler_view_components
;
451 buffer
->base
.get_surfaces
= vl_video_buffer_surfaces
;
452 buffer
->num_planes
= 0;
454 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
455 buffer
->resources
[i
] = resources
[i
];
457 buffer
->num_planes
++;
460 return &buffer
->base
;