xref: /OK3568_Linux_fs/external/xserver/glx/vndservermapping.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2016, NVIDIA CORPORATION.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun  * copy of this software and/or associated documentation files (the
6*4882a593Smuzhiyun  * "Materials"), to deal in the Materials without restriction, including
7*4882a593Smuzhiyun  * without limitation the rights to use, copy, modify, merge, publish,
8*4882a593Smuzhiyun  * distribute, sublicense, and/or sell copies of the Materials, and to
9*4882a593Smuzhiyun  * permit persons to whom the Materials are furnished to do so, subject to
10*4882a593Smuzhiyun  * the following conditions:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be included
13*4882a593Smuzhiyun  * unaltered in all copies or substantial portions of the Materials.
14*4882a593Smuzhiyun  * Any additions, deletions, or changes to the original source files
15*4882a593Smuzhiyun  * must be clearly indicated in accompanying documentation.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * If only executable code is distributed, then the accompanying
18*4882a593Smuzhiyun  * documentation must state that "this software is based in part on the
19*4882a593Smuzhiyun  * work of the Khronos Group."
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24*4882a593Smuzhiyun  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25*4882a593Smuzhiyun  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26*4882a593Smuzhiyun  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27*4882a593Smuzhiyun  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include "vndserver.h"
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #include <pixmapstr.h>
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include "vndservervendor.h"
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun static ClientPtr requestClient = NULL;
37*4882a593Smuzhiyun 
GlxSetRequestClient(ClientPtr client)38*4882a593Smuzhiyun void GlxSetRequestClient(ClientPtr client)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun     requestClient = client;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
LookupXIDMapResource(XID id)43*4882a593Smuzhiyun static GlxServerVendor *LookupXIDMapResource(XID id)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun     void *ptr = NULL;
46*4882a593Smuzhiyun     int rv;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun     rv = dixLookupResourceByType(&ptr, id, idResource, NULL, DixReadAccess);
49*4882a593Smuzhiyun     if (rv == Success) {
50*4882a593Smuzhiyun         return (GlxServerVendor *) ptr;
51*4882a593Smuzhiyun     } else {
52*4882a593Smuzhiyun         return NULL;
53*4882a593Smuzhiyun     }
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
GlxGetXIDMap(XID id)56*4882a593Smuzhiyun GlxServerVendor *GlxGetXIDMap(XID id)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun     GlxServerVendor *vendor = LookupXIDMapResource(id);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun     if (vendor == NULL) {
61*4882a593Smuzhiyun         // If we haven't seen this XID before, then it may be a drawable that
62*4882a593Smuzhiyun         // wasn't created through GLX, like a regular X window or pixmap. Try
63*4882a593Smuzhiyun         // to look up a matching drawable to find a screen number for it.
64*4882a593Smuzhiyun         void *ptr = NULL;
65*4882a593Smuzhiyun         int rv = dixLookupResourceByClass(&ptr, id, RC_DRAWABLE, NULL,
66*4882a593Smuzhiyun                                          DixGetAttrAccess);
67*4882a593Smuzhiyun         if (rv == Success && ptr != NULL) {
68*4882a593Smuzhiyun             DrawablePtr draw = (DrawablePtr) ptr;
69*4882a593Smuzhiyun             vendor = GlxGetVendorForScreen(requestClient, draw->pScreen);
70*4882a593Smuzhiyun         }
71*4882a593Smuzhiyun     }
72*4882a593Smuzhiyun     return vendor;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
GlxAddXIDMap(XID id,GlxServerVendor * vendor)75*4882a593Smuzhiyun Bool GlxAddXIDMap(XID id, GlxServerVendor *vendor)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun     if (id == 0 || vendor == NULL) {
78*4882a593Smuzhiyun         return FALSE;
79*4882a593Smuzhiyun     }
80*4882a593Smuzhiyun     if (LookupXIDMapResource(id) != NULL) {
81*4882a593Smuzhiyun         return FALSE;
82*4882a593Smuzhiyun     }
83*4882a593Smuzhiyun     return AddResource(id, idResource, vendor);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
GlxRemoveXIDMap(XID id)86*4882a593Smuzhiyun void GlxRemoveXIDMap(XID id)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun     FreeResourceByType(id, idResource, FALSE);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
GlxAllocContextTag(ClientPtr client,GlxServerVendor * vendor)91*4882a593Smuzhiyun GlxContextTagInfo *GlxAllocContextTag(ClientPtr client, GlxServerVendor *vendor)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun     GlxClientPriv *cl;
94*4882a593Smuzhiyun     unsigned int index;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun     if (vendor == NULL) {
97*4882a593Smuzhiyun         return NULL;
98*4882a593Smuzhiyun     }
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun     cl = GlxGetClientData(client);
101*4882a593Smuzhiyun     if (cl == NULL) {
102*4882a593Smuzhiyun         return NULL;
103*4882a593Smuzhiyun     }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun     // Look for a free tag index.
106*4882a593Smuzhiyun     for (index=0; index<cl->contextTagCount; index++) {
107*4882a593Smuzhiyun         if (cl->contextTags[index].vendor == NULL) {
108*4882a593Smuzhiyun             break;
109*4882a593Smuzhiyun         }
110*4882a593Smuzhiyun     }
111*4882a593Smuzhiyun     if (index >= cl->contextTagCount) {
112*4882a593Smuzhiyun         // We didn't find a free entry, so grow the array.
113*4882a593Smuzhiyun         GlxContextTagInfo *newTags;
114*4882a593Smuzhiyun         unsigned int newSize = cl->contextTagCount * 2;
115*4882a593Smuzhiyun         if (newSize == 0) {
116*4882a593Smuzhiyun             // TODO: What's a good starting size for this?
117*4882a593Smuzhiyun             newSize = 16;
118*4882a593Smuzhiyun         }
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun         newTags = (GlxContextTagInfo *)
121*4882a593Smuzhiyun             realloc(cl->contextTags, newSize * sizeof(GlxContextTagInfo));
122*4882a593Smuzhiyun         if (newTags == NULL) {
123*4882a593Smuzhiyun             return NULL;
124*4882a593Smuzhiyun         }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun         memset(&newTags[cl->contextTagCount], 0,
127*4882a593Smuzhiyun                 (newSize - cl->contextTagCount) * sizeof(GlxContextTagInfo));
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun         index = cl->contextTagCount;
130*4882a593Smuzhiyun         cl->contextTags = newTags;
131*4882a593Smuzhiyun         cl->contextTagCount = newSize;
132*4882a593Smuzhiyun     }
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun     assert(index >= 0);
135*4882a593Smuzhiyun     assert(index < cl->contextTagCount);
136*4882a593Smuzhiyun     memset(&cl->contextTags[index], 0, sizeof(GlxContextTagInfo));
137*4882a593Smuzhiyun     cl->contextTags[index].tag = (GLXContextTag) (index + 1);
138*4882a593Smuzhiyun     cl->contextTags[index].client = client;
139*4882a593Smuzhiyun     cl->contextTags[index].vendor = vendor;
140*4882a593Smuzhiyun     return &cl->contextTags[index];
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
GlxLookupContextTag(ClientPtr client,GLXContextTag tag)143*4882a593Smuzhiyun GlxContextTagInfo *GlxLookupContextTag(ClientPtr client, GLXContextTag tag)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun     GlxClientPriv *cl = GlxGetClientData(client);
146*4882a593Smuzhiyun     if (cl == NULL) {
147*4882a593Smuzhiyun         return NULL;
148*4882a593Smuzhiyun     }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun     if (tag > 0 && (tag - 1) < cl->contextTagCount) {
151*4882a593Smuzhiyun         if (cl->contextTags[tag - 1].vendor != NULL) {
152*4882a593Smuzhiyun             assert(cl->contextTags[tag - 1].client == client);
153*4882a593Smuzhiyun             return &cl->contextTags[tag - 1];
154*4882a593Smuzhiyun         }
155*4882a593Smuzhiyun     }
156*4882a593Smuzhiyun     return NULL;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
GlxFreeContextTag(GlxContextTagInfo * tagInfo)159*4882a593Smuzhiyun void GlxFreeContextTag(GlxContextTagInfo *tagInfo)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun     if (tagInfo != NULL) {
162*4882a593Smuzhiyun         tagInfo->vendor = NULL;
163*4882a593Smuzhiyun         tagInfo->vendor = NULL;
164*4882a593Smuzhiyun         tagInfo->data = NULL;
165*4882a593Smuzhiyun         tagInfo->context = None;
166*4882a593Smuzhiyun         tagInfo->drawable = None;
167*4882a593Smuzhiyun         tagInfo->readdrawable = None;
168*4882a593Smuzhiyun     }
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
GlxSetScreenVendor(ScreenPtr screen,GlxServerVendor * vendor)171*4882a593Smuzhiyun Bool GlxSetScreenVendor(ScreenPtr screen, GlxServerVendor *vendor)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun     GlxScreenPriv *priv;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun     if (vendor == NULL) {
176*4882a593Smuzhiyun         return FALSE;
177*4882a593Smuzhiyun     }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun     priv = GlxGetScreen(screen);
180*4882a593Smuzhiyun     if (priv == NULL) {
181*4882a593Smuzhiyun         return FALSE;
182*4882a593Smuzhiyun     }
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun     if (priv->vendor != NULL) {
185*4882a593Smuzhiyun         return FALSE;
186*4882a593Smuzhiyun     }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun     priv->vendor = vendor;
189*4882a593Smuzhiyun     return TRUE;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
GlxSetClientScreenVendor(ClientPtr client,ScreenPtr screen,GlxServerVendor * vendor)192*4882a593Smuzhiyun Bool GlxSetClientScreenVendor(ClientPtr client, ScreenPtr screen, GlxServerVendor *vendor)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun     GlxClientPriv *cl;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun     if (screen == NULL || screen->isGPU) {
197*4882a593Smuzhiyun         return FALSE;
198*4882a593Smuzhiyun     }
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun     cl = GlxGetClientData(client);
201*4882a593Smuzhiyun     if (cl == NULL) {
202*4882a593Smuzhiyun         return FALSE;
203*4882a593Smuzhiyun     }
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun     if (vendor != NULL) {
206*4882a593Smuzhiyun         cl->vendors[screen->myNum] = vendor;
207*4882a593Smuzhiyun     } else {
208*4882a593Smuzhiyun         cl->vendors[screen->myNum] = GlxGetVendorForScreen(NULL, screen);
209*4882a593Smuzhiyun     }
210*4882a593Smuzhiyun     return TRUE;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
GlxGetVendorForScreen(ClientPtr client,ScreenPtr screen)213*4882a593Smuzhiyun GlxServerVendor *GlxGetVendorForScreen(ClientPtr client, ScreenPtr screen)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun     // Note that the client won't be sending GPU screen numbers, so we don't
216*4882a593Smuzhiyun     // need per-client mappings for them.
217*4882a593Smuzhiyun     if (client != NULL && !screen->isGPU) {
218*4882a593Smuzhiyun         GlxClientPriv *cl = GlxGetClientData(client);
219*4882a593Smuzhiyun         if (cl != NULL) {
220*4882a593Smuzhiyun             return cl->vendors[screen->myNum];
221*4882a593Smuzhiyun         } else {
222*4882a593Smuzhiyun             return NULL;
223*4882a593Smuzhiyun         }
224*4882a593Smuzhiyun     } else {
225*4882a593Smuzhiyun         GlxScreenPriv *priv = GlxGetScreen(screen);
226*4882a593Smuzhiyun         if (priv != NULL) {
227*4882a593Smuzhiyun             return priv->vendor;
228*4882a593Smuzhiyun         } else {
229*4882a593Smuzhiyun             return NULL;
230*4882a593Smuzhiyun         }
231*4882a593Smuzhiyun     }
232*4882a593Smuzhiyun }
233