summaryrefslogtreecommitdiff
path: root/src/uxa/intel_dri3.c
blob: fed6b1857f9dafa2d44f9bc3f5252d000d639702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright © 2013-2014 Intel Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "xorg-server.h"
#include "xf86.h"
#include "fb.h"

#include "intel.h"
#if USE_UXA
#include "intel_uxa.h"
#endif
#include "dri3.h"
#include "intel_glamor.h"

static int
intel_dri3_open(ScreenPtr screen,
                RRProviderPtr provider,
                int *out)
{
	int fd;

	fd = intel_get_client_fd(xf86ScreenToScrn(screen));
	if (fd < 0)
		return -fd;

	*out = fd;
	return Success;
}

static PixmapPtr intel_dri3_pixmap_from_fd(ScreenPtr screen,
					   int fd,
					   CARD16 width,
					   CARD16 height,
					   CARD16 stride,
					   CARD8 depth,
					   CARD8 bpp)
{
	ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
	intel_screen_private *intel = intel_get_screen_private(scrn);
	struct intel_uxa_pixmap *priv;
	PixmapPtr pixmap;
	dri_bo *bo;

        pixmap = intel_glamor_pixmap_from_fd(screen, fd, width, height, stride, depth, bpp);
        if (pixmap)
                return pixmap;

	if (depth < 8)
		return NULL;

	switch (bpp) {
	case 8:
	case 16:
	case 32:
		break;
	default:
		return NULL;
	}

	pixmap = fbCreatePixmap(screen, 0, 0, depth, 0);
	if (!pixmap)
		return NULL;

	if (!screen->ModifyPixmapHeader(pixmap, width, height, 0, 0, stride, NULL))
		goto free_pixmap;

	bo = drm_intel_bo_gem_create_from_prime(intel->bufmgr,
						fd, (uint32_t)height * stride);
	if (bo == NULL)
		goto free_pixmap;

	intel_uxa_set_pixmap_bo(pixmap, bo);
	dri_bo_unreference(bo);

	priv = intel_uxa_get_pixmap_private(pixmap);
	if (priv == NULL)
		goto free_pixmap;

	priv->pinned |= PIN_DRI3;

	return pixmap;

free_pixmap:
	fbDestroyPixmap(pixmap);
	return NULL;
}

static int intel_dri3_fd_from_pixmap(ScreenPtr screen,
				     PixmapPtr pixmap,
				     CARD16 *stride,
				     CARD32 *size)
{
	struct intel_uxa_pixmap *priv;
	int fd;

        fd = intel_glamor_fd_from_pixmap(screen, pixmap, stride, size);
        if (fd >= 0)
                return fd;

	priv = intel_uxa_get_pixmap_private(pixmap);
	if (!priv)
		return -1;

	if (intel_pixmap_pitch(pixmap) > UINT16_MAX)
		return -1;

	if (drm_intel_bo_gem_export_to_prime(priv->bo, &fd) < 0)
		return -1;

	priv->pinned |= PIN_DRI3;

	*stride = intel_pixmap_pitch(pixmap);
	*size = priv->bo->size;
	return fd;
}

static dri3_screen_info_rec intel_dri3_screen_info = {
        .version = DRI3_SCREEN_INFO_VERSION,

        .open = intel_dri3_open,
        .pixmap_from_fd = intel_dri3_pixmap_from_fd,
        .fd_from_pixmap = intel_dri3_fd_from_pixmap
};

Bool
intel_dri3_screen_init(ScreenPtr screen)
{
        return dri3_screen_init(screen, &intel_dri3_screen_info);
}