g3dvl/vdpau: some more indention fixes
[mesa.git] / src / gallium / auxiliary / vl / vl_ycbcr_buffer.c
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 "vl_ycbcr_buffer.h"
29 #include <util/u_format.h>
30 #include <util/u_inlines.h>
31 #include <util/u_sampler.h>
32 #include <pipe/p_screen.h>
33 #include <pipe/p_context.h>
34 #include <assert.h>
35
36 bool vl_ycbcr_buffer_init(struct vl_ycbcr_buffer *buffer,
37 struct pipe_context *pipe,
38 unsigned width, unsigned height,
39 enum pipe_video_chroma_format chroma_format,
40 enum pipe_format resource_format,
41 unsigned usage)
42 {
43 struct pipe_resource templ;
44
45 assert(buffer && pipe);
46
47 memset(buffer, 0, sizeof(struct vl_ycbcr_buffer));
48 buffer->pipe = pipe;
49
50 memset(&templ, 0, sizeof(templ));
51 templ.target = PIPE_TEXTURE_2D;
52 templ.format = resource_format;
53 templ.width0 = width / util_format_get_nr_components(resource_format);
54 templ.height0 = height;
55 templ.depth0 = 1;
56 templ.array_size = 1;
57 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
58 templ.usage = usage;
59
60 buffer->resources.y = pipe->screen->resource_create(pipe->screen, &templ);
61 if (!buffer->resources.y)
62 goto error_resource_y;
63
64 if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) {
65 templ.width0 /= 2;
66 templ.height0 /= 2;
67 } else if (chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) {
68 templ.height0 /= 2;
69 }
70
71 buffer->resources.cb = pipe->screen->resource_create(pipe->screen, &templ);
72 if (!buffer->resources.cb)
73 goto error_resource_cb;
74
75 buffer->resources.cr = pipe->screen->resource_create(pipe->screen, &templ);
76 if (!buffer->resources.cr)
77 goto error_resource_cr;
78
79 return true;
80
81 error_resource_cr:
82 pipe_resource_reference(&buffer->resources.cb, NULL);
83
84 error_resource_cb:
85 pipe_resource_reference(&buffer->resources.y, NULL);
86
87 error_resource_y:
88 return false;
89 }
90
91 struct vl_ycbcr_sampler_views *vl_ycbcr_get_sampler_views(struct vl_ycbcr_buffer *buffer)
92 {
93 struct pipe_sampler_view sv_templ;
94 struct pipe_context *pipe;
95
96 assert(buffer);
97
98 pipe = buffer->pipe;
99
100 memset(&sv_templ, 0, sizeof(sv_templ));
101 u_sampler_view_default_template(&sv_templ, buffer->resources.y, buffer->resources.y->format);
102
103 if (util_format_get_nr_components(buffer->resources.y->format) == 1) {
104 sv_templ.swizzle_r = PIPE_SWIZZLE_RED;
105 sv_templ.swizzle_g = PIPE_SWIZZLE_RED;
106 sv_templ.swizzle_b = PIPE_SWIZZLE_RED;
107 sv_templ.swizzle_a = PIPE_SWIZZLE_RED;
108 }
109
110 if (!buffer->sampler_views.y) {
111 buffer->sampler_views.y = pipe->create_sampler_view(pipe, buffer->resources.y, &sv_templ);
112 if (!buffer->sampler_views.y)
113 goto error;
114 }
115
116 if (!buffer->sampler_views.cb) {
117 buffer->sampler_views.cb = pipe->create_sampler_view(pipe, buffer->resources.cb, &sv_templ);
118 if (!buffer->sampler_views.cb)
119 goto error;
120 }
121
122 if (!buffer->sampler_views.cr) {
123 buffer->sampler_views.cr = pipe->create_sampler_view(pipe, buffer->resources.cr, &sv_templ);
124 if (!buffer->sampler_views.cr)
125 goto error;
126 }
127
128 return &buffer->sampler_views;
129
130 error:
131 pipe_sampler_view_reference(&buffer->sampler_views.y, NULL);
132 pipe_sampler_view_reference(&buffer->sampler_views.cb, NULL);
133 pipe_sampler_view_reference(&buffer->sampler_views.cr, NULL);
134 return NULL;
135 }
136
137 struct vl_ycbcr_surfaces *vl_ycbcr_get_surfaces(struct vl_ycbcr_buffer *buffer)
138 {
139 struct pipe_surface surf_templ;
140 struct pipe_context *pipe;
141
142 assert(buffer);
143
144 pipe = buffer->pipe;
145
146 if (!buffer->surfaces.y) {
147 memset(&surf_templ, 0, sizeof(surf_templ));
148 surf_templ.format = buffer->resources.y->format;
149 surf_templ.usage = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
150 buffer->surfaces.y = pipe->create_surface(pipe, buffer->resources.y, &surf_templ);
151 if (!buffer->surfaces.y)
152 goto error;
153 }
154
155 if (!buffer->surfaces.cb) {
156 memset(&surf_templ, 0, sizeof(surf_templ));
157 surf_templ.format = buffer->resources.cb->format;
158 surf_templ.usage = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
159 buffer->surfaces.cb = pipe->create_surface(pipe, buffer->resources.cb, &surf_templ);
160 if (!buffer->surfaces.cb)
161 goto error;
162 }
163
164 if (!buffer->surfaces.cr) {
165 memset(&surf_templ, 0, sizeof(surf_templ));
166 surf_templ.format = buffer->resources.cr->format;
167 surf_templ.usage = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
168 buffer->surfaces.cr = pipe->create_surface(pipe, buffer->resources.cr, &surf_templ);
169 if (!buffer->surfaces.cr)
170 goto error;
171 }
172
173 return &buffer->surfaces;
174
175 error:
176 pipe_surface_reference(&buffer->surfaces.y, NULL);
177 pipe_surface_reference(&buffer->surfaces.cb, NULL);
178 pipe_surface_reference(&buffer->surfaces.cr, NULL);
179 return NULL;
180 }
181
182 void vl_ycbcr_buffer_cleanup(struct vl_ycbcr_buffer *buffer)
183 {
184 pipe_surface_reference(&buffer->surfaces.y, NULL);
185 pipe_surface_reference(&buffer->surfaces.cb, NULL);
186 pipe_surface_reference(&buffer->surfaces.cr, NULL);
187
188 pipe_sampler_view_reference(&buffer->sampler_views.y, NULL);
189 pipe_sampler_view_reference(&buffer->sampler_views.cb, NULL);
190 pipe_sampler_view_reference(&buffer->sampler_views.cr, NULL);
191
192 pipe_resource_reference(&buffer->resources.y, NULL);
193 pipe_resource_reference(&buffer->resources.cb, NULL);
194 pipe_resource_reference(&buffer->resources.cr, NULL);
195 }