xref: /aosp_15_r20/external/libva/va/x11/va_dricommon.c (revision 54e60f844a168e9a219354de272cd517ee8cd4b7)
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     if (dri_state->createDrawable) {
75         dri_drawable = dri_state->createDrawable(ctx, drawable);
76     }
77     if (dri_drawable) {
78         dri_drawable->x_drawable = drawable;
79         dri_drawable->is_window = is_window(ctx->native_dpy, drawable);
80         dri_drawable->next = dri_state->drawable_hash[index];
81         dri_state->drawable_hash[index] = dri_drawable;
82     }
83 
84     return dri_drawable;
85 }
86 
87 void
va_dri_free_drawable(VADriverContextP ctx,struct dri_drawable * dri_drawable)88 va_dri_free_drawable(VADriverContextP ctx, struct dri_drawable* dri_drawable)
89 {
90     struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
91     int i = 0;
92 
93     while (i < DRAWABLE_HASH_SZ) {
94         if (dri_drawable == dri_state->drawable_hash[i]) {
95             dri_state->destroyDrawable(ctx, dri_drawable);
96             dri_state->drawable_hash[i] = NULL;
97             return;
98         }
99         i++;
100     }
101 }
102 
103 void
va_dri_free_drawable_hashtable(VADriverContextP ctx)104 va_dri_free_drawable_hashtable(VADriverContextP ctx)
105 {
106     struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
107     int i;
108     struct dri_drawable *dri_drawable, *prev;
109 
110     for (i = 0; i < DRAWABLE_HASH_SZ; i++) {
111         dri_drawable = dri_state->drawable_hash[i];
112 
113         while (dri_drawable) {
114             prev = dri_drawable;
115             dri_drawable = prev->next;
116             dri_state->destroyDrawable(ctx, prev);
117         }
118 
119         dri_state->drawable_hash[i] = NULL;
120     }
121 }
122 
123 struct dri_drawable *
va_dri_get_drawable(VADriverContextP ctx,XID drawable)124 va_dri_get_drawable(VADriverContextP ctx, XID drawable)
125 {
126     return do_drawable_hash(ctx, drawable);
127 }
128 
129 void
va_dri_swap_buffer(VADriverContextP ctx,struct dri_drawable * dri_drawable)130 va_dri_swap_buffer(VADriverContextP ctx, struct dri_drawable *dri_drawable)
131 {
132     struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
133 
134     dri_state->swapBuffer(ctx, dri_drawable);
135 }
136 
137 union dri_buffer *
va_dri_get_rendering_buffer(VADriverContextP ctx,struct dri_drawable * dri_drawable)138     va_dri_get_rendering_buffer(VADriverContextP ctx, struct dri_drawable *dri_drawable)
139 {
140     struct dri_state *dri_state = (struct dri_state *)ctx->drm_state;
141 
142     return dri_state->getRenderingBuffer(ctx, dri_drawable);
143 }
144