summaryrefslogtreecommitdiffstats
path: root/src/gallium/winsys/g3dvl/xlib/xsp_winsys.c
blob: cc80583f088d25d8b8c86dfa196048682831d585 (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
/**************************************************************************
 *
 * Copyright 2009 Younes Manton.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include <vl_winsys.h>
#include <X11/Xlibint.h>
#include <state_tracker/xlib_sw_winsys.h>
#include <util/u_memory.h>
#include <util/u_format.h>
#include <softpipe/sp_public.h>
#include <softpipe/sp_video_context.h>

struct vl_xsp_screen
{
   struct vl_screen base;
   Display *display;
   int screen;
   Visual visual;
   struct xlib_drawable xdraw;
   struct pipe_surface *drawable_surface;
};

struct pipe_surface*
vl_drawable_surface_get(struct vl_screen *vscreen, Drawable drawable)
{
   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;
   Window root;
   int x, y;
   unsigned int width, height;
   unsigned int border_width;
   unsigned int depth;
   struct pipe_resource templat, *drawable_tex;
   struct pipe_surface *drawable_surface = NULL;

   assert(vscreen);
   assert(drawable != None);

   if (XGetGeometry(xsp_screen->display, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
      return NULL;

   xsp_screen->xdraw.drawable = drawable;

   if (xsp_screen->drawable_surface) {
      if (xsp_screen->drawable_surface->width == width &&
          xsp_screen->drawable_surface->height == height) {
         pipe_surface_reference(&drawable_surface, xsp_screen->drawable_surface);
         return drawable_surface;
      }
      else
         pipe_surface_reference(&xsp_screen->drawable_surface, NULL);
   }

   memset(&templat, 0, sizeof(struct pipe_resource));
   templat.target = PIPE_TEXTURE_2D;
   /* XXX: Need to figure out drawable's format */
   templat.format = PIPE_FORMAT_B8G8R8X8_UNORM;
   templat.last_level = 0;
   templat.width0 = width;
   templat.height0 = height;
   templat.depth0 = 1;
   templat.usage = PIPE_USAGE_DEFAULT;
   templat.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET;
   templat.flags = 0;

   drawable_tex = vscreen->pscreen->resource_create(vscreen->pscreen, &templat);
   if (!drawable_tex)
      return NULL;

   xsp_screen->drawable_surface = vscreen->pscreen->get_tex_surface(vscreen->pscreen, drawable_tex,
                                                                    0, 0, 0,
                                                                    templat.bind);
   pipe_resource_reference(&drawable_tex, NULL);

   if (!xsp_screen->drawable_surface)
      return NULL;

   pipe_surface_reference(&drawable_surface, xsp_screen->drawable_surface);

   xsp_screen->xdraw.depth = 24/*util_format_get_blocksizebits(templat.format) /
                             util_format_get_blockwidth(templat.format)*/;

   return drawable_surface;
}

void*
vl_contextprivate_get(struct vl_context *vctx, struct pipe_surface *drawable_surface)
{
   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vctx->vscreen;

   assert(vctx);
   assert(drawable_surface);
   assert(xsp_screen->drawable_surface == drawable_surface);

   return &xsp_screen->xdraw;
}

struct vl_screen*
vl_screen_create(Display *display, int screen)
{
   struct vl_xsp_screen *xsp_screen;
   struct sw_winsys *winsys;

   assert(display);

   xsp_screen = CALLOC_STRUCT(vl_xsp_screen);
   if (!xsp_screen)
      return NULL;

   winsys = xlib_create_sw_winsys(display);
   if (!winsys) {
      FREE(xsp_screen);
      return NULL;
   }

   xsp_screen->base.pscreen = softpipe_create_screen(winsys);
   if (!xsp_screen->base.pscreen) {
      winsys->destroy(winsys);
      FREE(xsp_screen);
      return NULL;
   }

   xsp_screen->display = display;
   xsp_screen->screen = screen;
   xsp_screen->xdraw.visual = XDefaultVisual(display, screen);

   return &xsp_screen->base;
}

void vl_screen_destroy(struct vl_screen *vscreen)
{
   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;

   assert(vscreen);

   pipe_surface_reference(&xsp_screen->drawable_surface, NULL);
   vscreen->pscreen->destroy(vscreen->pscreen);
   FREE(vscreen);
}

struct vl_context*
vl_video_create(struct vl_screen *vscreen,
                enum pipe_video_profile profile,
                enum pipe_video_chroma_format chroma_format,
                unsigned width, unsigned height)
{
   struct pipe_video_context *vpipe;
   struct vl_context *vctx;

   assert(vscreen);
   assert(width && height);
   assert(vscreen->pscreen->video_context_create);

   vpipe = vscreen->pscreen->video_context_create(vscreen->pscreen,
                                                  profile,
                                                  chroma_format,
                                                  width, height, NULL);
   if (!vpipe)
      return NULL;

   vctx = CALLOC_STRUCT(vl_context);
   if (!vctx) {
      vpipe->destroy(vpipe);
      return NULL;
   }

   vpipe->priv = vctx;
   vctx->vpipe = vpipe;
   vctx->vscreen = vscreen;

   return vctx;
}

void vl_video_destroy(struct vl_context *vctx)
{
   assert(vctx);

   vctx->vpipe->destroy(vctx->vpipe);
   FREE(vctx);
}