summaryrefslogtreecommitdiff
path: root/tests/kms_plane_alpha_blend.c
blob: eaec3766182d25b8d7a5bbdc486f6ba4e6adf162 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
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
/*
 * Copyright © 2018 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *   Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
 */

/**
 * TEST: kms plane alpha blend
 * Category: Display
 * Description: Test plane alpha and blending mode properties
 * Driver requirement: i915, xe
 * Functionality: plane
 * Mega feature: General Display Features
 * Test category: functionality test
 */

#include "igt.h"

/**
 * SUBTEST: alpha-%s
 * Description: Test to %arg[1]
 *
 * arg[1]:
 *
 * @7efc:                validate alpha values 0x7e and 0xfc are swappable on
 *                       pre-multiplied blend mode.
 * @basic:               basic plane alpha properties.
 * @opaque-fb:           alpha properties with opaque fb.
 * @transparent-fb:      alpha property with transparent fb.
 */

/**
 * SUBTEST: coverage-vs-premult-vs-constant
 * Description: Tests pipe coverage blending properties.
 *
 * SUBTEST: coverage-7efc
 * Description: Uses alpha values 0x7e and 0xfc to validate fg.alpha and
 *              plane_alpha are swappable on coverage blend mode.
 *
 * SUBTEST: constant-alpha-%s
 * Description: Tests plane alpha and blending properties with %arg[1].
 *
 * arg[1]:
 *
 * @max:            maximum alpha value
 * @mid:            medium  alpha value
 * @min:            minimum alpha value
 */

IGT_TEST_DESCRIPTION("Test plane alpha and blending mode properties");

static bool extended;
static enum pipe active_pipes[IGT_MAX_PIPES];
static uint32_t last_pipe;

typedef struct {
	int gfx_fd;
	igt_display_t display;
	struct igt_fb xrgb_fb, argb_fb_0, argb_fb_cov_0, argb_fb_7e, argb_fb_cov_7e, argb_fb_fc, argb_fb_cov_fc, argb_fb_100, black_fb, gray_fb;
	igt_crc_t ref_crc;
	igt_pipe_crc_t *pipe_crc;
} data_t;

static void __draw_gradient(struct igt_fb *fb, int w, int h, double a, cairo_t *cr)
{
	cairo_pattern_t *pat;

	pat = cairo_pattern_create_linear(0, 0, w, h);
	cairo_pattern_add_color_stop_rgba(pat, 0.00, 0.00, 0.00, 0.00, 1.);
	cairo_pattern_add_color_stop_rgba(pat, 0.25, 1.00, 1.00, 0.00, 1.);
	cairo_pattern_add_color_stop_rgba(pat, 0.50, 0.00, 1.00, 1.00, 1.);
	cairo_pattern_add_color_stop_rgba(pat, 0.75, 1.00, 0.00, 1.00, 1.);
	cairo_pattern_add_color_stop_rgba(pat, 1.00, 1.00, 1.00, 1.00, 1.);

	cairo_rectangle(cr, 0, 0, w, h);
	cairo_set_source(cr, pat);
	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	cairo_paint_with_alpha(cr, a);
	cairo_pattern_destroy(pat);
}

static void draw_gradient(struct igt_fb *fb, int w, int h, double a)
{
	cairo_t *cr = igt_get_cairo_ctx(fb->fd, fb);

	__draw_gradient(fb, w, h, a, cr);

	igt_put_cairo_ctx(cr);
}

static void draw_gradient_coverage(struct igt_fb *fb, int w, int h, uint8_t a)
{
	cairo_t *cr = igt_get_cairo_ctx(fb->fd, fb);
	uint8_t *data = cairo_image_surface_get_data(fb->cairo_surface);
	uint32_t stride = fb->strides[0];
	int i;

	__draw_gradient(fb, w, h, 1., cr);

	for (; h--; data += stride)
		for (i = 0; i < w; i++)
			data[i * 4 + 3] = a;

	igt_put_cairo_ctx(cr);
}

static void draw_squares(struct igt_fb *fb, int w, int h, double a)
{
	cairo_t *cr = igt_get_cairo_ctx(fb->fd, fb);

	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	igt_paint_color_alpha(cr, 0, 0,         w / 2, h / 2, 1., 0., 0., a);
	igt_paint_color_alpha(cr, w / 2, 0,     w / 2, h / 2, 0., 1., 0., a);
	igt_paint_color_alpha(cr, 0, h / 2,     w / 2, h / 2, 0., 0., 1., a);
	igt_paint_color_alpha(cr, w / 2, h / 2, w / 4, h / 2, 1., 1., 1., a);
	igt_paint_color_alpha(cr, w * 3 / 4, h / 2, w - (w * 3 / 4), h / 2, 0., 0., 0., a);

	igt_put_cairo_ctx(cr);
}

static void draw_squares_coverage(struct igt_fb *fb, int w, int h, uint8_t as)
{
	cairo_t *cr = igt_get_cairo_ctx(fb->fd, fb);
	int i, j;
	uint32_t *data = (void *)cairo_image_surface_get_data(fb->cairo_surface);
	uint32_t stride = fb->strides[0] / 4;
	uint32_t a = as << 24;

	for (j = 0; j < h / 2; j++) {
		for (i = 0; i < w / 2; i++)
			data[j * stride + i] = a | 0xff0000;

		for (; i < w; i++)
			data[j * stride + i] = a | 0xff00;
	}

	for (j = h / 2; j < h; j++) {
		for (i = 0; i < w / 2; i++)
			data[j * stride + i] = a | 0xff;

		for (; i < 3 * w / 4; i++)
			data[j * stride + i] = a | 0xffffff;

		for (; i < w; i++)
			data[j * stride + i] = a;
	}

	igt_put_cairo_ctx(cr);
}

static void reset_alpha(igt_display_t *display, enum pipe pipe)
{
	igt_plane_t *plane;

	for_each_plane_on_pipe(display, pipe, plane) {
		if (igt_plane_has_prop(plane, IGT_PLANE_ALPHA))
			igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0xffff);

		if (igt_plane_has_prop(plane, IGT_PLANE_PIXEL_BLEND_MODE))
			igt_plane_set_prop_enum(plane, IGT_PLANE_PIXEL_BLEND_MODE, "Pre-multiplied");
	}
}

static bool has_multiplied_alpha(data_t *data, igt_plane_t *plane)
{
	int ret;

	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0x8080);
	igt_plane_set_fb(plane, &data->argb_fb_100);
	ret = igt_display_try_commit_atomic(&data->display,
		DRM_MODE_ATOMIC_TEST_ONLY | DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0xffff);
	igt_plane_set_fb(plane, NULL);

	return ret == 0;
}

static void remove_fbs(data_t *data)
{
	igt_remove_fb(data->gfx_fd, &data->xrgb_fb);
	igt_remove_fb(data->gfx_fd, &data->argb_fb_0);
	igt_remove_fb(data->gfx_fd, &data->argb_fb_cov_0);
	igt_remove_fb(data->gfx_fd, &data->argb_fb_7e);
	igt_remove_fb(data->gfx_fd, &data->argb_fb_fc);
	igt_remove_fb(data->gfx_fd, &data->argb_fb_cov_7e);
	igt_remove_fb(data->gfx_fd, &data->argb_fb_cov_fc);
	igt_remove_fb(data->gfx_fd, &data->argb_fb_100);
	igt_remove_fb(data->gfx_fd, &data->black_fb);
	igt_remove_fb(data->gfx_fd, &data->gray_fb);
}

static void prepare_crtc(data_t *data, igt_output_t *output, enum pipe pipe)
{
	drmModeModeInfo *mode;
	igt_display_t *display = &data->display;
	int w, h;
	igt_plane_t *primary = igt_pipe_get_plane_type(&display->pipes[pipe], DRM_PLANE_TYPE_PRIMARY);

	/* create the pipe_crc object for this pipe */
	igt_pipe_crc_free(data->pipe_crc);
	data->pipe_crc = igt_pipe_crc_new(data->gfx_fd, pipe,
					  IGT_PIPE_CRC_SOURCE_AUTO);

	mode = igt_output_get_mode(output);
	w = mode->hdisplay;
	h = mode->vdisplay;

	/* recreate all fbs if incompatible */
	if (data->xrgb_fb.width != w || data->xrgb_fb.height != h) {
		cairo_t *cr;

		remove_fbs(data);

		igt_create_fb(data->gfx_fd, w, h,
			      DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR,
			      &data->xrgb_fb);
		draw_gradient(&data->xrgb_fb, w, h, 1.);

		igt_create_fb(data->gfx_fd, w, h,
			      DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_LINEAR,
			      &data->argb_fb_cov_0);
		draw_gradient_coverage(&data->argb_fb_cov_0, w, h, 0);

		igt_create_fb(data->gfx_fd, w, h,
			      DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_LINEAR,
			      &data->argb_fb_0);

		cr = igt_get_cairo_ctx(data->gfx_fd, &data->argb_fb_0);
		cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
		igt_paint_color_alpha(cr, 0, 0, w, h, 0., 0., 0., 0.0);
		igt_put_cairo_ctx(cr);

		igt_create_fb(data->gfx_fd, w, h,
			      DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_LINEAR,
			      &data->argb_fb_7e);
		draw_squares(&data->argb_fb_7e, w, h, 126. / 255.);

		igt_create_fb(data->gfx_fd, w, h,
			      DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_LINEAR,
			      &data->argb_fb_cov_7e);
		draw_squares_coverage(&data->argb_fb_cov_7e, w, h, 0x7e);

		igt_create_fb(data->gfx_fd, w, h,
			      DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_LINEAR,
			      &data->argb_fb_fc);
		draw_squares(&data->argb_fb_fc, w, h, 252. / 255.);

		igt_create_fb(data->gfx_fd, w, h,
			      DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_LINEAR,
			      &data->argb_fb_cov_fc);
		draw_squares_coverage(&data->argb_fb_cov_fc, w, h, 0xfc);

		igt_create_fb(data->gfx_fd, w, h,
			      DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_LINEAR,
			      &data->argb_fb_100);
		draw_gradient(&data->argb_fb_100, w, h, 1.);

		igt_create_fb(data->gfx_fd, w, h,
			      DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR,
			      &data->black_fb);

		igt_create_color_fb(data->gfx_fd, w, h,
				    DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR,
				    .5, .5, .5, &data->gray_fb);
	}

	igt_plane_set_fb(primary, &data->black_fb);
}

static void basic_alpha(data_t *data, enum pipe pipe, igt_plane_t *plane)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc, crc;
	int i;

	/* Testcase 1: alpha = 0.0, plane should be transparent. */
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_start(data->pipe_crc);
	igt_pipe_crc_get_single(data->pipe_crc, &ref_crc);

	igt_plane_set_fb(plane, &data->argb_fb_0);

	/* transparent fb should be transparent, no matter what.. */
	for (i = 7; i < 256; i += 8) {
		igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, i | (i << 8));
		igt_display_commit2(display, COMMIT_ATOMIC);

		igt_pipe_crc_get_current(display->drm_fd, data->pipe_crc, &crc);
		if (!igt_check_crc_equal(&ref_crc, &crc) && !igt_skip_crc_compare) {
			igt_pipe_crc_stop(data->pipe_crc);

			igt_assert_f(false, "crc mismatch with plane alpha value %d\n",
				     i | (i << 8));
		}
	}

	/* And test alpha = 0, should give same CRC, but doesn't on some i915 platforms. */
	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0);
	igt_display_commit2(display, COMMIT_ATOMIC);

	igt_pipe_crc_get_current(display->drm_fd, data->pipe_crc, &crc);
	igt_pipe_crc_stop(data->pipe_crc);
	igt_assert_crc_equal(&ref_crc, &crc);
}

static void argb_opaque(data_t *data, enum pipe pipe, igt_plane_t *plane)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc, crc;

	/* alpha = 1.0, plane should be fully opaque, test with an opaque fb */
	igt_plane_set_fb(plane, &data->xrgb_fb);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);

	igt_plane_set_fb(plane, &data->argb_fb_100);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);

	igt_assert_crc_equal(&ref_crc, &crc);
}

static void argb_transparent(data_t *data, enum pipe pipe, igt_plane_t *plane)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc, crc;

	/* alpha = 1.0, plane should be fully opaque, test with a transparent fb */
	igt_plane_set_fb(plane, NULL);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);

	igt_plane_set_fb(plane, &data->argb_fb_0);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);

	igt_assert_crc_equal(&ref_crc, &crc);
}

static void constant_alpha_min(data_t *data, enum pipe pipe, igt_plane_t *plane)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc, crc;

	igt_plane_set_fb(plane, NULL);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);

	igt_plane_set_prop_enum(plane, IGT_PLANE_PIXEL_BLEND_MODE, "None");
	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0);
	igt_plane_set_fb(plane, &data->argb_fb_100);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);

	igt_plane_set_fb(plane, &data->argb_fb_0);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);
}

static void constant_alpha_mid(data_t *data, enum pipe pipe, igt_plane_t *plane)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc, crc;

	if (plane->type != DRM_PLANE_TYPE_PRIMARY)
		igt_plane_set_fb(igt_pipe_get_plane_type(&display->pipes[pipe], DRM_PLANE_TYPE_PRIMARY), &data->gray_fb);

	igt_plane_set_prop_enum(plane, IGT_PLANE_PIXEL_BLEND_MODE, "None");
	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0x7fff);
	igt_plane_set_fb(plane, &data->xrgb_fb);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);

	igt_plane_set_fb(plane, &data->argb_fb_cov_0);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);

	igt_plane_set_fb(plane, &data->argb_fb_100);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);
}

static void constant_alpha_max(data_t *data, enum pipe pipe, igt_plane_t *plane)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc, crc;

	if (plane->type != DRM_PLANE_TYPE_PRIMARY)
		igt_plane_set_fb(igt_pipe_get_plane_type(&display->pipes[pipe], DRM_PLANE_TYPE_PRIMARY), &data->gray_fb);

	igt_plane_set_fb(plane, &data->argb_fb_100);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);

	igt_plane_set_prop_enum(plane, IGT_PLANE_PIXEL_BLEND_MODE, "None");
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);

	igt_plane_set_fb(plane, &data->argb_fb_cov_0);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);

	igt_plane_set_fb(plane, &data->xrgb_fb);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);

	igt_plane_set_fb(plane, NULL);
}

static void alpha_7efc(data_t *data, enum pipe pipe, igt_plane_t *plane)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc = {}, crc = {};

	if (plane->type != DRM_PLANE_TYPE_PRIMARY)
		igt_plane_set_fb(igt_pipe_get_plane_type(&display->pipes[pipe], DRM_PLANE_TYPE_PRIMARY), &data->gray_fb);

	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_start(data->pipe_crc);

	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0x7e7e);
	igt_plane_set_fb(plane, &data->argb_fb_fc);
	igt_display_commit2(display, COMMIT_ATOMIC);

	igt_pipe_crc_get_current(display->drm_fd, data->pipe_crc, &ref_crc);

	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0xfcfc);
	igt_plane_set_fb(plane, &data->argb_fb_7e);
	igt_display_commit2(display, COMMIT_ATOMIC);

	igt_pipe_crc_get_current(display->drm_fd, data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);

	igt_pipe_crc_stop(data->pipe_crc);
}

static void coverage_7efc(data_t *data, enum pipe pipe, igt_plane_t *plane)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc = {}, crc = {};

	igt_require(igt_plane_try_prop_enum(plane, IGT_PLANE_PIXEL_BLEND_MODE, "Coverage"));
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_start(data->pipe_crc);

	/* for coverage, plane alpha and fb alpha should be swappable, so swap fb and alpha */
	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0x7e7e);
	igt_plane_set_fb(plane, &data->argb_fb_cov_fc);
	igt_display_commit2(display, COMMIT_ATOMIC);

	igt_pipe_crc_get_current(display->drm_fd, data->pipe_crc, &ref_crc);

	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0xfcfc);
	igt_plane_set_fb(plane, &data->argb_fb_cov_7e);
	igt_display_commit2(display, COMMIT_ATOMIC);

	igt_pipe_crc_get_current(display->drm_fd, data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);

	igt_pipe_crc_stop(data->pipe_crc);
}

static void coverage_premult_constant(data_t *data, enum pipe pipe, igt_plane_t *plane)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc = {}, crc = {};

	/* Set a background color on the primary fb for testing */
	if (plane->type != DRM_PLANE_TYPE_PRIMARY)
		igt_plane_set_fb(igt_pipe_get_plane_type(&display->pipes[pipe], DRM_PLANE_TYPE_PRIMARY), &data->gray_fb);

	igt_require(igt_plane_try_prop_enum(plane, IGT_PLANE_PIXEL_BLEND_MODE, "Coverage"));
	igt_plane_set_fb(plane, &data->argb_fb_cov_7e);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_start(data->pipe_crc);
	igt_pipe_crc_get_single(data->pipe_crc, &ref_crc);

	igt_plane_set_prop_enum(plane, IGT_PLANE_PIXEL_BLEND_MODE, "Pre-multiplied");
	igt_plane_set_fb(plane, &data->argb_fb_7e);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_get_current(display->drm_fd, data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);

	igt_plane_set_prop_enum(plane, IGT_PLANE_PIXEL_BLEND_MODE, "None");
	igt_plane_set_prop_value(plane, IGT_PLANE_ALPHA, 0x7e7e);
	igt_plane_set_fb(plane, &data->argb_fb_cov_7e);
	igt_display_commit2(display, COMMIT_ATOMIC);
	igt_pipe_crc_get_current(display->drm_fd, data->pipe_crc, &crc);
	igt_assert_crc_equal(&ref_crc, &crc);

	igt_pipe_crc_stop(data->pipe_crc);
}

static void run_test_on_pipe_planes(data_t *data, enum pipe pipe, igt_output_t *output,
				    bool blend, bool must_multiply,
				    void(*test)(data_t *, enum pipe, igt_plane_t *))
{
	igt_display_t *display = &data->display;
	igt_plane_t *plane;
	int first_plane = -1;
	int last_plane = -1;

	for_each_plane_on_pipe(display, pipe, plane) {
		if (!igt_plane_has_prop(plane, IGT_PLANE_ALPHA))
			continue;

		if (blend && !igt_plane_has_prop(plane, IGT_PLANE_PIXEL_BLEND_MODE))
			continue;

		/* reset plane alpha properties between each plane */
		reset_alpha(display, pipe);

		if (must_multiply && !has_multiplied_alpha(data, plane))
			continue;

		/* Get first & last valid planes. */
		if (first_plane < 0)
			first_plane = j__;

		last_plane = j__;
	}

	for_each_plane_on_pipe(display, pipe, plane) {
		if (!igt_plane_has_prop(plane, IGT_PLANE_ALPHA))
			continue;

		if (blend && !igt_plane_has_prop(plane, IGT_PLANE_PIXEL_BLEND_MODE))
			continue;

		/* Reset plane alpha properties between each plane. */
		reset_alpha(display, pipe);

		if (must_multiply && !has_multiplied_alpha(data, plane))
			continue;

		if (!extended && j__ != first_plane && j__ != last_plane)
			continue;

		igt_info("Testing plane %u\n", plane->index);
		test(data, pipe, plane);
		igt_plane_set_fb(plane, NULL);
	}

	igt_output_set_pipe(output, PIPE_NONE);
	igt_display_commit2(display, COMMIT_ATOMIC);
}

static const struct {
	const char *name;
	void (*test)(data_t *, enum pipe, igt_plane_t *);
	bool blend;
	bool must_multiply;
	const char *desc;
} subtests[] = {
	{ .name = "alpha-basic",
	  .test = basic_alpha,
	  .blend = false,
	  .must_multiply = true,
	  .desc = "Tests basic plane alpha properties.",
	},
	{ .name = "alpha-7efc",
	  .test = alpha_7efc,
	  .blend = false,
	  .must_multiply = true,
	  .desc = "Uses alpha values 0x7e and 0xfc to validate fg.alpha and "
		  "plane_alpha are swappable on pre-multiplied blend mode.",
	},
	{ .name = "coverage-7efc",
	  .test = coverage_7efc,
	  .blend = true,
	  .must_multiply = true,
	  .desc = "Uses alpha values 0x7e and 0xfc to validate fg.alpha and "
		  "plane_alpha are swappable on coverage blend mode.",
	},
	{ .name = "coverage-vs-premult-vs-constant",
	  .test = coverage_premult_constant,
	  .blend = true,
	  .must_multiply = false,
	  .desc = "Tests pipe coverage blending properties.",
	},
	{ .name = "alpha-transparent-fb",
	  .test = argb_transparent,
	  .blend = false,
	  .must_multiply = false,
	  .desc = "Tests the alpha property with transparent fb.",
	},
	{ .name = "alpha-opaque-fb",
	  .test = argb_opaque,
	  .blend = false,
	  .must_multiply = false,
	  .desc = "Tests alpha properties with opaque fb.",
	},
	{ .name = "constant-alpha-min",
	  .test = constant_alpha_min,
	  .blend = true,
	  .must_multiply = false,
	  .desc = "Tests plane alpha and blending properties with minimum alpha value.",
	},
	{ .name = "constant-alpha-mid",
	  .test = constant_alpha_mid,
	  .blend = true,
	  .must_multiply = false,
	  .desc = "Tests plane alpha and blending properties with medium alpha value.",
	},
	{ .name = "constant-alpha-max",
	  .test = constant_alpha_max,
	  .blend = true,
	  .must_multiply = false,
	  .desc = "Tests plane alpha and blending properties with maximum alpha value.",
	},
};

static bool pipe_check(data_t *data, enum pipe pipe,
		       bool blend, bool must_multiply)
{
	igt_display_t *display = &data->display;
	igt_plane_t *plane;
	bool plane_alpha = false, plane_blend = false, multiply = false;

	igt_display_require_output_on_pipe(display, pipe);
	for_each_plane_on_pipe(display, pipe, plane) {
		if (!igt_plane_has_prop(plane, IGT_PLANE_ALPHA))
			continue;
		plane_alpha = true;

		if (blend && !igt_plane_has_prop(plane, IGT_PLANE_PIXEL_BLEND_MODE))
			continue;
		plane_blend = true;

		/* reset plane alpha properties between each plane */
		reset_alpha(display, pipe);

		if (must_multiply && !has_multiplied_alpha(data, plane))
			continue;
		multiply = true;

		break;
	}

	if (!plane_alpha || !plane_blend || !multiply) {
		if (!plane_alpha)
			igt_debug("No planes with alpha property found\n");
		if (!plane_blend)
			igt_debug("No planes with pixel blending mode property found\n");
		if (!multiply)
			igt_debug("Multiplied (plane x pixel) alpha not available\n");

		return false;
	} else {
		return true;
	}
}

static void run_subtests(data_t *data)
{
	igt_output_t *output;
	enum pipe pipe;

	for (int i = 0; i < ARRAY_SIZE(subtests); i++) {
		igt_describe_f("%s\n", subtests[i].desc);

		igt_subtest_with_dynamic(subtests[i].name) {
			for_each_pipe_with_single_output(&data->display, pipe, output) {
				if (!extended &&
				    pipe != active_pipes[0] &&
				    pipe != active_pipes[last_pipe])
					continue;

				igt_display_reset(&data->display);

				igt_output_set_pipe(output, pipe);
				if (!intel_pipe_output_combo_valid(&data->display))
					continue;

				prepare_crtc(data, output, pipe);
				if (!pipe_check(data, pipe, subtests[i].blend, subtests[i].must_multiply))
					continue;

				igt_dynamic_f("pipe-%s-%s", kmstest_pipe_name(pipe), output->name)
					run_test_on_pipe_planes(data, pipe, output, subtests[i].blend,
								subtests[i].must_multiply, subtests[i].test);
			}
		}
	}
}

static int opt_handler(int opt, int opt_index, void *_data)
{
	switch (opt) {
	case 'e':
		extended = true;
		break;
	default:
		return IGT_OPT_HANDLER_ERROR;
	}

	return IGT_OPT_HANDLER_SUCCESS;
}

const char *help_str =
	"  -e \tExtended tests.\n";

igt_main_args("e", NULL, help_str, opt_handler, NULL)
{
	data_t data = {};

	igt_fixture {
		enum pipe pipe;

		last_pipe = 0;

		data.gfx_fd = drm_open_driver_master(DRIVER_ANY);
		igt_require_pipe_crc(data.gfx_fd);
		igt_display_require(&data.display, data.gfx_fd);
		igt_require(data.display.is_atomic);

		/* Get active pipes. */
		for_each_pipe(&data.display, pipe)
			active_pipes[last_pipe++] = pipe;
		last_pipe--;
	}

	run_subtests(&data);

	igt_fixture {
		remove_fbs(&data);
		igt_display_reset(&data.display);
		igt_display_commit2(&data.display, data.display.is_atomic ?
				    COMMIT_ATOMIC : COMMIT_LEGACY);

		igt_display_fini(&data.display);
		drm_close_driver(data.gfx_fd);
	}
}