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_set_associated_data(struct pipe_video_buffer
*vbuf
,
103 struct pipe_video_decoder
*vdec
,
104 void *associated_data
,
105 void (*destroy_associated_data
)(void *))
107 vbuf
->decoder
= vdec
;
109 if (vbuf
->associated_data
== associated_data
)
112 if (vbuf
->associated_data
)
113 vbuf
->destroy_associated_data(vbuf
->associated_data
);
115 vbuf
->associated_data
= associated_data
;
116 vbuf
->destroy_associated_data
= destroy_associated_data
;
120 vl_video_buffer_get_associated_data(struct pipe_video_buffer
*vbuf
,
121 struct pipe_video_decoder
*vdec
)
123 if (vbuf
->decoder
== vdec
)
124 return vbuf
->associated_data
;
130 vl_video_buffer_destroy(struct pipe_video_buffer
*buffer
)
132 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
137 for (i
= 0; i
< VL_MAX_PLANES
; ++i
) {
138 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
139 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
140 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
141 pipe_resource_reference(&buf
->resources
[i
], NULL
);
143 vl_video_buffer_set_associated_data(buffer
, NULL
, NULL
, NULL
);
148 static struct pipe_sampler_view
**
149 vl_video_buffer_sampler_view_planes(struct pipe_video_buffer
*buffer
)
151 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
152 struct pipe_sampler_view sv_templ
;
153 struct pipe_context
*pipe
;
158 pipe
= buf
->base
.context
;
160 for (i
= 0; i
< buf
->num_planes
; ++i
) {
161 if (!buf
->sampler_view_planes
[i
]) {
162 memset(&sv_templ
, 0, sizeof(sv_templ
));
163 u_sampler_view_default_template(&sv_templ
, buf
->resources
[i
], buf
->resources
[i
]->format
);
165 if (util_format_get_nr_components(buf
->resources
[i
]->format
) == 1)
166 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= sv_templ
.swizzle_a
= PIPE_SWIZZLE_RED
;
168 buf
->sampler_view_planes
[i
] = pipe
->create_sampler_view(pipe
, buf
->resources
[i
], &sv_templ
);
169 if (!buf
->sampler_view_planes
[i
])
174 return buf
->sampler_view_planes
;
177 for (i
= 0; i
< buf
->num_planes
; ++i
)
178 pipe_sampler_view_reference(&buf
->sampler_view_planes
[i
], NULL
);
183 static struct pipe_sampler_view
**
184 vl_video_buffer_sampler_view_components(struct pipe_video_buffer
*buffer
)
186 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
187 struct pipe_sampler_view sv_templ
;
188 struct pipe_context
*pipe
;
189 unsigned i
, j
, component
;
193 pipe
= buf
->base
.context
;
195 for (component
= 0, i
= 0; i
< buf
->num_planes
; ++i
) {
196 unsigned nr_components
= util_format_get_nr_components(buf
->resources
[i
]->format
);
198 for (j
= 0; j
< nr_components
; ++j
, ++component
) {
199 assert(component
< VL_MAX_PLANES
);
201 if (!buf
->sampler_view_components
[component
]) {
202 memset(&sv_templ
, 0, sizeof(sv_templ
));
203 u_sampler_view_default_template(&sv_templ
, buf
->resources
[i
], buf
->resources
[i
]->format
);
204 sv_templ
.swizzle_r
= sv_templ
.swizzle_g
= sv_templ
.swizzle_b
= PIPE_SWIZZLE_RED
+ j
;
205 sv_templ
.swizzle_a
= PIPE_SWIZZLE_ONE
;
206 buf
->sampler_view_components
[component
] = pipe
->create_sampler_view(pipe
, buf
->resources
[i
], &sv_templ
);
207 if (!buf
->sampler_view_components
[component
])
213 return buf
->sampler_view_components
;
216 for (i
= 0; i
< VL_MAX_PLANES
; ++i
)
217 pipe_sampler_view_reference(&buf
->sampler_view_components
[i
], NULL
);
222 static struct pipe_surface
**
223 vl_video_buffer_surfaces(struct pipe_video_buffer
*buffer
)
225 struct vl_video_buffer
*buf
= (struct vl_video_buffer
*)buffer
;
226 struct pipe_surface surf_templ
;
227 struct pipe_context
*pipe
;
232 pipe
= buf
->base
.context
;
234 for (i
= 0; i
< buf
->num_planes
; ++i
) {
235 if (!buf
->surfaces
[i
]) {
236 memset(&surf_templ
, 0, sizeof(surf_templ
));
237 surf_templ
.format
= buf
->resources
[i
]->format
;
238 surf_templ
.usage
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
239 buf
->surfaces
[i
] = pipe
->create_surface(pipe
, buf
->resources
[i
], &surf_templ
);
240 if (!buf
->surfaces
[i
])
245 return buf
->surfaces
;
248 for (i
= 0; i
< buf
->num_planes
; ++i
)
249 pipe_surface_reference(&buf
->surfaces
[i
], NULL
);
254 struct pipe_video_buffer
*
255 vl_video_buffer_create(struct pipe_context
*pipe
,
256 const struct pipe_video_buffer
*tmpl
)
258 const enum pipe_format
*resource_formats
;
259 struct pipe_video_buffer templat
;
263 assert(tmpl
->width
> 0 && tmpl
->height
> 0);
265 pot_buffers
= !pipe
->screen
->get_video_param
268 PIPE_VIDEO_PROFILE_UNKNOWN
,
269 PIPE_VIDEO_CAP_NPOT_TEXTURES
272 resource_formats
= vl_video_buffer_formats(pipe
->screen
, tmpl
->buffer_format
);
273 if (!resource_formats
)
277 templat
.width
= pot_buffers
? util_next_power_of_two(tmpl
->width
)
278 : align(tmpl
->width
, MACROBLOCK_WIDTH
);
279 templat
.height
= pot_buffers
? util_next_power_of_two(tmpl
->height
)
280 : align(tmpl
->height
, MACROBLOCK_HEIGHT
);
282 return vl_video_buffer_create_ex
284 pipe
, &templat
, resource_formats
,
289 struct pipe_video_buffer
*
290 vl_video_buffer_create_ex(struct pipe_context
*pipe
,
291 const struct pipe_video_buffer
*tmpl
,
292 const enum pipe_format resource_formats
[VL_MAX_PLANES
],
293 unsigned depth
, unsigned usage
)
295 struct vl_video_buffer
*buffer
;
296 struct pipe_resource templ
;
301 buffer
= CALLOC_STRUCT(vl_video_buffer
);
303 buffer
->base
= *tmpl
;
304 buffer
->base
.context
= pipe
;
305 buffer
->base
.destroy
= vl_video_buffer_destroy
;
306 buffer
->base
.get_sampler_view_planes
= vl_video_buffer_sampler_view_planes
;
307 buffer
->base
.get_sampler_view_components
= vl_video_buffer_sampler_view_components
;
308 buffer
->base
.get_surfaces
= vl_video_buffer_surfaces
;
309 buffer
->num_planes
= 1;
311 memset(&templ
, 0, sizeof(templ
));
312 templ
.target
= depth
> 1 ? PIPE_TEXTURE_3D
: PIPE_TEXTURE_2D
;
313 templ
.format
= resource_formats
[0];
314 templ
.width0
= tmpl
->width
;
315 templ
.height0
= tmpl
->height
;
316 templ
.depth0
= depth
;
317 templ
.array_size
= 1;
318 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
321 buffer
->resources
[0] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
322 if (!buffer
->resources
[0])
325 if (resource_formats
[1] == PIPE_FORMAT_NONE
) {
326 assert(tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_444
);
327 assert(resource_formats
[2] == PIPE_FORMAT_NONE
);
328 return &buffer
->base
;
330 buffer
->num_planes
= 2;
332 templ
.format
= resource_formats
[1];
333 if (tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
336 } else if (tmpl
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
) {
340 buffer
->resources
[1] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
341 if (!buffer
->resources
[1])
344 if (resource_formats
[2] == PIPE_FORMAT_NONE
)
345 return &buffer
->base
;
347 buffer
->num_planes
= 3;
349 templ
.format
= resource_formats
[2];
350 buffer
->resources
[2] = pipe
->screen
->resource_create(pipe
->screen
, &templ
);
351 if (!buffer
->resources
[2])
354 return &buffer
->base
;
357 for (i
= 0; i
< VL_MAX_PLANES
; ++i
)
358 pipe_resource_reference(&buffer
->resources
[i
], NULL
);