summaryrefslogtreecommitdiff
path: root/src/wfdpipeline.c
blob: d90a1366e8022112834782aadc1ebbcb8b3ec8ac (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
/*
 * Copyright (c) 2011 Benjamin Franzke
 * 
 * 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 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.
 */

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>

#include "wfdpipeline.h"
#include "wfdsource.h"
#include "wfddevice.h"

#include <xf86drmMode.h>

#define ARRAY_SIZE(X) (sizeof (X) / sizeof((X)[0]))

struct wfd_pipeline {
	struct wfd_device *device;
	drmModeCrtcPtr crtc;

	uint32_t fb_id;
	uint32_t pending_fb_id;

	WFDint source_rect[4];
	WFDint dest_rect[4];

	int first_bind;
	struct wfd_source *source;
	struct wfd_source *previous_source;
	int source_updated;

	struct wfd_port *port;
};

WFDint
wfd_enumerate_pipelines(struct wfd_device *device,
		    WFDint *pipeline_ids,
		    WFDint pipeline_id_count,
		    WFDint *filter_list)
{
	drmModeResPtr resources = wfd_device_get_resources(device);
	int count = 0;
	int i;

	if (pipeline_ids == NULL)
		return resources->count_crtcs;	

	for (i = 0; i < resources->count_crtcs && count < pipeline_id_count; ++i) {
		pipeline_ids[count] = resources->crtcs[i];
		count++;
	}

	return count;
}

void
wfd_pipeline_destroy(struct wfd_device *device,
		     struct wfd_pipeline *pipeline)
{
	drmModeFreeCrtc(pipeline->crtc);
	free(pipeline);
}

struct wfd_pipeline *
wfd_pipeline_create(struct wfd_device *device,
		    WFDint pipeline_id,
		    const WFDint *attrib_list)
{
	struct wfd_pipeline *pipeline;
	int drm_fd = wfd_device_get_fd(device);

	pipeline = calloc(1, sizeof *pipeline);
	if (pipeline == NULL)
		return NULL;

	pipeline->device = device;

	pipeline->crtc = drmModeGetCrtc(drm_fd, pipeline_id);
	if (pipeline->crtc == NULL) {
		free(pipeline);
		return NULL;
	}

	pipeline->fb_id = 0;
	pipeline->first_bind = 1;

	return pipeline;
}

int
wfd_pipeline_bind_source(struct wfd_device *device,
			 struct wfd_pipeline *pipeline,
			 struct wfd_source *source,
			 WFDTransition transition,
			 const WFDRect *region)
{
	pipeline->previous_source = pipeline->source;
	pipeline->source = source;
	pipeline->source_updated = 1;

	return 0;
}

void
wfd_pipeline_destroy_pending_fb(struct wfd_pipeline *pipeline)
{
	int drm_fd = wfd_device_get_fd(pipeline->device);

	if (pipeline->pending_fb_id)
		drmModeRmFB(drm_fd, pipeline->pending_fb_id);

	pipeline->pending_fb_id = 0;
}

int
wfd_pipeline_commit(struct wfd_device *device,
		    struct wfd_pipeline *pipeline)
{
	int fd = wfd_device_get_fd(device);
	int err;

	if (pipeline->source_updated == 0)
		return 0;

	pipeline->pending_fb_id = pipeline->fb_id;

	err = drmModeAddFB(fd,
			   pipeline->source_rect[2],
			   pipeline->source_rect[3],
			   24, 32,
			   wfd_source_get_pitch(pipeline->source),
			   wfd_source_get_handle(pipeline->source),
			   &pipeline->fb_id);
	if (err)
		fprintf(stderr, "drmModeAddFB: %d %m\n", err);

	if (!pipeline->first_bind) {
		drmModePageFlip(fd, pipeline->crtc->crtc_id, pipeline->fb_id,
				DRM_MODE_PAGE_FLIP_EVENT, pipeline);
	}

	pipeline->first_bind = 0;
	pipeline->source_updated = 0;
	
	return 0;
}


uint32_t
wfd_pipeline_get_crtc_id(struct wfd_pipeline *pipeline)
{
	return pipeline->crtc->crtc_id;
}

uint32_t
wfd_pipeline_get_fb_id(struct wfd_pipeline *pipeline)
{
	return pipeline->fb_id;
}

int
wfd_pipeline_get_id(struct wfd_pipeline *pipeline)
{
	return pipeline->crtc->crtc_id;
}

struct wfd_source *
wfd_pipeline_get_previous_source(struct wfd_pipeline *pipeline)
{
	return pipeline->previous_source;
}

WFDint
wfd_pipeline_get_attribi(struct wfd_device *device,
			 struct wfd_pipeline *pipeline,
			 WFDPipelineConfigAttrib attrib)
{
	switch (attrib) {
	case WFD_PIPELINE_ID:
		return wfd_pipeline_get_id(pipeline);
	case WFD_PIPELINE_PORTID:
		return pipeline->port == NULL ? WFD_INVALID_PORT_ID : 
			wfd_port_get_attribi(device, pipeline->port, WFD_PORT_ID);
	case WFD_PIPELINE_LAYER:
		return 0;

	/* FIXME: will change */
	case WFD_PIPELINE_SHAREABLE:
		return WFD_FALSE;
	case WFD_PIPELINE_DIRECT_REFRESH:
		return WFD_TRUE;

	/* FIXME: there is not PIPELINE_FLIP_MIRROR_SUPPORT? */
	case WFD_PIPELINE_FLIP:
	case WFD_PIPELINE_MIRROR:
		return WFD_FALSE;

	case WFD_PIPELINE_ROTATION_SUPPORT:
		return WFD_ROTATION_SUPPORT_NONE;
	case WFD_PIPELINE_ROTATION:
		return 0;

	case WFD_PIPELINE_SCALE_FILTER:
		return WFD_SCALE_FILTER_NONE;
	case WFD_PIPELINE_TRANSPARENCY_ENABLE:
		return WFD_TRANSPARENCY_NONE;
		break;
	default:
		wfd_device_set_error(device, WFD_ERROR_BAD_ATTRIBUTE);
		break;
	}

	return 0;
}

WFDfloat
wfd_pipeline_get_attribf(struct wfd_device *device,
			 struct wfd_pipeline *pipeline,
			 WFDPipelineConfigAttrib attrib)
{
	switch (attrib) {
	case WFD_PIPELINE_GLOBAL_ALPHA:
		return 1.0;
	default:
		wfd_device_set_error(device, WFD_ERROR_BAD_ATTRIBUTE);
		break;
	};

	return 0.0;
}


void
wfd_pipeline_set_attribiv(struct wfd_device *device,
			  struct wfd_pipeline *pipeline,
			  WFDPipelineConfigAttrib attrib,
			  WFDint count,
			  const WFDint *value)
{
	switch (attrib) {
	case WFD_PIPELINE_SOURCE_RECTANGLE:
		if (count == ARRAY_SIZE(pipeline->source_rect))
			memcpy(pipeline->source_rect, value,
			       count * (sizeof *value));
		break;
	case WFD_PIPELINE_DESTINATION_RECTANGLE:
		if (count == ARRAY_SIZE(pipeline->dest_rect))
			memcpy(pipeline->dest_rect, value,
			       count * (sizeof *value));
		break;
	default:
		wfd_device_set_error(device, WFD_ERROR_BAD_ATTRIBUTE);
		break;
	}
}

void
wfd_pipeline_get_attribiv(struct wfd_device *device,
			  struct wfd_pipeline *pipeline,
			  WFDPipelineConfigAttrib attrib,
			  WFDint count,
			  WFDint *value)
{
	switch (attrib) {
	case WFD_PIPELINE_SOURCE_RECTANGLE:
		if (count == ARRAY_SIZE(pipeline->source_rect))
			memcpy(value, pipeline->source_rect,
			       count * (sizeof *value));
		break;
	case WFD_PIPELINE_DESTINATION_RECTANGLE:
		if (count == ARRAY_SIZE(pipeline->dest_rect))
			memcpy(value, pipeline->dest_rect,
			       count * (sizeof *value));
		break;
	case WFD_PIPELINE_MAX_SOURCE_SIZE:
		if (count != 2)
			break;
		drmModeResPtr resources = wfd_device_get_resources(device);

		value[0] = resources->max_width;
		value[1] = resources->max_height;
	default:
		wfd_device_set_error(device, WFD_ERROR_BAD_ATTRIBUTE);
		break;
	}
}

void
wfd_pipeline_get_attribfv(struct wfd_device *device,
			  struct wfd_pipeline *pipeline,
			  WFDPipelineConfigAttrib attrib,
			  WFDint count,
			  WFDfloat *value)
{
	switch (attrib) {
	case WFD_PIPELINE_SCALE_RANGE:
		if (count != 2)
			return;
		value[0] = value[1] = 1.0;
		break;
	default:
		wfd_device_set_error(device, WFD_ERROR_BAD_ATTRIBUTE);
		break;
	}
};

void
wfd_pipeline_set_port(struct wfd_device *device,
		      struct wfd_pipeline *pipeline,
		      struct wfd_port *port)
{
	pipeline->port = port;
}