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 #ifdef HAVE_XORG_CONFIG_H
24 #include <xorg-config.h>
25 #endif
26
27 #include "dri3_priv.h"
28
29 #include <drm_fourcc.h>
30
31 static int dri3_request;
32 DevPrivateKeyRec dri3_screen_private_key;
33
34 static int dri3_screen_generation;
35
36 static Bool
dri3_close_screen(ScreenPtr screen)37 dri3_close_screen(ScreenPtr screen)
38 {
39 dri3_screen_priv_ptr screen_priv = dri3_screen_priv(screen);
40
41 unwrap(screen_priv, screen, CloseScreen);
42
43 free(screen_priv);
44 return (*screen->CloseScreen) (screen);
45 }
46
47 Bool
dri3_screen_init(ScreenPtr screen,const dri3_screen_info_rec * info)48 dri3_screen_init(ScreenPtr screen, const dri3_screen_info_rec *info)
49 {
50 dri3_screen_generation = serverGeneration;
51
52 if (!dixRegisterPrivateKey(&dri3_screen_private_key, PRIVATE_SCREEN, 0))
53 return FALSE;
54
55 if (!dri3_screen_priv(screen)) {
56 dri3_screen_priv_ptr screen_priv = calloc(1, sizeof (dri3_screen_priv_rec));
57 if (!screen_priv)
58 return FALSE;
59
60 wrap(screen_priv, screen, CloseScreen, dri3_close_screen);
61
62 screen_priv->info = info;
63
64 dixSetPrivate(&screen->devPrivates, &dri3_screen_private_key, screen_priv);
65 }
66
67 return TRUE;
68 }
69
70 void
dri3_extension_init(void)71 dri3_extension_init(void)
72 {
73 ExtensionEntry *extension;
74 int i;
75
76 /* If no screens support DRI3, there's no point offering the
77 * extension at all
78 */
79 if (dri3_screen_generation != serverGeneration)
80 return;
81
82 #ifdef PANORAMIX
83 if (!noPanoramiXExtension)
84 return;
85 #endif
86
87 extension = AddExtension(DRI3_NAME, DRI3NumberEvents, DRI3NumberErrors,
88 proc_dri3_dispatch, sproc_dri3_dispatch,
89 NULL, StandardMinorOpcode);
90 if (!extension)
91 goto bail;
92
93 dri3_request = extension->base;
94
95 for (i = 0; i < screenInfo.numScreens; i++) {
96 if (!dri3_screen_init(screenInfo.screens[i], NULL))
97 goto bail;
98 }
99 return;
100
101 bail:
102 FatalError("Cannot initialize DRI3 extension");
103 }
104
105 uint32_t
drm_format_for_depth(uint32_t depth,uint32_t bpp)106 drm_format_for_depth(uint32_t depth, uint32_t bpp)
107 {
108 switch (bpp) {
109 case 16:
110 return DRM_FORMAT_RGB565;
111 case 24:
112 return DRM_FORMAT_XRGB8888;
113 case 30:
114 return DRM_FORMAT_XRGB2101010;
115 case 32:
116 return DRM_FORMAT_ARGB8888;
117 default:
118 return 0;
119 }
120 }
121