summaryrefslogtreecommitdiff
path: root/src/glamo-dri2.c
blob: 61985222fc9b14a9c31a574449267a420a7cfb4f (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * DRI for the SMedia Glamo3362 X.org Driver
 *
 * Modified: 2009 by Thomas White <taw@bitwiz.org.uk>
 *
 * Based on dri2.c from xf86-video-modesetting, to which the following
 * notice applies:
 *
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *
 * Author: Alan Hourihane <alanh@tungstengraphics.com>
 *
 */


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

#include <xf86.h>
#include <xf86_OSproc.h>
#include <xf86drm.h>
#include <dri2.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "glamo.h"
#include "glamo-dri2.h"
#include "glamo-kms-exa.h"


typedef struct {
	PixmapPtr pPixmap;
} GlamoDRI2BufferPrivateRec, *GlamoDRI2BufferPrivatePtr;


#if DRI2INFOREC_VERSION >= 3

static DRI2BufferPtr glamoCreateBuffer(DrawablePtr pDraw,
                                       unsigned int attachment,
                                       unsigned int format)
{
	DRI2BufferPtr buffer;
	return buffer;
}

#else

static DRI2BufferPtr glamoCreateBuffers(DrawablePtr pDraw,
                                        unsigned int *attachments, int count)
{
	ScreenPtr pScreen = pDraw->pScreen;
	DRI2BufferPtr buffers;
	int i;
	GlamoDRI2BufferPrivatePtr privates;
	PixmapPtr pPixmap, pDepthPixmap;

	buffers = xcalloc(count, sizeof *buffers);
	if ( buffers == NULL ) return NULL;
	privates = xcalloc(count, sizeof *privates);
	if ( privates == NULL ) {
		xfree(buffers);
		return NULL;
	}

	pDepthPixmap = NULL;
	/* For each attachment */
	for ( i=0; i<count; i++ ) {

		if ( attachments[i] == DRI2BufferFrontLeft ) {

			/* Front left buffer - just dig out the pixmap */
			if ( pDraw->type == DRAWABLE_PIXMAP ) {
				pPixmap = (PixmapPtr)pDraw;
			} else {
				pPixmap = (*pScreen->GetWindowPixmap)(
							(WindowPtr)pDraw);
			}
			pPixmap->refcnt++;

		} else {

			/* Anything else - create a new pixmap */
			pPixmap = (*pScreen->CreatePixmap)(pScreen,
			                                   pDraw->width,
			                                   pDraw->height,
			                                   pDraw->depth,
			                                   0);

		}

		if ( attachments[i] == DRI2BufferDepth ) pDepthPixmap = pPixmap;

		/* Set up the return data structure */
		buffers[i].attachment = attachments[i];
		buffers[i].pitch = pPixmap->devKind;
		buffers[i].cpp = pPixmap->drawable.bitsPerPixel / 8;
		buffers[i].driverPrivate = &privates[i];
		buffers[i].flags = 0;
		privates[i].pPixmap = pPixmap;

	}

	return buffers;
}

#endif

#if DRI2INFOREC_VERSION >= 3

static void glamoDestroyBuffer(DrawablePtr pDraw,
                               DRI2BufferPtr buffer)
{
	ScreenPtr pScreen = pDraw->pScreen;
	GlamoDRI2BufferPrivatePtr private;

	private = buffer->driverPrivate;
	(*pScreen->DestroyPixmap)(private->pPixmap);

	if ( buffer ) {
		xfree(buffer->driverPrivate);
	}
}

#else

static void glamoDestroyBuffers(DrawablePtr pDraw,
                                DRI2BufferPtr buffers, int count)
{
	ScreenPtr pScreen = pDraw->pScreen;
	int i;

	for ( i=0; i<count; i++ ) {
		GlamoDRI2BufferPrivatePtr private;
		private = buffers[i].driverPrivate;
		(*pScreen->DestroyPixmap)(private->pPixmap);
	}

	if ( buffers ) {
		xfree(buffers[0].driverPrivate);
		xfree(buffers);
	}
}

#endif


static void glamoCopyRegion(DrawablePtr pDraw, RegionPtr pRegion,
                            DRI2BufferPtr pDestBuffer, DRI2BufferPtr pSrcBuffer)
{
}


void driScreenInit(ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	GlamoPtr pGlamo = GlamoPTR(pScrn);
	DRI2InfoRec dri2info;
	char *p;
	struct stat sbuf;
	dev_t d;
	int i;

	fstat(pGlamo->drm_fd, &sbuf);
	d = sbuf.st_rdev;
	p = pGlamo->drm_devname;
	for ( i=0; i<DRM_MAX_MINOR; i++ ) {
		sprintf(p, DRM_DEV_NAME, DRM_DIR_NAME, i);
		if ( stat(p, &sbuf) == 0 && sbuf.st_rdev == d ) break;
	}
	if ( i == DRM_MAX_MINOR ) {
		xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
			   "[glamo-dri] Failed to find name of DRM device\n");
		return;
	}
	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		   "[glamo-dri] Name of DRM device is '%s'\n", p);

	dri2info.version = 1;
	dri2info.fd = pGlamo->drm_fd;
	dri2info.deviceName = p;
	dri2info.driverName = "glamo";

#if DRI2INFOREC_VERSION >= 3
	dri2info.CreateBuffer = glamoCreateBuffer;
	dri2info.DestroyBuffer = glamoDestroyBuffer;
#else
	dri2info.CreateBuffers = glamoCreateBuffers;
	dri2info.DestroyBuffers = glamoDestroyBuffers;
#endif
	dri2info.CopyRegion = glamoCopyRegion;

	if ( !DRI2ScreenInit(pScreen, &dri2info) ) return;
}


void driCloseScreen(ScreenPtr pScreen)
{
	DRI2CloseScreen(pScreen);
}