vl_video_buffer.c revision 8c2bfa34a0d70ab08de44e3b091b3a097abbad97
1/************************************************************************** 2 * 3 * Copyright 2011 Christian König. 4 * All Rights Reserved. 5 * 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: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 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. 25 * 26 **************************************************************************/ 27 28#include <assert.h> 29 30#include "pipe/p_screen.h" 31#include "pipe/p_context.h" 32#include "pipe/p_state.h" 33 34#include "util/u_format.h" 35#include "util/u_inlines.h" 36#include "util/u_sampler.h" 37#include "util/u_memory.h" 38 39#include "vl_video_buffer.h" 40 41const enum pipe_format const_resource_formats_YV12[3] = { 42 PIPE_FORMAT_R8_UNORM, 43 PIPE_FORMAT_R8_UNORM, 44 PIPE_FORMAT_R8_UNORM 45}; 46 47const enum pipe_format const_resource_formats_NV12[3] = { 48 PIPE_FORMAT_R8_UNORM, 49 PIPE_FORMAT_R8G8_UNORM, 50 PIPE_FORMAT_NONE 51}; 52 53const enum pipe_format * 54vl_video_buffer_formats(struct pipe_screen *screen, enum pipe_format format) 55{ 56 switch(format) { 57 case PIPE_FORMAT_YV12: 58 return const_resource_formats_YV12; 59 60 case PIPE_FORMAT_NV12: 61 return const_resource_formats_NV12; 62 63 default: 64 return NULL; 65 } 66} 67 68boolean 69vl_video_buffer_is_format_supported(struct pipe_screen *screen, 70 enum pipe_format format, 71 enum pipe_video_profile profile) 72{ 73 const enum pipe_format *resource_formats; 74 unsigned i; 75 76 resource_formats = vl_video_buffer_formats(screen, format); 77 if (!resource_formats) 78 return false; 79 80 for (i = 0; i < VL_MAX_PLANES; ++i) { 81 if (!resource_formats[i]) 82 continue; 83 84 if (!screen->is_format_supported(screen, resource_formats[i], PIPE_TEXTURE_2D, 0, PIPE_USAGE_STATIC)) 85 return false; 86 } 87 88 return true; 89} 90 91unsigned 92vl_video_buffer_max_size(struct pipe_screen *screen) 93{ 94 uint32_t max_2d_texture_level; 95 96 max_2d_texture_level = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS); 97 98 return 1 << (max_2d_texture_level-1); 99} 100 101void 102vl_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 *)) 106{ 107 vbuf->decoder = vdec; 108 109 if (vbuf->associated_data == associated_data) 110 return; 111 112 if (vbuf->associated_data) 113 vbuf->destroy_associated_data(vbuf->associated_data); 114 115 vbuf->associated_data = associated_data; 116 vbuf->destroy_associated_data = destroy_associated_data; 117} 118 119void * 120vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf, 121 struct pipe_video_decoder *vdec) 122{ 123 if (vbuf->decoder == vdec) 124 return vbuf->associated_data; 125 else 126 return NULL; 127} 128 129static void 130vl_video_buffer_destroy(struct pipe_video_buffer *buffer) 131{ 132 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer; 133 unsigned i; 134 135 assert(buf); 136 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); 142 } 143 vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL); 144 145 FREE(buffer); 146} 147 148static struct pipe_sampler_view ** 149vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer) 150{ 151 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer; 152 struct pipe_sampler_view sv_templ; 153 struct pipe_context *pipe; 154 unsigned i; 155 156 assert(buf); 157 158 pipe = buf->base.context; 159 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); 164 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; 167 168 buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ); 169 if (!buf->sampler_view_planes[i]) 170 goto error; 171 } 172 } 173 174 return buf->sampler_view_planes; 175 176error: 177 for (i = 0; i < buf->num_planes; ++i ) 178 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL); 179 180 return NULL; 181} 182 183static struct pipe_sampler_view ** 184vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer) 185{ 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; 190 191 assert(buf); 192 193 pipe = buf->base.context; 194 195 for (component = 0, i = 0; i < buf->num_planes; ++i ) { 196 unsigned nr_components = util_format_get_nr_components(buf->resources[i]->format); 197 198 for (j = 0; j < nr_components; ++j, ++component) { 199 assert(component < VL_MAX_PLANES); 200 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]) 208 goto error; 209 } 210 } 211 } 212 213 return buf->sampler_view_components; 214 215error: 216 for (i = 0; i < VL_MAX_PLANES; ++i ) 217 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL); 218 219 return NULL; 220} 221 222static struct pipe_surface ** 223vl_video_buffer_surfaces(struct pipe_video_buffer *buffer) 224{ 225 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer; 226 struct pipe_surface surf_templ; 227 struct pipe_context *pipe; 228 unsigned i; 229 230 assert(buf); 231 232 pipe = buf->base.context; 233 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]) 241 goto error; 242 } 243 } 244 245 return buf->surfaces; 246 247error: 248 for (i = 0; i < buf->num_planes; ++i ) 249 pipe_surface_reference(&buf->surfaces[i], NULL); 250 251 return NULL; 252} 253 254struct pipe_video_buffer * 255vl_video_buffer_create(struct pipe_context *pipe, 256 enum pipe_format buffer_format, 257 enum pipe_video_chroma_format chroma_format, 258 unsigned width, unsigned height) 259{ 260 const enum pipe_format *resource_formats; 261 struct pipe_video_buffer *result; 262 unsigned buffer_width, buffer_height; 263 bool pot_buffers; 264 265 assert(pipe); 266 assert(width > 0 && height > 0); 267 268 pot_buffers = !pipe->screen->get_video_param 269 ( 270 pipe->screen, 271 PIPE_VIDEO_PROFILE_UNKNOWN, 272 PIPE_VIDEO_CAP_NPOT_TEXTURES 273 ); 274 275 resource_formats = vl_video_buffer_formats(pipe->screen, buffer_format); 276 if (!resource_formats) 277 return NULL; 278 279 buffer_width = pot_buffers ? util_next_power_of_two(width) : align(width, MACROBLOCK_WIDTH); 280 buffer_height = pot_buffers ? util_next_power_of_two(height) : align(height, MACROBLOCK_HEIGHT); 281 282 result = vl_video_buffer_create_ex 283 ( 284 pipe, buffer_width, buffer_height, 1, 285 chroma_format, resource_formats, PIPE_USAGE_STATIC 286 ); 287 if (result) 288 result->buffer_format = buffer_format; 289 290 return result; 291} 292 293struct pipe_video_buffer * 294vl_video_buffer_create_ex(struct pipe_context *pipe, 295 unsigned width, unsigned height, unsigned depth, 296 enum pipe_video_chroma_format chroma_format, 297 const enum pipe_format resource_formats[VL_MAX_PLANES], 298 unsigned usage) 299{ 300 struct vl_video_buffer *buffer; 301 struct pipe_resource templ; 302 unsigned i; 303 304 assert(pipe); 305 306 buffer = CALLOC_STRUCT(vl_video_buffer); 307 308 buffer->base.context = pipe; 309 buffer->base.destroy = vl_video_buffer_destroy; 310 buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes; 311 buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components; 312 buffer->base.get_surfaces = vl_video_buffer_surfaces; 313 buffer->base.chroma_format = chroma_format; 314 buffer->base.width = width; 315 buffer->base.height = height; 316 buffer->num_planes = 1; 317 318 memset(&templ, 0, sizeof(templ)); 319 templ.target = depth > 1 ? PIPE_TEXTURE_3D : PIPE_TEXTURE_2D; 320 templ.format = resource_formats[0]; 321 templ.width0 = width; 322 templ.height0 = height; 323 templ.depth0 = depth; 324 templ.array_size = 1; 325 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET; 326 templ.usage = usage; 327 328 buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ); 329 if (!buffer->resources[0]) 330 goto error; 331 332 if (resource_formats[1] == PIPE_FORMAT_NONE) { 333 assert(chroma_format == PIPE_VIDEO_CHROMA_FORMAT_444); 334 assert(resource_formats[2] == PIPE_FORMAT_NONE); 335 return &buffer->base; 336 } else 337 buffer->num_planes = 2; 338 339 templ.format = resource_formats[1]; 340 if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) { 341 templ.width0 /= 2; 342 templ.height0 /= 2; 343 } else if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) { 344 templ.height0 /= 2; 345 } 346 347 buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ); 348 if (!buffer->resources[1]) 349 goto error; 350 351 if (resource_formats[2] == PIPE_FORMAT_NONE) 352 return &buffer->base; 353 else 354 buffer->num_planes = 3; 355 356 templ.format = resource_formats[2]; 357 buffer->resources[2] = pipe->screen->resource_create(pipe->screen, &templ); 358 if (!buffer->resources[2]) 359 goto error; 360 361 return &buffer->base; 362 363error: 364 for (i = 0; i < VL_MAX_PLANES; ++i) 365 pipe_resource_reference(&buffer->resources[i], NULL); 366 FREE(buffer); 367 368 return NULL; 369} 370