1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
|
/*
* Copyright (C) 2017 Red Hat
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include "config.h"
#include "backends/edid.h"
#include "backends/meta-output.h"
#include "backends/meta-crtc.h"
enum
{
PROP_0,
PROP_ID,
PROP_GPU,
PROP_INFO,
PROP_IS_PRIVACY_SCREEN_ENABLED,
N_PROPS
};
static GParamSpec *obj_props[N_PROPS];
enum
{
COLOR_SPACE_CHANGED,
HDR_METADATA_CHANGED,
N_SIGNALS
};
static guint signals[N_SIGNALS];
typedef struct _MetaOutputPrivate
{
uint64_t id;
MetaGpu *gpu;
MetaOutputInfo *info;
MetaMonitor *monitor;
/* The CRTC driving this output, NULL if the output is not enabled */
MetaCrtc *crtc;
gboolean is_primary;
gboolean is_presentation;
gboolean is_underscanning;
gboolean has_max_bpc;
unsigned int max_bpc;
int backlight;
MetaPrivacyScreenState privacy_screen_state;
gboolean is_privacy_screen_enabled;
MetaOutputHdrMetadata hdr_metadata;
MetaOutputColorspace color_space;
} MetaOutputPrivate;
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (MetaOutput, meta_output, G_TYPE_OBJECT)
G_DEFINE_BOXED_TYPE (MetaOutputInfo, meta_output_info,
meta_output_info_ref,
meta_output_info_unref)
MetaOutputInfo *
meta_output_info_new (void)
{
MetaOutputInfo *output_info;
output_info = g_new0 (MetaOutputInfo, 1);
g_ref_count_init (&output_info->ref_count);
return output_info;
}
MetaOutputInfo *
meta_output_info_ref (MetaOutputInfo *output_info)
{
g_ref_count_inc (&output_info->ref_count);
return output_info;
}
void
meta_output_info_unref (MetaOutputInfo *output_info)
{
if (g_ref_count_dec (&output_info->ref_count))
{
g_free (output_info->name);
g_free (output_info->vendor);
g_free (output_info->product);
g_free (output_info->serial);
g_free (output_info->edid_checksum_md5);
g_free (output_info->edid_info);
g_free (output_info->modes);
g_free (output_info->possible_crtcs);
g_free (output_info->possible_clones);
g_free (output_info);
}
}
uint64_t
meta_output_get_id (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->id;
}
MetaGpu *
meta_output_get_gpu (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->gpu;
}
MetaMonitor *
meta_output_get_monitor (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
g_warn_if_fail (priv->monitor);
return priv->monitor;
}
void
meta_output_set_monitor (MetaOutput *output,
MetaMonitor *monitor)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
g_warn_if_fail (!priv->monitor);
priv->monitor = monitor;
}
void
meta_output_unset_monitor (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
g_warn_if_fail (priv->monitor);
priv->monitor = NULL;
}
const char *
meta_output_get_name (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->info->name;
}
gboolean
meta_output_is_primary (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->is_primary;
}
gboolean
meta_output_is_presentation (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->is_presentation;
}
gboolean
meta_output_is_underscanning (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->is_underscanning;
}
gboolean
meta_output_get_max_bpc (MetaOutput *output,
unsigned int *max_bpc)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
if (priv->has_max_bpc && max_bpc)
*max_bpc = priv->max_bpc;
return priv->has_max_bpc;
}
void
meta_output_set_backlight (MetaOutput *output,
int backlight)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
priv->backlight = backlight;
}
int
meta_output_get_backlight (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->backlight;
}
void
meta_output_add_possible_clone (MetaOutput *output,
MetaOutput *possible_clone)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
MetaOutputInfo *output_info = priv->info;
output_info->n_possible_clones++;
output_info->possible_clones = g_renew (MetaOutput *,
output_info->possible_clones,
output_info->n_possible_clones);
output_info->possible_clones[output_info->n_possible_clones - 1] =
possible_clone;
}
const MetaOutputInfo *
meta_output_get_info (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->info;
}
void
meta_output_assign_crtc (MetaOutput *output,
MetaCrtc *crtc,
const MetaOutputAssignment *output_assignment)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
g_assert (crtc);
meta_output_unassign_crtc (output);
g_set_object (&priv->crtc, crtc);
meta_crtc_assign_output (crtc, output);
priv->is_primary = output_assignment->is_primary;
priv->is_presentation = output_assignment->is_presentation;
priv->is_underscanning = output_assignment->is_underscanning;
priv->has_max_bpc = output_assignment->has_max_bpc;
if (priv->has_max_bpc)
priv->max_bpc = output_assignment->max_bpc;
}
void
meta_output_unassign_crtc (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
if (priv->crtc)
{
meta_crtc_unassign_output (priv->crtc, output);
g_clear_object (&priv->crtc);
}
priv->is_primary = FALSE;
priv->is_presentation = FALSE;
}
MetaCrtc *
meta_output_get_assigned_crtc (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->crtc;
}
MetaMonitorTransform
meta_output_logical_to_crtc_transform (MetaOutput *output,
MetaMonitorTransform transform)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
MetaMonitorTransform panel_orientation_transform;
panel_orientation_transform = priv->info->panel_orientation_transform;
return meta_monitor_transform_transform (transform,
panel_orientation_transform);
}
MetaMonitorTransform
meta_output_crtc_to_logical_transform (MetaOutput *output,
MetaMonitorTransform transform)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
MetaMonitorTransform inverted_panel_orientation_transform;
inverted_panel_orientation_transform =
meta_monitor_transform_invert (priv->info->panel_orientation_transform);
return meta_monitor_transform_transform (transform,
inverted_panel_orientation_transform);
}
static void
set_output_details_from_edid (MetaOutputInfo *output_info,
MetaEdidInfo *edid_info)
{
output_info->vendor = g_strndup (edid_info->manufacturer_code, 4);
if (!g_utf8_validate (output_info->vendor, -1, NULL))
g_clear_pointer (&output_info->vendor, g_free);
output_info->product = g_strndup (edid_info->dsc_product_name, 14);
if (!g_utf8_validate (output_info->product, -1, NULL) ||
output_info->product[0] == '\0')
{
g_clear_pointer (&output_info->product, g_free);
output_info->product =
g_strdup_printf ("0x%04x", (unsigned) edid_info->product_code);
}
output_info->serial = g_strndup (edid_info->dsc_serial_number, 14);
if (!g_utf8_validate (output_info->serial, -1, NULL) ||
output_info->serial[0] == '\0')
{
g_clear_pointer (&output_info->serial, g_free);
output_info->serial =
g_strdup_printf ("0x%08x", edid_info->serial_number);
}
}
void
meta_output_info_parse_edid (MetaOutputInfo *output_info,
GBytes *edid)
{
MetaEdidInfo *edid_info;
size_t len;
gconstpointer data;
g_return_if_fail (!output_info->edid_info);
g_return_if_fail (edid);
data = g_bytes_get_data (edid, &len);
edid_info = meta_edid_info_new_parse (data);
output_info->edid_checksum_md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5,
data, len);
if (edid_info)
{
output_info->edid_info = edid_info;
set_output_details_from_edid (output_info, edid_info);
}
}
gboolean
meta_output_is_laptop (MetaOutput *output)
{
const MetaOutputInfo *output_info = meta_output_get_info (output);
switch (output_info->connector_type)
{
case META_CONNECTOR_TYPE_eDP:
case META_CONNECTOR_TYPE_LVDS:
case META_CONNECTOR_TYPE_DSI:
return TRUE;
default:
return FALSE;
}
}
static void
meta_output_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
MetaOutput *output = META_OUTPUT (object);
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
switch (prop_id)
{
case PROP_ID:
priv->id = g_value_get_uint64 (value);
break;
case PROP_GPU:
priv->gpu = g_value_get_object (value);
break;
case PROP_INFO:
priv->info = meta_output_info_ref (g_value_get_boxed (value));
break;
case PROP_IS_PRIVACY_SCREEN_ENABLED:
priv->is_privacy_screen_enabled = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
meta_output_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
MetaOutput *output = META_OUTPUT (object);
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
switch (prop_id)
{
case PROP_ID:
g_value_set_uint64 (value, priv->id);
break;
case PROP_GPU:
g_value_set_object (value, priv->gpu);
break;
case PROP_INFO:
g_value_set_boxed (value, priv->info);
break;
case PROP_IS_PRIVACY_SCREEN_ENABLED:
g_value_set_boolean (value, priv->is_privacy_screen_enabled);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
meta_output_dispose (GObject *object)
{
MetaOutput *output = META_OUTPUT (object);
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
g_clear_object (&priv->crtc);
G_OBJECT_CLASS (meta_output_parent_class)->dispose (object);
}
static void
meta_output_finalize (GObject *object)
{
MetaOutput *output = META_OUTPUT (object);
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
g_clear_pointer (&priv->info, meta_output_info_unref);
G_OBJECT_CLASS (meta_output_parent_class)->finalize (object);
}
MetaPrivacyScreenState
meta_output_get_privacy_screen_state (MetaOutput *output)
{
MetaOutputClass *output_class = META_OUTPUT_GET_CLASS (output);
if (!output_class->get_privacy_screen_state)
return META_PRIVACY_SCREEN_UNAVAILABLE;
return output_class->get_privacy_screen_state (output);
}
gboolean
meta_output_is_privacy_screen_supported (MetaOutput *output)
{
return !(meta_output_get_privacy_screen_state (output) ==
META_PRIVACY_SCREEN_UNAVAILABLE);
}
gboolean
meta_output_is_privacy_screen_enabled (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->privacy_screen_state;
}
gboolean
meta_output_set_privacy_screen_enabled (MetaOutput *output,
gboolean enabled,
GError **error)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
MetaPrivacyScreenState state;
state = priv->privacy_screen_state;
if (state == META_PRIVACY_SCREEN_UNAVAILABLE)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"The privacy screen is not supported by this output");
return FALSE;
}
if (state & META_PRIVACY_SCREEN_LOCKED)
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
"The privacy screen is locked at hardware level, "
"impossible to set it");
return FALSE;
}
if (priv->is_privacy_screen_enabled == enabled)
return TRUE;
priv->is_privacy_screen_enabled = enabled;
g_object_notify_by_pspec (G_OBJECT (output),
obj_props[PROP_IS_PRIVACY_SCREEN_ENABLED]);
return TRUE;
}
gboolean
meta_output_info_is_color_space_supported (const MetaOutputInfo *output_info,
MetaOutputColorspace color_space)
{
MetaEdidColorimetry colorimetry;
if (!output_info->edid_info)
return FALSE;
colorimetry = output_info->edid_info->colorimetry;
switch (color_space)
{
case META_OUTPUT_COLORSPACE_DEFAULT:
return TRUE;
case META_OUTPUT_COLORSPACE_BT2020:
return !!(colorimetry & META_EDID_COLORIMETRY_BT2020RGB);
default:
return FALSE;
}
}
gboolean
meta_output_is_color_space_supported (MetaOutput *output,
MetaOutputColorspace color_space)
{
MetaOutputClass *output_class = META_OUTPUT_GET_CLASS (output);
if (!output_class->is_color_space_supported)
return FALSE;
return output_class->is_color_space_supported (output, color_space);
}
void
meta_output_set_color_space (MetaOutput *output,
MetaOutputColorspace color_space)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
priv->color_space = color_space;
g_signal_emit (output, signals[COLOR_SPACE_CHANGED], 0);
}
MetaOutputColorspace
meta_output_peek_color_space (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return priv->color_space;
}
gboolean
meta_output_is_hdr_metadata_supported (MetaOutput *output,
MetaOutputHdrMetadataEOTF eotf)
{
MetaOutputClass *output_class = META_OUTPUT_GET_CLASS (output);
const MetaOutputInfo *output_info = meta_output_get_info (output);
MetaEdidTransferFunction tf = 0;
g_assert (output_info != NULL);
if (!output_info->edid_info)
return FALSE;
if ((output_info->edid_info->hdr_static_metadata.sm &
META_EDID_STATIC_METADATA_TYPE1) == 0)
return FALSE;
switch (eotf)
{
case META_OUTPUT_HDR_METADATA_EOTF_TRADITIONAL_GAMMA_SDR:
tf = META_EDID_TF_TRADITIONAL_GAMMA_SDR;
break;
case META_OUTPUT_HDR_METADATA_EOTF_TRADITIONAL_GAMMA_HDR:
tf = META_EDID_TF_TRADITIONAL_GAMMA_HDR;
break;
case META_OUTPUT_HDR_METADATA_EOTF_PQ:
tf = META_EDID_TF_PQ;
break;
case META_OUTPUT_HDR_METADATA_EOTF_HLG:
tf = META_EDID_TF_HLG;
break;
}
if ((output_info->edid_info->hdr_static_metadata.tf & tf) == 0)
return FALSE;
if (!output_class->is_hdr_metadata_supported)
return FALSE;
return output_class->is_hdr_metadata_supported (output);
}
void
meta_output_set_hdr_metadata (MetaOutput *output,
MetaOutputHdrMetadata *metadata)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
priv->hdr_metadata = *metadata;
g_signal_emit (output, signals[HDR_METADATA_CHANGED], 0);
}
MetaOutputHdrMetadata *
meta_output_peek_hdr_metadata (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
return &priv->hdr_metadata;
}
static void
meta_output_init (MetaOutput *output)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
priv->backlight = -1;
priv->color_space = META_OUTPUT_COLORSPACE_DEFAULT;
priv->hdr_metadata.active = FALSE;
}
static void
meta_output_class_init (MetaOutputClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = meta_output_set_property;
object_class->get_property = meta_output_get_property;
object_class->dispose = meta_output_dispose;
object_class->finalize = meta_output_finalize;
obj_props[PROP_ID] =
g_param_spec_uint64 ("id",
"id",
"CRTC id",
0, UINT64_MAX, 0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
obj_props[PROP_GPU] =
g_param_spec_object ("gpu",
"gpu",
"MetaGpu",
META_TYPE_GPU,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
obj_props[PROP_INFO] =
g_param_spec_boxed ("info",
"info",
"MetaOutputInfo",
META_TYPE_OUTPUT_INFO,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
obj_props[PROP_IS_PRIVACY_SCREEN_ENABLED] =
g_param_spec_boolean ("is-privacy-screen-enabled",
"is-privacy-screen-enabled",
"Is privacy screen enabled",
FALSE,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPS, obj_props);
signals[COLOR_SPACE_CHANGED] =
g_signal_new ("color-space-changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL, NULL,
G_TYPE_NONE, 0);
signals[HDR_METADATA_CHANGED] =
g_signal_new ("hdr-metadata-changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL, NULL,
G_TYPE_NONE, 0);
}
gboolean
meta_tile_info_equal (MetaTileInfo *a,
MetaTileInfo *b)
{
if (a == b)
return TRUE;
if (a->group_id != b->group_id)
return FALSE;
if (a->flags != b->flags)
return FALSE;
if (a->max_h_tiles != b->max_h_tiles)
return FALSE;
if (a->max_v_tiles != b->max_v_tiles)
return FALSE;
if (a->loc_h_tile != b->loc_h_tile)
return FALSE;
if (a->loc_v_tile != b->loc_v_tile)
return FALSE;
if (a->tile_w != b->tile_w)
return FALSE;
if (a->tile_h != b->tile_h)
return FALSE;
return TRUE;
}
void
meta_output_update_modes (MetaOutput *output,
MetaCrtcMode *preferred_mode,
MetaCrtcMode **modes,
int n_modes)
{
MetaOutputPrivate *priv = meta_output_get_instance_private (output);
int i;
for (i = 0; i < priv->info->n_modes; i++)
g_object_unref (priv->info->modes[i]);
g_free (priv->info->modes);
priv->info->preferred_mode = preferred_mode;
priv->info->modes = modes;
priv->info->n_modes = n_modes;
}
|