summaryrefslogtreecommitdiff
path: root/widget_provider/src/fb_wayland.c
blob: 1f9316b9478106148e4dfe2e32c5a5b3cb17eb9c (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
 * Copyright 2013  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/shm.h>
#include <sys/ipc.h>

#include <dlog.h>
#include <widget_errno.h>
#include <widget_service.h>
#include <widget_buffer.h>
#include <widget_util.h>

#include <wayland-client.h>
#include <tbm_bufmgr.h>

#include "debug.h"
#include "util.h"
#include "widget_provider.h"
#include "widget_provider_buffer.h"
#include "provider_buffer_internal.h"
#include "fb.h"
#include "dlist.h"

int errno;

struct gem_data {
	tbm_bo pixmap_bo;
	int count;
	int buf_count;
	int w;
	int h;
	int depth;
	void *data; /* Gem layer */
	int refcnt;
	int pixmap; /* FD in case of wayland */
};

static struct info {
	tbm_bufmgr bufmgr;
	int fd;
	struct wl_display *disp;
	int disp_is_opened;
	int master_disconnected;
	struct dlist *gem_list;
} s_info = {
	.bufmgr = NULL,
	.fd = -1,
	.disp = NULL,
	.disp_is_opened = 0,
	.master_disconnected = 0,
	.gem_list = NULL,
};

int fb_init(void *disp)
{
	int ret;

	s_info.disp = disp;
	if (!s_info.disp) {
		s_info.disp = wl_display_connect(NULL);
		if (!s_info.disp) {
			ErrPrint("Failed to open a display\n");
			return WIDGET_ERROR_FAULT;
		}

		s_info.disp_is_opened = 1;
	}

	ret = widget_util_get_drm_fd(s_info.disp, &s_info.fd);
	if (ret != WIDGET_ERROR_NONE || s_info.fd < 0) {
		ErrPrint("Failed to open a drm device: (%d)\n", errno);
		return WIDGET_ERROR_NONE;
	}

	s_info.bufmgr = tbm_bufmgr_init(s_info.fd);
	if (!s_info.bufmgr) {
		ErrPrint("Failed to init bufmgr\n");
		widget_util_release_drm_fd(s_info.fd);
		s_info.fd = -1;
		return WIDGET_ERROR_NONE;
	}

	s_info.master_disconnected = 0;
	return WIDGET_ERROR_NONE;
}

int fb_fini(void)
{
	if (s_info.bufmgr) {
		tbm_bufmgr_deinit(s_info.bufmgr);
		s_info.bufmgr = NULL;
	}

	if (s_info.fd >= 0) {
		widget_util_release_drm_fd(s_info.fd);
		s_info.fd = -1;
	}

	if (s_info.disp_is_opened && s_info.disp) {
		wl_display_disconnect(s_info.disp);
		s_info.disp = NULL;
	}

	return WIDGET_ERROR_NONE;
}

static inline int sync_for_file(struct fb_info *info)
{
	int fd;
	widget_fb_t buffer;
	const char *path;

	buffer = info->buffer;
	if (!buffer) {
		DbgPrint("Buffer is NIL, skip sync\n");
		return WIDGET_ERROR_NONE;
	}

	if (buffer->state != WIDGET_FB_STATE_CREATED) {
		ErrPrint("Invalid state of a FB\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	if (buffer->type != WIDGET_FB_TYPE_FILE) {
		DbgPrint("Ingore sync\n");
		return WIDGET_ERROR_NONE;
	}

	path = widget_util_uri_to_path(info->id);
	if (!path) {
		ErrPrint("Invalid id: %s\n", info->id);
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	fd = open(path, O_WRONLY | O_CREAT, 0644);
	if (fd < 0) {
		ErrPrint("open %s: %d\n", path, errno);
		return WIDGET_ERROR_IO_ERROR;
	}

	if (write(fd, buffer->data, info->bufsz) != info->bufsz) {
		ErrPrint("write: %d\n", errno);
		if (close(fd) < 0) {
			ErrPrint("close: %d\n", errno);
		}
		return WIDGET_ERROR_IO_ERROR;
	}

	if (close(fd) < 0) {
		ErrPrint("close: %d\n", errno);
	}
	return WIDGET_ERROR_NONE;
}

static inline int sync_for_pixmap(struct fb_info *info)
{
	widget_fb_t buffer;

	buffer = info->buffer;
	if (!buffer) {
		DbgPrint("Buffer is NIL, skip sync\n");
		return WIDGET_ERROR_NONE;
	}

	if (buffer->state != WIDGET_FB_STATE_CREATED) {
		ErrPrint("Invalid state of a FB\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	if (buffer->type != WIDGET_FB_TYPE_PIXMAP) {
		DbgPrint("Invalid buffer\n");
		return WIDGET_ERROR_NONE;
	}

	if (info->handle == 0) {
		ErrPrint("Pixmap ID is not valid\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	if (info->bufsz == 0) {
		DbgPrint("Nothing can be sync\n");
		return WIDGET_ERROR_NONE;
	}

	/**
	 * @note
	 * Do nothing for Wayland
	 */

	return WIDGET_ERROR_NONE;
}

int fb_sync(struct fb_info *info)
{
	if (!info) {
		ErrPrint("FB Handle is not valid\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	if (!info->id || info->id[0] == '\0') {
		DbgPrint("Ingore sync\n");
		return WIDGET_ERROR_NONE;
	}

	if (!strncasecmp(info->id, SCHEMA_FILE, strlen(SCHEMA_FILE))) {
		return sync_for_file(info);
	} else if (!strncasecmp(info->id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
		return sync_for_pixmap(info);
	} else if (!strncasecmp(info->id, SCHEMA_SHM, strlen(SCHEMA_SHM))) {
		return WIDGET_ERROR_NONE;
	}

	ErrPrint("Invalid URI: [%s]\n", info->id);
	return WIDGET_ERROR_INVALID_PARAMETER;
}

struct fb_info *fb_create(const char *id, int w, int h)
{
	struct fb_info *info;

	if (!id || id[0] == '\0') {
		ErrPrint("Invalid id\n");
		return NULL;
	}

	info = calloc(1, sizeof(*info));
	if (!info) {
		ErrPrint("calloc: %d\n", errno);
		return NULL;
	}

	info->id = strdup(id);
	if (!info->id) {
		ErrPrint("strdup: %d\n", errno);
		free(info);
		return NULL;
	}

	info->pixels = sizeof(int);

	if (sscanf(info->id, SCHEMA_SHM "%d", &info->handle) == 1) {
		DbgPrint("SHMID: %d\n", info->handle);
	} else if (sscanf(info->id, SCHEMA_PIXMAP "%d:%d", &info->handle, &info->pixels) == 2) {
		DbgPrint("PIXMAP: %d\n", info->handle);
	} else if (!strncasecmp(info->id, SCHEMA_FILE, strlen(SCHEMA_FILE))) {
		info->handle = -1;
	} else {
		ErrPrint("Unsupported schema: %s\n", info->id);
		free(info->id);
		free(info);
		return NULL;
	}

	info->w = w;
	info->h = h;

	return info;
}

int fb_has_gem(struct fb_info *info)
{
	return !strncasecmp(info->id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)) && (s_info.fd >= 0 || info->gem);
}

static inline struct gem_data *create_gem(unsigned int pixmap, int w, int h, int depth, int auto_align)
{
	struct gem_data *gem;

	if (!s_info.bufmgr) {
		/*
		 * @note
		 * In rare case,
		 * if the mastser is disconnected, service provider should not try to create the gem buffer
		 * To avoid this, returns NULL from here.
		 */
		ErrPrint("Buffer manager is not initialized\n");
		return NULL;
	}

	gem = calloc(1, sizeof(*gem));
	if (!gem) {
		ErrPrint("calloc: %d\n", errno);
		return NULL;
	}

	gem->count = 1;
	gem->w = w;
	gem->h = h;
	gem->depth = depth;
	gem->pixmap = pixmap;

	gem->pixmap_bo = tbm_bo_import(s_info.bufmgr, gem->pixmap);
	if (!gem->pixmap_bo) {
		ErrPrint("Failed to import BO\n");
		free(gem);
		return NULL;
	}

	s_info.gem_list = dlist_append(s_info.gem_list, gem);
	return gem;
}

static inline int destroy_gem(struct gem_data *gem)
{
	dlist_remove_data(s_info.gem_list, gem);

	if (!s_info.master_disconnected && gem->pixmap_bo) {
		DbgPrint("unref pixmap bo\n");
		tbm_bo_unref(gem->pixmap_bo);
		gem->pixmap_bo = NULL;
	} else {
		ErrPrint("Resource is not trusted anymore(%u)\n", gem->pixmap);
	}

	free(gem);
	return WIDGET_ERROR_NONE;
}

int fb_create_gem(struct fb_info *info, int auto_align)
{
	if (info->gem) {
		DbgPrint("Already created\n");
		return WIDGET_ERROR_NONE;
	}

	info->gem = create_gem(info->handle, info->w, info->h, info->pixels, auto_align);
	return info->gem ? WIDGET_ERROR_NONE : WIDGET_ERROR_FAULT;
}

int fb_destroy_gem(struct fb_info *info)
{
	if (!info || !info->gem) {
		ErrPrint("GEM is not created\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	destroy_gem(info->gem);
	info->gem = NULL;
	return WIDGET_ERROR_NONE;
}

void *fb_acquire_gem(struct fb_info *info)
{
	struct gem_data *gem;

	if (!info->gem) {
		ErrPrint("Invalid FB info\n");
		return NULL;
	}

	gem = info->gem;

	if (!gem->data) {
		tbm_bo_handle handle;
		handle = tbm_bo_map(gem->pixmap_bo, TBM_DEVICE_CPU, TBM_OPTION_READ | TBM_OPTION_WRITE);
		gem->data = handle.ptr;
		if (!gem->data) {
			ErrPrint("Failed to get BO\n");
		}
	}

	gem->refcnt++;
	return gem->data;
}

int fb_release_gem(struct fb_info *info)
{
	struct gem_data *gem;

	gem = info->gem;

	gem->refcnt--;
	if (gem->refcnt == 0) {
		tbm_bo_unmap(gem->pixmap_bo);
		gem->data = NULL;
	} else if (gem->refcnt < 0) {
		ErrPrint("Invalid refcnt: %d (reset)\n", gem->refcnt);
		gem->refcnt = 0;
	}

	return WIDGET_ERROR_NONE;
}

int fb_destroy(struct fb_info *info)
{
	if (!info) {
		ErrPrint("Handle is not valid\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	if (info->buffer) {
		widget_fb_t buffer;
		buffer = info->buffer;
		buffer->info = NULL;
	}

	free(info->id);
	free(info);
	return WIDGET_ERROR_NONE;
}

int fb_is_created(struct fb_info *info)
{
	if (!info) {
		ErrPrint("Handle is not valid\n");
		return 0;
	}

	if (!strncasecmp(info->id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP)) && info->handle != 0) {
		return 1;
	} else if (!strncasecmp(info->id, SCHEMA_SHM, strlen(SCHEMA_SHM)) && info->handle > 0) {
		return 1;
	} else {
		const char *path;
		path = widget_util_uri_to_path(info->id);
		if (path && access(path, F_OK | R_OK) == 0) {
			return 1;
		} else {
			ErrPrint("access: %d (%s)\n", errno, path);
		}
	}

	return 0;
}

void *fb_acquire_buffer(struct fb_info *info)
{
	widget_fb_t buffer;
	void *addr;

	if (!info) {
		return NULL;
	}

	if (!info->buffer) {
		if (!strncasecmp(info->id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
			ErrPrint("S/W Backed is not supported\n");
			return NULL;
		} else if (!strncasecmp(info->id, SCHEMA_FILE, strlen(SCHEMA_FILE))) {
			info->bufsz = info->w * info->h * info->pixels;
			buffer = calloc(1, sizeof(*buffer) + info->bufsz);
			if (!buffer) {
				ErrPrint("calloc: %d\n", errno);
				info->bufsz = 0;
				return NULL;
			}

			buffer->type = WIDGET_FB_TYPE_FILE;
			buffer->refcnt = 0;
			buffer->state = WIDGET_FB_STATE_CREATED;
			buffer->info = info;
			info->buffer = buffer;
		} else if (!strncasecmp(info->id, SCHEMA_SHM, strlen(SCHEMA_SHM))) {
			buffer = shmat(info->handle, NULL, 0);
			if (buffer == (void *)-1) {
				ErrPrint("shmat: %d\n", errno);
				return NULL;
			}

			info->buffer = buffer;
		} else {
			DbgPrint("Buffer is NIL\n");
			return NULL;
		}
	}

	buffer = info->buffer;
	switch (buffer->type) {
	case WIDGET_FB_TYPE_FILE:
		buffer->refcnt++;
		/* Fall through */
	case WIDGET_FB_TYPE_SHM:
		addr = buffer->data;
		break;
	case WIDGET_FB_TYPE_PIXMAP:
		ErrPrint("S/W Backend is not supported\n");
	default:
		addr = NULL;
		break;
	}

	return addr;
}

int fb_release_buffer(void *data)
{
	widget_fb_t buffer;

	if (!data) {
		return WIDGET_ERROR_NONE;
	}

	buffer = container_of(data, struct widget_fb, data);

	if (buffer->state != WIDGET_FB_STATE_CREATED) {
		ErrPrint("Invalid handle\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	switch (buffer->type) {
	case WIDGET_FB_TYPE_SHM:
		/*!
		 * \note
		 * SHM can not use the "info"
		 * it is NULL
		 */
		if (shmdt(buffer) < 0) {
			ErrPrint("shmdt: %d\n", errno);
		}
		break;
	case WIDGET_FB_TYPE_PIXMAP:
		ErrPrint("S/W Backend is not supported\n");
		return WIDGET_ERROR_NOT_SUPPORTED;
	case WIDGET_FB_TYPE_FILE:
		buffer->refcnt--;
		if (buffer->refcnt == 0) {
			struct fb_info *info;
			info = buffer->info;
			if (info && info->buffer == buffer) {
				info->buffer = NULL;
			}
			buffer->state = WIDGET_FB_STATE_DESTROYED;
			free(buffer);
		}
		break;
	default:
		ErrPrint("Unknown type\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	return WIDGET_ERROR_NONE;
}

int fb_type(struct fb_info *info)
{
	widget_fb_t buffer;

	if (!info) {
		return WIDGET_FB_TYPE_ERROR;
	}

	buffer = info->buffer;
	if (!buffer) {
		int type = WIDGET_FB_TYPE_ERROR;
		/*!
		 * \note
		 * Try to get this from SCHEMA
		 */
		if (info->id) {
			if (!strncasecmp(info->id, SCHEMA_FILE, strlen(SCHEMA_FILE))) {
				type = WIDGET_FB_TYPE_FILE;
			} else if (!strncasecmp(info->id, SCHEMA_PIXMAP, strlen(SCHEMA_PIXMAP))) {
				type = WIDGET_FB_TYPE_PIXMAP;
			} else if (!strncasecmp(info->id, SCHEMA_SHM, strlen(SCHEMA_SHM))) {
				type = WIDGET_FB_TYPE_SHM;
			}
		}

		return type;
	}

	return buffer->type;
}

int fb_refcnt(void *data)
{
	widget_fb_t buffer;
	struct shmid_ds buf;
	int ret;

	if (!data) {
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	buffer = container_of(data, struct widget_fb, data);

	if (buffer->state != WIDGET_FB_STATE_CREATED) {
		ErrPrint("Invalid handle\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	switch (buffer->type) {
	case WIDGET_FB_TYPE_SHM:
		if (shmctl(buffer->refcnt, IPC_STAT, &buf) < 0) {
			ErrPrint("shmctl: %d\n", errno);
			ret = WIDGET_ERROR_FAULT;
			break;
		}

		ret = buf.shm_nattch; /*!< This means attached count of process */
		break;
	case WIDGET_FB_TYPE_PIXMAP:
		ret = buffer->refcnt;
		break;
	case WIDGET_FB_TYPE_FILE:
		ret = buffer->refcnt;
		break;
	default:
		ret = WIDGET_ERROR_INVALID_PARAMETER;
		break;
	}

	return ret;
}

const char *fb_id(struct fb_info *info)
{
	return info ? info->id : NULL;
}

int fb_get_size(struct fb_info *info, int *w, int *h)
{
	if (!info) {
		ErrPrint("Handle is not valid\n");
		return WIDGET_ERROR_INVALID_PARAMETER;
	}

	*w = info->w;
	*h = info->h;
	return WIDGET_ERROR_NONE;
}

int fb_size(struct fb_info *info)
{
	if (!info) {
		ErrPrint("Handle is not valid\n");
		return 0;
	}

	info->bufsz = info->w * info->h * info->pixels;
	return info->bufsz;
}

int fb_sync_xdamage(struct fb_info *info, widget_damage_region_s *region)
{
	return WIDGET_ERROR_NOT_SUPPORTED;
}

int fb_stride(struct fb_info *info)
{
	int stride;

	stride = info->w * info->pixels;
	DbgPrint("Stride: %d\n", stride);

	return stride;
}

void fb_master_disconnected(void)
{
	s_info.master_disconnected = 1;
}

/* End of a file */