1 /*
2  * Copyright © 2013 Keith Packard
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #include "dri3_priv.h"
24 
25 #include <drm_fourcc.h>
26 
27 static int dri3_request;
28 DevPrivateKeyRec dri3_screen_private_key;
29 
30 static int dri3_screen_generation;
31 
32 static Bool
dri3_close_screen(ScreenPtr screen)33 dri3_close_screen(ScreenPtr screen)
34 {
35     dri3_screen_priv_ptr screen_priv = dri3_screen_priv(screen);
36 
37     unwrap(screen_priv, screen, CloseScreen);
38 
39     free(screen_priv);
40     return (*screen->CloseScreen) (screen);
41 }
42 
43 Bool
dri3_screen_init(ScreenPtr screen,const dri3_screen_info_rec * info)44 dri3_screen_init(ScreenPtr screen, const dri3_screen_info_rec *info)
45 {
46     dri3_screen_generation = serverGeneration;
47 
48     if (!dixRegisterPrivateKey(&dri3_screen_private_key, PRIVATE_SCREEN, 0))
49         return FALSE;
50 
51     if (!dri3_screen_priv(screen)) {
52         dri3_screen_priv_ptr screen_priv = calloc(1, sizeof (dri3_screen_priv_rec));
53         if (!screen_priv)
54             return FALSE;
55 
56         wrap(screen_priv, screen, CloseScreen, dri3_close_screen);
57 
58         screen_priv->info = info;
59 
60         dixSetPrivate(&screen->devPrivates, &dri3_screen_private_key, screen_priv);
61     }
62 
63     return TRUE;
64 }
65 
66 void
dri3_extension_init(void)67 dri3_extension_init(void)
68 {
69     ExtensionEntry *extension;
70     int i;
71 
72     /* If no screens support DRI3, there's no point offering the
73      * extension at all
74      */
75     if (dri3_screen_generation != serverGeneration)
76         return;
77 
78 #ifdef PANORAMIX
79     if (!noPanoramiXExtension)
80         return;
81 #endif
82 
83     extension = AddExtension(DRI3_NAME, DRI3NumberEvents, DRI3NumberErrors,
84                              proc_dri3_dispatch, sproc_dri3_dispatch,
85                              NULL, StandardMinorOpcode);
86     if (!extension)
87         goto bail;
88 
89     dri3_request = extension->base;
90 
91     for (i = 0; i < screenInfo.numScreens; i++) {
92         if (!dri3_screen_init(screenInfo.screens[i], NULL))
93             goto bail;
94     }
95     return;
96 
97 bail:
98     FatalError("Cannot initialize DRI3 extension");
99 }
100 
101 uint32_t
drm_format_for_depth(uint32_t depth,uint32_t bpp)102 drm_format_for_depth(uint32_t depth, uint32_t bpp)
103 {
104     switch (bpp) {
105         case 16:
106             return DRM_FORMAT_RGB565;
107         case 24:
108             return DRM_FORMAT_XRGB8888;
109         case 30:
110             return DRM_FORMAT_XRGB2101010;
111         case 32:
112             return DRM_FORMAT_ARGB8888;
113         default:
114             return 0;
115     }
116 }
117