1 /*
2  * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "va_dricommon.h"
25 
26 // X error trap
27 static int x11_error_code = 0;
28 static int (*old_error_handler)(Display *, XErrorEvent *);
29 
30 static int
error_handler(Display * dpy,XErrorEvent * error)31 error_handler(Display *dpy, XErrorEvent *error)
32 {
33     x11_error_code = error->error_code;
34     return 0;
35 }
36 
37 static void
x11_trap_errors(void)38 x11_trap_errors(void)
39 {
40     x11_error_code    = 0;
41     old_error_handler = XSetErrorHandler(error_handler);
42 }
43 
44 static int
x11_untrap_errors(void)45 x11_untrap_errors(void)
46 {
47     XSetErrorHandler(old_error_handler);
48     return x11_error_code;
49 }
50 
51 static int
is_window(Display * dpy,Drawable drawable)52 is_window(Display *dpy, Drawable drawable)
53 {
54     XWindowAttributes wattr;
55 
56     x11_trap_errors();
57     XGetWindowAttributes(dpy, drawable, &wattr);
58     return x11_untrap_errors() == 0;
59 }
60 
61 static struct dri_drawable *
do_drawable_hash(VADriverContextP ctx,XID drawable)62 do_drawable_hash(VADriverContextP ctx, XID drawable)
63 {
64     struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
65     int index = drawable % DRAWABLE_HASH_SZ;
66     struct dri_drawable *dri_drawable = dri_state->drawable_hash[index];
67 
68     while (dri_drawable) {
69         if (dri_drawable->x_drawable == drawable)
70             return dri_drawable;
71         dri_drawable = dri_drawable->next;
72     }
73 
74     dri_drawable = dri_state->createDrawable(ctx, drawable);
75     dri_drawable->x_drawable = drawable;
76     dri_drawable->is_window = is_window(ctx->native_dpy, drawable);
77     dri_drawable->next = dri_state->drawable_hash[index];
78     dri_state->drawable_hash[index] = dri_drawable;
79 
80     return dri_drawable;
81 }
82 
83 void
free_drawable(VADriverContextP ctx,struct dri_drawable * dri_drawable)84 free_drawable(VADriverContextP ctx, struct dri_drawable* dri_drawable)
85 {
86     struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
87     int i = 0;
88 
89     while (i++ < DRAWABLE_HASH_SZ) {
90 	if (dri_drawable == dri_state->drawable_hash[i]) {
91 	    dri_state->destroyDrawable(ctx, dri_drawable);
92 	    dri_state->drawable_hash[i] = NULL;
93 	}
94     }
95 }
96 
97 void
free_drawable_hashtable(VADriverContextP ctx)98 free_drawable_hashtable(VADriverContextP ctx)
99 {
100     struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
101     int i;
102     struct dri_drawable *dri_drawable, *prev;
103 
104     for (i = 0; i < DRAWABLE_HASH_SZ; i++) {
105         dri_drawable = dri_state->drawable_hash[i];
106 
107         while (dri_drawable) {
108             prev = dri_drawable;
109             dri_drawable = prev->next;
110             dri_state->destroyDrawable(ctx, prev);
111         }
112 
113 	dri_state->drawable_hash[i] = NULL;
114     }
115 }
116 
117 struct dri_drawable *
dri_get_drawable(VADriverContextP ctx,XID drawable)118 dri_get_drawable(VADriverContextP ctx, XID drawable)
119 {
120     return do_drawable_hash(ctx, drawable);
121 }
122 
123 void
dri_swap_buffer(VADriverContextP ctx,struct dri_drawable * dri_drawable)124 dri_swap_buffer(VADriverContextP ctx, struct dri_drawable *dri_drawable)
125 {
126     struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
127 
128     dri_state->swapBuffer(ctx, dri_drawable);
129 }
130 
131 union dri_buffer *
dri_get_rendering_buffer(VADriverContextP ctx,struct dri_drawable * dri_drawable)132 dri_get_rendering_buffer(VADriverContextP ctx, struct dri_drawable *dri_drawable)
133 {
134     struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
135 
136     return dri_state->getRenderingBuffer(ctx, dri_drawable);
137 }
138