summaryrefslogtreecommitdiff
path: root/src/sna/sna_display_fake.c
blob: 97c463c13f74c61458e5f7b59a813410d5bf1827 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright © 2013 Intel Corporation
 *
 * 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, sublicense,
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 * Authors:
 *	Chris Wilson <chris@chris-wilson.co.uk>
 *
 */

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

#include "sna.h"

static bool add_fake_output(struct sna *sna, bool late);

static void
sna_crtc_dpms(xf86CrtcPtr crtc, int mode)
{
}

static char *outputs_for_crtc(xf86CrtcPtr crtc, char *outputs, int max)
{
	xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
	int len, i;

	for (i = len = 0; i < config->num_output; i++) {
		xf86OutputPtr output = config->output[i];

		if (output->crtc != crtc)
			continue;

		len += snprintf(outputs+len, max-len, "%s, ", output->name);
	}
	assert(len >= 2);
	outputs[len-2] = '\0';

	return outputs;
}

static const char *rotation_to_str(Rotation rotation)
{
	switch (rotation & RR_Rotate_All) {
	case 0:
	case RR_Rotate_0: return "normal";
	case RR_Rotate_90: return "left";
	case RR_Rotate_180: return "inverted";
	case RR_Rotate_270: return "right";
	default: return "unknown";
	}
}

static const char *reflection_to_str(Rotation rotation)
{
	switch (rotation & RR_Reflect_All) {
	case 0: return "none";
	case RR_Reflect_X: return "X axis";
	case RR_Reflect_Y: return "Y axis";
	case RR_Reflect_X | RR_Reflect_Y: return "X and Y axes";
	default: return "invalid";
	}
}

static Bool
sna_crtc_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
			Rotation rotation, int x, int y)
{
	char outputs[256];

	xf86DrvMsg(crtc->scrn->scrnIndex, X_INFO,
		   "switch to mode %dx%d on %s, position (%d, %d), rotation %s, reflection %s\n",
		   mode->HDisplay, mode->VDisplay,
		   outputs_for_crtc(crtc, outputs, sizeof(outputs)),
		   x, y, rotation_to_str(rotation), reflection_to_str(rotation));

	return TRUE;
}

static void
sna_crtc_gamma_set(xf86CrtcPtr crtc,
		       CARD16 *red, CARD16 *green, CARD16 *blue, int size)
{
}

static void
sna_crtc_destroy(xf86CrtcPtr crtc)
{
}

static const xf86CrtcFuncsRec sna_crtc_funcs = {
	.dpms = sna_crtc_dpms,
	.set_mode_major = sna_crtc_set_mode_major,
	.gamma_set = sna_crtc_gamma_set,
	.destroy = sna_crtc_destroy,
};

static void
sna_output_create_resources(xf86OutputPtr output)
{
}

static Bool
sna_output_set_property(xf86OutputPtr output, Atom property,
			    RRPropertyValuePtr value)
{
	return TRUE;
}

static Bool
sna_output_get_property(xf86OutputPtr output, Atom property)
{
	return FALSE;
}

static void
sna_output_dpms(xf86OutputPtr output, int dpms)
{
}

static xf86OutputStatus
sna_output_detect(xf86OutputPtr output)
{
	DBG(("%s(%s) has user modes? %d\n",
	     __FUNCTION__, output->name,
	     output->randr_output && output->randr_output->numUserModes));

	if (output->randr_output && output->randr_output->numUserModes) {
		xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(output->scrn);

		if (xf86_config->output[xf86_config->num_output-1] == output)
			add_fake_output(to_sna(output->scrn), true);

		return XF86OutputStatusConnected;
	}

	return XF86OutputStatusDisconnected;
}

static Bool
sna_output_mode_valid(xf86OutputPtr output, DisplayModePtr mode)
{
	if (mode->type & M_T_DEFAULT)
		return MODE_BAD;

	return MODE_OK;
}

static DisplayModePtr
sna_output_get_modes(xf86OutputPtr output)
{
	return NULL;
}

static void
sna_output_destroy(xf86OutputPtr output)
{
}

static const xf86OutputFuncsRec sna_output_funcs = {
	.create_resources = sna_output_create_resources,
#ifdef RANDR_12_INTERFACE
	.set_property = sna_output_set_property,
	.get_property = sna_output_get_property,
#endif
	.dpms = sna_output_dpms,
	.detect = sna_output_detect,
	.mode_valid = sna_output_mode_valid,

	.get_modes = sna_output_get_modes,
	.destroy = sna_output_destroy
};

static Bool
sna_mode_resize(ScrnInfoPtr scrn, int width, int height)
{
	ScreenPtr screen = scrn->pScreen;
	PixmapPtr new_front;

	DBG(("%s (%d, %d) -> (%d, %d)\n", __FUNCTION__,
	     scrn->virtualX, scrn->virtualY,
	     width, height));

	if (scrn->virtualX == width && scrn->virtualY == height)
		return TRUE;

	assert(to_sna_from_screen(screen)->front);
	assert(screen->GetScreenPixmap(screen) == to_sna_from_screen(screen)->front);

	DBG(("%s: creating new framebuffer %dx%d\n",
	     __FUNCTION__, width, height));

	new_front = screen->CreatePixmap(screen,
					 width, height, scrn->depth,
					 0);
	if (!new_front)
		return FALSE;

	scrn->virtualX = width;
	scrn->virtualY = height;
	scrn->displayWidth = width;

	screen->SetScreenPixmap(new_front);
	assert(screen->GetScreenPixmap(screen) == new_front);
	assert(to_sna_from_screen(screen)->front == new_front);

	screen->DestroyPixmap(new_front);

	return TRUE;
}

static const xf86CrtcConfigFuncsRec sna_mode_funcs = {
	sna_mode_resize
};

static bool add_fake_output(struct sna *sna, bool late)
{
	ScrnInfoPtr scrn = sna->scrn;
	xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
	xf86OutputPtr output;
	xf86CrtcPtr crtc;
	RROutputPtr clones[32];
	RRCrtcPtr crtcs[32];
	char buf[80];
	int i, len;

	if (sna->mode.num_fake >= 32)
		return false;

	DBG(("%s(late=%d, num_fake=%d)\n", __FUNCTION__, late, sna->mode.num_fake+1));

	crtc = xf86CrtcCreate(scrn, &sna_crtc_funcs);
	if (crtc == NULL)
		return false;

	len = sprintf(buf, "VIRTUAL%d", sna->mode.num_fake+1);
	output = xf86OutputCreate(scrn, &sna_output_funcs, buf);
	if (!output) {
		xf86CrtcDestroy(crtc);
		return false;
	}

	output->mm_width = 0;
	output->mm_height = 0;
	output->interlaceAllowed = FALSE;
	output->subpixel_order = SubPixelNone;

	output->possible_crtcs = ~((1 << sna->mode.num_real_crtc) - 1);
	output->possible_clones = ~((1 << sna->mode.num_real_output) - 1);

	if (late) {
		ScreenPtr screen = xf86ScrnToScreen(scrn);

		crtc->randr_crtc = RRCrtcCreate(screen, crtc);
		output->randr_output = RROutputCreate(screen, buf, len, output);
		if (crtc->randr_crtc == NULL || output->randr_output == NULL) {
			xf86OutputDestroy(output);
			xf86CrtcDestroy(crtc);
			return false;
		}

		RRPostPendingProperties(output->randr_output);

		for (i = sna->mode.num_real_output; i < xf86_config->num_output; i++)
			clones[i - sna->mode.num_real_output] = xf86_config->output[i]->randr_output;
		assert(i - sna->mode.num_real_output == sna->mode.num_fake + 1);

		for (i = sna->mode.num_real_crtc; i < xf86_config->num_crtc; i++)
			crtcs[i - sna->mode.num_real_crtc] = xf86_config->crtc[i]->randr_crtc;
		assert(i - sna->mode.num_real_crtc == sna->mode.num_fake + 1);

		for (i = sna->mode.num_real_output; i < xf86_config->num_output; i++) {
			RROutputPtr rr_output = xf86_config->output[i]->randr_output;

			if (!RROutputSetCrtcs(rr_output, crtcs, sna->mode.num_fake + 1) ||
			    !RROutputSetClones(rr_output, clones, sna->mode.num_fake + 1))
				goto err;
		}

		RRCrtcSetRotations(crtc->randr_crtc,
				   RR_Rotate_All | RR_Reflect_All);
	}

	sna->mode.num_fake++;
	return true;

err:
	for (i = 0; i < xf86_config->num_output; i++) {
		output = xf86_config->output[i];
		if (output->driver_private)
			continue;

		xf86OutputDestroy(output);
	}

	for (i = 0; i < xf86_config->num_crtc; i++) {
		crtc = xf86_config->crtc[i];
		if (crtc->driver_private)
			continue;
		xf86CrtcDestroy(crtc);
	}
	sna->mode.num_fake = -1;
	return false;
}

bool sna_mode_fake_init(struct sna *sna, int num_fake)
{
	bool ret;

	if (num_fake == 0)
		return true;

	if (sna->mode.num_real_crtc == 0)
		xf86CrtcConfigInit(sna->scrn, &sna_mode_funcs);

	ret = true;
	while (ret && num_fake--)
		ret = add_fake_output(sna, false);
	return ret;
}