1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**************************************************************************
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2009 Younes Manton.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All Rights Reserved.
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "Software"), to deal in the Software without restriction, including
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribute, sub license, and/or sell copies of the Software, and to
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the following conditions:
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * next paragraph) shall be included in all copies or substantial portions
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the Software.
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org **************************************************************************/
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* directly referenced from target Makefile, because of X dependencies */
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <sys/time.h>
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_state.h"
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_memory.h"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_format.h"
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_inlines.h"
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/xlib_sw_winsys.h"
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "softpipe/sp_public.h"
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "vl/vl_compositor.h"
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "vl/vl_winsys.h"
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct vl_xsp_screen
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct vl_screen base;
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   Display *display;
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int screen;
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   Visual visual;
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct xlib_drawable xdraw;
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_resource *tex;
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct u_rect dirty_area;
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct pipe_resource*
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvl_screen_texture_from_drawable(struct vl_screen *vscreen, Drawable drawable)
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   Window root;
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int x, y;
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned int width, height;
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned int border_width;
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned int depth;
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_resource templat;
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(vscreen);
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(drawable != None);
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (XGetGeometry(xsp_screen->display, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return NULL;
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   xsp_screen->xdraw.drawable = drawable;
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (xsp_screen->tex) {
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (xsp_screen->tex->width0 == width && xsp_screen->tex->height0 == height)
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         return xsp_screen->tex;
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_resource_reference(&xsp_screen->tex, NULL);
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      vl_compositor_reset_dirty_area(&xsp_screen->dirty_area);
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   memset(&templat, 0, sizeof(struct pipe_resource));
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   templat.target = PIPE_TEXTURE_2D;
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* XXX: Need to figure out drawable's format */
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   templat.format = PIPE_FORMAT_B8G8R8X8_UNORM;
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   templat.last_level = 0;
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   templat.width0 = width;
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   templat.height0 = height;
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   templat.depth0 = 1;
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   templat.usage = PIPE_USAGE_DEFAULT;
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   templat.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_DISPLAY_TARGET;
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   templat.flags = 0;
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   xsp_screen->xdraw.depth = 24/*util_format_get_blocksizebits(templat.format) /
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                             util_format_get_blockwidth(templat.format)*/;
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe_resource_reference(&xsp_screen->tex, vscreen->pscreen->resource_create(vscreen->pscreen, &templat));
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return xsp_screen->tex;
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct u_rect *
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvl_screen_get_dirty_area(struct vl_screen *vscreen)
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return &xsp_screen->dirty_area;
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orguint64_t
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvl_screen_get_timestamp(struct vl_screen *vscreen, Drawable drawable)
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct timeval tv;
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   gettimeofday(&tv, NULL);
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return (uint64_t)tv.tv_sec * 1000000000LL + (uint64_t)tv.tv_usec * 1000LL;
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvl_screen_set_next_timestamp(struct vl_screen *vscreen, uint64_t stamp)
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* not supported on softpipe and so only a dummy */
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid*
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvl_screen_get_private(struct vl_screen *vscreen)
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return &xsp_screen->xdraw;
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstruct vl_screen*
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvl_screen_create(Display *display, int screen)
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct vl_xsp_screen *xsp_screen;
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct sw_winsys *winsys;
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(display);
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   xsp_screen = CALLOC_STRUCT(vl_xsp_screen);
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!xsp_screen)
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return NULL;
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   winsys = xlib_create_sw_winsys(display);
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!winsys) {
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      FREE(xsp_screen);
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return NULL;
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   xsp_screen->base.pscreen = softpipe_create_screen(winsys);
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!xsp_screen->base.pscreen) {
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      winsys->destroy(winsys);
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      FREE(xsp_screen);
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return NULL;
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   xsp_screen->display = display;
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   xsp_screen->screen = screen;
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   xsp_screen->xdraw.visual = XDefaultVisual(display, screen);
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   vl_compositor_reset_dirty_area(&xsp_screen->dirty_area);
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return &xsp_screen->base;
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid vl_screen_destroy(struct vl_screen *vscreen)
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct vl_xsp_screen *xsp_screen = (struct vl_xsp_screen*)vscreen;
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(vscreen);
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe_resource_reference(&xsp_screen->tex, NULL);
168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   vscreen->pscreen->destroy(vscreen->pscreen);
169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   FREE(vscreen);
170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
171