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 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.
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 enum pipe_format const_resource_formats_YUVX
[3] = {
66 PIPE_FORMAT_R8G8B8X8_UNORM
,
71 const enum pipe_format const_resource_formats_VUYX
[3] = {
72 PIPE_FORMAT_B8G8R8X8_UNORM
,
77 const enum pipe_format const_resource_formats_YUYV
[3] = {
78 PIPE_FORMAT_R8G8_R8B8_UNORM
,
83 const enum pipe_format const_resource_formats_UYVY
[3] = {
84 PIPE_FORMAT_G8R8_B8R8_UNORM
,
89 const enum pipe_format const_resource_formats_P016
[3] = {
90 PIPE_FORMAT_R16_UNORM
,
91 PIPE_FORMAT_R16G16_UNORM
,
95 const unsigned const_resource_plane_order_YUV
[3] = {
101 const unsigned const_resource_plane_order_YVU
[3] = {
107 const enum pipe_format
*
108 vl_video_buffer_formats(struct pipe_screen
*screen
, enum pipe_format format
)
111 case PIPE_FORMAT_YV12
:
112 return const_resource_formats_YV12
;
114 case PIPE_FORMAT_NV12
:
115 return const_resource_formats_NV12
;
117 case PIPE_FORMAT_R8G8B8A8_UNORM
:
118 return const_resource_formats_YUVA
;
120 case PIPE_FORMAT_B8G8R8A8_UNORM
:
121 return const_resource_formats_VUYA
;
123 case PIPE_FORMAT_R8G8B8X8_UNORM
:
124 return const_resource_formats_YUVX
;
126 case PIPE_FORMAT_B8G8R8X8_UNORM
:
127 return const_resource_formats_VUYX
;
129 case PIPE_FORMAT_YUYV
:
130 return const_resource_formats_YUYV
;
132 case PIPE_FORMAT_UYVY
:
133 return const_resource_formats_UYVY
;
135 case PIPE_FORMAT_P016
:
136 return const_resource_formats_P016
;
144 vl_video_buffer_plane_order(enum pipe_format format
)
147 case PIPE_FORMAT_YV12
:
148 return const_resource_plane_order_YVU
;
150 case PIPE_FORMAT_NV12
:
151 case PIPE_FORMAT_R8G8B8A8_UNORM
:
152 case PIPE_FORMAT_B8G8R8A8_UNORM
:
153 case PIPE_FORMAT_YUYV
:
154 case PIPE_FORMAT_UYVY
:
155 case PIPE_FORMAT_P016
:
156 return const_resource_plane_order_YUV
;
163 static enum pipe_format
164 vl_video_buffer_surface_format(enum pipe_format format
)
166 const struct util_format_description
*desc
= util_format_description(format
);
168 /* a subsampled formats can't work as surface use RGBA instead */
169 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
170 return PIPE_FORMAT_R8G8B8A8_UNORM
;
176 vl_video_buffer_is_format_supported(struct pipe_screen
*screen
,
177 enum pipe_format format
,
178 enum pipe_video_profile profile
,
179 enum pipe_video_entrypoint entrypoint
)
181 const enum pipe_format
*resource_formats
;
184 resource_formats
= vl_video_buffer_formats(screen
, format
);
185 if (!resource_formats
)
188 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
189 enum pipe_format format
= resource_formats
[i
];
191 if (format
== PIPE_FORMAT_NONE
)
194 /* we at least need to sample from it */
195 if (!screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
))
198 format
= vl_video_buffer_surface_format(format
);
199 if (!screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
, 0, PIPE_BIND_RENDER_TARGET
))
207 vl_video_buffer_max_size(struct pipe_screen
*screen
)
209 uint32_t max_2d_texture_level
;
211 max_2d_texture_level
= screen
->get_param(screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
);
213 return 1 << (max_2d_texture_level
-1);
217 vl_video_buffer_set_associated_data(struct pipe_video_buffer
*vbuf
,
218 struct pipe_video_codec
*vcodec
,
219 void *associated_data
,
220 void (*destroy_associated_data
)(void *))
222 vbuf
->codec
= vcodec
;
224 if (vbuf
->associated_data
== associated_data
)
227 if (vbuf
->associated_data
)
228 vbuf
->destroy_associated_data(vbuf
->associated_data
);
230 vbuf
->associated_data
= associated_data
;
231 vbuf
->destroy_associated_data
= destroy_associated_data
;
235 vl_video_buffer_get_associated_data(struct pipe_video_buffer
*vbuf
,
236 struct pipe_video_codec
*vcodec
)
238 if (vbuf
->codec
== vcodec
)
239 return vbuf
->associated_data
;
245 vl_video_buffer_template(struct pipe_resource
*templ
,
246 const struct pipe_video_buffer
*tmpl
,
247 enum pipe_format resource_format
,
248 unsigned depth
, unsigned array_size
,
249 unsigned usage
, unsigned plane
)
251 unsigned height
= tmpl
->height
;
253 memset(templ
, 0, sizeof(*templ
));
255 templ
->target
= PIPE_TEXTURE_3D
;
256 else if (array_size
> 1)
257 templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
259 templ
->target
= PIPE_TEXTURE_2D
;
260 templ
->format
= resource_format
;
261 templ
->width0
= tmpl
->width
;
262 templ
->depth0
= depth
;
263 templ
->array_size
= array_size
;
264 templ
->bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
| tmpl
->bind
;
265 templ
->usage
= usage
;
267 vl_video_buffer_adjust_size(&templ
->width0
, &height
, plane
,
268 tmpl
->chroma_format
, false);
269 templ
->height0
= height
;
273 vl_video_buffer_destroy(struct pipe_video_buffer
*buffer
)
275 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
280 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
281 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
282 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
283 pipe_resource_reference(&buf
->resources
[i
], NULL
);
286 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
)
287 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
289 vl_video_buffer_set_associated_data(buffer
, NULL
, NULL
, NULL
);
294 static struct pipe_sampler_view
**
295 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer
*buffer
)
297 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
298 struct pipe_sampler_view sv_templ
;
299 struct pipe_context
*pipe
;
304 pipe
= buf
->base
.context
;
306 for (i
= 0; i
< buf
->num_planes
; ++i
) {
307 if (!buf
->sampler_view_planes
[i
]) {
308 memset(&sv_templ
, 0, sizeof(sv_templ
));
309 u_sampler_view_default_template(&sv_templ
, buf
->resources
[i
], buf
->resources
[i
]->format
);
311 if (util_format_get_nr_components(buf
->resources
[i
]->format
) == 1)
312 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= sv_templ
.swizzle_a
= PIPE_SWIZZLE_X
;
314 buf
->sampler_view_planes
[i
] = pipe
->create_sampler_view(pipe
, buf
->resources
[i
], &sv_templ
);
315 if (!buf
->sampler_view_planes
[i
])
320 return buf
->sampler_view_planes
;
323 for (i
= 0; i
< buf
->num_planes
; ++i
)
324 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
329 static struct pipe_sampler_view
**
330 vl_video_buffer_sampler_view_components(struct pipe_video_buffer
*buffer
)
332 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
333 struct pipe_sampler_view sv_templ
;
334 struct pipe_context
*pipe
;
335 const enum pipe_format
*sampler_format
;
336 const unsigned *plane_order
;
337 unsigned i
, j
, component
;
341 pipe
= buf
->base
.context
;
343 sampler_format
= vl_video_buffer_formats(pipe
->screen
, buf
->base
.buffer_format
);
344 plane_order
= vl_video_buffer_plane_order(buf
->base
.buffer_format
);
346 for (component
= 0, i
= 0; i
< buf
->num_planes
; ++i
) {
347 struct pipe_resource
*res
= buf
->resources
[plane_order
[i
]];
348 const struct util_format_description
*desc
= util_format_description(res
->format
);
349 unsigned nr_components
= util_format_get_nr_components(res
->format
);
350 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
353 for (j
= 0; j
< nr_components
&& component
< VL_NUM_COMPONENTS
; ++j
, ++component
) {
354 if (buf
->sampler_view_components
[component
])
357 memset(&sv_templ
, 0, sizeof(sv_templ
));
358 u_sampler_view_default_template(&sv_templ
, res
, sampler_format
[plane_order
[i
]]);
359 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= PIPE_SWIZZLE_X
+ j
;
360 sv_templ
.swizzle_a
= PIPE_SWIZZLE_1
;
361 buf
->sampler_view_components
[component
] = pipe
->create_sampler_view(pipe
, res
, &sv_templ
);
362 if (!buf
->sampler_view_components
[component
])
366 assert(component
== VL_NUM_COMPONENTS
);
368 return buf
->sampler_view_components
;
371 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
)
372 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
377 static struct pipe_surface
**
378 vl_video_buffer_surfaces(struct pipe_video_buffer
*buffer
)
380 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
381 struct pipe_surface surf_templ
;
382 struct pipe_context
*pipe
;
383 unsigned i
, j
, array_size
, surf
;
387 pipe
= buf
->base
.context
;
389 array_size
= buffer
->interlaced
? 2 : 1;
390 for (i
= 0, surf
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
391 for (j
= 0; j
< array_size
; ++j
, ++surf
) {
392 assert(surf
< VL_MAX_SURFACES
);
394 if (!buf
->resources
[i
]) {
395 pipe_surface_reference(&buf
->surfaces
[surf
], NULL
);
399 if (!buf
->surfaces
[surf
]) {
400 memset(&surf_templ
, 0, sizeof(surf_templ
));
401 surf_templ
.format
= vl_video_buffer_surface_format(buf
->resources
[i
]->format
);
402 surf_templ
.u
.tex
.first_layer
= surf_templ
.u
.tex
.last_layer
= j
;
403 buf
->surfaces
[surf
] = pipe
->create_surface(pipe
, buf
->resources
[i
], &surf_templ
);
404 if (!buf
->surfaces
[surf
])
410 return buf
->surfaces
;
413 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
)
414 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
419 struct pipe_video_buffer
*
420 vl_video_buffer_create(struct pipe_context
*pipe
,
421 const struct pipe_video_buffer
*tmpl
)
423 const enum pipe_format
*resource_formats
;
424 struct pipe_video_buffer templat
, *result
;
428 assert(tmpl
->width
> 0 && tmpl
->height
> 0);
430 pot_buffers
= !pipe
->screen
->get_video_param
433 PIPE_VIDEO_PROFILE_UNKNOWN
,
434 PIPE_VIDEO_ENTRYPOINT_UNKNOWN
,
435 PIPE_VIDEO_CAP_NPOT_TEXTURES
438 resource_formats
= vl_video_buffer_formats(pipe
->screen
, tmpl
->buffer_format
);
439 if (!resource_formats
)
443 templat
.width
= pot_buffers
? util_next_power_of_two(tmpl
->width
)
444 : align(tmpl
->width
, VL_MACROBLOCK_WIDTH
);
445 templat
.height
= pot_buffers
? util_next_power_of_two(tmpl
->height
)
446 : align(tmpl
->height
, VL_MACROBLOCK_HEIGHT
);
448 if (tmpl
->interlaced
)
451 result
= vl_video_buffer_create_ex
453 pipe
, &templat
, resource_formats
,
454 1, tmpl
->interlaced
? 2 : 1, PIPE_USAGE_DEFAULT
458 if (result
&& tmpl
->interlaced
)
464 struct pipe_video_buffer
*
465 vl_video_buffer_create_ex(struct pipe_context
*pipe
,
466 const struct pipe_video_buffer
*tmpl
,
467 const enum pipe_format resource_formats
[VL_NUM_COMPONENTS
],
468 unsigned depth
, unsigned array_size
, unsigned usage
)
470 struct pipe_resource res_tmpl
;
471 struct pipe_resource
*resources
[VL_NUM_COMPONENTS
];
476 memset(resources
, 0, sizeof resources
);
478 vl_video_buffer_template(&res_tmpl
, tmpl
, resource_formats
[0], depth
, array_size
, usage
, 0);
479 resources
[0] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
483 if (resource_formats
[1] == PIPE_FORMAT_NONE
) {
484 assert(resource_formats
[2] == PIPE_FORMAT_NONE
);
485 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
488 vl_video_buffer_template(&res_tmpl
, tmpl
, resource_formats
[1], depth
, array_size
, usage
, 1);
489 resources
[1] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
493 if (resource_formats
[2] == PIPE_FORMAT_NONE
)
494 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
496 vl_video_buffer_template(&res_tmpl
, tmpl
, resource_formats
[2], depth
, array_size
, usage
, 2);
497 resources
[2] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
501 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
504 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
)
505 pipe_resource_reference(&resources
[i
], NULL
);
510 struct pipe_video_buffer
*
511 vl_video_buffer_create_ex2(struct pipe_context
*pipe
,
512 const struct pipe_video_buffer
*tmpl
,
513 struct pipe_resource
*resources
[VL_NUM_COMPONENTS
])
515 struct vl_video_buffer
*buffer
;
518 buffer
= CALLOC_STRUCT(vl_video_buffer
);
522 buffer
->base
= *tmpl
;
523 buffer
->base
.context
= pipe
;
524 buffer
->base
.destroy
= vl_video_buffer_destroy
;
525 buffer
->base
.get_sampler_view_planes
= vl_video_buffer_sampler_view_planes
;
526 buffer
->base
.get_sampler_view_components
= vl_video_buffer_sampler_view_components
;
527 buffer
->base
.get_surfaces
= vl_video_buffer_surfaces
;
528 buffer
->num_planes
= 0;
530 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
531 buffer
->resources
[i
] = resources
[i
];
533 buffer
->num_planes
++;
536 return &buffer
->base
;