vl_video_buffer.c revision 00b4e48560f4d576b7b1924257322f5167e58c8d
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_context *pipe, 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 68static void 69vl_video_buffer_destroy(struct pipe_video_buffer *buffer) 70{ 71 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer; 72 unsigned i; 73 74 assert(buf); 75 76 for (i = 0; i < VL_MAX_PLANES; ++i) { 77 pipe_surface_reference(&buf->surfaces[i], NULL); 78 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL); 79 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL); 80 pipe_resource_reference(&buf->resources[i], NULL); 81 } 82} 83 84static struct pipe_sampler_view ** 85vl_video_buffer_sampler_view_planes(struct pipe_video_buffer *buffer) 86{ 87 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer; 88 struct pipe_sampler_view sv_templ; 89 struct pipe_context *pipe; 90 unsigned i; 91 92 assert(buf); 93 94 pipe = buf->pipe; 95 96 for (i = 0; i < buf->num_planes; ++i ) { 97 if (!buf->sampler_view_planes[i]) { 98 memset(&sv_templ, 0, sizeof(sv_templ)); 99 u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format); 100 101 if (util_format_get_nr_components(buf->resources[i]->format) == 1) 102 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = sv_templ.swizzle_a = PIPE_SWIZZLE_RED; 103 104 buf->sampler_view_planes[i] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ); 105 if (!buf->sampler_view_planes[i]) 106 goto error; 107 } 108 } 109 110 return buf->sampler_view_planes; 111 112error: 113 for (i = 0; i < buf->num_planes; ++i ) 114 pipe_sampler_view_reference(&buf->sampler_view_planes[i], NULL); 115 116 return NULL; 117} 118 119static struct pipe_sampler_view ** 120vl_video_buffer_sampler_view_components(struct pipe_video_buffer *buffer) 121{ 122 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer; 123 struct pipe_sampler_view sv_templ; 124 struct pipe_context *pipe; 125 unsigned i, j, component; 126 127 assert(buf); 128 129 pipe = buf->pipe; 130 131 for (component = 0, i = 0; i < buf->num_planes; ++i ) { 132 unsigned nr_components = util_format_get_nr_components(buf->resources[i]->format); 133 134 for (j = 0; j < nr_components; ++j, ++component) { 135 assert(component < VL_MAX_PLANES); 136 137 if (!buf->sampler_view_components[component]) { 138 memset(&sv_templ, 0, sizeof(sv_templ)); 139 u_sampler_view_default_template(&sv_templ, buf->resources[i], buf->resources[i]->format); 140 sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_RED + j; 141 sv_templ.swizzle_a = PIPE_SWIZZLE_ONE; 142 buf->sampler_view_components[component] = pipe->create_sampler_view(pipe, buf->resources[i], &sv_templ); 143 if (!buf->sampler_view_components[component]) 144 goto error; 145 } 146 } 147 } 148 149 return buf->sampler_view_components; 150 151error: 152 for (i = 0; i < VL_MAX_PLANES; ++i ) 153 pipe_sampler_view_reference(&buf->sampler_view_components[i], NULL); 154 155 return NULL; 156} 157 158static struct pipe_surface ** 159vl_video_buffer_surfaces(struct pipe_video_buffer *buffer) 160{ 161 struct vl_video_buffer *buf = (struct vl_video_buffer *)buffer; 162 struct pipe_surface surf_templ; 163 struct pipe_context *pipe; 164 unsigned i; 165 166 assert(buf); 167 168 pipe = buf->pipe; 169 170 for (i = 0; i < buf->num_planes; ++i ) { 171 if (!buf->surfaces[i]) { 172 memset(&surf_templ, 0, sizeof(surf_templ)); 173 surf_templ.format = buf->resources[i]->format; 174 surf_templ.usage = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET; 175 buf->surfaces[i] = pipe->create_surface(pipe, buf->resources[i], &surf_templ); 176 if (!buf->surfaces[i]) 177 goto error; 178 } 179 } 180 181 return buf->surfaces; 182 183error: 184 for (i = 0; i < buf->num_planes; ++i ) 185 pipe_surface_reference(&buf->surfaces[i], NULL); 186 187 return NULL; 188} 189 190struct pipe_video_buffer * 191vl_video_buffer_init(struct pipe_video_context *context, 192 struct pipe_context *pipe, 193 unsigned width, unsigned height, unsigned depth, 194 enum pipe_video_chroma_format chroma_format, 195 const enum pipe_format resource_formats[VL_MAX_PLANES], 196 unsigned usage) 197{ 198 struct vl_video_buffer *buffer; 199 struct pipe_resource templ; 200 unsigned i; 201 202 assert(context && pipe); 203 204 buffer = CALLOC_STRUCT(vl_video_buffer); 205 206 buffer->base.destroy = vl_video_buffer_destroy; 207 buffer->base.get_sampler_view_planes = vl_video_buffer_sampler_view_planes; 208 buffer->base.get_sampler_view_components = vl_video_buffer_sampler_view_components; 209 buffer->base.get_surfaces = vl_video_buffer_surfaces; 210 buffer->base.chroma_format = chroma_format; 211 buffer->base.width = width; 212 buffer->base.height = height; 213 buffer->pipe = pipe; 214 buffer->num_planes = 1; 215 216 memset(&templ, 0, sizeof(templ)); 217 templ.target = depth > 1 ? PIPE_TEXTURE_3D : PIPE_TEXTURE_2D; 218 templ.format = resource_formats[0]; 219 templ.width0 = width; 220 templ.height0 = height; 221 templ.depth0 = depth; 222 templ.array_size = 1; 223 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET; 224 templ.usage = usage; 225 226 buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ); 227 if (!buffer->resources[0]) 228 goto error; 229 230 if (resource_formats[1] == PIPE_FORMAT_NONE) { 231 assert(chroma_format == PIPE_VIDEO_CHROMA_FORMAT_444); 232 assert(resource_formats[2] == PIPE_FORMAT_NONE); 233 return &buffer->base; 234 } else 235 buffer->num_planes = 2; 236 237 templ.format = resource_formats[1]; 238 if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) { 239 templ.width0 /= 2; 240 templ.height0 /= 2; 241 } else if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) { 242 templ.height0 /= 2; 243 } 244 245 buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ); 246 if (!buffer->resources[1]) 247 goto error; 248 249 if (resource_formats[2] == PIPE_FORMAT_NONE) 250 return &buffer->base; 251 else 252 buffer->num_planes = 3; 253 254 templ.format = resource_formats[2]; 255 buffer->resources[2] = pipe->screen->resource_create(pipe->screen, &templ); 256 if (!buffer->resources[2]) 257 goto error; 258 259 return &buffer->base; 260 261error: 262 for (i = 0; i < VL_MAX_PLANES; ++i) 263 pipe_resource_reference(&buffer->resources[i], NULL); 264 FREE(buffer); 265 266 return NULL; 267} 268