1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2017, 2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
6 * Copyright (c) 2018 DisplayLink (UK) Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial
18 * portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include "config.h"
31
32 #include <errno.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <linux/input.h>
40 #include <linux/vt.h>
41 #include <assert.h>
42 #include <sys/mman.h>
43 #include <time.h>
44
45 #include <xf86drm.h>
46 #include <xf86drmMode.h>
47 #include <drm_fourcc.h>
48
49 #include <libudev.h>
50
51 #include <libweston/libweston.h>
52 #include <libweston/backend-drm.h>
53 #include <libweston/weston-log.h>
54 #include "drm-internal.h"
55 #include "shared/helpers.h"
56 #include "shared/timespec-util.h"
57 #include "shared/string-helpers.h"
58 #include "pixman-renderer.h"
59 #include "pixel-formats.h"
60 #include "libbacklight.h"
61 #include "libinput-seat.h"
62 #include "launcher-util.h"
63 #include "vaapi-recorder.h"
64 #include "presentation-time-server-protocol.h"
65 #include "linux-dmabuf.h"
66 #include "linux-dmabuf-unstable-v1-server-protocol.h"
67 #include "linux-explicit-synchronization.h"
68 #include "wayland_drm_auth_server.h" // OHOS drm auth
69
70 #include "libweston/trace.h"
71 #include "libweston/soft_vsync.h" //OHOS vsync module
72 #include "mix_renderer.h"
73 DEFINE_LOG_LABEL("DrmBackend");
74
75 static const char default_seat[] = "seat0";
76
77 static void
drm_backend_create_faked_zpos(struct drm_backend * b)78 drm_backend_create_faked_zpos(struct drm_backend *b)
79 {
80 struct drm_plane *plane;
81 uint64_t zpos = 0ULL;
82 uint64_t zpos_min_primary;
83 uint64_t zpos_min_overlay;
84 uint64_t zpos_min_cursor;
85
86 zpos_min_primary = zpos;
87 wl_list_for_each(plane, &b->plane_list, link) {
88 /* if the property is there, bail out sooner */
89 if (plane->props[WDRM_PLANE_ZPOS].prop_id != 0)
90 return;
91
92 if (plane->type != WDRM_PLANE_TYPE_PRIMARY)
93 continue;
94 zpos++;
95 }
96
97 zpos_min_overlay = zpos;
98 wl_list_for_each(plane, &b->plane_list, link) {
99 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
100 continue;
101 zpos++;
102 }
103
104 zpos_min_cursor = zpos;
105 wl_list_for_each(plane, &b->plane_list, link) {
106 if (plane->type != WDRM_PLANE_TYPE_CURSOR)
107 continue;
108 zpos++;
109 }
110
111 drm_debug(b, "[drm-backend] zpos property not found. "
112 "Using invented immutable zpos values:\n");
113 /* assume that invented zpos values are immutable */
114 wl_list_for_each(plane, &b->plane_list, link) {
115 if (plane->type == WDRM_PLANE_TYPE_PRIMARY) {
116 plane->zpos_min = zpos_min_primary;
117 plane->zpos_max = zpos_min_primary;
118 } else if (plane->type == WDRM_PLANE_TYPE_OVERLAY) {
119 plane->zpos_min = zpos_min_overlay;
120 plane->zpos_max = zpos_min_overlay;
121 } else if (plane->type == WDRM_PLANE_TYPE_CURSOR) {
122 plane->zpos_min = zpos_min_cursor;
123 plane->zpos_max = zpos_min_cursor;
124 }
125 drm_debug(b, "\t[plane] %s plane %d, zpos_min %"PRIu64", "
126 "zpos_max %"PRIu64"\n",
127 drm_output_get_plane_type_name(plane),
128 plane->plane_id, plane->zpos_min, plane->zpos_max);
129 }
130 }
131
132 static void
wl_array_remove_uint32(struct wl_array * array,uint32_t elm)133 wl_array_remove_uint32(struct wl_array *array, uint32_t elm)
134 {
135 uint32_t *pos, *end;
136
137 end = (uint32_t *) ((char *) array->data + array->size);
138
139 wl_array_for_each(pos, array) {
140 if (*pos != elm)
141 continue;
142
143 array->size -= sizeof(*pos);
144 if (pos + 1 == end)
145 break;
146
147 memmove(pos, pos + 1, (char *) end - (char *) (pos + 1));
148 break;
149 }
150 }
151
152 static int
pageflip_timeout(void * data)153 pageflip_timeout(void *data) {
154 /*
155 * Our timer just went off, that means we're not receiving drm
156 * page flip events anymore for that output. Let's gracefully exit
157 * weston with a return value so devs can debug what's going on.
158 */
159 struct drm_output *output = data;
160 struct weston_compositor *compositor = output->base.compositor;
161
162 weston_log("Pageflip timeout reached on output %s, your "
163 "driver is probably buggy! Exiting.\n",
164 output->base.name);
165 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
166
167 return 0;
168 }
169
170 /* Creates the pageflip timer. Note that it isn't armed by default */
171 static int
drm_output_pageflip_timer_create(struct drm_output * output)172 drm_output_pageflip_timer_create(struct drm_output *output)
173 {
174 struct wl_event_loop *loop = NULL;
175 struct weston_compositor *ec = output->base.compositor;
176
177 loop = wl_display_get_event_loop(ec->wl_display);
178 assert(loop);
179 output->pageflip_timer = wl_event_loop_add_timer(loop,
180 pageflip_timeout,
181 output);
182
183 if (output->pageflip_timer == NULL) {
184 weston_log("creating drm pageflip timer failed: %s\n",
185 strerror(errno));
186 return -1;
187 }
188
189 return 0;
190 }
191
192 static void
193 drm_output_destroy(struct weston_output *output_base);
194
195 /**
196 * Returns true if the plane can be used on the given output for its current
197 * repaint cycle.
198 */
199 bool
drm_plane_is_available(struct drm_plane * plane,struct drm_output * output)200 drm_plane_is_available(struct drm_plane *plane, struct drm_output *output)
201 {
202 assert(plane->state_cur);
203
204 if (output->virtual)
205 return false;
206
207 /* The plane still has a request not yet completed by the kernel. */
208 if (!plane->state_cur->complete)
209 return false;
210
211 /* The plane is still active on another output. */
212 if (plane->state_cur->output && plane->state_cur->output != output)
213 return false;
214
215 /* Check whether the plane can be used with this CRTC; possible_crtcs
216 * is a bitmask of CRTC indices (pipe), rather than CRTC object ID. */
217 return !!(plane->possible_crtcs & (1 << output->pipe));
218 }
219
220 struct drm_output *
drm_output_find_by_crtc(struct drm_backend * b,uint32_t crtc_id)221 drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
222 {
223 struct drm_output *output;
224
225 wl_list_for_each(output, &b->compositor->output_list, base.link) {
226 if (output->crtc_id == crtc_id)
227 return output;
228 }
229
230 return NULL;
231 }
232
233 struct drm_head *
drm_head_find_by_connector(struct drm_backend * backend,uint32_t connector_id)234 drm_head_find_by_connector(struct drm_backend *backend, uint32_t connector_id)
235 {
236 struct weston_head *base;
237 struct drm_head *head;
238
239 wl_list_for_each(base,
240 &backend->compositor->head_list, compositor_link) {
241 head = to_drm_head(base);
242 if (head->connector_id == connector_id)
243 return head;
244 }
245
246 return NULL;
247 }
248
249 /**
250 * Get output state to disable output
251 *
252 * Returns a pointer to an output_state object which can be used to disable
253 * an output (e.g. DPMS off).
254 *
255 * @param pending_state The pending state object owning this update
256 * @param output The output to disable
257 * @returns A drm_output_state to disable the output
258 */
259 static struct drm_output_state *
drm_output_get_disable_state(struct drm_pending_state * pending_state,struct drm_output * output)260 drm_output_get_disable_state(struct drm_pending_state *pending_state,
261 struct drm_output *output)
262 {
263 struct drm_output_state *output_state;
264
265 output_state = drm_output_state_duplicate(output->state_cur,
266 pending_state,
267 DRM_OUTPUT_STATE_CLEAR_PLANES);
268 output_state->dpms = WESTON_DPMS_OFF;
269
270 output_state->protection = WESTON_HDCP_DISABLE;
271
272 return output_state;
273 }
274
275
276 /**
277 * Mark a drm_output_state (the output's last state) as complete. This handles
278 * any post-completion actions such as updating the repaint timer, disabling the
279 * output, and finally freeing the state.
280 */
281 void
drm_output_update_complete(struct drm_output * output,uint32_t flags,unsigned int sec,unsigned int usec)282 drm_output_update_complete(struct drm_output *output, uint32_t flags,
283 unsigned int sec, unsigned int usec)
284 {
285 struct drm_backend *b = to_drm_backend(output->base.compositor);
286 struct drm_plane_state *ps;
287 struct timespec ts;
288
289 /* Stop the pageflip timer instead of rearming it here */
290 if (output->pageflip_timer)
291 wl_event_source_timer_update(output->pageflip_timer, 0);
292
293 wl_list_for_each(ps, &output->state_cur->plane_list, link)
294 ps->complete = true;
295
296 drm_output_state_free(output->state_last);
297 output->state_last = NULL;
298
299 if (output->destroy_pending) {
300 output->destroy_pending = false;
301 output->disable_pending = false;
302 output->dpms_off_pending = false;
303 drm_output_destroy(&output->base);
304 return;
305 } else if (output->disable_pending) {
306 output->disable_pending = false;
307 output->dpms_off_pending = false;
308 weston_output_disable(&output->base);
309 return;
310 } else if (output->dpms_off_pending) {
311 struct drm_pending_state *pending = drm_pending_state_alloc(b);
312 output->dpms_off_pending = false;
313 drm_output_get_disable_state(pending, output);
314 drm_pending_state_apply_sync(pending);
315 }
316 if (output->state_cur->dpms == WESTON_DPMS_OFF &&
317 output->base.repaint_status != REPAINT_AWAITING_COMPLETION) {
318 /* DPMS can happen to us either in the middle of a repaint
319 * cycle (when we have painted fresh content, only to throw it
320 * away for DPMS off), or at any other random point. If the
321 * latter is true, then we cannot go through finish_frame,
322 * because the repaint machinery does not expect this. */
323 return;
324 }
325
326 ts.tv_sec = sec;
327 ts.tv_nsec = usec * 1000;
328 LOG_PASS();
329 weston_output_finish_frame(&output->base, &ts, flags);
330
331 /* We can't call this from frame_notify, because the output's
332 * repaint needed flag is cleared just after that */
333 if (output->recorder)
334 weston_output_schedule_repaint(&output->base);
335 }
336
337 static struct drm_fb *
drm_output_render_pixman(struct drm_output_state * state,pixman_region32_t * damage)338 drm_output_render_pixman(struct drm_output_state *state,
339 pixman_region32_t *damage)
340 {
341 struct drm_output *output = state->output;
342 struct weston_compositor *ec = output->base.compositor;
343
344 output->current_image ^= 1;
345
346 pixman_renderer_output_set_buffer(&output->base,
347 output->image[output->current_image]);
348 pixman_renderer_output_set_hw_extra_damage(&output->base,
349 &output->previous_damage);
350
351 ec->renderer->repaint_output(&output->base, damage);
352
353 pixman_region32_copy(&output->previous_damage, damage);
354
355 return drm_fb_ref(output->dumb[output->current_image]);
356 }
357
358 void
drm_output_render(struct drm_output_state * state,pixman_region32_t * damage)359 drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
360 {
361 struct drm_output *output = state->output;
362 struct weston_compositor *c = output->base.compositor;
363 struct drm_plane_state *scanout_state;
364 struct drm_plane *scanout_plane = output->scanout_plane;
365 struct drm_property_info *damage_info =
366 &scanout_plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS];
367 struct drm_backend *b = to_drm_backend(c);
368 struct drm_fb *fb;
369 pixman_region32_t scanout_damage;
370 pixman_box32_t *rects;
371 int n_rects;
372
373 /* If we already have a client buffer promoted to scanout, then we don't
374 * want to render. */
375 scanout_state = drm_output_state_get_plane(state,
376 output->scanout_plane);
377 if (scanout_state->fb)
378 return;
379
380 /*
381 * If we don't have any damage on the primary plane, and we already
382 * have a renderer buffer active, we can reuse it; else we pass
383 * the damaged region into the renderer to re-render the affected
384 * area.
385 */
386 if (!pixman_region32_not_empty(damage) &&
387 scanout_plane->state_cur->fb &&
388 (scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE ||
389 scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB)) {
390 fb = drm_fb_ref(scanout_plane->state_cur->fb);
391 } else if (b->use_pixman) {
392 fb = drm_output_render_pixman(state, damage);
393 } else {
394 fb = drm_output_render_gl(state, damage);
395 }
396
397 if (!fb) {
398 drm_plane_state_put_back(scanout_state);
399 return;
400 }
401
402 scanout_state->fb = fb;
403 scanout_state->output = output;
404
405 scanout_state->src_x = 0;
406 scanout_state->src_y = 0;
407 scanout_state->src_w = fb->width << 16;
408 scanout_state->src_h = fb->height << 16;
409
410 scanout_state->dest_x = 0;
411 scanout_state->dest_y = 0;
412 scanout_state->dest_w = output->base.current_mode->width;
413 scanout_state->dest_h = output->base.current_mode->height;
414
415 pixman_region32_subtract(&c->primary_plane.damage,
416 &c->primary_plane.damage, damage);
417
418 /* Don't bother calculating plane damage if the plane doesn't support it */
419 if (damage_info->prop_id == 0)
420 return;
421
422 pixman_region32_init(&scanout_damage);
423 pixman_region32_copy(&scanout_damage, damage);
424
425 if (output->base.zoom.active) {
426 weston_matrix_transform_region(&scanout_damage,
427 &output->base.matrix,
428 &scanout_damage);
429 } else {
430 pixman_region32_translate(&scanout_damage,
431 -output->base.x, -output->base.y);
432 weston_transformed_region(output->base.width,
433 output->base.height,
434 output->base.transform,
435 output->base.current_scale,
436 &scanout_damage,
437 &scanout_damage);
438 }
439
440 assert(scanout_state->damage_blob_id == 0);
441
442 rects = pixman_region32_rectangles(&scanout_damage, &n_rects);
443
444 /*
445 * If this function fails, the blob id should still be 0.
446 * This tells the kernel there is no damage information, which means
447 * that it will consider the whole plane damaged. While this may
448 * affect efficiency, it should still produce correct results.
449 */
450 drmModeCreatePropertyBlob(b->drm.fd, rects,
451 sizeof(*rects) * n_rects,
452 &scanout_state->damage_blob_id);
453
454 pixman_region32_fini(&scanout_damage);
455 }
456
457 static int
drm_output_repaint(struct weston_output * output_base,pixman_region32_t * damage,void * repaint_data)458 drm_output_repaint(struct weston_output *output_base,
459 pixman_region32_t *damage,
460 void *repaint_data)
461 {
462 struct drm_pending_state *pending_state = repaint_data;
463 struct drm_output *output = to_drm_output(output_base);
464 struct drm_output_state *state = NULL;
465 struct drm_plane_state *scanout_state;
466
467 assert(!output->virtual);
468
469 if (output->disable_pending || output->destroy_pending)
470 goto err;
471
472 assert(!output->state_last);
473
474 /* If planes have been disabled in the core, we might not have
475 * hit assign_planes at all, so might not have valid output state
476 * here. */
477 state = drm_pending_state_get_output(pending_state, output);
478 if (!state)
479 state = drm_output_state_duplicate(output->state_cur,
480 pending_state,
481 DRM_OUTPUT_STATE_CLEAR_PLANES);
482 state->dpms = WESTON_DPMS_ON;
483
484 if (output_base->allow_protection)
485 state->protection = output_base->desired_protection;
486 else
487 state->protection = WESTON_HDCP_DISABLE;
488
489 drm_output_render(state, damage);
490 scanout_state = drm_output_state_get_plane(state,
491 output->scanout_plane);
492 if (!scanout_state || !scanout_state->fb)
493 goto err;
494
495 wl_signal_emit(&output_base->frame_signal, damage); // OHOS ScreenShot: move to output->repaint
496 return 0;
497
498 err:
499 drm_output_state_free(state);
500 return -1;
501 }
502
503 /* Determine the type of vblank synchronization to use for the output.
504 *
505 * The pipe parameter indicates which CRTC is in use. Knowing this, we
506 * can determine which vblank sequence type to use for it. Traditional
507 * cards had only two CRTCs, with CRTC 0 using no special flags, and
508 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
509 * parameter indicates this.
510 *
511 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
512 * 0-31. If this is non-zero it indicates we're dealing with a
513 * multi-gpu situation and we need to calculate the vblank sync
514 * using DRM_BLANK_HIGH_CRTC_MASK.
515 */
516 static unsigned int
drm_waitvblank_pipe(struct drm_output * output)517 drm_waitvblank_pipe(struct drm_output *output)
518 {
519 if (output->pipe > 1)
520 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
521 DRM_VBLANK_HIGH_CRTC_MASK;
522 else if (output->pipe > 0)
523 return DRM_VBLANK_SECONDARY;
524 else
525 return 0;
526 }
527
528 // OHOS vsync module
529 static unsigned int
drm_waitvblank_pipe_vsync(int pipe)530 drm_waitvblank_pipe_vsync(int pipe)
531 {
532 if (pipe > 1)
533 return (pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
534 DRM_VBLANK_HIGH_CRTC_MASK;
535 else if (pipe > 0)
536 return DRM_VBLANK_SECONDARY;
537 else
538 return 0;
539 }
540
541 static int
drm_output_start_repaint_loop(struct weston_output * output_base)542 drm_output_start_repaint_loop(struct weston_output *output_base)
543 {
544 struct drm_output *output = to_drm_output(output_base);
545 struct drm_pending_state *pending_state;
546 struct drm_plane *scanout_plane = output->scanout_plane;
547 struct drm_backend *backend =
548 to_drm_backend(output_base->compositor);
549 struct timespec ts, tnow;
550 struct timespec vbl2now;
551 int64_t refresh_nsec;
552 int ret;
553 drmVBlank vbl = {
554 .request.type = DRM_VBLANK_RELATIVE,
555 .request.sequence = 0,
556 .request.signal = 0,
557 };
558
559 if (output->disable_pending || output->destroy_pending)
560 return 0;
561
562 if (!output->scanout_plane->state_cur->fb) {
563 /* We can't page flip if there's no mode set */
564 goto finish_frame;
565 }
566
567 /* Need to smash all state in from scratch; current timings might not
568 * be what we want, page flip might not work, etc.
569 */
570 if (backend->state_invalid)
571 goto finish_frame;
572
573 assert(scanout_plane->state_cur->output == output);
574
575 /* Try to get current msc and timestamp via instant query */
576 vbl.request.type |= drm_waitvblank_pipe(output);
577 ret = drmWaitVBlank(backend->drm.fd, &vbl);
578
579 /* Error ret or zero timestamp means failure to get valid timestamp */
580 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
581 ts.tv_sec = vbl.reply.tval_sec;
582 ts.tv_nsec = vbl.reply.tval_usec * 1000;
583
584 /* Valid timestamp for most recent vblank - not stale?
585 * Stale ts could happen on Linux 3.17+, so make sure it
586 * is not older than 1 refresh duration since now.
587 */
588 weston_compositor_read_presentation_clock(backend->compositor,
589 &tnow);
590 timespec_sub(&vbl2now, &tnow, &ts);
591 refresh_nsec =
592 millihz_to_nsec(output->base.current_mode->refresh);
593 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
594 drm_output_update_msc(output, vbl.reply.sequence);
595 LOG_PASS();
596 weston_output_finish_frame(output_base, &ts,
597 WP_PRESENTATION_FEEDBACK_INVALID);
598 return 0;
599 }
600 }
601
602 /* Immediate query didn't provide valid timestamp.
603 * Use pageflip fallback.
604 */
605
606 assert(!output->page_flip_pending);
607 assert(!output->state_last);
608
609 pending_state = drm_pending_state_alloc(backend);
610 drm_output_state_duplicate(output->state_cur, pending_state,
611 DRM_OUTPUT_STATE_PRESERVE_PLANES);
612
613 ret = drm_pending_state_apply(pending_state);
614 if (ret != 0) {
615 weston_log("applying repaint-start state failed: %s\n",
616 strerror(errno));
617 if (ret == -EACCES)
618 return -1;
619 goto finish_frame;
620 }
621
622 return 0;
623
624 finish_frame:
625 /* if we cannot page-flip, immediately finish frame */
626 LOG_PASS();
627 weston_output_finish_frame(output_base, NULL,
628 WP_PRESENTATION_FEEDBACK_INVALID);
629 return 0;
630 }
631
632 /**
633 * Begin a new repaint cycle
634 *
635 * Called by the core compositor at the beginning of a repaint cycle. Creates
636 * a new pending_state structure to own any output state created by individual
637 * output repaint functions until the repaint is flushed or cancelled.
638 */
639 static void *
drm_repaint_begin(struct weston_compositor * compositor)640 drm_repaint_begin(struct weston_compositor *compositor)
641 {
642 struct drm_backend *b = to_drm_backend(compositor);
643 struct drm_pending_state *ret;
644
645 ret = drm_pending_state_alloc(b);
646 b->repaint_data = ret;
647
648 // OHOS remove logger
649 // if (weston_log_scope_is_enabled(b->debug)) {
650 // char *dbg = weston_compositor_print_scene_graph(compositor);
651 // drm_debug(b, "[repaint] Beginning repaint; pending_state %p\n",
652 // ret);
653 // drm_debug(b, "%s", dbg);
654 // free(dbg);
655 // }
656
657 return ret;
658 }
659
660 /**
661 * Flush a repaint set
662 *
663 * Called by the core compositor when a repaint cycle has been completed
664 * and should be flushed. Frees the pending state, transitioning ownership
665 * of the output state from the pending state, to the update itself. When
666 * the update completes (see drm_output_update_complete), the output
667 * state will be freed.
668 */
669 static int
drm_repaint_flush(struct weston_compositor * compositor,void * repaint_data)670 drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
671 {
672 struct drm_backend *b = to_drm_backend(compositor);
673 struct drm_pending_state *pending_state = repaint_data;
674 int ret;
675
676 ret = drm_pending_state_apply(pending_state);
677 if (ret != 0)
678 weston_log("repaint-flush failed: %s\n", strerror(errno));
679
680 drm_debug(b, "[repaint] flushed pending_state %p\n", pending_state);
681 b->repaint_data = NULL;
682
683 return (ret == -EACCES) ? -1 : 0;
684 }
685
686 /**
687 * Cancel a repaint set
688 *
689 * Called by the core compositor when a repaint has finished, so the data
690 * held across the repaint cycle should be discarded.
691 */
692 static void
drm_repaint_cancel(struct weston_compositor * compositor,void * repaint_data)693 drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
694 {
695 struct drm_backend *b = to_drm_backend(compositor);
696 struct drm_pending_state *pending_state = repaint_data;
697
698 drm_pending_state_free(pending_state);
699 drm_debug(b, "[repaint] cancel pending_state %p\n", pending_state);
700 b->repaint_data = NULL;
701 }
702
703 static int
704 drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
705 static void
706 drm_output_fini_pixman(struct drm_output *output);
707
708 static int
drm_output_switch_mode(struct weston_output * output_base,struct weston_mode * mode)709 drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
710 {
711 struct drm_output *output = to_drm_output(output_base);
712 struct drm_backend *b = to_drm_backend(output_base->compositor);
713 struct drm_mode *drm_mode = drm_output_choose_mode(output, mode);
714
715 if (!drm_mode) {
716 weston_log("%s: invalid resolution %dx%d\n",
717 output_base->name, mode->width, mode->height);
718 return -1;
719 }
720
721 if (&drm_mode->base == output->base.current_mode)
722 return 0;
723
724 output->base.current_mode->flags = 0;
725
726 output->base.current_mode = &drm_mode->base;
727 output->base.current_mode->flags =
728 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
729
730 /* XXX: This drops our current buffer too early, before we've started
731 * displaying it. Ideally this should be much more atomic and
732 * integrated with a full repaint cycle, rather than doing a
733 * sledgehammer modeswitch first, and only later showing new
734 * content.
735 */
736 b->state_invalid = true;
737
738 if (b->use_pixman) {
739 drm_output_fini_pixman(output);
740 if (drm_output_init_pixman(output, b) < 0) {
741 weston_log("failed to init output pixman state with "
742 "new mode\n");
743 return -1;
744 }
745 } else {
746 drm_output_fini_egl(output);
747 if (drm_output_init_egl(output, b) < 0) {
748 weston_log("failed to init output egl state with "
749 "new mode");
750 return -1;
751 }
752 }
753
754 return 0;
755 }
756
757 static int
init_pixman(struct drm_backend * b)758 init_pixman(struct drm_backend *b)
759 {
760 return pixman_renderer_init(b->compositor);
761 }
762
763 /**
764 * Create a drm_plane for a hardware plane
765 *
766 * Creates one drm_plane structure for a hardware plane, and initialises its
767 * properties and formats.
768 *
769 * In the absence of universal plane support, where KMS does not explicitly
770 * expose the primary and cursor planes to userspace, this may also create
771 * an 'internal' plane for internal management.
772 *
773 * This function does not add the plane to the list of usable planes in Weston
774 * itself; the caller is responsible for this.
775 *
776 * Call drm_plane_destroy to clean up the plane.
777 *
778 * @sa drm_output_find_special_plane
779 * @param b DRM compositor backend
780 * @param kplane DRM plane to create, or NULL if creating internal plane
781 * @param output Output to create internal plane for, or NULL
782 * @param type Type to use when creating internal plane, or invalid
783 * @param format Format to use for internal planes, or 0
784 */
785 static struct drm_plane *
drm_plane_create(struct drm_backend * b,const drmModePlane * kplane,struct drm_output * output,enum wdrm_plane_type type,uint32_t format)786 drm_plane_create(struct drm_backend *b, const drmModePlane *kplane,
787 struct drm_output *output, enum wdrm_plane_type type,
788 uint32_t format)
789 {
790 struct drm_plane *plane;
791 drmModeObjectProperties *props;
792 uint64_t *zpos_range_values;
793 uint32_t num_formats = (kplane) ? kplane->count_formats : 1;
794
795 plane = zalloc(sizeof(*plane) +
796 (sizeof(plane->formats[0]) * num_formats));
797 if (!plane) {
798 weston_log("%s: out of memory\n", __func__);
799 return NULL;
800 }
801
802 plane->backend = b;
803 plane->count_formats = num_formats;
804 plane->state_cur = drm_plane_state_alloc(NULL, plane);
805 plane->state_cur->complete = true;
806
807 if (kplane) {
808 plane->possible_crtcs = kplane->possible_crtcs;
809 plane->plane_id = kplane->plane_id;
810
811 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
812 DRM_MODE_OBJECT_PLANE);
813 if (!props) {
814 weston_log("couldn't get plane properties\n");
815 goto err;
816 }
817 drm_property_info_populate(b, plane_props, plane->props,
818 WDRM_PLANE__COUNT, props);
819 plane->type =
820 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
821 props,
822 WDRM_PLANE_TYPE__COUNT);
823
824 zpos_range_values =
825 drm_property_get_range_values(&plane->props[WDRM_PLANE_ZPOS],
826 props);
827
828 if (zpos_range_values) {
829 plane->zpos_min = zpos_range_values[0];
830 plane->zpos_max = zpos_range_values[1];
831 } else {
832 plane->zpos_min = DRM_PLANE_ZPOS_INVALID_PLANE;
833 plane->zpos_max = DRM_PLANE_ZPOS_INVALID_PLANE;
834 }
835
836 if (drm_plane_populate_formats(plane, kplane, props,
837 b->fb_modifiers) < 0) {
838 drmModeFreeObjectProperties(props);
839 goto err;
840 }
841
842 drmModeFreeObjectProperties(props);
843 }
844 else {
845 plane->possible_crtcs = (1 << output->pipe);
846 plane->plane_id = 0;
847
848 // OHOS fix
849 if (!b->use_tde) {
850 plane->plane_id = 1;
851 }
852 weston_log("plane->plane_id: %{public}d", plane->plane_id);
853
854 plane->count_formats = 1;
855 plane->formats[0].format = format;
856 plane->type = type;
857 plane->zpos_max = DRM_PLANE_ZPOS_INVALID_PLANE;
858 plane->zpos_min = DRM_PLANE_ZPOS_INVALID_PLANE;
859 }
860
861 if (plane->type == WDRM_PLANE_TYPE__COUNT)
862 goto err_props;
863
864 /* With universal planes, everything is a DRM plane; without
865 * universal planes, the only DRM planes are overlay planes.
866 * Everything else is a fake plane. */
867 if (b->universal_planes) {
868 assert(kplane);
869 } else {
870 if (kplane)
871 assert(plane->type == WDRM_PLANE_TYPE_OVERLAY);
872 else
873 assert(plane->type != WDRM_PLANE_TYPE_OVERLAY &&
874 output);
875 }
876
877 weston_plane_init(&plane->base, b->compositor, 0, 0);
878 wl_list_insert(&b->plane_list, &plane->link);
879
880 return plane;
881
882 err_props:
883 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
884 err:
885 drm_plane_state_free(plane->state_cur, true);
886 free(plane);
887 return NULL;
888 }
889
890 /**
891 * Find, or create, a special-purpose plane
892 *
893 * Primary and cursor planes are a special case, in that before universal
894 * planes, they are driven by non-plane API calls. Without universal plane
895 * support, the only way to configure a primary plane is via drmModeSetCrtc,
896 * and the only way to configure a cursor plane is drmModeSetCursor2.
897 *
898 * Although they may actually be regular planes in the hardware, without
899 * universal plane support, these planes are not actually exposed to
900 * userspace in the regular plane list.
901 *
902 * However, for ease of internal tracking, we want to manage all planes
903 * through the same drm_plane structures. Therefore, when we are running
904 * without universal plane support, we create fake drm_plane structures
905 * to track these planes.
906 *
907 * @param b DRM backend
908 * @param output Output to use for plane
909 * @param type Type of plane
910 */
911 static struct drm_plane *
drm_output_find_special_plane(struct drm_backend * b,struct drm_output * output,enum wdrm_plane_type type)912 drm_output_find_special_plane(struct drm_backend *b, struct drm_output *output,
913 enum wdrm_plane_type type)
914 {
915 struct drm_plane *plane;
916
917 if (!b->universal_planes) {
918 uint32_t format;
919
920 switch (type) {
921 case WDRM_PLANE_TYPE_CURSOR:
922 format = DRM_FORMAT_ARGB8888;
923 break;
924 case WDRM_PLANE_TYPE_PRIMARY:
925 /* We don't know what formats the primary plane supports
926 * before universal planes, so we just assume that the
927 * GBM format works; however, this isn't set until after
928 * the output is created. */
929 format = 0;
930 break;
931 default:
932 assert(!"invalid type in drm_output_find_special_plane");
933 break;
934 }
935
936 return drm_plane_create(b, NULL, output, type, format);
937 }
938
939 wl_list_for_each(plane, &b->plane_list, link) {
940 struct drm_output *tmp;
941 bool found_elsewhere = false;
942
943 if (plane->type != type)
944 continue;
945 if (!drm_plane_is_available(plane, output))
946 continue;
947
948 /* On some platforms, primary/cursor planes can roam
949 * between different CRTCs, so make sure we don't claim the
950 * same plane for two outputs. */
951 wl_list_for_each(tmp, &b->compositor->output_list,
952 base.link) {
953 if (tmp->cursor_plane == plane ||
954 tmp->scanout_plane == plane) {
955 found_elsewhere = true;
956 break;
957 }
958 }
959
960 if (found_elsewhere)
961 continue;
962
963 plane->possible_crtcs = (1 << output->pipe);
964 return plane;
965 }
966
967 return NULL;
968 }
969
970 /**
971 * Destroy one DRM plane
972 *
973 * Destroy a DRM plane, removing it from screen and releasing its retained
974 * buffers in the process. The counterpart to drm_plane_create.
975 *
976 * @param plane Plane to deallocate (will be freed)
977 */
978 static void
drm_plane_destroy(struct drm_plane * plane)979 drm_plane_destroy(struct drm_plane *plane)
980 {
981 if (plane->type == WDRM_PLANE_TYPE_OVERLAY)
982 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id,
983 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
984 drm_plane_state_free(plane->state_cur, true);
985 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
986 weston_plane_release(&plane->base);
987 wl_list_remove(&plane->link);
988 free(plane);
989 }
990
991 /**
992 * Initialise sprites (overlay planes)
993 *
994 * Walk the list of provided DRM planes, and add overlay planes.
995 *
996 * Call destroy_sprites to free these planes.
997 *
998 * @param b DRM compositor backend
999 */
1000 static void
create_sprites(struct drm_backend * b)1001 create_sprites(struct drm_backend *b)
1002 {
1003 drmModePlaneRes *kplane_res;
1004 drmModePlane *kplane;
1005 struct drm_plane *drm_plane;
1006 uint32_t i;
1007 kplane_res = drmModeGetPlaneResources(b->drm.fd);
1008 if (!kplane_res) {
1009 weston_log("failed to get plane resources: %s\n",
1010 strerror(errno));
1011 return;
1012 }
1013
1014 for (i = 0; i < kplane_res->count_planes; i++) {
1015 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
1016 if (!kplane)
1017 continue;
1018
1019 drm_plane = drm_plane_create(b, kplane, NULL,
1020 WDRM_PLANE_TYPE__COUNT, 0);
1021 drmModeFreePlane(kplane);
1022 if (!drm_plane)
1023 continue;
1024
1025 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
1026 weston_compositor_stack_plane(b->compositor,
1027 &drm_plane->base,
1028 &b->compositor->primary_plane);
1029 }
1030
1031 drmModeFreePlaneResources(kplane_res);
1032 }
1033
1034 /**
1035 * Clean up sprites (overlay planes)
1036 *
1037 * The counterpart to create_sprites.
1038 *
1039 * @param b DRM compositor backend
1040 */
1041 static void
destroy_sprites(struct drm_backend * b)1042 destroy_sprites(struct drm_backend *b)
1043 {
1044 struct drm_plane *plane, *next;
1045
1046 wl_list_for_each_safe(plane, next, &b->plane_list, link)
1047 drm_plane_destroy(plane);
1048 }
1049
1050 /* returns a value between 0-255 range, where higher is brighter */
1051 static uint32_t
drm_get_backlight(struct drm_head * head)1052 drm_get_backlight(struct drm_head *head)
1053 {
1054 long brightness, max_brightness, norm;
1055
1056 brightness = backlight_get_brightness(head->backlight);
1057 max_brightness = backlight_get_max_brightness(head->backlight);
1058
1059 /* convert it on a scale of 0 to 255 */
1060 norm = (brightness * 255)/(max_brightness);
1061
1062 return (uint32_t) norm;
1063 }
1064
1065 /* values accepted are between 0-255 range */
1066 static void
drm_set_backlight(struct weston_output * output_base,uint32_t value)1067 drm_set_backlight(struct weston_output *output_base, uint32_t value)
1068 {
1069 struct drm_output *output = to_drm_output(output_base);
1070 struct drm_head *head;
1071 long max_brightness, new_brightness;
1072
1073 if (value > 255)
1074 return;
1075
1076 wl_list_for_each(head, &output->base.head_list, base.output_link) {
1077 if (!head->backlight)
1078 return;
1079
1080 max_brightness = backlight_get_max_brightness(head->backlight);
1081
1082 /* get denormalized value */
1083 new_brightness = (value * max_brightness) / 255;
1084
1085 backlight_set_brightness(head->backlight, new_brightness);
1086 }
1087 }
1088
1089 static void
drm_output_init_backlight(struct drm_output * output)1090 drm_output_init_backlight(struct drm_output *output)
1091 {
1092 struct weston_head *base;
1093 struct drm_head *head;
1094
1095 output->base.set_backlight = NULL;
1096
1097 wl_list_for_each(base, &output->base.head_list, output_link) {
1098 head = to_drm_head(base);
1099
1100 if (head->backlight) {
1101 weston_log("Initialized backlight for head '%s', device %s\n",
1102 head->base.name, head->backlight->path);
1103
1104 if (!output->base.set_backlight) {
1105 output->base.set_backlight = drm_set_backlight;
1106 output->base.backlight_current =
1107 drm_get_backlight(head);
1108 }
1109 }
1110 }
1111 }
1112
1113 /**
1114 * Power output on or off
1115 *
1116 * The DPMS/power level of an output is used to switch it on or off. This
1117 * is DRM's hook for doing so, which can called either as part of repaint,
1118 * or independently of the repaint loop.
1119 *
1120 * If we are called as part of repaint, we simply set the relevant bit in
1121 * state and return.
1122 *
1123 * This function is never called on a virtual output.
1124 */
1125 static void
drm_set_dpms(struct weston_output * output_base,enum dpms_enum level)1126 drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1127 {
1128 struct drm_output *output = to_drm_output(output_base);
1129 struct drm_backend *b = to_drm_backend(output_base->compositor);
1130 struct drm_pending_state *pending_state = b->repaint_data;
1131 struct drm_output_state *state;
1132 int ret;
1133
1134 assert(!output->virtual);
1135
1136 if (output->state_cur->dpms == level)
1137 return;
1138
1139 /* If we're being called during the repaint loop, then this is
1140 * simple: discard any previously-generated state, and create a new
1141 * state where we disable everything. When we come to flush, this
1142 * will be applied.
1143 *
1144 * However, we need to be careful: we can be called whilst another
1145 * output is in its repaint cycle (pending_state exists), but our
1146 * output still has an incomplete state application outstanding.
1147 * In that case, we need to wait until that completes. */
1148 if (pending_state && !output->state_last) {
1149 /* The repaint loop already sets DPMS on; we don't need to
1150 * explicitly set it on here, as it will already happen
1151 * whilst applying the repaint state. */
1152 if (level == WESTON_DPMS_ON)
1153 return;
1154
1155 state = drm_pending_state_get_output(pending_state, output);
1156 if (state)
1157 drm_output_state_free(state);
1158 state = drm_output_get_disable_state(pending_state, output);
1159 return;
1160 }
1161
1162 /* As we throw everything away when disabling, just send us back through
1163 * a repaint cycle. */
1164 if (level == WESTON_DPMS_ON) {
1165 if (output->dpms_off_pending)
1166 output->dpms_off_pending = false;
1167 weston_output_schedule_repaint(output_base);
1168 return;
1169 }
1170
1171 /* If we've already got a request in the pipeline, then we need to
1172 * park our DPMS request until that request has quiesced. */
1173 if (output->state_last) {
1174 output->dpms_off_pending = true;
1175 return;
1176 }
1177
1178 pending_state = drm_pending_state_alloc(b);
1179 drm_output_get_disable_state(pending_state, output);
1180 ret = drm_pending_state_apply_sync(pending_state);
1181 if (ret != 0)
1182 weston_log("drm_set_dpms: couldn't disable output?\n");
1183 }
1184
1185 static const char * const connector_type_names[] = {
1186 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1187 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1188 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1189 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1190 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1191 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1192 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1193 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1194 [DRM_MODE_CONNECTOR_Component] = "Component",
1195 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1196 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1197 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1198 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1199 [DRM_MODE_CONNECTOR_TV] = "TV",
1200 [DRM_MODE_CONNECTOR_eDP] = "eDP",
1201 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1202 [DRM_MODE_CONNECTOR_DSI] = "DSI",
1203 [DRM_MODE_CONNECTOR_DPI] = "DPI",
1204 };
1205
1206 /** Create a name given a DRM connector
1207 *
1208 * \param con The DRM connector whose type and id form the name.
1209 * \return A newly allocate string, or NULL on error. Must be free()'d
1210 * after use.
1211 *
1212 * The name does not identify the DRM display device.
1213 */
1214 static char *
make_connector_name(const drmModeConnector * con)1215 make_connector_name(const drmModeConnector *con)
1216 {
1217 char *name;
1218 const char *type_name = NULL;
1219 int ret;
1220
1221 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1222 type_name = connector_type_names[con->connector_type];
1223
1224 if (!type_name)
1225 type_name = "UNNAMED";
1226
1227 ret = asprintf(&name, "%s-%d", type_name, con->connector_type_id);
1228 if (ret < 0)
1229 return NULL;
1230
1231 return name;
1232 }
1233
1234 static int
drm_output_init_pixman(struct drm_output * output,struct drm_backend * b)1235 drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
1236 {
1237 int w = output->base.current_mode->width;
1238 int h = output->base.current_mode->height;
1239 uint32_t format = output->gbm_format;
1240 uint32_t pixman_format;
1241 unsigned int i;
1242 b->use_pixman_shadow = false; // OHOS
1243 const struct pixman_renderer_output_options options = {
1244 .use_shadow = b->use_pixman_shadow,
1245 };
1246
1247 switch (format) {
1248 case DRM_FORMAT_XRGB8888:
1249 pixman_format = PIXMAN_x8r8g8b8;
1250 break;
1251 case DRM_FORMAT_RGB565:
1252 pixman_format = PIXMAN_r5g6b5;
1253 break;
1254 default:
1255 weston_log("Unsupported pixman format 0x%x\n", format);
1256 return -1;
1257 }
1258
1259 /* FIXME error checking */
1260 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1261 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
1262 if (!output->dumb[i])
1263 goto err;
1264
1265 output->image[i] =
1266 pixman_image_create_bits(pixman_format, w, h,
1267 output->dumb[i]->map,
1268 output->dumb[i]->strides[0]);
1269 if (!output->image[i])
1270 goto err;
1271 }
1272
1273 if (pixman_renderer_output_create(&output->base, &options) < 0)
1274 goto err;
1275
1276 weston_log("DRM: output %s %s shadow framebuffer.\n", output->base.name,
1277 b->use_pixman_shadow ? "uses" : "does not use");
1278
1279 pixman_region32_init_rect(&output->previous_damage,
1280 output->base.x, output->base.y, output->base.width, output->base.height);
1281
1282 return 0;
1283
1284 err:
1285 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1286 if (output->dumb[i])
1287 drm_fb_unref(output->dumb[i]);
1288 if (output->image[i])
1289 pixman_image_unref(output->image[i]);
1290
1291 output->dumb[i] = NULL;
1292 output->image[i] = NULL;
1293 }
1294
1295 return -1;
1296 }
1297
1298 static void
drm_output_fini_pixman(struct drm_output * output)1299 drm_output_fini_pixman(struct drm_output *output)
1300 {
1301 struct drm_backend *b = to_drm_backend(output->base.compositor);
1302 unsigned int i;
1303
1304 /* Destroying the Pixman surface will destroy all our buffers,
1305 * regardless of refcount. Ensure we destroy them here. */
1306 if (!b->shutting_down &&
1307 output->scanout_plane->state_cur->fb &&
1308 output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
1309 drm_plane_reset_state(output->scanout_plane);
1310 }
1311
1312 pixman_renderer_output_destroy(&output->base);
1313 pixman_region32_fini(&output->previous_damage);
1314
1315 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1316 pixman_image_unref(output->image[i]);
1317 drm_fb_unref(output->dumb[i]);
1318 output->dumb[i] = NULL;
1319 output->image[i] = NULL;
1320 }
1321 }
1322
1323 static void
setup_output_seat_constraint(struct drm_backend * b,struct weston_output * output,const char * s)1324 setup_output_seat_constraint(struct drm_backend *b,
1325 struct weston_output *output,
1326 const char *s)
1327 {
1328 if (strcmp(s, "") != 0) {
1329 struct weston_pointer *pointer;
1330 struct udev_seat *seat;
1331
1332 seat = udev_seat_get_named(&b->input, s);
1333 if (!seat)
1334 return;
1335
1336 seat->base.output = output;
1337
1338 pointer = weston_seat_get_pointer(&seat->base);
1339 if (pointer)
1340 weston_pointer_clamp(pointer,
1341 &pointer->x,
1342 &pointer->y);
1343 }
1344 }
1345
1346 static int
drm_output_attach_head(struct weston_output * output_base,struct weston_head * head_base)1347 drm_output_attach_head(struct weston_output *output_base,
1348 struct weston_head *head_base)
1349 {
1350 struct drm_backend *b = to_drm_backend(output_base->compositor);
1351
1352 if (wl_list_length(&output_base->head_list) >= MAX_CLONED_CONNECTORS)
1353 return -1;
1354
1355 if (!output_base->enabled)
1356 return 0;
1357
1358 /* XXX: ensure the configuration will work.
1359 * This is actually impossible without major infrastructure
1360 * work. */
1361
1362 /* Need to go through modeset to add connectors. */
1363 /* XXX: Ideally we'd do this per-output, not globally. */
1364 /* XXX: Doing it globally, what guarantees another output's update
1365 * will not clear the flag before this output is updated?
1366 */
1367 b->state_invalid = true;
1368
1369 weston_output_schedule_repaint(output_base);
1370
1371 return 0;
1372 }
1373
1374 static void
drm_output_detach_head(struct weston_output * output_base,struct weston_head * head_base)1375 drm_output_detach_head(struct weston_output *output_base,
1376 struct weston_head *head_base)
1377 {
1378 struct drm_backend *b = to_drm_backend(output_base->compositor);
1379
1380 if (!output_base->enabled)
1381 return;
1382
1383 /* Need to go through modeset to drop connectors that should no longer
1384 * be driven. */
1385 /* XXX: Ideally we'd do this per-output, not globally. */
1386 b->state_invalid = true;
1387
1388 weston_output_schedule_repaint(output_base);
1389 }
1390
1391 int
parse_gbm_format(const char * s,uint32_t default_value,uint32_t * gbm_format)1392 parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
1393 {
1394 const struct pixel_format_info *pinfo;
1395
1396 if (s == NULL) {
1397 *gbm_format = default_value;
1398
1399 return 0;
1400 }
1401
1402 pinfo = pixel_format_get_info_by_drm_name(s);
1403 if (!pinfo) {
1404 weston_log("fatal: unrecognized pixel format: %s\n", s);
1405
1406 return -1;
1407 }
1408
1409 /* GBM formats and DRM formats are identical. */
1410 *gbm_format = pinfo->format;
1411
1412 return 0;
1413 }
1414
1415 static int
drm_head_read_current_setup(struct drm_head * head,struct drm_backend * backend)1416 drm_head_read_current_setup(struct drm_head *head, struct drm_backend *backend)
1417 {
1418 int drm_fd = backend->drm.fd;
1419 drmModeEncoder *encoder;
1420 drmModeCrtc *crtc;
1421
1422 /* Get the current mode on the crtc that's currently driving
1423 * this connector. */
1424 encoder = drmModeGetEncoder(drm_fd, head->connector->encoder_id);
1425 if (encoder != NULL) {
1426 head->inherited_crtc_id = encoder->crtc_id;
1427
1428 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
1429 drmModeFreeEncoder(encoder);
1430
1431 if (crtc == NULL)
1432 return -1;
1433 if (crtc->mode_valid)
1434 head->inherited_mode = crtc->mode;
1435 drmModeFreeCrtc(crtc);
1436 }
1437
1438 return 0;
1439 }
1440
1441 static void
drm_output_set_gbm_format(struct weston_output * base,const char * gbm_format)1442 drm_output_set_gbm_format(struct weston_output *base,
1443 const char *gbm_format)
1444 {
1445 struct drm_output *output = to_drm_output(base);
1446 struct drm_backend *b = to_drm_backend(base->compositor);
1447
1448 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
1449 output->gbm_format = b->gbm_format;
1450 }
1451
1452 static void
drm_output_set_seat(struct weston_output * base,const char * seat)1453 drm_output_set_seat(struct weston_output *base,
1454 const char *seat)
1455 {
1456 struct drm_output *output = to_drm_output(base);
1457 struct drm_backend *b = to_drm_backend(base->compositor);
1458
1459 setup_output_seat_constraint(b, &output->base,
1460 seat ? seat : "");
1461 }
1462
1463 static int
drm_output_init_gamma_size(struct drm_output * output)1464 drm_output_init_gamma_size(struct drm_output *output)
1465 {
1466 struct drm_backend *backend = to_drm_backend(output->base.compositor);
1467 drmModeCrtc *crtc;
1468
1469 assert(output->base.compositor);
1470 assert(output->crtc_id != 0);
1471 crtc = drmModeGetCrtc(backend->drm.fd, output->crtc_id);
1472 if (!crtc)
1473 return -1;
1474
1475 output->base.gamma_size = crtc->gamma_size;
1476
1477 drmModeFreeCrtc(crtc);
1478
1479 return 0;
1480 }
1481
1482 static uint32_t
drm_head_get_possible_crtcs_mask(struct drm_head * head)1483 drm_head_get_possible_crtcs_mask(struct drm_head *head)
1484 {
1485 uint32_t possible_crtcs = 0;
1486 drmModeEncoder *encoder;
1487 int i;
1488
1489 for (i = 0; i < head->connector->count_encoders; i++) {
1490 encoder = drmModeGetEncoder(head->backend->drm.fd,
1491 head->connector->encoders[i]);
1492 if (!encoder)
1493 continue;
1494
1495 possible_crtcs |= encoder->possible_crtcs;
1496 drmModeFreeEncoder(encoder);
1497 }
1498
1499 return possible_crtcs;
1500 }
1501
1502 static int
drm_crtc_get_index(drmModeRes * resources,uint32_t crtc_id)1503 drm_crtc_get_index(drmModeRes *resources, uint32_t crtc_id)
1504 {
1505 int i;
1506
1507 for (i = 0; i < resources->count_crtcs; i++) {
1508 if (resources->crtcs[i] == crtc_id)
1509 return i;
1510 }
1511
1512 assert(0 && "unknown crtc id");
1513 return -1;
1514 }
1515
1516 /** Pick a CRTC that might be able to drive all attached connectors
1517 *
1518 * @param output The output whose attached heads to include.
1519 * @param resources The DRM KMS resources.
1520 * @return CRTC index, or -1 on failure or not found.
1521 */
1522 static int
drm_output_pick_crtc(struct drm_output * output,drmModeRes * resources)1523 drm_output_pick_crtc(struct drm_output *output, drmModeRes *resources)
1524 {
1525 struct drm_backend *backend;
1526 struct weston_head *base;
1527 struct drm_head *head;
1528 uint32_t possible_crtcs = 0xffffffff;
1529 int existing_crtc[32];
1530 unsigned j, n = 0;
1531 uint32_t crtc_id;
1532 int best_crtc_index = -1;
1533 int fallback_crtc_index = -1;
1534 int i;
1535 bool match;
1536
1537 backend = to_drm_backend(output->base.compositor);
1538
1539 /* This algorithm ignores drmModeEncoder::possible_clones restriction,
1540 * because it is more often set wrong than not in the kernel. */
1541
1542 /* Accumulate a mask of possible crtcs and find existing routings. */
1543 wl_list_for_each(base, &output->base.head_list, output_link) {
1544 head = to_drm_head(base);
1545
1546 possible_crtcs &= drm_head_get_possible_crtcs_mask(head);
1547
1548 crtc_id = head->inherited_crtc_id;
1549 if (crtc_id > 0 && n < ARRAY_LENGTH(existing_crtc))
1550 existing_crtc[n++] = drm_crtc_get_index(resources,
1551 crtc_id);
1552 }
1553
1554 /* Find a crtc that could drive each connector individually at least,
1555 * and prefer existing routings. */
1556 for (i = 0; i < resources->count_crtcs; i++) {
1557 crtc_id = resources->crtcs[i];
1558
1559 /* Could the crtc not drive each connector? */
1560 if (!(possible_crtcs & (1 << i)))
1561 continue;
1562
1563 /* Is the crtc already in use? */
1564 if (drm_output_find_by_crtc(backend, crtc_id))
1565 continue;
1566
1567 /* Try to preserve the existing CRTC -> connector routing;
1568 * it makes initialisation faster, and also since we have a
1569 * very dumb picking algorithm, may preserve a better
1570 * choice. */
1571 for (j = 0; j < n; j++) {
1572 if (existing_crtc[j] == i)
1573 return i;
1574 }
1575
1576 /* Check if any other head had existing routing to this CRTC.
1577 * If they did, this is not the best CRTC as it might be needed
1578 * for another output we haven't enabled yet. */
1579 match = false;
1580 wl_list_for_each(base, &backend->compositor->head_list,
1581 compositor_link) {
1582 head = to_drm_head(base);
1583
1584 if (head->base.output == &output->base)
1585 continue;
1586
1587 if (weston_head_is_enabled(&head->base))
1588 continue;
1589
1590 if (head->inherited_crtc_id == crtc_id) {
1591 match = true;
1592 break;
1593 }
1594 }
1595 if (!match)
1596 best_crtc_index = i;
1597
1598 fallback_crtc_index = i;
1599 }
1600
1601 if (best_crtc_index != -1)
1602 return best_crtc_index;
1603
1604 if (fallback_crtc_index != -1)
1605 return fallback_crtc_index;
1606
1607 /* Likely possible_crtcs was empty due to asking for clones,
1608 * but since the DRM documentation says the kernel lies, let's
1609 * pick one crtc anyway. Trial and error is the only way to
1610 * be sure if something doesn't work. */
1611
1612 /* First pick any existing assignment. */
1613 for (j = 0; j < n; j++) {
1614 crtc_id = resources->crtcs[existing_crtc[j]];
1615 if (!drm_output_find_by_crtc(backend, crtc_id))
1616 return existing_crtc[j];
1617 }
1618
1619 /* Otherwise pick any available crtc. */
1620 for (i = 0; i < resources->count_crtcs; i++) {
1621 crtc_id = resources->crtcs[i];
1622
1623 if (!drm_output_find_by_crtc(backend, crtc_id))
1624 return i;
1625 }
1626
1627 return -1;
1628 }
1629
1630 /** Allocate a CRTC for the output
1631 *
1632 * @param output The output with no allocated CRTC.
1633 * @param resources DRM KMS resources.
1634 * @return 0 on success, -1 on failure.
1635 *
1636 * Finds a free CRTC that might drive the attached connectors, reserves the CRTC
1637 * for the output, and loads the CRTC properties.
1638 *
1639 * Populates the cursor and scanout planes.
1640 *
1641 * On failure, the output remains without a CRTC.
1642 */
1643 static int
drm_output_init_crtc(struct drm_output * output,drmModeRes * resources)1644 drm_output_init_crtc(struct drm_output *output, drmModeRes *resources)
1645 {
1646 struct drm_backend *b = to_drm_backend(output->base.compositor);
1647 drmModeObjectPropertiesPtr props;
1648 int i;
1649
1650 assert(output->crtc_id == 0);
1651
1652 i = drm_output_pick_crtc(output, resources);
1653 if (i < 0) {
1654 weston_log("Output '%s': No available CRTCs.\n",
1655 output->base.name);
1656 return -1;
1657 }
1658
1659 output->crtc_id = resources->crtcs[i];
1660 output->pipe = i;
1661 b->pipe = i; // OHOS vsync module
1662
1663 props = drmModeObjectGetProperties(b->drm.fd, output->crtc_id,
1664 DRM_MODE_OBJECT_CRTC);
1665 if (!props) {
1666 weston_log("failed to get CRTC properties\n");
1667 goto err_crtc;
1668 }
1669 drm_property_info_populate(b, crtc_props, output->props_crtc,
1670 WDRM_CRTC__COUNT, props);
1671 drmModeFreeObjectProperties(props);
1672
1673 output->scanout_plane =
1674 drm_output_find_special_plane(b, output,
1675 WDRM_PLANE_TYPE_PRIMARY);
1676 if (!output->scanout_plane) {
1677 weston_log("Failed to find primary plane for output %s\n",
1678 output->base.name);
1679 goto err_crtc;
1680 }
1681
1682 /* Without universal planes, we can't discover which formats are
1683 * supported by the primary plane; we just hope that the GBM format
1684 * works. */
1685 if (!b->universal_planes)
1686 output->scanout_plane->formats[0].format = output->gbm_format;
1687
1688 /* Failing to find a cursor plane is not fatal, as we'll fall back
1689 * to software cursor. */
1690 output->cursor_plane =
1691 drm_output_find_special_plane(b, output,
1692 WDRM_PLANE_TYPE_CURSOR);
1693
1694 wl_array_remove_uint32(&b->unused_crtcs, output->crtc_id);
1695
1696 return 0;
1697
1698 err_crtc:
1699 output->crtc_id = 0;
1700 output->pipe = 0;
1701
1702 return -1;
1703 }
1704
1705 /** Free the CRTC from the output
1706 *
1707 * @param output The output whose CRTC to deallocate.
1708 *
1709 * The CRTC reserved for the given output becomes free to use again.
1710 */
1711 static void
drm_output_fini_crtc(struct drm_output * output)1712 drm_output_fini_crtc(struct drm_output *output)
1713 {
1714 struct drm_backend *b = to_drm_backend(output->base.compositor);
1715 uint32_t *unused;
1716
1717 /* If the compositor is already shutting down, the planes have already
1718 * been destroyed. */
1719 if (!b->shutting_down) {
1720 if (!b->universal_planes) {
1721 /* Without universal planes, our special planes are
1722 * pseudo-planes allocated at output creation, freed at
1723 * output destruction, and not usable by other outputs.
1724 */
1725 if (output->cursor_plane)
1726 drm_plane_destroy(output->cursor_plane);
1727 if (output->scanout_plane)
1728 drm_plane_destroy(output->scanout_plane);
1729 } else {
1730 /* With universal planes, the 'special' planes are
1731 * allocated at startup, freed at shutdown, and live on
1732 * the plane list in between. We want the planes to
1733 * continue to exist and be freed up for other outputs.
1734 */
1735 if (output->cursor_plane)
1736 drm_plane_reset_state(output->cursor_plane);
1737 if (output->scanout_plane)
1738 drm_plane_reset_state(output->scanout_plane);
1739 }
1740 }
1741
1742 drm_property_info_free(output->props_crtc, WDRM_CRTC__COUNT);
1743
1744 assert(output->crtc_id != 0);
1745
1746 unused = wl_array_add(&b->unused_crtcs, sizeof(*unused));
1747 *unused = output->crtc_id;
1748
1749 /* Force resetting unused CRTCs */
1750 b->state_invalid = true;
1751
1752 output->crtc_id = 0;
1753 output->cursor_plane = NULL;
1754 output->scanout_plane = NULL;
1755 }
1756
1757 static int
drm_output_enable(struct weston_output * base)1758 drm_output_enable(struct weston_output *base)
1759 {
1760 struct drm_output *output = to_drm_output(base);
1761 struct drm_backend *b = to_drm_backend(base->compositor);
1762 drmModeRes *resources;
1763 int ret;
1764
1765 assert(!output->virtual);
1766
1767 resources = drmModeGetResources(b->drm.fd);
1768 if (!resources) {
1769 weston_log("drmModeGetResources failed\n");
1770 return -1;
1771 }
1772 ret = drm_output_init_crtc(output, resources);
1773 drmModeFreeResources(resources);
1774 if (ret < 0)
1775 return -1;
1776
1777 if (drm_output_init_gamma_size(output) < 0)
1778 goto err;
1779
1780 if (b->pageflip_timeout)
1781 drm_output_pageflip_timer_create(output);
1782
1783 if (b->use_pixman) {
1784 if (drm_output_init_pixman(output, b) < 0) {
1785 weston_log("Failed to init output pixman state\n");
1786 goto err;
1787 }
1788 } else if (drm_output_init_egl(output, b) < 0) {
1789 weston_log("Failed to init output gl state\n");
1790 goto err;
1791 }
1792
1793 drm_output_init_backlight(output);
1794
1795 output->base.start_repaint_loop = drm_output_start_repaint_loop;
1796 output->base.repaint = drm_output_repaint;
1797 output->base.assign_planes = drm_assign_planes;
1798 output->base.set_dpms = drm_set_dpms;
1799 output->base.switch_mode = drm_output_switch_mode;
1800 output->base.set_gamma = drm_output_set_gamma;
1801
1802 if (output->cursor_plane)
1803 weston_compositor_stack_plane(b->compositor,
1804 &output->cursor_plane->base,
1805 NULL);
1806 else
1807 b->cursors_are_broken = true;
1808
1809 weston_compositor_stack_plane(b->compositor,
1810 &output->scanout_plane->base,
1811 &b->compositor->primary_plane);
1812
1813 weston_log("Output %s (crtc %d) video modes:\n",
1814 output->base.name, output->crtc_id);
1815 drm_output_print_modes(output);
1816
1817 return 0;
1818
1819 err:
1820 drm_output_fini_crtc(output);
1821
1822 return -1;
1823 }
1824
1825 static void
drm_output_deinit(struct weston_output * base)1826 drm_output_deinit(struct weston_output *base)
1827 {
1828 struct drm_output *output = to_drm_output(base);
1829 struct drm_backend *b = to_drm_backend(base->compositor);
1830
1831 if (b->use_pixman)
1832 drm_output_fini_pixman(output);
1833 else
1834 drm_output_fini_egl(output);
1835
1836 /* Since our planes are no longer in use anywhere, remove their base
1837 * weston_plane's link from the plane stacking list, unless we're
1838 * shutting down, in which case the plane has already been
1839 * destroyed. */
1840 if (!b->shutting_down) {
1841 wl_list_remove(&output->scanout_plane->base.link);
1842 wl_list_init(&output->scanout_plane->base.link);
1843
1844 if (output->cursor_plane) {
1845 wl_list_remove(&output->cursor_plane->base.link);
1846 wl_list_init(&output->cursor_plane->base.link);
1847 /* Turn off hardware cursor */
1848 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
1849 }
1850 }
1851
1852 drm_output_fini_crtc(output);
1853 }
1854
1855 static void
1856 drm_head_destroy(struct drm_head *head);
1857
1858 static void
drm_output_destroy(struct weston_output * base)1859 drm_output_destroy(struct weston_output *base)
1860 {
1861 struct drm_output *output = to_drm_output(base);
1862 struct drm_backend *b = to_drm_backend(base->compositor);
1863
1864 assert(!output->virtual);
1865
1866 if (output->page_flip_pending || output->atomic_complete_pending) {
1867 output->destroy_pending = true;
1868 weston_log("destroy output while page flip pending\n");
1869 return;
1870 }
1871
1872 if (output->base.enabled)
1873 drm_output_deinit(&output->base);
1874
1875 drm_mode_list_destroy(b, &output->base.mode_list);
1876
1877 if (output->pageflip_timer)
1878 wl_event_source_remove(output->pageflip_timer);
1879
1880 weston_output_release(&output->base);
1881
1882 assert(!output->state_last);
1883 drm_output_state_free(output->state_cur);
1884
1885 free(output);
1886 }
1887
1888 static int
drm_output_disable(struct weston_output * base)1889 drm_output_disable(struct weston_output *base)
1890 {
1891 struct drm_output *output = to_drm_output(base);
1892
1893 assert(!output->virtual);
1894
1895 if (output->page_flip_pending || output->atomic_complete_pending) {
1896 output->disable_pending = true;
1897 return -1;
1898 }
1899
1900 weston_log("Disabling output %s\n", output->base.name);
1901
1902 if (output->base.enabled)
1903 drm_output_deinit(&output->base);
1904
1905 output->disable_pending = false;
1906
1907 return 0;
1908 }
1909
1910 /**
1911 * Update the list of unused connectors and CRTCs
1912 *
1913 * This keeps the unused_crtc arrays up to date.
1914 *
1915 * @param b Weston backend structure
1916 * @param resources DRM resources for this device
1917 */
1918 static void
drm_backend_update_unused_outputs(struct drm_backend * b,drmModeRes * resources)1919 drm_backend_update_unused_outputs(struct drm_backend *b, drmModeRes *resources)
1920 {
1921 int i;
1922
1923 wl_array_release(&b->unused_crtcs);
1924 wl_array_init(&b->unused_crtcs);
1925
1926 for (i = 0; i < resources->count_crtcs; i++) {
1927 struct drm_output *output;
1928 uint32_t *crtc_id;
1929
1930 output = drm_output_find_by_crtc(b, resources->crtcs[i]);
1931 if (output && output->base.enabled)
1932 continue;
1933
1934 crtc_id = wl_array_add(&b->unused_crtcs, sizeof(*crtc_id));
1935 *crtc_id = resources->crtcs[i];
1936 }
1937 }
1938
1939 /*
1940 * This function converts the protection status from drm values to
1941 * weston_hdcp_protection status. The drm values as read from the connector
1942 * properties "Content Protection" and "HDCP Content Type" need to be converted
1943 * to appropriate weston values, that can be sent to a client application.
1944 */
1945 static int
get_weston_protection_from_drm(enum wdrm_content_protection_state protection,enum wdrm_hdcp_content_type type,enum weston_hdcp_protection * weston_protection)1946 get_weston_protection_from_drm(enum wdrm_content_protection_state protection,
1947 enum wdrm_hdcp_content_type type,
1948 enum weston_hdcp_protection *weston_protection)
1949
1950 {
1951 if (protection >= WDRM_CONTENT_PROTECTION__COUNT)
1952 return -1;
1953 if (protection == WDRM_CONTENT_PROTECTION_DESIRED ||
1954 protection == WDRM_CONTENT_PROTECTION_UNDESIRED) {
1955 *weston_protection = WESTON_HDCP_DISABLE;
1956 return 0;
1957 }
1958 if (type >= WDRM_HDCP_CONTENT_TYPE__COUNT)
1959 return -1;
1960 if (type == WDRM_HDCP_CONTENT_TYPE0) {
1961 *weston_protection = WESTON_HDCP_ENABLE_TYPE_0;
1962 return 0;
1963 }
1964 if (type == WDRM_HDCP_CONTENT_TYPE1) {
1965 *weston_protection = WESTON_HDCP_ENABLE_TYPE_1;
1966 return 0;
1967 }
1968 return -1;
1969 }
1970
1971 /**
1972 * Get current content-protection status for a given head.
1973 *
1974 * @param head drm_head, whose protection is to be retrieved
1975 * @param props drm property object of the connector, related to the head
1976 * @return protection status in case of success, -1 otherwise
1977 */
1978 static enum weston_hdcp_protection
drm_head_get_current_protection(struct drm_head * head,drmModeObjectProperties * props)1979 drm_head_get_current_protection(struct drm_head *head,
1980 drmModeObjectProperties *props)
1981 {
1982 struct drm_property_info *info;
1983 enum wdrm_content_protection_state protection;
1984 enum wdrm_hdcp_content_type type;
1985 enum weston_hdcp_protection weston_hdcp = WESTON_HDCP_DISABLE;
1986
1987 info = &head->props_conn[WDRM_CONNECTOR_CONTENT_PROTECTION];
1988 protection = drm_property_get_value(info, props,
1989 WDRM_CONTENT_PROTECTION__COUNT);
1990
1991 if (protection == WDRM_CONTENT_PROTECTION__COUNT)
1992 return WESTON_HDCP_DISABLE;
1993
1994 info = &head->props_conn[WDRM_CONNECTOR_HDCP_CONTENT_TYPE];
1995 type = drm_property_get_value(info, props,
1996 WDRM_HDCP_CONTENT_TYPE__COUNT);
1997
1998 /*
1999 * In case of platforms supporting HDCP1.4, only property
2000 * 'Content Protection' is exposed and not the 'HDCP Content Type'
2001 * for such cases HDCP Type 0 should be considered as the content-type.
2002 */
2003
2004 if (type == WDRM_HDCP_CONTENT_TYPE__COUNT)
2005 type = WDRM_HDCP_CONTENT_TYPE0;
2006
2007 if (get_weston_protection_from_drm(protection, type,
2008 &weston_hdcp) == -1) {
2009 weston_log("Invalid drm protection:%d type:%d, for head:%s connector-id:%d\n",
2010 protection, type, head->base.name,
2011 head->connector_id);
2012 return WESTON_HDCP_DISABLE;
2013 }
2014
2015 return weston_hdcp;
2016 }
2017
2018 /** Replace connector data and monitor information
2019 *
2020 * @param head The head to update.
2021 * @param connector The connector data to be owned by the head, must match
2022 * the head's connector ID.
2023 * @return 0 on success, -1 on failure.
2024 *
2025 * Takes ownership of @c connector on success, not on failure.
2026 *
2027 * May schedule a heads changed call.
2028 */
2029 static int
drm_head_assign_connector_info(struct drm_head * head,drmModeConnector * connector)2030 drm_head_assign_connector_info(struct drm_head *head,
2031 drmModeConnector *connector)
2032 {
2033 drmModeObjectProperties *props;
2034
2035 assert(connector);
2036 assert(head->connector_id == connector->connector_id);
2037
2038 props = drmModeObjectGetProperties(head->backend->drm.fd,
2039 head->connector_id,
2040 DRM_MODE_OBJECT_CONNECTOR);
2041 if (!props) {
2042 weston_log("Error: failed to get connector '%s' properties\n",
2043 head->base.name);
2044 return -1;
2045 }
2046
2047 if (head->connector)
2048 drmModeFreeConnector(head->connector);
2049 head->connector = connector;
2050
2051 drm_property_info_populate(head->backend, connector_props,
2052 head->props_conn,
2053 WDRM_CONNECTOR__COUNT, props);
2054 update_head_from_connector(head, props);
2055
2056 weston_head_set_content_protection_status(&head->base,
2057 drm_head_get_current_protection(head, props));
2058 drmModeFreeObjectProperties(props);
2059
2060 return 0;
2061 }
2062
2063 static void
drm_head_log_info(struct drm_head * head,const char * msg)2064 drm_head_log_info(struct drm_head *head, const char *msg)
2065 {
2066 if (head->base.connected) {
2067 weston_log("DRM: head '%s' %s, connector %d is connected, "
2068 "EDID make '%s', model '%s', serial '%s'\n",
2069 head->base.name, msg, head->connector_id,
2070 head->base.make, head->base.model,
2071 head->base.serial_number ?: "");
2072 } else {
2073 weston_log("DRM: head '%s' %s, connector %d is disconnected.\n",
2074 head->base.name, msg, head->connector_id);
2075 }
2076 }
2077
2078 /** Update connector and monitor information
2079 *
2080 * @param head The head to update.
2081 *
2082 * Re-reads the DRM property lists for the connector and updates monitor
2083 * information and connection status. This may schedule a heads changed call
2084 * to the user.
2085 */
2086 static void
drm_head_update_info(struct drm_head * head)2087 drm_head_update_info(struct drm_head *head)
2088 {
2089 drmModeConnector *connector;
2090
2091 connector = drmModeGetConnector(head->backend->drm.fd,
2092 head->connector_id);
2093 if (!connector) {
2094 weston_log("DRM: getting connector info for '%s' failed.\n",
2095 head->base.name);
2096 return;
2097 }
2098
2099 if (drm_head_assign_connector_info(head, connector) < 0)
2100 drmModeFreeConnector(connector);
2101
2102 if (head->base.device_changed)
2103 drm_head_log_info(head, "updated");
2104 }
2105
2106 /**
2107 * Create a Weston head for a connector
2108 *
2109 * Given a DRM connector, create a matching drm_head structure and add it
2110 * to Weston's head list.
2111 *
2112 * @param backend Weston backend structure
2113 * @param connector_id DRM connector ID for the head
2114 * @param drm_device udev device pointer
2115 * @returns The new head, or NULL on failure.
2116 */
2117 static struct drm_head *
drm_head_create(struct drm_backend * backend,uint32_t connector_id,struct udev_device * drm_device)2118 drm_head_create(struct drm_backend *backend, uint32_t connector_id,
2119 struct udev_device *drm_device)
2120 {
2121 struct drm_head *head;
2122 drmModeConnector *connector;
2123 char *name;
2124
2125 head = zalloc(sizeof *head);
2126 if (!head)
2127 return NULL;
2128
2129 connector = drmModeGetConnector(backend->drm.fd, connector_id);
2130 if (!connector)
2131 goto err_alloc;
2132
2133 name = make_connector_name(connector);
2134 if (!name)
2135 goto err_alloc;
2136
2137 weston_head_init(&head->base, name);
2138 free(name);
2139
2140 head->connector_id = connector_id;
2141 head->backend = backend;
2142
2143 head->backlight = backlight_init(drm_device, connector->connector_type);
2144
2145 if (drm_head_assign_connector_info(head, connector) < 0)
2146 goto err_init;
2147
2148 if (head->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2149 head->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
2150 weston_head_set_internal(&head->base);
2151
2152 if (drm_head_read_current_setup(head, backend) < 0) {
2153 weston_log("Failed to retrieve current mode from connector %d.\n",
2154 head->connector_id);
2155 /* Not fatal. */
2156 }
2157
2158 weston_compositor_add_head(backend->compositor, &head->base);
2159 drm_head_log_info(head, "found");
2160
2161 return head;
2162
2163 err_init:
2164 weston_head_release(&head->base);
2165
2166 err_alloc:
2167 if (connector)
2168 drmModeFreeConnector(connector);
2169
2170 free(head);
2171
2172 return NULL;
2173 }
2174
2175 static void
drm_head_destroy(struct drm_head * head)2176 drm_head_destroy(struct drm_head *head)
2177 {
2178 weston_head_release(&head->base);
2179
2180 drm_property_info_free(head->props_conn, WDRM_CONNECTOR__COUNT);
2181 drmModeFreeConnector(head->connector);
2182
2183 if (head->backlight)
2184 backlight_destroy(head->backlight);
2185
2186 free(head);
2187 }
2188
2189 /**
2190 * Create a Weston output structure
2191 *
2192 * Create an "empty" drm_output. This is the implementation of
2193 * weston_backend::create_output.
2194 *
2195 * Creating an output is usually followed by drm_output_attach_head()
2196 * and drm_output_enable() to make use of it.
2197 *
2198 * @param compositor The compositor instance.
2199 * @param name Name for the new output.
2200 * @returns The output, or NULL on failure.
2201 */
2202 static struct weston_output *
drm_output_create(struct weston_compositor * compositor,const char * name)2203 drm_output_create(struct weston_compositor *compositor, const char *name)
2204 {
2205 struct drm_backend *b = to_drm_backend(compositor);
2206 struct drm_output *output;
2207
2208 output = zalloc(sizeof *output);
2209 if (output == NULL)
2210 return NULL;
2211
2212 output->backend = b;
2213 #ifdef BUILD_DRM_GBM
2214 output->gbm_bo_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
2215 #endif
2216
2217 weston_output_init(&output->base, compositor, name);
2218
2219 output->base.enable = drm_output_enable;
2220 output->base.destroy = drm_output_destroy;
2221 output->base.disable = drm_output_disable;
2222 output->base.attach_head = drm_output_attach_head;
2223 output->base.detach_head = drm_output_detach_head;
2224
2225 output->destroy_pending = false;
2226 output->disable_pending = false;
2227
2228 output->state_cur = drm_output_state_alloc(output, NULL);
2229
2230 weston_compositor_add_pending_output(&output->base, b->compositor);
2231
2232 return &output->base;
2233 }
2234
2235 static int
drm_backend_create_heads(struct drm_backend * b,struct udev_device * drm_device)2236 drm_backend_create_heads(struct drm_backend *b, struct udev_device *drm_device)
2237 {
2238 struct drm_head *head;
2239 drmModeRes *resources;
2240 int i;
2241
2242 resources = drmModeGetResources(b->drm.fd);
2243 if (!resources) {
2244 weston_log("drmModeGetResources failed\n");
2245 return -1;
2246 }
2247
2248 b->min_width = resources->min_width;
2249 b->max_width = resources->max_width;
2250 b->min_height = resources->min_height;
2251 b->max_height = resources->max_height;
2252
2253 for (i = 0; i < resources->count_connectors; i++) {
2254 uint32_t connector_id = resources->connectors[i];
2255
2256 head = drm_head_create(b, connector_id, drm_device);
2257 if (!head) {
2258 weston_log("DRM: failed to create head for connector %d.\n",
2259 connector_id);
2260 }
2261 }
2262
2263 drm_backend_update_unused_outputs(b, resources);
2264
2265 drmModeFreeResources(resources);
2266
2267 return 0;
2268 }
2269
2270 static void
drm_backend_update_heads(struct drm_backend * b,struct udev_device * drm_device)2271 drm_backend_update_heads(struct drm_backend *b, struct udev_device *drm_device)
2272 {
2273 drmModeRes *resources;
2274 struct weston_head *base, *next;
2275 struct drm_head *head;
2276 int i;
2277
2278 resources = drmModeGetResources(b->drm.fd);
2279 if (!resources) {
2280 weston_log("drmModeGetResources failed\n");
2281 return;
2282 }
2283
2284 /* collect new connectors that have appeared, e.g. MST */
2285 for (i = 0; i < resources->count_connectors; i++) {
2286 uint32_t connector_id = resources->connectors[i];
2287
2288 head = drm_head_find_by_connector(b, connector_id);
2289 if (head) {
2290 drm_head_update_info(head);
2291 } else {
2292 head = drm_head_create(b, connector_id, drm_device);
2293 if (!head)
2294 weston_log("DRM: failed to create head for hot-added connector %d.\n",
2295 connector_id);
2296 }
2297 }
2298
2299 /* Remove connectors that have disappeared. */
2300 wl_list_for_each_safe(base, next,
2301 &b->compositor->head_list, compositor_link) {
2302 bool removed = true;
2303
2304 head = to_drm_head(base);
2305
2306 for (i = 0; i < resources->count_connectors; i++) {
2307 if (resources->connectors[i] == head->connector_id) {
2308 removed = false;
2309 break;
2310 }
2311 }
2312
2313 if (!removed)
2314 continue;
2315
2316 weston_log("DRM: head '%s' (connector %d) disappeared.\n",
2317 head->base.name, head->connector_id);
2318 drm_head_destroy(head);
2319 }
2320
2321 drm_backend_update_unused_outputs(b, resources);
2322
2323 drmModeFreeResources(resources);
2324 }
2325
2326 static enum wdrm_connector_property
drm_head_find_property_by_id(struct drm_head * head,uint32_t property_id)2327 drm_head_find_property_by_id(struct drm_head *head, uint32_t property_id)
2328 {
2329 int i;
2330 enum wdrm_connector_property prop = WDRM_CONNECTOR__COUNT;
2331
2332 if (!head || !property_id)
2333 return WDRM_CONNECTOR__COUNT;
2334
2335 for (i = 0; i < WDRM_CONNECTOR__COUNT; i++)
2336 if (head->props_conn[i].prop_id == property_id) {
2337 prop = (enum wdrm_connector_property) i;
2338 break;
2339 }
2340 return prop;
2341 }
2342
2343 static void
drm_backend_update_conn_props(struct drm_backend * b,uint32_t connector_id,uint32_t property_id)2344 drm_backend_update_conn_props(struct drm_backend *b,
2345 uint32_t connector_id,
2346 uint32_t property_id)
2347 {
2348 struct drm_head *head;
2349 enum wdrm_connector_property conn_prop;
2350 drmModeObjectProperties *props;
2351
2352 head = drm_head_find_by_connector(b, connector_id);
2353 if (!head) {
2354 weston_log("DRM: failed to find head for connector id: %d.\n",
2355 connector_id);
2356 return;
2357 }
2358
2359 conn_prop = drm_head_find_property_by_id(head, property_id);
2360 if (conn_prop >= WDRM_CONNECTOR__COUNT)
2361 return;
2362
2363 props = drmModeObjectGetProperties(b->drm.fd,
2364 connector_id,
2365 DRM_MODE_OBJECT_CONNECTOR);
2366 if (!props) {
2367 weston_log("Error: failed to get connector '%s' properties\n",
2368 head->base.name);
2369 return;
2370 }
2371 if (conn_prop == WDRM_CONNECTOR_CONTENT_PROTECTION) {
2372 weston_head_set_content_protection_status(&head->base,
2373 drm_head_get_current_protection(head, props));
2374 }
2375 drmModeFreeObjectProperties(props);
2376 }
2377
2378 static int
udev_event_is_hotplug(struct drm_backend * b,struct udev_device * device)2379 udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
2380 {
2381 const char *sysnum;
2382 const char *val;
2383
2384 sysnum = udev_device_get_sysnum(device);
2385 if (!sysnum || atoi(sysnum) != b->drm.id)
2386 return 0;
2387
2388 val = udev_device_get_property_value(device, "HOTPLUG");
2389 if (!val)
2390 return 0;
2391
2392 return strcmp(val, "1") == 0;
2393 }
2394
2395 static int
udev_event_is_conn_prop_change(struct drm_backend * b,struct udev_device * device,uint32_t * connector_id,uint32_t * property_id)2396 udev_event_is_conn_prop_change(struct drm_backend *b,
2397 struct udev_device *device,
2398 uint32_t *connector_id,
2399 uint32_t *property_id)
2400
2401 {
2402 const char *val;
2403 int id;
2404
2405 val = udev_device_get_property_value(device, "CONNECTOR");
2406 if (!val || !safe_strtoint(val, &id))
2407 return 0;
2408 else
2409 *connector_id = id;
2410
2411 val = udev_device_get_property_value(device, "PROPERTY");
2412 if (!val || !safe_strtoint(val, &id))
2413 return 0;
2414 else
2415 *property_id = id;
2416
2417 return 1;
2418 }
2419
2420 static int
udev_drm_event(int fd,uint32_t mask,void * data)2421 udev_drm_event(int fd, uint32_t mask, void *data)
2422 {
2423 struct drm_backend *b = data;
2424 struct udev_device *event;
2425 uint32_t conn_id, prop_id;
2426
2427 event = udev_monitor_receive_device(b->udev_monitor);
2428
2429 if (udev_event_is_hotplug(b, event)) {
2430 if (udev_event_is_conn_prop_change(b, event, &conn_id, &prop_id))
2431 drm_backend_update_conn_props(b, conn_id, prop_id);
2432 else
2433 drm_backend_update_heads(b, event);
2434 }
2435
2436 udev_device_unref(event);
2437
2438 return 1;
2439 }
2440
2441 static void
drm_destroy(struct weston_compositor * ec)2442 drm_destroy(struct weston_compositor *ec)
2443 {
2444 struct drm_backend *b = to_drm_backend(ec);
2445 struct weston_head *base, *next;
2446
2447 udev_input_destroy(&b->input);
2448
2449 wl_event_source_remove(b->udev_drm_source);
2450 wl_event_source_remove(b->drm_source);
2451
2452 b->shutting_down = true;
2453
2454 destroy_sprites(b);
2455
2456 // OHOS remove logger
2457 // weston_log_scope_destroy(b->debug);
2458 b->debug = NULL;
2459 weston_compositor_shutdown(ec);
2460
2461 wl_list_for_each_safe(base, next, &ec->head_list, compositor_link)
2462 drm_head_destroy(to_drm_head(base));
2463
2464 #ifdef BUILD_DRM_GBM
2465 if (b->gbm)
2466 gbm_device_destroy(b->gbm);
2467 #endif
2468
2469 udev_monitor_unref(b->udev_monitor);
2470 udev_unref(b->udev);
2471
2472 weston_launcher_destroy(ec->launcher);
2473
2474 wl_array_release(&b->unused_crtcs);
2475
2476 DeInitWaylandDrmAuthService(); // OHOS drm auth
2477
2478 // OHOS vsync module
2479 b->vsync_thread_running = false;
2480 pthread_join(b->vsync_thread, NULL);
2481
2482 close(b->drm.fd);
2483 free(b->drm.filename);
2484 free(b);
2485 }
2486
2487 static void
session_notify(struct wl_listener * listener,void * data)2488 session_notify(struct wl_listener *listener, void *data)
2489 {
2490 struct weston_compositor *compositor = data;
2491 struct drm_backend *b = to_drm_backend(compositor);
2492 struct drm_plane *plane;
2493 struct drm_output *output;
2494
2495 if (compositor->session_active) {
2496 weston_log("activating session\n");
2497 weston_compositor_wake(compositor);
2498 weston_compositor_damage_all(compositor);
2499 b->state_invalid = true;
2500 udev_input_enable(&b->input);
2501 } else {
2502 weston_log("deactivating session\n");
2503 udev_input_disable(&b->input);
2504
2505 weston_compositor_offscreen(compositor);
2506
2507 /* If we have a repaint scheduled (either from a
2508 * pending pageflip or the idle handler), make sure we
2509 * cancel that so we don't try to pageflip when we're
2510 * vt switched away. The OFFSCREEN state will prevent
2511 * further attempts at repainting. When we switch
2512 * back, we schedule a repaint, which will process
2513 * pending frame callbacks. */
2514
2515 wl_list_for_each(output, &compositor->output_list, base.link) {
2516 output->base.repaint_needed = false;
2517 if (output->cursor_plane)
2518 drmModeSetCursor(b->drm.fd, output->crtc_id,
2519 0, 0, 0);
2520 }
2521
2522 output = container_of(compositor->output_list.next,
2523 struct drm_output, base.link);
2524
2525 wl_list_for_each(plane, &b->plane_list, link) {
2526 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
2527 continue;
2528
2529 drmModeSetPlane(b->drm.fd,
2530 plane->plane_id,
2531 output->crtc_id, 0, 0,
2532 0, 0, 0, 0, 0, 0, 0, 0);
2533 }
2534 }
2535 }
2536
2537
2538 /**
2539 * Handle KMS GPU being added/removed
2540 *
2541 * If the device being added/removed is the KMS device, we activate/deactivate
2542 * the compositor session.
2543 *
2544 * @param compositor The compositor instance.
2545 * @param device The device being added/removed.
2546 * @param added Whether the device is being added (or removed)
2547 */
2548 static void
drm_device_changed(struct weston_compositor * compositor,dev_t device,bool added)2549 drm_device_changed(struct weston_compositor *compositor,
2550 dev_t device, bool added)
2551 {
2552 struct drm_backend *b = to_drm_backend(compositor);
2553
2554 if (b->drm.fd < 0 || b->drm.devnum != device ||
2555 compositor->session_active == added)
2556 return;
2557
2558 compositor->session_active = added;
2559 wl_signal_emit(&compositor->session_signal, compositor);
2560 }
2561
2562 /**
2563 * Determines whether or not a device is capable of modesetting. If successful,
2564 * sets b->drm.fd and b->drm.filename to the opened device.
2565 */
2566 static bool
drm_device_is_kms(struct drm_backend * b,struct udev_device * device)2567 drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
2568 {
2569 const char *filename = udev_device_get_devnode(device);
2570 const char *sysnum = udev_device_get_sysnum(device);
2571 dev_t devnum = udev_device_get_devnum(device);
2572 drmModeRes *res;
2573 int id = -1, fd;
2574
2575 if (!filename)
2576 return false;
2577
2578 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR | O_CLOEXEC); // OHOS
2579 if (fd < 0)
2580 return false;
2581
2582 res = drmModeGetResources(fd);
2583 if (!res)
2584 goto out_fd;
2585
2586 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
2587 res->count_encoders <= 0)
2588 goto out_res;
2589
2590 if (sysnum)
2591 id = atoi(sysnum);
2592 if (!sysnum || id < 0) {
2593 weston_log("couldn't get sysnum for device %s\n", filename);
2594 goto out_res;
2595 }
2596
2597 /* We can be called successfully on multiple devices; if we have,
2598 * clean up old entries. */
2599 if (b->drm.fd >= 0)
2600 weston_launcher_close(b->compositor->launcher, b->drm.fd);
2601 free(b->drm.filename);
2602
2603 b->drm.fd = fd;
2604 b->drm.id = id;
2605 b->drm.filename = strdup(filename);
2606 b->drm.devnum = devnum;
2607
2608 drmModeFreeResources(res);
2609
2610 return true;
2611
2612 out_res:
2613 drmModeFreeResources(res);
2614 out_fd:
2615 weston_launcher_close(b->compositor->launcher, fd);
2616 return false;
2617 }
2618
2619 /*
2620 * Find primary GPU
2621 * Some systems may have multiple DRM devices attached to a single seat. This
2622 * function loops over all devices and tries to find a PCI device with the
2623 * boot_vga sysfs attribute set to 1.
2624 * If no such device is found, the first DRM device reported by udev is used.
2625 * Devices are also vetted to make sure they are are capable of modesetting,
2626 * rather than pure render nodes (GPU with no display), or pure
2627 * memory-allocation devices (VGEM).
2628 */
2629 static struct udev_device*
find_primary_gpu(struct drm_backend * b,const char * seat)2630 find_primary_gpu(struct drm_backend *b, const char *seat)
2631 {
2632 struct udev_enumerate *e;
2633 struct udev_list_entry *entry;
2634 const char *path, *device_seat, *id;
2635 struct udev_device *device, *drm_device, *pci;
2636
2637 e = udev_enumerate_new(b->udev);
2638 udev_enumerate_add_match_subsystem(e, "drm");
2639 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2640
2641 udev_enumerate_scan_devices(e);
2642 drm_device = NULL;
2643 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2644 bool is_boot_vga = false;
2645
2646 path = udev_list_entry_get_name(entry);
2647 device = udev_device_new_from_syspath(b->udev, path);
2648 if (!device)
2649 continue;
2650 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2651 if (!device_seat)
2652 device_seat = default_seat;
2653 if (strcmp(device_seat, seat)) {
2654 udev_device_unref(device);
2655 continue;
2656 }
2657
2658 pci = udev_device_get_parent_with_subsystem_devtype(device,
2659 "pci", NULL);
2660 if (pci) {
2661 id = udev_device_get_sysattr_value(pci, "boot_vga");
2662 if (id && !strcmp(id, "1"))
2663 is_boot_vga = true;
2664 }
2665
2666 /* If we already have a modesetting-capable device, and this
2667 * device isn't our boot-VGA device, we aren't going to use
2668 * it. */
2669 if (!is_boot_vga && drm_device) {
2670 udev_device_unref(device);
2671 continue;
2672 }
2673
2674 /* Make sure this device is actually capable of modesetting;
2675 * if this call succeeds, b->drm.{fd,filename} will be set,
2676 * and any old values freed. */
2677 if (!drm_device_is_kms(b, device)) {
2678 udev_device_unref(device);
2679 continue;
2680 }
2681
2682 /* There can only be one boot_vga device, and we try to use it
2683 * at all costs. */
2684 if (is_boot_vga) {
2685 if (drm_device)
2686 udev_device_unref(drm_device);
2687 drm_device = device;
2688 break;
2689 }
2690
2691 /* Per the (!is_boot_vga && drm_device) test above, we only
2692 * trump existing saved devices with boot-VGA devices, so if
2693 * we end up here, this must be the first device we've seen. */
2694 assert(!drm_device);
2695 drm_device = device;
2696 }
2697
2698 /* If we're returning a device to use, we must have an open FD for
2699 * it. */
2700 assert(!!drm_device == (b->drm.fd >= 0));
2701
2702 udev_enumerate_unref(e);
2703 return drm_device;
2704 }
2705
2706 static struct udev_device *
open_specific_drm_device(struct drm_backend * b,const char * name)2707 open_specific_drm_device(struct drm_backend *b, const char *name)
2708 {
2709 struct udev_device *device;
2710
2711 device = udev_device_new_from_subsystem_sysname(b->udev, "drm", name);
2712 if (!device) {
2713 weston_log("ERROR: could not open DRM device '%s'\n", name);
2714 return NULL;
2715 }
2716
2717 if (!drm_device_is_kms(b, device)) {
2718 udev_device_unref(device);
2719 weston_log("ERROR: DRM device '%s' is not a KMS device.\n", name);
2720 return NULL;
2721 }
2722
2723 /* If we're returning a device to use, we must have an open FD for
2724 * it. */
2725 assert(b->drm.fd >= 0);
2726
2727 return device;
2728 }
2729
2730 static void
planes_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)2731 planes_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2732 uint32_t key, void *data)
2733 {
2734 struct drm_backend *b = data;
2735
2736 switch (key) {
2737 case KEY_C:
2738 b->cursors_are_broken ^= true;
2739 break;
2740 case KEY_V:
2741 /* We don't support overlay-plane usage with legacy KMS. */
2742 if (b->atomic_modeset)
2743 b->sprites_are_broken ^= true;
2744 break;
2745 default:
2746 break;
2747 }
2748 }
2749
2750 #ifdef BUILD_VAAPI_RECORDER
2751 static void
recorder_destroy(struct drm_output * output)2752 recorder_destroy(struct drm_output *output)
2753 {
2754 vaapi_recorder_destroy(output->recorder);
2755 output->recorder = NULL;
2756
2757 weston_output_disable_planes_decr(&output->base);
2758
2759 wl_list_remove(&output->recorder_frame_listener.link);
2760 weston_log("[libva recorder] done\n");
2761 }
2762
2763 static void
recorder_frame_notify(struct wl_listener * listener,void * data)2764 recorder_frame_notify(struct wl_listener *listener, void *data)
2765 {
2766 struct drm_output *output;
2767 struct drm_backend *b;
2768 int fd, ret;
2769
2770 output = container_of(listener, struct drm_output,
2771 recorder_frame_listener);
2772 b = to_drm_backend(output->base.compositor);
2773
2774 if (!output->recorder)
2775 return;
2776
2777 ret = drmPrimeHandleToFD(b->drm.fd,
2778 output->scanout_plane->state_cur->fb->handles[0],
2779 DRM_CLOEXEC, &fd);
2780 if (ret) {
2781 weston_log("[libva recorder] "
2782 "failed to create prime fd for front buffer\n");
2783 return;
2784 }
2785
2786 ret = vaapi_recorder_frame(output->recorder, fd,
2787 output->scanout_plane->state_cur->fb->strides[0]);
2788 if (ret < 0) {
2789 weston_log("[libva recorder] aborted: %s\n", strerror(errno));
2790 recorder_destroy(output);
2791 }
2792 }
2793
2794 static void *
create_recorder(struct drm_backend * b,int width,int height,const char * filename)2795 create_recorder(struct drm_backend *b, int width, int height,
2796 const char *filename)
2797 {
2798 int fd;
2799 drm_magic_t magic;
2800
2801 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
2802 if (fd < 0)
2803 return NULL;
2804
2805 drmGetMagic(fd, &magic);
2806 drmAuthMagic(b->drm.fd, magic);
2807
2808 return vaapi_recorder_create(fd, width, height, filename);
2809 }
2810
2811 static void
recorder_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)2812 recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2813 uint32_t key, void *data)
2814 {
2815 struct drm_backend *b = data;
2816 struct drm_output *output;
2817 int width, height;
2818
2819 output = container_of(b->compositor->output_list.next,
2820 struct drm_output, base.link);
2821
2822 if (!output->recorder) {
2823 if (output->gbm_format != DRM_FORMAT_XRGB8888) {
2824 weston_log("failed to start vaapi recorder: "
2825 "output format not supported\n");
2826 return;
2827 }
2828
2829 width = output->base.current_mode->width;
2830 height = output->base.current_mode->height;
2831
2832 output->recorder =
2833 create_recorder(b, width, height, "capture.h264");
2834 if (!output->recorder) {
2835 weston_log("failed to create vaapi recorder\n");
2836 return;
2837 }
2838
2839 weston_output_disable_planes_incr(&output->base);
2840
2841 output->recorder_frame_listener.notify = recorder_frame_notify;
2842 wl_signal_add(&output->base.frame_signal,
2843 &output->recorder_frame_listener);
2844
2845 weston_output_schedule_repaint(&output->base);
2846
2847 weston_log("[libva recorder] initialized\n");
2848 } else {
2849 recorder_destroy(output);
2850 }
2851 }
2852 #else
2853 static void
recorder_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)2854 recorder_binding(struct weston_keyboard *keyboard, const struct timespec *time,
2855 uint32_t key, void *data)
2856 {
2857 weston_log("Compiled without libva support\n");
2858 }
2859 #endif
2860
2861
2862 static const struct weston_drm_output_api api = {
2863 drm_output_set_mode,
2864 drm_output_set_gbm_format,
2865 drm_output_set_seat,
2866 };
2867
2868 // OHOS vsync module
2869 static void
ohos_drm_vsync_thread_main(void * backend)2870 ohos_drm_vsync_thread_main(void *backend)
2871 {
2872 struct drm_backend *b = backend;
2873 while (b->vsync_thread_running) {
2874 drmVBlank vbl = {
2875 .request = {
2876 .type = DRM_VBLANK_RELATIVE | drm_waitvblank_pipe_vsync(b->pipe),
2877 .sequence = 1,
2878 },
2879 };
2880 if (drmWaitVBlank(b->drm.fd, &vbl) != 0) {
2881 weston_log("Failed to drmWaitVBlank (the first vblank event), because %{public}d", errno);
2882 sleep(0);
2883 } else {
2884 VsyncModuleTrigger();
2885 StopSoftVsyncThread();
2886 }
2887 }
2888 }
2889
2890 static struct drm_backend *
drm_backend_create(struct weston_compositor * compositor,struct weston_drm_backend_config * config)2891 drm_backend_create(struct weston_compositor *compositor,
2892 struct weston_drm_backend_config *config)
2893 {
2894 struct drm_backend *b;
2895 struct udev_device *drm_device;
2896 struct wl_event_loop *loop;
2897 const char *seat_id = default_seat;
2898 const char *session_seat;
2899 int ret;
2900
2901 session_seat = getenv("XDG_SEAT");
2902 if (session_seat)
2903 seat_id = session_seat;
2904
2905 if (config->seat_id)
2906 seat_id = config->seat_id;
2907
2908 weston_log("initializing drm backend\n");
2909
2910 b = zalloc(sizeof *b);
2911 if (b == NULL)
2912 return NULL;
2913
2914 b->state_invalid = true;
2915 b->drm.fd = -1;
2916 wl_array_init(&b->unused_crtcs);
2917
2918 // OHOS fix
2919 {
2920 int fd = drmOpen("hisilicon", NULL);
2921 if (fd >= 0) {
2922 b->use_tde = 1;
2923 drmClose(fd);
2924 weston_log("tde open success, \n");
2925 } else {
2926 b->use_tde = 0;
2927 weston_log("tde open failure\n");
2928 }
2929 }
2930
2931 b->compositor = compositor;
2932 b->use_pixman = config->use_pixman;
2933 b->pageflip_timeout = config->pageflip_timeout;
2934 b->use_pixman_shadow = config->use_pixman_shadow;
2935 // OHOS remove logger
2936 // b->debug = weston_compositor_add_log_scope(compositor, "drm-backend",
2937 // "Debug messages from DRM/KMS backend\n",
2938 // NULL, NULL, NULL);
2939
2940 compositor->backend = &b->base;
2941 compositor->require_input = !config->continue_without_input;
2942
2943 if (parse_gbm_format(config->gbm_format, DRM_FORMAT_XRGB8888, &b->gbm_format) < 0)
2944 goto err_compositor;
2945
2946 /* Check if we run drm-backend using weston-launch */
2947 compositor->launcher = weston_launcher_connect(compositor, config->tty,
2948 seat_id, true);
2949 if (compositor->launcher == NULL) {
2950 weston_log("fatal: drm backend should be run using "
2951 "weston-launch binary, or your system should "
2952 "provide the logind D-Bus API.\n");
2953 goto err_compositor;
2954 }
2955
2956 b->udev = udev_new();
2957 if (b->udev == NULL) {
2958 weston_log("failed to initialize udev context\n");
2959 goto err_launcher;
2960 }
2961
2962 b->session_listener.notify = session_notify;
2963 wl_signal_add(&compositor->session_signal, &b->session_listener);
2964
2965 if (config->specific_device)
2966 drm_device = open_specific_drm_device(b, config->specific_device);
2967 else
2968 drm_device = find_primary_gpu(b, seat_id);
2969 if (drm_device == NULL) {
2970 weston_log("no drm device found\n");
2971 goto err_udev;
2972 }
2973
2974 // OHOS vsync module
2975 b->vsync_thread_running = true;
2976 pthread_create(&b->vsync_thread, NULL, ohos_drm_vsync_thread_main, b);
2977
2978 // OHOS drm auth: init the wayland drm auth service
2979 InitWaylandDrmAuthService(compositor->wl_display, b->drm.fd);
2980
2981 if (init_kms_caps(b) < 0) {
2982 weston_log("failed to initialize kms\n");
2983 goto err_udev_dev;
2984 }
2985
2986 if (b->use_pixman) {
2987 if (init_pixman(b) < 0) {
2988 weston_log("failed to initialize pixman renderer\n");
2989 goto err_udev_dev;
2990 }
2991 } else {
2992 // OHOS mix renderer
2993 if (mix_renderer_init(compositor) < 0) {
2994 LOG_ERROR("mix_renderer_init failed");
2995 goto err_udev_dev;
2996 }
2997 if (init_egl(b) < 0) {
2998 weston_log("failed to initialize egl\n");
2999 goto err_udev_dev;
3000 }
3001 }
3002
3003 b->base.destroy = drm_destroy;
3004 b->base.repaint_begin = drm_repaint_begin;
3005 b->base.repaint_flush = drm_repaint_flush;
3006 b->base.repaint_cancel = drm_repaint_cancel;
3007 b->base.create_output = drm_output_create;
3008 b->base.device_changed = drm_device_changed;
3009 b->base.can_scanout_dmabuf = drm_can_scanout_dmabuf;
3010
3011 weston_setup_vt_switch_bindings(compositor);
3012
3013 wl_list_init(&b->plane_list);
3014 create_sprites(b);
3015
3016 if (udev_input_init(&b->input,
3017 compositor, b->udev, seat_id,
3018 config->configure_device) < 0) {
3019 weston_log("failed to create input devices\n");
3020 // OHOS build
3021 // goto err_sprite;
3022 }
3023
3024 if (drm_backend_create_heads(b, drm_device) < 0) {
3025 weston_log("Failed to create heads for %s\n", b->drm.filename);
3026 goto err_udev_input;
3027 }
3028
3029 /* 'compute' faked zpos values in case HW doesn't expose any */
3030 drm_backend_create_faked_zpos(b);
3031
3032 /* A this point we have some idea of whether or not we have a working
3033 * cursor plane. */
3034 if (!b->cursors_are_broken)
3035 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
3036
3037 loop = wl_display_get_event_loop(compositor->wl_display);
3038 b->drm_source =
3039 wl_event_loop_add_fd(loop, b->drm.fd,
3040 WL_EVENT_READABLE, on_drm_input, b);
3041
3042 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3043 if (b->udev_monitor == NULL) {
3044 weston_log("failed to initialize udev monitor\n");
3045 goto err_drm_source;
3046 }
3047 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
3048 "drm", NULL);
3049 b->udev_drm_source =
3050 wl_event_loop_add_fd(loop,
3051 udev_monitor_get_fd(b->udev_monitor),
3052 WL_EVENT_READABLE, udev_drm_event, b);
3053
3054 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
3055 weston_log("failed to enable udev-monitor receiving\n");
3056 goto err_udev_monitor;
3057 }
3058
3059 udev_device_unref(drm_device);
3060
3061 // OHOS remove debugger
3062 // weston_compositor_add_debug_binding(compositor, KEY_O,
3063 // planes_binding, b);
3064 // weston_compositor_add_debug_binding(compositor, KEY_C,
3065 // planes_binding, b);
3066 // weston_compositor_add_debug_binding(compositor, KEY_V,
3067 // planes_binding, b);
3068 // weston_compositor_add_debug_binding(compositor, KEY_Q,
3069 // recorder_binding, b);
3070 // weston_compositor_add_debug_binding(compositor, KEY_W,
3071 // renderer_switch_binding, b);
3072
3073
3074 if (compositor->renderer->import_dmabuf) {
3075 if (linux_dmabuf_setup(compositor) < 0)
3076 weston_log("Error: initializing dmabuf "
3077 "support failed.\n");
3078 if (weston_direct_display_setup(compositor) < 0)
3079 weston_log("Error: initializing direct-display "
3080 "support failed.\n");
3081 }
3082
3083 if (compositor->capabilities & WESTON_CAP_EXPLICIT_SYNC) {
3084 if (linux_explicit_synchronization_setup(compositor) < 0)
3085 weston_log("Error: initializing explicit "
3086 " synchronization support failed.\n");
3087 }
3088
3089 if (b->atomic_modeset)
3090 if (weston_compositor_enable_content_protection(compositor) < 0)
3091 weston_log("Error: initializing content-protection "
3092 "support failed.\n");
3093
3094 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3095 &api, sizeof(api));
3096
3097 if (ret < 0) {
3098 weston_log("Failed to register output API.\n");
3099 goto err_udev_monitor;
3100 }
3101
3102 ret = drm_backend_init_virtual_output_api(compositor);
3103 if (ret < 0) {
3104 weston_log("Failed to register virtual output API.\n");
3105 goto err_udev_monitor;
3106 }
3107
3108 return b;
3109
3110 err_udev_monitor:
3111 wl_event_source_remove(b->udev_drm_source);
3112 udev_monitor_unref(b->udev_monitor);
3113 err_drm_source:
3114 wl_event_source_remove(b->drm_source);
3115 err_udev_input:
3116 udev_input_destroy(&b->input);
3117 // OHOS build
3118 // err_sprite:
3119 #ifdef BUILD_DRM_GBM
3120 if (b->gbm)
3121 gbm_device_destroy(b->gbm);
3122 #endif
3123 destroy_sprites(b);
3124 err_udev_dev:
3125 udev_device_unref(drm_device);
3126 err_udev:
3127 udev_unref(b->udev);
3128 err_launcher:
3129 weston_launcher_destroy(compositor->launcher);
3130 err_compositor:
3131 weston_compositor_shutdown(compositor);
3132 free(b);
3133 return NULL;
3134 }
3135
3136 static void
config_init_to_defaults(struct weston_drm_backend_config * config)3137 config_init_to_defaults(struct weston_drm_backend_config *config)
3138 {
3139 // OHOS
3140 // config->use_pixman_shadow = true;
3141 config->use_pixman_shadow = false;
3142 }
3143
3144 WL_EXPORT int
weston_backend_init(struct weston_compositor * compositor,struct weston_backend_config * config_base)3145 weston_backend_init(struct weston_compositor *compositor,
3146 struct weston_backend_config *config_base)
3147 {
3148 struct drm_backend *b;
3149 struct weston_drm_backend_config config = {{ 0, }};
3150
3151 if (config_base == NULL ||
3152 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3153 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3154 weston_log("drm backend config structure is invalid\n");
3155 return -1;
3156 }
3157
3158 config_init_to_defaults(&config);
3159 memcpy(&config, config_base, config_base->struct_size);
3160
3161 b = drm_backend_create(compositor, &config);
3162 if (b == NULL)
3163 return -1;
3164
3165 return 0;
3166 }
3167