st/xvmc: Mark exported funcs with PUBLIC.
[mesa.git] / src / gallium / state_trackers / xorg / xvmc / subpicture.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
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 #include <X11/Xlibint.h>
30 #include <X11/extensions/XvMClib.h>
31 #include <xorg/fourcc.h>
32 #include <vl_winsys.h>
33 #include <pipe/p_screen.h>
34 #include <pipe/p_video_context.h>
35 #include <pipe/p_state.h>
36 #include <util/u_memory.h>
37 #include <util/u_math.h>
38 #include "xvmc_private.h"
39
40 #define FOURCC_RGB 0x0000003
41
42 static enum pipe_format XvIDToPipe(int xvimage_id)
43 {
44 switch (xvimage_id) {
45 case FOURCC_RGB:
46 return PIPE_FORMAT_B8G8R8X8_UNORM;
47 default:
48 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", xvimage_id);
49 return PIPE_FORMAT_NONE;
50 }
51 }
52
53 static int PipeToComponentOrder(enum pipe_format format, char *component_order)
54 {
55 assert(component_order);
56
57 switch (format) {
58 case PIPE_FORMAT_B8G8R8X8_UNORM:
59 return 0;
60 default:
61 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized PIPE_FORMAT 0x%08X.\n", format);
62 component_order[0] = 0;
63 component_order[1] = 0;
64 component_order[2] = 0;
65 component_order[3] = 0;
66 }
67
68 return 0;
69 }
70
71 static Status Validate(Display *dpy, XvPortID port, int surface_type_id, int xvimage_id)
72 {
73 XvImageFormatValues *subpictures;
74 int num_subpics;
75 unsigned int i;
76
77 subpictures = XvMCListSubpictureTypes(dpy, port, surface_type_id, &num_subpics);
78 if (num_subpics < 1) {
79 if (subpictures)
80 XFree(subpictures);
81 return BadMatch;
82 }
83 if (!subpictures)
84 return BadAlloc;
85
86 for (i = 0; i < num_subpics; ++i) {
87 if (subpictures[i].id == xvimage_id) {
88 XVMC_MSG(XVMC_TRACE, "[XvMC] Found requested subpicture format.\n" \
89 "[XvMC] port=%u\n" \
90 "[XvMC] surface id=0x%08X\n" \
91 "[XvMC] image id=0x%08X\n" \
92 "[XvMC] type=%08X\n" \
93 "[XvMC] byte order=%08X\n" \
94 "[XvMC] bits per pixel=%u\n" \
95 "[XvMC] format=%08X\n" \
96 "[XvMC] num planes=%d\n",
97 port, surface_type_id, xvimage_id, subpictures[i].type, subpictures[i].byte_order,
98 subpictures[i].bits_per_pixel, subpictures[i].format, subpictures[i].num_planes);
99 if (subpictures[i].type == XvRGB) {
100 XVMC_MSG(XVMC_TRACE, "[XvMC] depth=%d\n" \
101 "[XvMC] red mask=0x%08X\n" \
102 "[XvMC] green mask=0x%08X\n" \
103 "[XvMC] blue mask=0x%08X\n",
104 subpictures[i].depth, subpictures[i].red_mask, subpictures[i].green_mask, subpictures[i].blue_mask);
105 }
106 else if (subpictures[i].type == XvYUV) {
107 XVMC_MSG(XVMC_TRACE, "[XvMC] y sample bits=0x%08X\n" \
108 "[XvMC] u sample bits=0x%08X\n" \
109 "[XvMC] v sample bits=0x%08X\n" \
110 "[XvMC] horz y period=%u\n" \
111 "[XvMC] horz u period=%u\n" \
112 "[XvMC] horz v period=%u\n" \
113 "[XvMC] vert y period=%u\n" \
114 "[XvMC] vert u period=%u\n" \
115 "[XvMC] vert v period=%u\n",
116 subpictures[i].y_sample_bits, subpictures[i].u_sample_bits, subpictures[i].v_sample_bits,
117 subpictures[i].horz_y_period, subpictures[i].horz_u_period, subpictures[i].horz_v_period,
118 subpictures[i].vert_y_period, subpictures[i].vert_u_period, subpictures[i].vert_v_period);
119 }
120 break;
121 }
122 }
123
124 XFree(subpictures);
125
126 return i < num_subpics ? Success : BadMatch;
127 }
128
129 PUBLIC
130 Status XvMCCreateSubpicture(Display *dpy, XvMCContext *context, XvMCSubpicture *subpicture,
131 unsigned short width, unsigned short height, int xvimage_id)
132 {
133 XvMCContextPrivate *context_priv;
134 XvMCSubpicturePrivate *subpicture_priv;
135 struct pipe_video_context *vpipe;
136 struct pipe_texture template;
137 struct pipe_texture *tex;
138 Status ret;
139
140 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating subpicture %p.\n", subpicture);
141
142 assert(dpy);
143
144 if (!context)
145 return XvMCBadContext;
146
147 context_priv = context->privData;
148 vpipe = context_priv->vctx->vpipe;
149
150 if (!subpicture)
151 return XvMCBadSubpicture;
152
153 if (width > context_priv->subpicture_max_width ||
154 height > context_priv->subpicture_max_height)
155 return BadValue;
156
157 ret = Validate(dpy, context->port, context->surface_type_id, xvimage_id);
158 if (ret != Success)
159 return ret;
160
161 subpicture_priv = CALLOC(1, sizeof(XvMCSubpicturePrivate));
162 if (!subpicture_priv)
163 return BadAlloc;
164
165 memset(&template, 0, sizeof(struct pipe_texture));
166 template.target = PIPE_TEXTURE_2D;
167 template.format = XvIDToPipe(xvimage_id);
168 template.last_level = 0;
169 if (vpipe->get_param(vpipe, PIPE_CAP_NPOT_TEXTURES)) {
170 template.width0 = width;
171 template.height0 = height;
172 }
173 else {
174 template.width0 = util_next_power_of_two(width);
175 template.height0 = util_next_power_of_two(height);
176 }
177 template.depth0 = 1;
178 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
179
180 subpicture_priv->context = context;
181 tex = vpipe->screen->texture_create(vpipe->screen, &template);
182 subpicture_priv->sfc = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
183 PIPE_BUFFER_USAGE_CPU_WRITE |
184 PIPE_BUFFER_USAGE_GPU_READ);
185 pipe_texture_reference(&tex, NULL);
186 if (!subpicture_priv->sfc) {
187 FREE(subpicture_priv);
188 return BadAlloc;
189 }
190
191 subpicture->subpicture_id = XAllocID(dpy);
192 subpicture->context_id = context->context_id;
193 subpicture->xvimage_id = xvimage_id;
194 subpicture->width = width;
195 subpicture->height = height;
196 subpicture->num_palette_entries = 0;
197 subpicture->entry_bytes = PipeToComponentOrder(template.format, subpicture->component_order);
198 subpicture->privData = subpicture_priv;
199
200 SyncHandle();
201
202 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p created.\n", subpicture);
203
204 return Success;
205 }
206
207 PUBLIC
208 Status XvMCClearSubpicture(Display *dpy, XvMCSubpicture *subpicture, short x, short y,
209 unsigned short width, unsigned short height, unsigned int color)
210 {
211 XvMCSubpicturePrivate *subpicture_priv;
212 XvMCContextPrivate *context_priv;
213
214 assert(dpy);
215
216 if (!subpicture)
217 return XvMCBadSubpicture;
218
219 subpicture_priv = subpicture->privData;
220 context_priv = subpicture_priv->context->privData;
221 /* TODO: Assert clear rect is within bounds? Or clip? */
222 context_priv->vctx->vpipe->surface_fill(context_priv->vctx->vpipe,
223 subpicture_priv->sfc, x, y,
224 width, height, color);
225
226 return Success;
227 }
228
229 PUBLIC
230 Status XvMCCompositeSubpicture(Display *dpy, XvMCSubpicture *subpicture, XvImage *image,
231 short srcx, short srcy, unsigned short width, unsigned short height,
232 short dstx, short dsty)
233 {
234 XvMCSubpicturePrivate *subpicture_priv;
235 XvMCContextPrivate *context_priv;
236 struct pipe_screen *screen;
237 struct pipe_transfer *xfer;
238 unsigned char *src, *dst;
239 unsigned x, y;
240
241 XVMC_MSG(XVMC_TRACE, "[XvMC] Compositing subpicture %p.\n", subpicture);
242
243 assert(dpy);
244
245 if (!subpicture)
246 return XvMCBadSubpicture;
247
248 assert(image);
249
250 if (subpicture->xvimage_id != image->id)
251 return BadMatch;
252
253 /* No planar support for now */
254 if (image->num_planes != 1)
255 return BadMatch;
256
257 subpicture_priv = subpicture->privData;
258 context_priv = subpicture_priv->context->privData;
259 screen = context_priv->vctx->vpipe->screen;
260
261 /* TODO: Assert rects are within bounds? Or clip? */
262
263 xfer = screen->get_tex_transfer(screen, subpicture_priv->sfc->texture, 0, 0, 0,
264 PIPE_TRANSFER_WRITE, dstx, dsty, width, height);
265 if (!xfer)
266 return BadAlloc;
267
268 src = image->data;
269 dst = screen->transfer_map(screen, xfer);
270 if (!dst) {
271 screen->tex_transfer_destroy(xfer);
272 return BadAlloc;
273 }
274
275 switch (image->id) {
276 case FOURCC_RGB:
277 assert(subpicture_priv->sfc->format == XvIDToPipe(image->id));
278 for (y = 0; y < height; ++y) {
279 for (x = 0; x < width; ++x, src += 3, dst += 4) {
280 /* TODO: Confirm or fix */
281 dst[0] = src[0];
282 dst[1] = src[1];
283 dst[2] = src[2];
284 }
285 }
286 break;
287 default:
288 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized Xv image ID 0x%08X.\n", image->id);
289 }
290
291 screen->transfer_unmap(screen, xfer);
292 screen->tex_transfer_destroy(xfer);
293
294 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p composited.\n", subpicture);
295
296 return Success;
297 }
298
299 PUBLIC
300 Status XvMCDestroySubpicture(Display *dpy, XvMCSubpicture *subpicture)
301 {
302 XvMCSubpicturePrivate *subpicture_priv;
303
304 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying subpicture %p.\n", subpicture);
305
306 assert(dpy);
307
308 if (!subpicture)
309 return XvMCBadSubpicture;
310
311 subpicture_priv = subpicture->privData;
312 pipe_surface_reference(&subpicture_priv->sfc, NULL);
313 FREE(subpicture_priv);
314
315 XVMC_MSG(XVMC_TRACE, "[XvMC] Subpicture %p destroyed.\n", subpicture);
316
317 return Success;
318 }
319
320 PUBLIC
321 Status XvMCSetSubpicturePalette(Display *dpy, XvMCSubpicture *subpicture, unsigned char *palette)
322 {
323 assert(dpy);
324
325 if (!subpicture)
326 return XvMCBadSubpicture;
327
328 assert(palette);
329
330 /* We don't support paletted subpictures */
331 return BadMatch;
332 }
333
334 PUBLIC
335 Status XvMCBlendSubpicture(Display *dpy, XvMCSurface *target_surface, XvMCSubpicture *subpicture,
336 short subx, short suby, unsigned short subw, unsigned short subh,
337 short surfx, short surfy, unsigned short surfw, unsigned short surfh)
338 {
339 XvMCSurfacePrivate *surface_priv;
340 XvMCSubpicturePrivate *subpicture_priv;
341
342 XVMC_MSG(XVMC_TRACE, "[XvMC] Associating subpicture %p with surface %p.\n", subpicture, target_surface);
343
344 assert(dpy);
345
346 if (!target_surface)
347 return XvMCBadSurface;
348
349 if (!subpicture)
350 return XvMCBadSubpicture;
351
352 if (target_surface->context_id != subpicture->context_id)
353 return BadMatch;
354
355 /* TODO: Verify against subpicture independent scaling */
356
357 surface_priv = target_surface->privData;
358 subpicture_priv = subpicture->privData;
359
360 /* TODO: Assert rects are within bounds? Or clip? */
361
362 surface_priv->subpicture = subpicture;
363 surface_priv->subx = subx;
364 surface_priv->suby = suby;
365 surface_priv->subw = subw;
366 surface_priv->subh = subh;
367 surface_priv->surfx = surfx;
368 surface_priv->surfy = surfy;
369 surface_priv->surfw = surfw;
370 surface_priv->surfh = surfh;
371 subpicture_priv->surface = target_surface;
372
373 return Success;
374 }
375
376 PUBLIC
377 Status XvMCBlendSubpicture2(Display *dpy, XvMCSurface *source_surface, XvMCSurface *target_surface,
378 XvMCSubpicture *subpicture, short subx, short suby, unsigned short subw, unsigned short subh,
379 short surfx, short surfy, unsigned short surfw, unsigned short surfh)
380 {
381 assert(dpy);
382
383 if (!source_surface || !target_surface)
384 return XvMCBadSurface;
385
386 if (!subpicture)
387 return XvMCBadSubpicture;
388
389 if (source_surface->context_id != subpicture->context_id)
390 return BadMatch;
391
392 if (source_surface->context_id != subpicture->context_id)
393 return BadMatch;
394
395 /* TODO: Assert rects are within bounds? Or clip? */
396
397 return Success;
398 }
399
400 PUBLIC
401 Status XvMCSyncSubpicture(Display *dpy, XvMCSubpicture *subpicture)
402 {
403 assert(dpy);
404
405 if (!subpicture)
406 return XvMCBadSubpicture;
407
408 return Success;
409 }
410
411 PUBLIC
412 Status XvMCFlushSubpicture(Display *dpy, XvMCSubpicture *subpicture)
413 {
414 assert(dpy);
415
416 if (!subpicture)
417 return XvMCBadSubpicture;
418
419 return Success;
420 }
421
422 PUBLIC
423 Status XvMCGetSubpictureStatus(Display *dpy, XvMCSubpicture *subpicture, int *status)
424 {
425 assert(dpy);
426
427 if (!subpicture)
428 return XvMCBadSubpicture;
429
430 assert(status);
431
432 /* TODO */
433 *status = 0;
434
435 return Success;
436 }