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
|
// SPDX-License-Identifier: GPL-2.0+
#include <linux/crc32.h>
#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_blend.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_fixed.h>
#include <drm/drm_gem_framebuffer_helper.h>
#include <drm/drm_vblank.h>
#include <linux/minmax.h>
#include "vkms_drv.h"
static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
{
u32 new_color;
new_color = (src * 0xffff + dst * (0xffff - alpha));
return DIV_ROUND_CLOSEST(new_color, 0xffff);
}
/**
* pre_mul_alpha_blend - alpha blending equation
* @stage_buffer: The line with the pixels from src_plane
* @output_buffer: A line buffer that receives all the blends output
* @x_start: The start offset
* @pixel_count: The number of pixels to blend
*
* The pixels [@x_start;@x_start+@pixel_count) in stage_buffer are blended at
* [@x_start;@x_start+@pixel_count) in output_buffer.
*
* The current DRM assumption is that pixel color values have been already
* pre-multiplied with the alpha channel values. See more
* drm_plane_create_blend_mode_property(). Also, this formula assumes a
* completely opaque background.
*/
static void pre_mul_alpha_blend(const struct line_buffer *stage_buffer,
struct line_buffer *output_buffer, int x_start, int pixel_count)
{
struct pixel_argb_u16 *out = &output_buffer->pixels[x_start];
const struct pixel_argb_u16 *in = &stage_buffer->pixels[x_start];
for (int i = 0; i < pixel_count; i++) {
out[i].a = (u16)0xffff;
out[i].r = pre_mul_blend_channel(in[i].r, out[i].r, in[i].a);
out[i].g = pre_mul_blend_channel(in[i].g, out[i].g, in[i].a);
out[i].b = pre_mul_blend_channel(in[i].b, out[i].b, in[i].a);
}
}
static void fill_background(const struct pixel_argb_u16 *background_color,
struct line_buffer *output_buffer)
{
for (size_t i = 0; i < output_buffer->n_pixels; i++)
output_buffer->pixels[i] = *background_color;
}
// lerp(a, b, t) = a + (b - a) * t
static u16 lerp_u16(u16 a, u16 b, s64 t)
{
s64 a_fp = drm_int2fixp(a);
s64 b_fp = drm_int2fixp(b);
s64 delta = drm_fixp_mul(b_fp - a_fp, t);
return drm_fixp2int_round(a_fp + delta);
}
static s64 get_lut_index(const struct vkms_color_lut *lut, u16 channel_value)
{
s64 color_channel_fp = drm_int2fixp(channel_value);
return drm_fixp_mul(color_channel_fp, lut->channel_value2index_ratio);
}
/*
* This enum is related to the positions of the variables inside
* `struct drm_color_lut`, so the order of both needs to be the same.
*/
enum lut_channel {
LUT_RED = 0,
LUT_GREEN,
LUT_BLUE,
LUT_RESERVED
};
static u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_value,
enum lut_channel channel)
{
s64 lut_index = get_lut_index(lut, channel_value);
u16 *floor_lut_value, *ceil_lut_value;
u16 floor_channel_value, ceil_channel_value;
/*
* This checks if `struct drm_color_lut` has any gap added by the compiler
* between the struct fields.
*/
static_assert(sizeof(struct drm_color_lut) == sizeof(__u16) * 4);
floor_lut_value = (__u16 *)&lut->base[drm_fixp2int(lut_index)];
if (drm_fixp2int(lut_index) == (lut->lut_length - 1))
/* We're at the end of the LUT array, use same value for ceil and floor */
ceil_lut_value = floor_lut_value;
else
ceil_lut_value = (__u16 *)&lut->base[drm_fixp2int_ceil(lut_index)];
floor_channel_value = floor_lut_value[channel];
ceil_channel_value = ceil_lut_value[channel];
return lerp_u16(floor_channel_value, ceil_channel_value,
lut_index & DRM_FIXED_DECIMAL_MASK);
}
static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buffer *output_buffer)
{
if (!crtc_state->gamma_lut.base)
return;
if (!crtc_state->gamma_lut.lut_length)
return;
for (size_t x = 0; x < output_buffer->n_pixels; x++) {
struct pixel_argb_u16 *pixel = &output_buffer->pixels[x];
pixel->r = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->r, LUT_RED);
pixel->g = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->g, LUT_GREEN);
pixel->b = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->b, LUT_BLUE);
}
}
/**
* direction_for_rotation() - Get the correct reading direction for a given rotation
*
* @rotation: Rotation to analyze. It correspond the field @frame_info.rotation.
*
* This function will use the @rotation setting of a source plane to compute the reading
* direction in this plane which correspond to a "left to right writing" in the CRTC.
* For example, if the buffer is reflected on X axis, the pixel must be read from right to left
* to be written from left to right on the CRTC.
*/
static enum pixel_read_direction direction_for_rotation(unsigned int rotation)
{
struct drm_rect tmp_a, tmp_b;
int x, y;
/*
* Points A and B are depicted as zero-size rectangles on the CRTC.
* The CRTC writing direction is from A to B. The plane reading direction
* is discovered by inverse-transforming A and B.
* The reading direction is computed by rotating the vector AB (top-left to top-right) in a
* 1x1 square.
*/
tmp_a = DRM_RECT_INIT(0, 0, 0, 0);
tmp_b = DRM_RECT_INIT(1, 0, 0, 0);
drm_rect_rotate_inv(&tmp_a, 1, 1, rotation);
drm_rect_rotate_inv(&tmp_b, 1, 1, rotation);
x = tmp_b.x1 - tmp_a.x1;
y = tmp_b.y1 - tmp_a.y1;
if (x == 1 && y == 0)
return READ_LEFT_TO_RIGHT;
else if (x == -1 && y == 0)
return READ_RIGHT_TO_LEFT;
else if (y == 1 && x == 0)
return READ_TOP_TO_BOTTOM;
else if (y == -1 && x == 0)
return READ_BOTTOM_TO_TOP;
WARN_ONCE(true, "The inverse of the rotation gives an incorrect direction.");
return READ_LEFT_TO_RIGHT;
}
/**
* clamp_line_coordinates() - Compute and clamp the coordinate to read and write during the blend
* process.
*
* @direction: direction of the reading
* @current_plane: current plane blended
* @src_line: source line of the reading. Only the top-left coordinate is used. This rectangle
* must be rotated and have a shape of 1*pixel_count if @direction is vertical and a shape of
* pixel_count*1 if @direction is horizontal.
* @src_x_start: x start coordinate for the line reading
* @src_y_start: y start coordinate for the line reading
* @dst_x_start: x coordinate to blend the read line
* @pixel_count: number of pixels to blend
*
* This function is mainly a safety net to avoid reading outside the source buffer. As the
* userspace should never ask to read outside the source plane, all the cases covered here should
* be dead code.
*/
static void clamp_line_coordinates(enum pixel_read_direction direction,
const struct vkms_plane_state *current_plane,
const struct drm_rect *src_line, int *src_x_start,
int *src_y_start, int *dst_x_start, int *pixel_count)
{
/* By default the start points are correct */
*src_x_start = src_line->x1;
*src_y_start = src_line->y1;
*dst_x_start = current_plane->frame_info->dst.x1;
/* Get the correct number of pixel to blend, it depends of the direction */
switch (direction) {
case READ_LEFT_TO_RIGHT:
case READ_RIGHT_TO_LEFT:
*pixel_count = drm_rect_width(src_line);
break;
case READ_BOTTOM_TO_TOP:
case READ_TOP_TO_BOTTOM:
*pixel_count = drm_rect_height(src_line);
break;
}
/*
* Clamp the coordinates to avoid reading outside the buffer
*
* This is mainly a security check to avoid reading outside the buffer, the userspace
* should never request to read outside the source buffer.
*/
switch (direction) {
case READ_LEFT_TO_RIGHT:
case READ_RIGHT_TO_LEFT:
if (*src_x_start < 0) {
*pixel_count += *src_x_start;
*dst_x_start -= *src_x_start;
*src_x_start = 0;
}
if (*src_x_start + *pixel_count > current_plane->frame_info->fb->width)
*pixel_count = max(0, (int)current_plane->frame_info->fb->width -
*src_x_start);
break;
case READ_BOTTOM_TO_TOP:
case READ_TOP_TO_BOTTOM:
if (*src_y_start < 0) {
*pixel_count += *src_y_start;
*dst_x_start -= *src_y_start;
*src_y_start = 0;
}
if (*src_y_start + *pixel_count > current_plane->frame_info->fb->height)
*pixel_count = max(0, (int)current_plane->frame_info->fb->height -
*src_y_start);
break;
}
}
/**
* blend_line() - Blend a line from a plane to the output buffer
*
* @current_plane: current plane to work on
* @y: line to write in the output buffer
* @crtc_x_limit: width of the output buffer
* @stage_buffer: temporary buffer to convert the pixel line from the source buffer
* @output_buffer: buffer to blend the read line into.
*/
static void blend_line(struct vkms_plane_state *current_plane, int y,
int crtc_x_limit, struct line_buffer *stage_buffer,
struct line_buffer *output_buffer)
{
int src_x_start, src_y_start, dst_x_start, pixel_count;
struct drm_rect dst_line, tmp_src, src_line;
/* Avoid rendering useless lines */
if (y < current_plane->frame_info->dst.y1 ||
|