summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/xorg/xvmc/context.c
blob: 33f47838f5159a1310e418ec42f23c60398ce277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <assert.h>
#include <X11/Xlibint.h>
#include <X11/extensions/XvMClib.h>
#include <pipe/p_screen.h>
#include <pipe/p_video_context.h>
#include <pipe/p_video_state.h>
#include <pipe/p_state.h>
#include <vl_winsys.h>
#include <util/u_memory.h>
#include "xvmc_private.h"

static Status Validate(Display *dpy, XvPortID port, int surface_type_id,
                       unsigned int width, unsigned int height, int flags,
                       bool *found_port, int *screen, int *chroma_format, int *mc_type)
{
   bool found_surface = false;
   XvAdaptorInfo *adaptor_info;
   unsigned int num_adaptors;
   int num_types;
   unsigned int max_width, max_height;
   Status ret;

   assert(dpy);
   assert(found_port);
   assert(screen);
   assert(chroma_format);
   assert(mc_type);

   *found_port = false;

   for (unsigned int i = 0; i < XScreenCount(dpy); ++i)
   {
      ret = XvQueryAdaptors(dpy, XRootWindow(dpy, i), &num_adaptors, &adaptor_info);
      if (ret != Success)
         return ret;

      for (unsigned int j = 0; j < num_adaptors && !*found_port; ++j)
      {
         for (unsigned int k = 0; k < adaptor_info[j].num_ports && !*found_port; ++k)
         {
            XvMCSurfaceInfo *surface_info;

            if (adaptor_info[j].base_id + k != port)
               continue;

            *found_port = true;

            surface_info = XvMCListSurfaceTypes(dpy, adaptor_info[j].base_id, &num_types);
            if (!surface_info)
            {
               XvFreeAdaptorInfo(adaptor_info);
               return BadAlloc;
            }

            for (unsigned int l = 0; l < num_types && !found_surface; ++l)
            {
               if (surface_info[l].surface_type_id != surface_type_id)
                  continue;

               found_surface = true;
               max_width = surface_info[l].max_width;
               max_height = surface_info[l].max_height;
               *chroma_format = surface_info[l].chroma_format;
               *mc_type = surface_info[l].mc_type;
               *screen = i;
            }

         XFree(surface_info);
         }
      }

      XvFreeAdaptorInfo(adaptor_info);
   }

   if (!*found_port)
      return XvBadPort;
   if (!found_surface)
      return BadMatch;
   if (width > max_width || height > max_height)
      return BadValue;
   if (flags != XVMC_DIRECT && flags != 0)
      return BadValue;

   return Success;
}

static enum pipe_video_profile ProfileToPipe(int xvmc_profile)
{
   if (xvmc_profile & XVMC_MPEG_1)
      assert(0);
   if (xvmc_profile & XVMC_MPEG_2)
      return PIPE_VIDEO_PROFILE_MPEG2_MAIN;
   if (xvmc_profile & XVMC_H263)
      assert(0);
   if (xvmc_profile & XVMC_MPEG_4)
      assert(0);
	
   assert(0);

   return -1;
}

static enum pipe_video_chroma_format FormatToPipe(int xvmc_format)
{
   switch (xvmc_format)
   {
      case XVMC_CHROMA_FORMAT_420:
         return PIPE_VIDEO_CHROMA_FORMAT_420;
      case XVMC_CHROMA_FORMAT_422:
         return PIPE_VIDEO_CHROMA_FORMAT_422;
      case XVMC_CHROMA_FORMAT_444:
         return PIPE_VIDEO_CHROMA_FORMAT_444;
      default:
         assert(0);
   }

   return -1;
}

Status XvMCCreateContext(Display *dpy, XvPortID port, int surface_type_id,
                         int width, int height, int flags, XvMCContext *context)
{
   bool found_port;
   int scrn;
   int chroma_format;
   int mc_type;
   Status ret;
   struct pipe_screen *screen;
   struct pipe_video_context *vpipe;
   XvMCContextPrivate *context_priv;

   assert(dpy);

   if (!context)
      return XvMCBadContext;

   ret = Validate(dpy, port, surface_type_id, width, height, flags,
                  &found_port, &scrn, &chroma_format, &mc_type);

   /* Success and XvBadPort have the same value */
   if (ret != Success || !found_port)
      return ret;

   context_priv = CALLOC(1, sizeof(XvMCContextPrivate));
   if (!context_priv)
      return BadAlloc;

   /* TODO: Reuse screen if process creates another context */
   screen = vl_screen_create(dpy, scrn);

   if (!screen)
   {
      FREE(context_priv);
      return BadAlloc;
   }

   vpipe = vl_video_create(screen, ProfileToPipe(mc_type),
                           FormatToPipe(chroma_format), width, height);

   if (!vpipe)
   {
      screen->destroy(screen);
      FREE(context_priv);
      return BadAlloc;
   }

   context_priv->vpipe = vpipe;

   context->context_id = XAllocID(dpy);
   context->surface_type_id = surface_type_id;
   context->width = width;
   context->height = height;
   context->flags = flags;
   context->port = port;
   context->privData = context_priv;
	
   SyncHandle();

   return Success;
}

Status XvMCDestroyContext(Display *dpy, XvMCContext *context)
{
   struct pipe_screen *screen;
   struct pipe_video_context *vpipe;
   XvMCContextPrivate *context_priv;

   assert(dpy);

   if (!context || !context->privData)
      return XvMCBadContext;

   context_priv = context->privData;
   vpipe = context_priv->vpipe;
   pipe_surface_reference(&context_priv->backbuffer, NULL);
   screen = vpipe->screen;
   vpipe->destroy(vpipe);
   screen->destroy(screen);
   FREE(context_priv);
   context->privData = NULL;

   return Success;
}