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 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. 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 const_resource_formats_YUVA[3] = { 54 PIPE_FORMAT_R8G8B8A8_UNORM, 55 PIPE_FORMAT_NONE, 56 PIPE_FORMAT_NONE 57}; 58 59const enum pipe_format const_resource_formats_VUYA[3] = { 60 PIPE_FORMAT_B8G8R8A8_UNORM, 61 PIPE_FORMAT_NONE, 62 PIPE_FORMAT_NONE 63}; 64 65const enum pipe_format const_resource_formats_YUVX[3] = { 66 PIPE_FORMAT_R8G8B8X8_UNORM, 67 PIPE_FORMAT_NONE, 68 PIPE_FORMAT_NONE 69}; 70 71const enum pipe_format const_resource_formats_VUYX[3] = { 72 PIPE_FORMAT_B8G8R8X8_UNORM, 73 PIPE_FORMAT_NONE, 74 PIPE_FORMAT_NONE 75}; 76 77const enum pipe_format const_resource_formats_YUYV[3] = { 78 PIPE_FORMAT_R8G8_R8B8_UNORM, 79 PIPE_FORMAT_NONE, 80 PIPE_FORMAT_NONE 81}; 82 83const enum pipe_format const_resource_formats_UYVY[3] = { 84 PIPE_FORMAT_G8R8_B8R8_UNORM, 85 PIPE_FORMAT_NONE, 86 PIPE_FORMAT_NONE 87}; 88 89const enum pipe_format const_resource_formats_P016[3] = { 90 PIPE_FORMAT_R16_UNORM, 91 PIPE_FORMAT_R16G16_UNORM, 92 PIPE_FORMAT_NONE 93}; 94 95const unsigned const_resource_plane_order_YUV[3] = { 96 0, 97 1, 98 2 99}; 100 101const unsigned const_resource_plane_order_YVU[3] = { 102 0, 103 2, 104 1 105}; 106 107const enum pipe_format * 108vl_video_buffer_formats(struct pipe_screen *screen, enum pipe_format format) 109{ 110 switch(format) { 111 case PIPE_FORMAT_YV12: 112 return const_resource_formats_YV12; 113 114 case PIPE_FORMAT_NV12: 115 return const_resource_formats_NV12; 116 117 case PIPE_FORMAT_R8G8B8A8_UNORM: 118 return const_resource_formats_YUVA; 119 120 case PIPE_FORMAT_B8G8R8A8_UNORM: 121 return const_resource_formats_VUYA; 122 123 case PIPE_FORMAT_R8G8B8X8_UNORM: 124 return const_resource_formats_YUVX; 125 126 case PIPE_FORMAT_B8G8R8X8_UNORM: 127 return const_resource_formats_VUYX; 128 129 case PIPE_FORMAT_YUYV: 130 return const_resource_formats_YUYV; 131 132 case PIPE_FORMAT_UYVY: 133 return const_resource_formats_UYVY; 134 135 case PIPE_FORMAT_P016: 136 return const_resource_formats_P016; 137 138 default: 139 return NULL; 140 } 141} 142 143const unsigned * 144vl_video_buffer_plane_order(enum pipe_format format) 145{ 146 switch(format) { 147 case PIPE_FORMAT_YV12: 148 return const_resource_plane_order_YVU; 149 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; 157 158 default: 159 return NULL; 160 } 161} 162 163static enum pipe_format 164vl_video_buffer_surface_format(enum pipe_format format) 165{ 166 const struct util_format_description *desc = util_format_description(format); 167 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; 171 172 return format; 173} 174 175boolean 176vl_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) 180{ 181 const enum pipe_format *resource_formats; 182 unsigned i; 183 184 resource_formats = vl_video_buffer_formats(screen, format); 185 if (!resource_formats) 186 return false; 187 188 for (i = 0; i < VL_NUM_COMPONENTS; ++i) { 189 enum pipe_format format = resource_formats[i]; 190 191 if (format == PIPE_FORMAT_NONE) 192 continue; 193 194 /* we at least need to sample from it */ 195 if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW)) 196 return false; 197 198 format = vl_video_buffer_surface_format(format); 199 if (!screen->is_format_supported(screen, format, PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_RENDER_TARGET)) 200 return false; 201 } 202 203 return true; 204} 205 206unsigned 207vl_video_buffer_max_size(struct pipe_screen *screen) 208{ 209 uint32_t max_2d_texture_level; 210 211 max_2d_texture_level = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS); 212 213 return 1 << (max_2d_texture_level-1); 214} 215 216void 217vl_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 *)) 221{ 222 vbuf->codec = vcodec; 223 224 if (vbuf->associated_data == associated_data) 225 return; 226 227 if (vbuf->associated_data) 228 vbuf->destroy_associated_data(vbuf->associated_data); 229 230 vbuf->associated_data = associated_data; 231 vbuf->destroy_associated_data = destroy_associated_data; 232} 233 234void * 235vl_video_buffer_get_associated_data(struct pipe_video_buffer *vbuf, 236 struct pipe_video_codec *vcodec) 237{ 238 if (vbuf->codec == vcodec) 239 return vbuf->associated_data; 240 else 241 return NULL; 242} 243 244void 245vl_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) 250{ 251 unsigned height = tmpl->height; 252 253 memset(templ, 0, sizeof(*templ)); 254 if (depth > 1) 255 templ->target = PIPE_TEXTURE_3D; 256 else if (array_size > 1) 257 templ->target = PIPE_TEXTURE_2D_ARRAY; 258 else 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; 266 267 vl_video_buffer_adjust_size(&templ->width0, &height, plane, 268 tmpl->chroma_format, false); 269 templ->height0 = height; 270} 271 272static void 273vl_video_buffer_destroy(struct pipe_video_buffer *buffer) 274{ 275 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer; 276 unsigned i; 277 278 assert(buf); 279 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); 284 } 285 286 for (i = 0; i < VL_MAX_SURFACES; ++i) 287 pipe_surface_reference(&buf->surfaces[i], NULL); 288 289 vl_video_buffer_set_associated_data(buffer, NULL, NULL, NULL); 290 291 FREE(buffer); 292} 293 294static struct pipe_sampler_view ** 295vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer) 296{ 297 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer; 298 struct pipe_sampler_view sv_templ; 299 struct pipe_context *pipe; 300 unsigned i; 301 302 assert(buf); 303 304 pipe = buf->base.context; 305 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); 310 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; 313 314 buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ); 315 if (!buf->sampler_view_planes[i]) 316 goto error; 317 } 318 } 319 320 return buf->sampler_view_planes; 321 322error: 323 for (i = 0; i < buf->num_planes; ++i ) 324 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL); 325 326 return NULL; 327} 328 329static struct pipe_sampler_view ** 330vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer) 331{ 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; 338 339 assert(buf); 340 341 pipe = buf->base.context; 342 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); 345 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) 351 nr_components = 3; 352 353 for (j = 0; j < nr_components && component < VL_NUM_COMPONENTS; ++j, ++component) { 354 if (buf->sampler_view_components[component]) 355 continue; 356 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]) 363 goto error; 364 } 365 } 366 assert(component == VL_NUM_COMPONENTS); 367 368 return buf->sampler_view_components; 369 370error: 371 for (i = 0; i < VL_NUM_COMPONENTS; ++i ) 372 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL); 373 374 return NULL; 375} 376 377static struct pipe_surface ** 378vl_video_buffer_surfaces(struct pipe_video_buffer *buffer) 379{ 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; 384 385 assert(buf); 386 387 pipe = buf->base.context; 388 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); 393 394 if (!buf->resources[i]) { 395 pipe_surface_reference(&buf->surfaces[surf], NULL); 396 continue; 397 } 398 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]) 405 goto error; 406 } 407 } 408 } 409 410 return buf->surfaces; 411 412error: 413 for (i = 0; i < VL_MAX_SURFACES; ++i ) 414 pipe_surface_reference(&buf->surfaces[i], NULL); 415 416 return NULL; 417} 418 419struct pipe_video_buffer * 420vl_video_buffer_create(struct pipe_context *pipe, 421 const struct pipe_video_buffer *tmpl) 422{ 423 const enum pipe_format *resource_formats; 424 struct pipe_video_buffer templat, *result; 425 bool pot_buffers; 426 427 assert(pipe); 428 assert(tmpl->width > 0 && tmpl->height > 0); 429 430 pot_buffers = !pipe->screen->get_video_param 431 ( 432 pipe->screen, 433 PIPE_VIDEO_PROFILE_UNKNOWN, 434 PIPE_VIDEO_ENTRYPOINT_UNKNOWN, 435 PIPE_VIDEO_CAP_NPOT_TEXTURES 436 ); 437 438 resource_formats = vl_video_buffer_formats(pipe->screen, tmpl->buffer_format); 439 if (!resource_formats) 440 return NULL; 441 442 templat = *tmpl; 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); 447 448 if (tmpl->interlaced) 449 templat.height /= 2; 450 451 result = vl_video_buffer_create_ex 452 ( 453 pipe, &templat, resource_formats, 454 1, tmpl->interlaced ? 2 : 1, PIPE_USAGE_DEFAULT 455 ); 456 457 458 if (result && tmpl->interlaced) 459 result->height *= 2; 460 461 return result; 462} 463 464struct pipe_video_buffer * 465vl_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) 469{ 470 struct pipe_resource res_tmpl; 471 struct pipe_resource *resources[VL_NUM_COMPONENTS]; 472 unsigned i; 473 474 assert(pipe); 475 476 memset(resources, 0, sizeof resources); 477 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); 480 if (!resources[0]) 481 goto error; 482 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); 486 } 487 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); 490 if (!resources[1]) 491 goto error; 492 493 if (resource_formats[2] == PIPE_FORMAT_NONE) 494 return vl_video_buffer_create_ex2(pipe, tmpl, resources); 495 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); 498 if (!resources[2]) 499 goto error; 500 501 return vl_video_buffer_create_ex2(pipe, tmpl, resources); 502 503error: 504 for (i = 0; i < VL_NUM_COMPONENTS; ++i) 505 pipe_resource_reference(&resources[i], NULL); 506 507 return NULL; 508} 509 510struct pipe_video_buffer * 511vl_video_buffer_create_ex2(struct pipe_context *pipe, 512 const struct pipe_video_buffer *tmpl, 513 struct pipe_resource *resources[VL_NUM_COMPONENTS]) 514{ 515 struct vl_video_buffer *buffer; 516 unsigned i; 517 518 buffer = CALLOC_STRUCT(vl_video_buffer); 519 if (!buffer) 520 return NULL; 521 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; 529 530 for (i = 0; i < VL_NUM_COMPONENTS; ++i) { 531 buffer->resources[i] = resources[i]; 532 if (resources[i]) 533 buffer->num_planes++; 534 } 535 536 return &buffer->base; 537} 538