dri3.c revision 1b5d61b8
135c4bbdfSmrg/*
235c4bbdfSmrg * Copyright © 2013 Keith Packard
335c4bbdfSmrg *
435c4bbdfSmrg * Permission to use, copy, modify, distribute, and sell this software and its
535c4bbdfSmrg * documentation for any purpose is hereby granted without fee, provided that
635c4bbdfSmrg * the above copyright notice appear in all copies and that both that copyright
735c4bbdfSmrg * notice and this permission notice appear in supporting documentation, and
835c4bbdfSmrg * that the name of the copyright holders not be used in advertising or
935c4bbdfSmrg * publicity pertaining to distribution of the software without specific,
1035c4bbdfSmrg * written prior permission.  The copyright holders make no representations
1135c4bbdfSmrg * about the suitability of this software for any purpose.  It is provided "as
1235c4bbdfSmrg * is" without express or implied warranty.
1335c4bbdfSmrg *
1435c4bbdfSmrg * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1535c4bbdfSmrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1635c4bbdfSmrg * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1735c4bbdfSmrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
1835c4bbdfSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
1935c4bbdfSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
2035c4bbdfSmrg * OF THIS SOFTWARE.
2135c4bbdfSmrg */
2235c4bbdfSmrg
2335c4bbdfSmrg#ifdef HAVE_XORG_CONFIG_H
2435c4bbdfSmrg#include <xorg-config.h>
2535c4bbdfSmrg#endif
2635c4bbdfSmrg
2735c4bbdfSmrg#include "dri3_priv.h"
2835c4bbdfSmrg
291b5d61b8Smrg#include <drm_fourcc.h>
301b5d61b8Smrg
3135c4bbdfSmrgstatic int dri3_request;
3235c4bbdfSmrgDevPrivateKeyRec dri3_screen_private_key;
3335c4bbdfSmrg
3435c4bbdfSmrgstatic int dri3_screen_generation;
3535c4bbdfSmrg
3635c4bbdfSmrgstatic Bool
3735c4bbdfSmrgdri3_close_screen(ScreenPtr screen)
3835c4bbdfSmrg{
3935c4bbdfSmrg    dri3_screen_priv_ptr screen_priv = dri3_screen_priv(screen);
4035c4bbdfSmrg
4135c4bbdfSmrg    unwrap(screen_priv, screen, CloseScreen);
4235c4bbdfSmrg
4335c4bbdfSmrg    free(screen_priv);
4435c4bbdfSmrg    return (*screen->CloseScreen) (screen);
4535c4bbdfSmrg}
4635c4bbdfSmrg
4735c4bbdfSmrgBool
481b5d61b8Smrgdri3_screen_init(ScreenPtr screen, const dri3_screen_info_rec *info)
4935c4bbdfSmrg{
5035c4bbdfSmrg    dri3_screen_generation = serverGeneration;
5135c4bbdfSmrg
5235c4bbdfSmrg    if (!dixRegisterPrivateKey(&dri3_screen_private_key, PRIVATE_SCREEN, 0))
5335c4bbdfSmrg        return FALSE;
5435c4bbdfSmrg
5535c4bbdfSmrg    if (!dri3_screen_priv(screen)) {
5635c4bbdfSmrg        dri3_screen_priv_ptr screen_priv = calloc(1, sizeof (dri3_screen_priv_rec));
5735c4bbdfSmrg        if (!screen_priv)
5835c4bbdfSmrg            return FALSE;
5935c4bbdfSmrg
6035c4bbdfSmrg        wrap(screen_priv, screen, CloseScreen, dri3_close_screen);
6135c4bbdfSmrg
6235c4bbdfSmrg        screen_priv->info = info;
6335c4bbdfSmrg
6435c4bbdfSmrg        dixSetPrivate(&screen->devPrivates, &dri3_screen_private_key, screen_priv);
6535c4bbdfSmrg    }
6635c4bbdfSmrg
6735c4bbdfSmrg    return TRUE;
6835c4bbdfSmrg}
6935c4bbdfSmrg
7035c4bbdfSmrgvoid
7135c4bbdfSmrgdri3_extension_init(void)
7235c4bbdfSmrg{
7335c4bbdfSmrg    ExtensionEntry *extension;
7435c4bbdfSmrg    int i;
7535c4bbdfSmrg
7635c4bbdfSmrg    /* If no screens support DRI3, there's no point offering the
7735c4bbdfSmrg     * extension at all
7835c4bbdfSmrg     */
7935c4bbdfSmrg    if (dri3_screen_generation != serverGeneration)
8035c4bbdfSmrg        return;
8135c4bbdfSmrg
8235c4bbdfSmrg#ifdef PANORAMIX
8335c4bbdfSmrg    if (!noPanoramiXExtension)
8435c4bbdfSmrg        return;
8535c4bbdfSmrg#endif
8635c4bbdfSmrg
8735c4bbdfSmrg    extension = AddExtension(DRI3_NAME, DRI3NumberEvents, DRI3NumberErrors,
8835c4bbdfSmrg                             proc_dri3_dispatch, sproc_dri3_dispatch,
8935c4bbdfSmrg                             NULL, StandardMinorOpcode);
9035c4bbdfSmrg    if (!extension)
9135c4bbdfSmrg        goto bail;
9235c4bbdfSmrg
9335c4bbdfSmrg    dri3_request = extension->base;
9435c4bbdfSmrg
9535c4bbdfSmrg    for (i = 0; i < screenInfo.numScreens; i++) {
9635c4bbdfSmrg        if (!dri3_screen_init(screenInfo.screens[i], NULL))
9735c4bbdfSmrg            goto bail;
9835c4bbdfSmrg    }
9935c4bbdfSmrg    return;
10035c4bbdfSmrg
10135c4bbdfSmrgbail:
10235c4bbdfSmrg    FatalError("Cannot initialize DRI3 extension");
10335c4bbdfSmrg}
1041b5d61b8Smrg
1051b5d61b8Smrguint32_t
1061b5d61b8Smrgdrm_format_for_depth(uint32_t depth, uint32_t bpp)
1071b5d61b8Smrg{
1081b5d61b8Smrg    switch (bpp) {
1091b5d61b8Smrg        case 16:
1101b5d61b8Smrg            return DRM_FORMAT_RGB565;
1111b5d61b8Smrg        case 24:
1121b5d61b8Smrg            return DRM_FORMAT_XRGB8888;
1131b5d61b8Smrg        case 30:
1141b5d61b8Smrg            return DRM_FORMAT_XRGB2101010;
1151b5d61b8Smrg        case 32:
1161b5d61b8Smrg            return DRM_FORMAT_ARGB8888;
1171b5d61b8Smrg        default:
1181b5d61b8Smrg            return 0;
1191b5d61b8Smrg    }
1201b5d61b8Smrg}
121