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/* directly referenced from target Makefile, because of X dependencies */
29
30#include <sys/time.h>
31
32#include "pipe/p_state.h"
33
34#include "util/u_memory.h"
35#include "util/u_format.h"
36#include "util/u_inlines.h"
37
38#include "state_tracker/xlib_sw_winsys.h"
39#include "softpipe/sp_public.h"
40
41#include "vl/vl_compositor.h"
42#include "vl/vl_winsys.h"
43
44struct vl_xsp_screen
45{
46   struct vl_screen base;
47   Display *display;
48   int screen;
49   Visual visual;
50   struct xlib_drawable xdraw;
51   struct pipe_resource *tex;
52   struct u_rect dirty_area;
53};
54
55struct pipe_resource*
56vl_screen_texture_from_drawable(struct vl_screen *vscreen, Drawable drawable)
57{
58   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;
59   Window root;
60   int x, y;
61   unsigned int width, height;
62   unsigned int border_width;
63   unsigned int depth;
64   struct pipe_resource templat;
65
66   assert(vscreen);
67   assert(drawable != None);
68
69   if (XGetGeometry(xsp_screen->display, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
70      return NULL;
71
72   xsp_screen->xdraw.drawable = drawable;
73
74   if (xsp_screen->tex) {
75      if (xsp_screen->tex->width0 == width && xsp_screen->tex->height0 == height)
76         return xsp_screen->tex;
77      pipe_resource_reference(&xsp_screen->tex, NULL);
78      vl_compositor_reset_dirty_area(&xsp_screen->dirty_area);
79   }
80
81   memset(&templat, 0, sizeof(struct pipe_resource));
82   templat.target = PIPE_TEXTURE_2D;
83   /* XXX: Need to figure out drawable's format */
84   templat.format = PIPE_FORMAT_B8G8R8X8_UNORM;
85   templat.last_level = 0;
86   templat.width0 = width;
87   templat.height0 = height;
88   templat.depth0 = 1;
89   templat.usage = PIPE_USAGE_DEFAULT;
90   templat.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET;
91   templat.flags = 0;
92
93   xsp_screen->xdraw.depth = 24/*util_format_get_blocksizebits(templat.format) /
94                             util_format_get_blockwidth(templat.format)*/;
95
96   pipe_resource_reference(&xsp_screen->tex, vscreen->pscreen->resource_create(vscreen->pscreen, &templat));
97   return xsp_screen->tex;
98}
99
100struct u_rect *
101vl_screen_get_dirty_area(struct vl_screen *vscreen)
102{
103   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;
104   return &xsp_screen->dirty_area;
105}
106
107uint64_t
108vl_screen_get_timestamp(struct vl_screen *vscreen, Drawable drawable)
109{
110   struct timeval tv;
111   gettimeofday(&tv, NULL);
112   return (uint64_t)tv.tv_sec * 1000000000LL + (uint64_t)tv.tv_usec * 1000LL;
113}
114
115void
116vl_screen_set_next_timestamp(struct vl_screen *vscreen, uint64_t stamp)
117{
118   /* not supported on softpipe and so only a dummy */
119}
120
121void*
122vl_screen_get_private(struct vl_screen *vscreen)
123{
124   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;
125   return &xsp_screen->xdraw;
126}
127
128struct vl_screen*
129vl_screen_create(Display *display, int screen)
130{
131   struct vl_xsp_screen *xsp_screen;
132   struct sw_winsys *winsys;
133
134   assert(display);
135
136   xsp_screen = CALLOC_STRUCT(vl_xsp_screen);
137   if (!xsp_screen)
138      return NULL;
139
140   winsys = xlib_create_sw_winsys(display);
141   if (!winsys) {
142      FREE(xsp_screen);
143      return NULL;
144   }
145
146   xsp_screen->base.pscreen = softpipe_create_screen(winsys);
147   if (!xsp_screen->base.pscreen) {
148      winsys->destroy(winsys);
149      FREE(xsp_screen);
150      return NULL;
151   }
152
153   xsp_screen->display = display;
154   xsp_screen->screen = screen;
155   xsp_screen->xdraw.visual = XDefaultVisual(display, screen);
156   vl_compositor_reset_dirty_area(&xsp_screen->dirty_area);
157
158   return &xsp_screen->base;
159}
160
161void vl_screen_destroy(struct vl_screen *vscreen)
162{
163   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;
164
165   assert(vscreen);
166
167   pipe_resource_reference(&xsp_screen->tex, NULL);
168   vscreen->pscreen->destroy(vscreen->pscreen);
169   FREE(vscreen);
170}
171