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_YUYV
[3] = {
66 PIPE_FORMAT_R8G8_R8B8_UNORM
,
71 const enum pipe_format const_resource_formats_UYVY
[3] = {
72 PIPE_FORMAT_G8R8_B8R8_UNORM
,
77 const unsigned const_resource_plane_order_YUV
[3] = {
83 const unsigned const_resource_plane_order_YVU
[3] = {
89 const enum pipe_format
*
90 vl_video_buffer_formats(struct pipe_screen
*screen
, enum pipe_format format
)
93 case PIPE_FORMAT_YV12
:
94 return const_resource_formats_YV12
;
96 case PIPE_FORMAT_NV12
:
97 return const_resource_formats_NV12
;
99 case PIPE_FORMAT_R8G8B8A8_UNORM
:
100 return const_resource_formats_YUVA
;
102 case PIPE_FORMAT_B8G8R8A8_UNORM
:
103 return const_resource_formats_VUYA
;
105 case PIPE_FORMAT_YUYV
:
106 return const_resource_formats_YUYV
;
108 case PIPE_FORMAT_UYVY
:
109 return const_resource_formats_UYVY
;
117 vl_video_buffer_plane_order(enum pipe_format format
)
120 case PIPE_FORMAT_YV12
:
121 return const_resource_plane_order_YVU
;
123 case PIPE_FORMAT_NV12
:
124 case PIPE_FORMAT_R8G8B8A8_UNORM
:
125 case PIPE_FORMAT_B8G8R8A8_UNORM
:
126 case PIPE_FORMAT_YUYV
:
127 case PIPE_FORMAT_UYVY
:
128 return const_resource_plane_order_YUV
;
135 static enum pipe_format
136 vl_video_buffer_surface_format(enum pipe_format format
)
138 const struct util_format_description
*desc
= util_format_description(format
);
140 /* a subsampled formats can't work as surface use RGBA instead */
141 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
142 return PIPE_FORMAT_R8G8B8A8_UNORM
;
148 vl_video_buffer_is_format_supported(struct pipe_screen
*screen
,
149 enum pipe_format format
,
150 enum pipe_video_profile profile
,
151 enum pipe_video_entrypoint entrypoint
)
153 const enum pipe_format
*resource_formats
;
156 resource_formats
= vl_video_buffer_formats(screen
, format
);
157 if (!resource_formats
)
160 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
161 enum pipe_format format
= resource_formats
[i
];
163 if (format
== PIPE_FORMAT_NONE
)
166 /* we at least need to sample from it */
167 if (!screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
, 0, PIPE_BIND_SAMPLER_VIEW
))
170 format
= vl_video_buffer_surface_format(format
);
171 if (!screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
, 0, PIPE_BIND_RENDER_TARGET
))
179 vl_video_buffer_max_size(struct pipe_screen
*screen
)
181 uint32_t max_2d_texture_level
;
183 max_2d_texture_level
= screen
->get_param(screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
);
185 return 1 << (max_2d_texture_level
-1);
189 vl_video_buffer_set_associated_data(struct pipe_video_buffer
*vbuf
,
190 struct pipe_video_codec
*vcodec
,
191 void *associated_data
,
192 void (*destroy_associated_data
)(void *))
194 vbuf
->codec
= vcodec
;
196 if (vbuf
->associated_data
== associated_data
)
199 if (vbuf
->associated_data
)
200 vbuf
->destroy_associated_data(vbuf
->associated_data
);
202 vbuf
->associated_data
= associated_data
;
203 vbuf
->destroy_associated_data
= destroy_associated_data
;
207 vl_video_buffer_get_associated_data(struct pipe_video_buffer
*vbuf
,
208 struct pipe_video_codec
*vcodec
)
210 if (vbuf
->codec
== vcodec
)
211 return vbuf
->associated_data
;
217 vl_video_buffer_template(struct pipe_resource
*templ
,
218 const struct pipe_video_buffer
*tmpl
,
219 enum pipe_format resource_format
,
220 unsigned depth
, unsigned array_size
,
221 unsigned usage
, unsigned plane
)
223 memset(templ
, 0, sizeof(*templ
));
225 templ
->target
= PIPE_TEXTURE_3D
;
226 else if (array_size
> 1)
227 templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
229 templ
->target
= PIPE_TEXTURE_2D
;
230 templ
->format
= resource_format
;
231 templ
->width0
= tmpl
->width
;
232 templ
->height0
= tmpl
->height
;
233 templ
->depth0
= depth
;
234 templ
->array_size
= array_size
;
235 templ
->bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
236 templ
->usage
= usage
;
239 if (tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
242 } else if (tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
249 vl_video_buffer_destroy(struct pipe_video_buffer
*buffer
)
251 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
256 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
257 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
258 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
259 pipe_resource_reference(&buf
->resources
[i
], NULL
);
262 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
)
263 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
265 vl_video_buffer_set_associated_data(buffer
, NULL
, NULL
, NULL
);
270 static struct pipe_sampler_view
**
271 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer
*buffer
)
273 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
274 struct pipe_sampler_view sv_templ
;
275 struct pipe_context
*pipe
;
280 pipe
= buf
->base
.context
;
282 for (i
= 0; i
< buf
->num_planes
; ++i
) {
283 if (!buf
->sampler_view_planes
[i
]) {
284 memset(&sv_templ
, 0, sizeof(sv_templ
));
285 u_sampler_view_default_template(&sv_templ
, buf
->resources
[i
], buf
->resources
[i
]->format
);
287 if (util_format_get_nr_components(buf
->resources
[i
]->format
) == 1)
288 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= sv_templ
.swizzle_a
= PIPE_SWIZZLE_RED
;
290 buf
->sampler_view_planes
[i
] = pipe
->create_sampler_view(pipe
, buf
->resources
[i
], &sv_templ
);
291 if (!buf
->sampler_view_planes
[i
])
296 return buf
->sampler_view_planes
;
299 for (i
= 0; i
< buf
->num_planes
; ++i
)
300 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
305 static struct pipe_sampler_view
**
306 vl_video_buffer_sampler_view_components(struct pipe_video_buffer
*buffer
)
308 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
309 struct pipe_sampler_view sv_templ
;
310 struct pipe_context
*pipe
;
311 const enum pipe_format
*sampler_format
;
312 const unsigned *plane_order
;
313 unsigned i
, j
, component
;
317 pipe
= buf
->base
.context
;
319 sampler_format
= vl_video_buffer_formats(pipe
->screen
, buf
->base
.buffer_format
);
320 plane_order
= vl_video_buffer_plane_order(buf
->base
.buffer_format
);
322 for (component
= 0, i
= 0; i
< buf
->num_planes
; ++i
) {
323 struct pipe_resource
*res
= buf
->resources
[plane_order
[i
]];
324 const struct util_format_description
*desc
= util_format_description(res
->format
);
325 unsigned nr_components
= util_format_get_nr_components(res
->format
);
326 if (desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
)
329 for (j
= 0; j
< nr_components
&& component
< VL_NUM_COMPONENTS
; ++j
, ++component
) {
330 if (buf
->sampler_view_components
[component
])
333 memset(&sv_templ
, 0, sizeof(sv_templ
));
334 u_sampler_view_default_template(&sv_templ
, res
, sampler_format
[plane_order
[i
]]);
335 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= PIPE_SWIZZLE_RED
+ j
;
336 sv_templ
.swizzle_a
= PIPE_SWIZZLE_ONE
;
337 buf
->sampler_view_components
[component
] = pipe
->create_sampler_view(pipe
, res
, &sv_templ
);
338 if (!buf
->sampler_view_components
[component
])
342 assert(component
== VL_NUM_COMPONENTS
);
344 return buf
->sampler_view_components
;
347 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
)
348 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
353 static struct pipe_surface
**
354 vl_video_buffer_surfaces(struct pipe_video_buffer
*buffer
)
356 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
357 struct pipe_surface surf_templ
;
358 struct pipe_context
*pipe
;
359 unsigned i
, j
, array_size
, surf
;
363 pipe
= buf
->base
.context
;
365 array_size
= buffer
->interlaced
? 2 : 1;
366 for (i
= 0, surf
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
367 for (j
= 0; j
< array_size
; ++j
, ++surf
) {
368 assert(surf
< VL_MAX_SURFACES
);
370 if (!buf
->resources
[i
]) {
371 pipe_surface_reference(&buf
->surfaces
[surf
], NULL
);
375 if (!buf
->surfaces
[surf
]) {
376 memset(&surf_templ
, 0, sizeof(surf_templ
));
377 surf_templ
.format
= vl_video_buffer_surface_format(buf
->resources
[i
]->format
);
378 surf_templ
.u
.tex
.first_layer
= surf_templ
.u
.tex
.last_layer
= j
;
379 buf
->surfaces
[surf
] = pipe
->create_surface(pipe
, buf
->resources
[i
], &surf_templ
);
380 if (!buf
->surfaces
[surf
])
386 return buf
->surfaces
;
389 for (i
= 0; i
< VL_MAX_SURFACES
; ++i
)
390 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
395 struct pipe_video_buffer
*
396 vl_video_buffer_create(struct pipe_context
*pipe
,
397 const struct pipe_video_buffer
*tmpl
)
399 const enum pipe_format
*resource_formats
;
400 struct pipe_video_buffer templat
, *result
;
404 assert(tmpl
->width
> 0 && tmpl
->height
> 0);
406 pot_buffers
= !pipe
->screen
->get_video_param
409 PIPE_VIDEO_PROFILE_UNKNOWN
,
410 PIPE_VIDEO_ENTRYPOINT_UNKNOWN
,
411 PIPE_VIDEO_CAP_NPOT_TEXTURES
414 resource_formats
= vl_video_buffer_formats(pipe
->screen
, tmpl
->buffer_format
);
415 if (!resource_formats
)
419 templat
.width
= pot_buffers
? util_next_power_of_two(tmpl
->width
)
420 : align(tmpl
->width
, VL_MACROBLOCK_WIDTH
);
421 templat
.height
= pot_buffers
? util_next_power_of_two(tmpl
->height
)
422 : align(tmpl
->height
, VL_MACROBLOCK_HEIGHT
);
424 if (tmpl
->interlaced
)
427 result
= vl_video_buffer_create_ex
429 pipe
, &templat
, resource_formats
,
430 1, tmpl
->interlaced
? 2 : 1, PIPE_USAGE_DEFAULT
434 if (result
&& tmpl
->interlaced
)
440 struct pipe_video_buffer
*
441 vl_video_buffer_create_ex(struct pipe_context
*pipe
,
442 const struct pipe_video_buffer
*tmpl
,
443 const enum pipe_format resource_formats
[VL_NUM_COMPONENTS
],
444 unsigned depth
, unsigned array_size
, unsigned usage
)
446 struct pipe_resource res_tmpl
;
447 struct pipe_resource
*resources
[VL_NUM_COMPONENTS
];
452 memset(resources
, 0, sizeof resources
);
454 vl_video_buffer_template(&res_tmpl
, tmpl
, resource_formats
[0], depth
, array_size
, usage
, 0);
455 resources
[0] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
459 if (resource_formats
[1] == PIPE_FORMAT_NONE
) {
460 assert(resource_formats
[2] == PIPE_FORMAT_NONE
);
461 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
464 vl_video_buffer_template(&res_tmpl
, tmpl
, resource_formats
[1], depth
, array_size
, usage
, 1);
465 resources
[1] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
469 if (resource_formats
[2] == PIPE_FORMAT_NONE
)
470 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
472 vl_video_buffer_template(&res_tmpl
, tmpl
, resource_formats
[2], depth
, array_size
, usage
, 2);
473 resources
[2] = pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
477 return vl_video_buffer_create_ex2(pipe
, tmpl
, resources
);
480 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
)
481 pipe_resource_reference(&resources
[i
], NULL
);
486 struct pipe_video_buffer
*
487 vl_video_buffer_create_ex2(struct pipe_context
*pipe
,
488 const struct pipe_video_buffer
*tmpl
,
489 struct pipe_resource
*resources
[VL_NUM_COMPONENTS
])
491 struct vl_video_buffer
*buffer
;
494 buffer
= CALLOC_STRUCT(vl_video_buffer
);
498 buffer
->base
= *tmpl
;
499 buffer
->base
.context
= pipe
;
500 buffer
->base
.destroy
= vl_video_buffer_destroy
;
501 buffer
->base
.get_sampler_view_planes
= vl_video_buffer_sampler_view_planes
;
502 buffer
->base
.get_sampler_view_components
= vl_video_buffer_sampler_view_components
;
503 buffer
->base
.get_surfaces
= vl_video_buffer_surfaces
;
504 buffer
->num_planes
= 0;
506 for (i
= 0; i
< VL_NUM_COMPONENTS
; ++i
) {
507 buffer
->resources
[i
] = resources
[i
];
509 buffer
->num_planes
++;
512 return &buffer
->base
;