1 /* 2 * Copyright 2016 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: AMD 23 * 24 */ 25 26 #include "dc.h" 27 #include "opp.h" 28 #include "color_gamma.h" 29 30 31 #define NUM_PTS_IN_REGION 16 32 #define NUM_REGIONS 32 33 #define MAX_HW_POINTS (NUM_PTS_IN_REGION*NUM_REGIONS) 34 35 static struct hw_x_point coordinates_x[MAX_HW_POINTS + 2]; 36 37 static struct fixed31_32 pq_table[MAX_HW_POINTS + 2]; 38 static struct fixed31_32 de_pq_table[MAX_HW_POINTS + 2]; 39 40 static bool pq_initialized; /* = false; */ 41 static bool de_pq_initialized; /* = false; */ 42 43 /* one-time setup of X points */ 44 void setup_x_points_distribution(void) 45 { 46 struct fixed31_32 region_size = dc_fixpt_from_int(128); 47 int32_t segment; 48 uint32_t seg_offset; 49 uint32_t index; 50 struct fixed31_32 increment; 51 52 coordinates_x[MAX_HW_POINTS].x = region_size; 53 coordinates_x[MAX_HW_POINTS + 1].x = region_size; 54 55 for (segment = 6; segment > (6 - NUM_REGIONS); segment--) { 56 region_size = dc_fixpt_div_int(region_size, 2); 57 increment = dc_fixpt_div_int(region_size, 58 NUM_PTS_IN_REGION); 59 seg_offset = (segment + (NUM_REGIONS - 7)) * NUM_PTS_IN_REGION; 60 coordinates_x[seg_offset].x = region_size; 61 62 for (index = seg_offset + 1; 63 index < seg_offset + NUM_PTS_IN_REGION; 64 index++) { 65 coordinates_x[index].x = dc_fixpt_add 66 (coordinates_x[index-1].x, increment); 67 } 68 } 69 } 70 71 static void compute_pq(struct fixed31_32 in_x, struct fixed31_32 *out_y) 72 { 73 /* consts for PQ gamma formula. */ 74 const struct fixed31_32 m1 = 75 dc_fixpt_from_fraction(159301758, 1000000000); 76 const struct fixed31_32 m2 = 77 dc_fixpt_from_fraction(7884375, 100000); 78 const struct fixed31_32 c1 = 79 dc_fixpt_from_fraction(8359375, 10000000); 80 const struct fixed31_32 c2 = 81 dc_fixpt_from_fraction(188515625, 10000000); 82 const struct fixed31_32 c3 = 83 dc_fixpt_from_fraction(186875, 10000); 84 85 struct fixed31_32 l_pow_m1; 86 struct fixed31_32 base; 87 88 if (dc_fixpt_lt(in_x, dc_fixpt_zero)) 89 in_x = dc_fixpt_zero; 90 91 l_pow_m1 = dc_fixpt_pow(in_x, m1); 92 base = dc_fixpt_div( 93 dc_fixpt_add(c1, 94 (dc_fixpt_mul(c2, l_pow_m1))), 95 dc_fixpt_add(dc_fixpt_one, 96 (dc_fixpt_mul(c3, l_pow_m1)))); 97 *out_y = dc_fixpt_pow(base, m2); 98 } 99 100 static void compute_de_pq(struct fixed31_32 in_x, struct fixed31_32 *out_y) 101 { 102 /* consts for dePQ gamma formula. */ 103 const struct fixed31_32 m1 = 104 dc_fixpt_from_fraction(159301758, 1000000000); 105 const struct fixed31_32 m2 = 106 dc_fixpt_from_fraction(7884375, 100000); 107 const struct fixed31_32 c1 = 108 dc_fixpt_from_fraction(8359375, 10000000); 109 const struct fixed31_32 c2 = 110 dc_fixpt_from_fraction(188515625, 10000000); 111 const struct fixed31_32 c3 = 112 dc_fixpt_from_fraction(186875, 10000); 113 114 struct fixed31_32 l_pow_m1; 115 struct fixed31_32 base, div; 116 117 118 if (dc_fixpt_lt(in_x, dc_fixpt_zero)) 119 in_x = dc_fixpt_zero; 120 121 l_pow_m1 = dc_fixpt_pow(in_x, 122 dc_fixpt_div(dc_fixpt_one, m2)); 123 base = dc_fixpt_sub(l_pow_m1, c1); 124 125 if (dc_fixpt_lt(base, dc_fixpt_zero)) 126 base = dc_fixpt_zero; 127 128 div = dc_fixpt_sub(c2, dc_fixpt_mul(c3, l_pow_m1)); 129 130 *out_y = dc_fixpt_pow(dc_fixpt_div(base, div), 131 dc_fixpt_div(dc_fixpt_one, m1)); 132 133 } 134 135 /*de gamma, none linear to linear*/ 136 static void compute_hlg_oetf(struct fixed31_32 in_x, bool is_light0_12, struct fixed31_32 *out_y) 137 { 138 struct fixed31_32 a; 139 struct fixed31_32 b; 140 struct fixed31_32 c; 141 struct fixed31_32 threshold; 142 struct fixed31_32 reference_white_level; 143 144 a = dc_fixpt_from_fraction(17883277, 100000000); 145 if (is_light0_12) { 146 /*light 0-12*/ 147 b = dc_fixpt_from_fraction(28466892, 100000000); 148 c = dc_fixpt_from_fraction(55991073, 100000000); 149 threshold = dc_fixpt_one; 150 reference_white_level = dc_fixpt_half; 151 } else { 152 /*light 0-1*/ 153 b = dc_fixpt_from_fraction(2372241, 100000000); 154 c = dc_fixpt_add(dc_fixpt_one, dc_fixpt_from_fraction(429347, 100000000)); 155 threshold = dc_fixpt_from_fraction(1, 12); 156 reference_white_level = dc_fixpt_pow(dc_fixpt_from_fraction(3, 1), dc_fixpt_half); 157 } 158 if (dc_fixpt_lt(threshold, in_x)) 159 *out_y = dc_fixpt_add(c, dc_fixpt_mul(a, dc_fixpt_log(dc_fixpt_sub(in_x, b)))); 160 else 161 *out_y = dc_fixpt_mul(dc_fixpt_pow(in_x, dc_fixpt_half), reference_white_level); 162 } 163 164 /*re gamma, linear to none linear*/ 165 static void compute_hlg_eotf(struct fixed31_32 in_x, bool is_light0_12, struct fixed31_32 *out_y) 166 { 167 struct fixed31_32 a; 168 struct fixed31_32 b; 169 struct fixed31_32 c; 170 struct fixed31_32 reference_white_level; 171 172 a = dc_fixpt_from_fraction(17883277, 100000000); 173 if (is_light0_12) { 174 /*light 0-12*/ 175 b = dc_fixpt_from_fraction(28466892, 100000000); 176 c = dc_fixpt_from_fraction(55991073, 100000000); 177 reference_white_level = dc_fixpt_from_fraction(4, 1); 178 } else { 179 /*light 0-1*/ 180 b = dc_fixpt_from_fraction(2372241, 100000000); 181 c = dc_fixpt_add(dc_fixpt_one, dc_fixpt_from_fraction(429347, 100000000)); 182 reference_white_level = dc_fixpt_from_fraction(1, 3); 183 } 184 if (dc_fixpt_lt(dc_fixpt_half, in_x)) 185 *out_y = dc_fixpt_add(dc_fixpt_exp(dc_fixpt_div(dc_fixpt_sub(in_x, c), a)), b); 186 else 187 *out_y = dc_fixpt_mul(dc_fixpt_pow(in_x, dc_fixpt_from_fraction(2, 1)), reference_white_level); 188 } 189 190 191 /* one-time pre-compute PQ values - only for sdr_white_level 80 */ 192 void precompute_pq(void) 193 { 194 int i; 195 struct fixed31_32 x; 196 const struct hw_x_point *coord_x = coordinates_x + 32; 197 struct fixed31_32 scaling_factor = 198 dc_fixpt_from_fraction(80, 10000); 199 200 /* pow function has problems with arguments too small */ 201 for (i = 0; i < 32; i++) 202 pq_table[i] = dc_fixpt_zero; 203 204 for (i = 32; i <= MAX_HW_POINTS; i++) { 205 x = dc_fixpt_mul(coord_x->x, scaling_factor); 206 compute_pq(x, &pq_table[i]); 207 ++coord_x; 208 } 209 } 210 211 /* one-time pre-compute dePQ values - only for max pixel value 125 FP16 */ 212 void precompute_de_pq(void) 213 { 214 int i; 215 struct fixed31_32 y; 216 uint32_t begin_index, end_index; 217 218 struct fixed31_32 scaling_factor = dc_fixpt_from_int(125); 219 220 /* X points is 2^-25 to 2^7 221 * De-gamma X is 2^-12 to 2^0 – we are skipping first -12-(-25) = 13 regions 222 */ 223 begin_index = 13 * NUM_PTS_IN_REGION; 224 end_index = begin_index + 12 * NUM_PTS_IN_REGION; 225 226 for (i = 0; i <= begin_index; i++) 227 de_pq_table[i] = dc_fixpt_zero; 228 229 for (; i <= end_index; i++) { 230 compute_de_pq(coordinates_x[i].x, &y); 231 de_pq_table[i] = dc_fixpt_mul(y, scaling_factor); 232 } 233 234 for (; i <= MAX_HW_POINTS; i++) 235 de_pq_table[i] = de_pq_table[i-1]; 236 } 237 struct dividers { 238 struct fixed31_32 divider1; 239 struct fixed31_32 divider2; 240 struct fixed31_32 divider3; 241 }; 242 243 static void build_coefficients(struct gamma_coefficients *coefficients, bool is_2_4) 244 { 245 static const int32_t numerator01[] = { 31308, 180000}; 246 static const int32_t numerator02[] = { 12920, 4500}; 247 static const int32_t numerator03[] = { 55, 99}; 248 static const int32_t numerator04[] = { 55, 99}; 249 static const int32_t numerator05[] = { 2400, 2200}; 250 251 uint32_t i = 0; 252 uint32_t index = is_2_4 == true ? 0:1; 253 254 do { 255 coefficients->a0[i] = dc_fixpt_from_fraction( 256 numerator01[index], 10000000); 257 coefficients->a1[i] = dc_fixpt_from_fraction( 258 numerator02[index], 1000); 259 coefficients->a2[i] = dc_fixpt_from_fraction( 260 numerator03[index], 1000); 261 coefficients->a3[i] = dc_fixpt_from_fraction( 262 numerator04[index], 1000); 263 coefficients->user_gamma[i] = dc_fixpt_from_fraction( 264 numerator05[index], 1000); 265 266 ++i; 267 } while (i != ARRAY_SIZE(coefficients->a0)); 268 } 269 270 static struct fixed31_32 translate_from_linear_space( 271 struct fixed31_32 arg, 272 struct fixed31_32 a0, 273 struct fixed31_32 a1, 274 struct fixed31_32 a2, 275 struct fixed31_32 a3, 276 struct fixed31_32 gamma) 277 { 278 const struct fixed31_32 one = dc_fixpt_from_int(1); 279 280 if (dc_fixpt_lt(one, arg)) 281 return one; 282 283 if (dc_fixpt_le(arg, dc_fixpt_neg(a0))) 284 return dc_fixpt_sub( 285 a2, 286 dc_fixpt_mul( 287 dc_fixpt_add( 288 one, 289 a3), 290 dc_fixpt_pow( 291 dc_fixpt_neg(arg), 292 dc_fixpt_recip(gamma)))); 293 else if (dc_fixpt_le(a0, arg)) 294 return dc_fixpt_sub( 295 dc_fixpt_mul( 296 dc_fixpt_add( 297 one, 298 a3), 299 dc_fixpt_pow( 300 arg, 301 dc_fixpt_recip(gamma))), 302 a2); 303 else 304 return dc_fixpt_mul( 305 arg, 306 a1); 307 } 308 309 static struct fixed31_32 translate_to_linear_space( 310 struct fixed31_32 arg, 311 struct fixed31_32 a0, 312 struct fixed31_32 a1, 313 struct fixed31_32 a2, 314 struct fixed31_32 a3, 315 struct fixed31_32 gamma) 316 { 317 struct fixed31_32 linear; 318 319 a0 = dc_fixpt_mul(a0, a1); 320 if (dc_fixpt_le(arg, dc_fixpt_neg(a0))) 321 322 linear = dc_fixpt_neg( 323 dc_fixpt_pow( 324 dc_fixpt_div( 325 dc_fixpt_sub(a2, arg), 326 dc_fixpt_add( 327 dc_fixpt_one, a3)), gamma)); 328 329 else if (dc_fixpt_le(dc_fixpt_neg(a0), arg) && 330 dc_fixpt_le(arg, a0)) 331 linear = dc_fixpt_div(arg, a1); 332 else 333 linear = dc_fixpt_pow( 334 dc_fixpt_div( 335 dc_fixpt_add(a2, arg), 336 dc_fixpt_add( 337 dc_fixpt_one, a3)), gamma); 338 339 return linear; 340 } 341 342 static inline struct fixed31_32 translate_from_linear_space_ex( 343 struct fixed31_32 arg, 344 struct gamma_coefficients *coeff, 345 uint32_t color_index) 346 { 347 return translate_from_linear_space( 348 arg, 349 coeff->a0[color_index], 350 coeff->a1[color_index], 351 coeff->a2[color_index], 352 coeff->a3[color_index], 353 coeff->user_gamma[color_index]); 354 } 355 356 357 static inline struct fixed31_32 translate_to_linear_space_ex( 358 struct fixed31_32 arg, 359 struct gamma_coefficients *coeff, 360 uint32_t color_index) 361 { 362 return translate_to_linear_space( 363 arg, 364 coeff->a0[color_index], 365 coeff->a1[color_index], 366 coeff->a2[color_index], 367 coeff->a3[color_index], 368 coeff->user_gamma[color_index]); 369 } 370 371 372 static bool find_software_points( 373 const struct dc_gamma *ramp, 374 const struct gamma_pixel *axis_x, 375 struct fixed31_32 hw_point, 376 enum channel_name channel, 377 uint32_t *index_to_start, 378 uint32_t *index_left, 379 uint32_t *index_right, 380 enum hw_point_position *pos) 381 { 382 const uint32_t max_number = ramp->num_entries + 3; 383 384 struct fixed31_32 left, right; 385 386 uint32_t i = *index_to_start; 387 388 while (i < max_number) { 389 if (channel == CHANNEL_NAME_RED) { 390 left = axis_x[i].r; 391 392 if (i < max_number - 1) 393 right = axis_x[i + 1].r; 394 else 395 right = axis_x[max_number - 1].r; 396 } else if (channel == CHANNEL_NAME_GREEN) { 397 left = axis_x[i].g; 398 399 if (i < max_number - 1) 400 right = axis_x[i + 1].g; 401 else 402 right = axis_x[max_number - 1].g; 403 } else { 404 left = axis_x[i].b; 405 406 if (i < max_number - 1) 407 right = axis_x[i + 1].b; 408 else 409 right = axis_x[max_number - 1].b; 410 } 411 412 if (dc_fixpt_le(left, hw_point) && 413 dc_fixpt_le(hw_point, right)) { 414 *index_to_start = i; 415 *index_left = i; 416 417 if (i < max_number - 1) 418 *index_right = i + 1; 419 else 420 *index_right = max_number - 1; 421 422 *pos = HW_POINT_POSITION_MIDDLE; 423 424 return true; 425 } else if ((i == *index_to_start) && 426 dc_fixpt_le(hw_point, left)) { 427 *index_to_start = i; 428 *index_left = i; 429 *index_right = i; 430 431 *pos = HW_POINT_POSITION_LEFT; 432 433 return true; 434 } else if ((i == max_number - 1) && 435 dc_fixpt_le(right, hw_point)) { 436 *index_to_start = i; 437 *index_left = i; 438 *index_right = i; 439 440 *pos = HW_POINT_POSITION_RIGHT; 441 442 return true; 443 } 444 445 ++i; 446 } 447 448 return false; 449 } 450 451 static bool build_custom_gamma_mapping_coefficients_worker( 452 const struct dc_gamma *ramp, 453 struct pixel_gamma_point *coeff, 454 const struct hw_x_point *coordinates_x, 455 const struct gamma_pixel *axis_x, 456 enum channel_name channel, 457 uint32_t number_of_points) 458 { 459 uint32_t i = 0; 460 461 while (i <= number_of_points) { 462 struct fixed31_32 coord_x; 463 464 uint32_t index_to_start = 0; 465 uint32_t index_left = 0; 466 uint32_t index_right = 0; 467 468 enum hw_point_position hw_pos; 469 470 struct gamma_point *point; 471 472 struct fixed31_32 left_pos; 473 struct fixed31_32 right_pos; 474 475 if (channel == CHANNEL_NAME_RED) 476 coord_x = coordinates_x[i].regamma_y_red; 477 else if (channel == CHANNEL_NAME_GREEN) 478 coord_x = coordinates_x[i].regamma_y_green; 479 else 480 coord_x = coordinates_x[i].regamma_y_blue; 481 482 if (!find_software_points( 483 ramp, axis_x, coord_x, channel, 484 &index_to_start, &index_left, &index_right, &hw_pos)) { 485 BREAK_TO_DEBUGGER(); 486 return false; 487 } 488 489 if (index_left >= ramp->num_entries + 3) { 490 BREAK_TO_DEBUGGER(); 491 return false; 492 } 493 494 if (index_right >= ramp->num_entries + 3) { 495 BREAK_TO_DEBUGGER(); 496 return false; 497 } 498 499 if (channel == CHANNEL_NAME_RED) { 500 point = &coeff[i].r; 501 502 left_pos = axis_x[index_left].r; 503 right_pos = axis_x[index_right].r; 504 } else if (channel == CHANNEL_NAME_GREEN) { 505 point = &coeff[i].g; 506 507 left_pos = axis_x[index_left].g; 508 right_pos = axis_x[index_right].g; 509 } else { 510 point = &coeff[i].b; 511 512 left_pos = axis_x[index_left].b; 513 right_pos = axis_x[index_right].b; 514 } 515 516 if (hw_pos == HW_POINT_POSITION_MIDDLE) 517 point->coeff = dc_fixpt_div( 518 dc_fixpt_sub( 519 coord_x, 520 left_pos), 521 dc_fixpt_sub( 522 right_pos, 523 left_pos)); 524 else if (hw_pos == HW_POINT_POSITION_LEFT) 525 point->coeff = dc_fixpt_zero; 526 else if (hw_pos == HW_POINT_POSITION_RIGHT) 527 point->coeff = dc_fixpt_from_int(2); 528 else { 529 BREAK_TO_DEBUGGER(); 530 return false; 531 } 532 533 point->left_index = index_left; 534 point->right_index = index_right; 535 point->pos = hw_pos; 536 537 ++i; 538 } 539 540 return true; 541 } 542 543 static struct fixed31_32 calculate_mapped_value( 544 struct pwl_float_data *rgb, 545 const struct pixel_gamma_point *coeff, 546 enum channel_name channel, 547 uint32_t max_index) 548 { 549 const struct gamma_point *point; 550 551 struct fixed31_32 result; 552 553 if (channel == CHANNEL_NAME_RED) 554 point = &coeff->r; 555 else if (channel == CHANNEL_NAME_GREEN) 556 point = &coeff->g; 557 else 558 point = &coeff->b; 559 560 if ((point->left_index < 0) || (point->left_index > max_index)) { 561 BREAK_TO_DEBUGGER(); 562 return dc_fixpt_zero; 563 } 564 565 if ((point->right_index < 0) || (point->right_index > max_index)) { 566 BREAK_TO_DEBUGGER(); 567 return dc_fixpt_zero; 568 } 569 570 if (point->pos == HW_POINT_POSITION_MIDDLE) 571 if (channel == CHANNEL_NAME_RED) 572 result = dc_fixpt_add( 573 dc_fixpt_mul( 574 point->coeff, 575 dc_fixpt_sub( 576 rgb[point->right_index].r, 577 rgb[point->left_index].r)), 578 rgb[point->left_index].r); 579 else if (channel == CHANNEL_NAME_GREEN) 580 result = dc_fixpt_add( 581 dc_fixpt_mul( 582 point->coeff, 583 dc_fixpt_sub( 584 rgb[point->right_index].g, 585 rgb[point->left_index].g)), 586 rgb[point->left_index].g); 587 else 588 result = dc_fixpt_add( 589 dc_fixpt_mul( 590 point->coeff, 591 dc_fixpt_sub( 592 rgb[point->right_index].b, 593 rgb[point->left_index].b)), 594 rgb[point->left_index].b); 595 else if (point->pos == HW_POINT_POSITION_LEFT) { 596 BREAK_TO_DEBUGGER(); 597 result = dc_fixpt_zero; 598 } else { 599 BREAK_TO_DEBUGGER(); 600 result = dc_fixpt_one; 601 } 602 603 return result; 604 } 605 606 static void build_pq(struct pwl_float_data_ex *rgb_regamma, 607 uint32_t hw_points_num, 608 const struct hw_x_point *coordinate_x, 609 uint32_t sdr_white_level) 610 { 611 uint32_t i, start_index; 612 613 struct pwl_float_data_ex *rgb = rgb_regamma; 614 const struct hw_x_point *coord_x = coordinate_x; 615 struct fixed31_32 x; 616 struct fixed31_32 output; 617 struct fixed31_32 scaling_factor = 618 dc_fixpt_from_fraction(sdr_white_level, 10000); 619 620 if (!pq_initialized && sdr_white_level == 80) { 621 precompute_pq(); 622 pq_initialized = true; 623 } 624 625 /* TODO: start index is from segment 2^-24, skipping first segment 626 * due to x values too small for power calculations 627 */ 628 start_index = 32; 629 rgb += start_index; 630 coord_x += start_index; 631 632 for (i = start_index; i <= hw_points_num; i++) { 633 /* Multiply 0.008 as regamma is 0-1 and FP16 input is 0-125. 634 * FP 1.0 = 80nits 635 */ 636 if (sdr_white_level == 80) { 637 output = pq_table[i]; 638 } else { 639 x = dc_fixpt_mul(coord_x->x, scaling_factor); 640 compute_pq(x, &output); 641 } 642 643 /* should really not happen? */ 644 if (dc_fixpt_lt(output, dc_fixpt_zero)) 645 output = dc_fixpt_zero; 646 else if (dc_fixpt_lt(dc_fixpt_one, output)) 647 output = dc_fixpt_one; 648 649 rgb->r = output; 650 rgb->g = output; 651 rgb->b = output; 652 653 ++coord_x; 654 ++rgb; 655 } 656 } 657 658 static void build_de_pq(struct pwl_float_data_ex *de_pq, 659 uint32_t hw_points_num, 660 const struct hw_x_point *coordinate_x) 661 { 662 uint32_t i; 663 struct fixed31_32 output; 664 665 struct fixed31_32 scaling_factor = dc_fixpt_from_int(125); 666 667 if (!de_pq_initialized) { 668 precompute_de_pq(); 669 de_pq_initialized = true; 670 } 671 672 673 for (i = 0; i <= hw_points_num; i++) { 674 output = de_pq_table[i]; 675 /* should really not happen? */ 676 if (dc_fixpt_lt(output, dc_fixpt_zero)) 677 output = dc_fixpt_zero; 678 else if (dc_fixpt_lt(scaling_factor, output)) 679 output = scaling_factor; 680 de_pq[i].r = output; 681 de_pq[i].g = output; 682 de_pq[i].b = output; 683 } 684 } 685 686 static void build_regamma(struct pwl_float_data_ex *rgb_regamma, 687 uint32_t hw_points_num, 688 const struct hw_x_point *coordinate_x, bool is_2_4) 689 { 690 uint32_t i; 691 692 struct gamma_coefficients coeff; 693 struct pwl_float_data_ex *rgb = rgb_regamma; 694 const struct hw_x_point *coord_x = coordinate_x; 695 696 build_coefficients(&coeff, is_2_4); 697 698 i = 0; 699 700 while (i != hw_points_num + 1) { 701 /*TODO use y vs r,g,b*/ 702 rgb->r = translate_from_linear_space_ex( 703 coord_x->x, &coeff, 0); 704 rgb->g = rgb->r; 705 rgb->b = rgb->r; 706 ++coord_x; 707 ++rgb; 708 ++i; 709 } 710 } 711 712 static void build_degamma(struct pwl_float_data_ex *curve, 713 uint32_t hw_points_num, 714 const struct hw_x_point *coordinate_x, bool is_2_4) 715 { 716 uint32_t i; 717 struct gamma_coefficients coeff; 718 uint32_t begin_index, end_index; 719 720 build_coefficients(&coeff, is_2_4); 721 i = 0; 722 723 /* X points is 2^-25 to 2^7 724 * De-gamma X is 2^-12 to 2^0 – we are skipping first -12-(-25) = 13 regions 725 */ 726 begin_index = 13 * NUM_PTS_IN_REGION; 727 end_index = begin_index + 12 * NUM_PTS_IN_REGION; 728 729 while (i != begin_index) { 730 curve[i].r = dc_fixpt_zero; 731 curve[i].g = dc_fixpt_zero; 732 curve[i].b = dc_fixpt_zero; 733 i++; 734 } 735 736 while (i != end_index) { 737 curve[i].r = translate_to_linear_space_ex( 738 coordinate_x[i].x, &coeff, 0); 739 curve[i].g = curve[i].r; 740 curve[i].b = curve[i].r; 741 i++; 742 } 743 while (i != hw_points_num + 1) { 744 curve[i].r = dc_fixpt_one; 745 curve[i].g = dc_fixpt_one; 746 curve[i].b = dc_fixpt_one; 747 i++; 748 } 749 } 750 751 static void build_hlg_degamma(struct pwl_float_data_ex *degamma, 752 uint32_t hw_points_num, 753 const struct hw_x_point *coordinate_x, bool is_light0_12) 754 { 755 uint32_t i; 756 757 struct pwl_float_data_ex *rgb = degamma; 758 const struct hw_x_point *coord_x = coordinate_x; 759 760 i = 0; 761 762 while (i != hw_points_num + 1) { 763 compute_hlg_oetf(coord_x->x, is_light0_12, &rgb->r); 764 rgb->g = rgb->r; 765 rgb->b = rgb->r; 766 ++coord_x; 767 ++rgb; 768 ++i; 769 } 770 } 771 772 static void build_hlg_regamma(struct pwl_float_data_ex *regamma, 773 uint32_t hw_points_num, 774 const struct hw_x_point *coordinate_x, bool is_light0_12) 775 { 776 uint32_t i; 777 778 struct pwl_float_data_ex *rgb = regamma; 779 const struct hw_x_point *coord_x = coordinate_x; 780 781 i = 0; 782 783 while (i != hw_points_num + 1) { 784 compute_hlg_eotf(coord_x->x, is_light0_12, &rgb->r); 785 rgb->g = rgb->r; 786 rgb->b = rgb->r; 787 ++coord_x; 788 ++rgb; 789 ++i; 790 } 791 } 792 793 static void scale_gamma(struct pwl_float_data *pwl_rgb, 794 const struct dc_gamma *ramp, 795 struct dividers dividers) 796 { 797 const struct fixed31_32 max_driver = dc_fixpt_from_int(0xFFFF); 798 const struct fixed31_32 max_os = dc_fixpt_from_int(0xFF00); 799 struct fixed31_32 scaler = max_os; 800 uint32_t i; 801 struct pwl_float_data *rgb = pwl_rgb; 802 struct pwl_float_data *rgb_last = rgb + ramp->num_entries - 1; 803 804 i = 0; 805 806 do { 807 if (dc_fixpt_lt(max_os, ramp->entries.red[i]) || 808 dc_fixpt_lt(max_os, ramp->entries.green[i]) || 809 dc_fixpt_lt(max_os, ramp->entries.blue[i])) { 810 scaler = max_driver; 811 break; 812 } 813 ++i; 814 } while (i != ramp->num_entries); 815 816 i = 0; 817 818 do { 819 rgb->r = dc_fixpt_div( 820 ramp->entries.red[i], scaler); 821 rgb->g = dc_fixpt_div( 822 ramp->entries.green[i], scaler); 823 rgb->b = dc_fixpt_div( 824 ramp->entries.blue[i], scaler); 825 826 ++rgb; 827 ++i; 828 } while (i != ramp->num_entries); 829 830 rgb->r = dc_fixpt_mul(rgb_last->r, 831 dividers.divider1); 832 rgb->g = dc_fixpt_mul(rgb_last->g, 833 dividers.divider1); 834 rgb->b = dc_fixpt_mul(rgb_last->b, 835 dividers.divider1); 836 837 ++rgb; 838 839 rgb->r = dc_fixpt_mul(rgb_last->r, 840 dividers.divider2); 841 rgb->g = dc_fixpt_mul(rgb_last->g, 842 dividers.divider2); 843 rgb->b = dc_fixpt_mul(rgb_last->b, 844 dividers.divider2); 845 846 ++rgb; 847 848 rgb->r = dc_fixpt_mul(rgb_last->r, 849 dividers.divider3); 850 rgb->g = dc_fixpt_mul(rgb_last->g, 851 dividers.divider3); 852 rgb->b = dc_fixpt_mul(rgb_last->b, 853 dividers.divider3); 854 } 855 856 static void scale_gamma_dx(struct pwl_float_data *pwl_rgb, 857 const struct dc_gamma *ramp, 858 struct dividers dividers) 859 { 860 uint32_t i; 861 struct fixed31_32 min = dc_fixpt_zero; 862 struct fixed31_32 max = dc_fixpt_one; 863 864 struct fixed31_32 delta = dc_fixpt_zero; 865 struct fixed31_32 offset = dc_fixpt_zero; 866 867 for (i = 0 ; i < ramp->num_entries; i++) { 868 if (dc_fixpt_lt(ramp->entries.red[i], min)) 869 min = ramp->entries.red[i]; 870 871 if (dc_fixpt_lt(ramp->entries.green[i], min)) 872 min = ramp->entries.green[i]; 873 874 if (dc_fixpt_lt(ramp->entries.blue[i], min)) 875 min = ramp->entries.blue[i]; 876 877 if (dc_fixpt_lt(max, ramp->entries.red[i])) 878 max = ramp->entries.red[i]; 879 880 if (dc_fixpt_lt(max, ramp->entries.green[i])) 881 max = ramp->entries.green[i]; 882 883 if (dc_fixpt_lt(max, ramp->entries.blue[i])) 884 max = ramp->entries.blue[i]; 885 } 886 887 if (dc_fixpt_lt(min, dc_fixpt_zero)) 888 delta = dc_fixpt_neg(min); 889 890 offset = dc_fixpt_add(min, max); 891 892 for (i = 0 ; i < ramp->num_entries; i++) { 893 pwl_rgb[i].r = dc_fixpt_div( 894 dc_fixpt_add( 895 ramp->entries.red[i], delta), offset); 896 pwl_rgb[i].g = dc_fixpt_div( 897 dc_fixpt_add( 898 ramp->entries.green[i], delta), offset); 899 pwl_rgb[i].b = dc_fixpt_div( 900 dc_fixpt_add( 901 ramp->entries.blue[i], delta), offset); 902 903 } 904 905 pwl_rgb[i].r = dc_fixpt_sub(dc_fixpt_mul_int( 906 pwl_rgb[i-1].r, 2), pwl_rgb[i-2].r); 907 pwl_rgb[i].g = dc_fixpt_sub(dc_fixpt_mul_int( 908 pwl_rgb[i-1].g, 2), pwl_rgb[i-2].g); 909 pwl_rgb[i].b = dc_fixpt_sub(dc_fixpt_mul_int( 910 pwl_rgb[i-1].b, 2), pwl_rgb[i-2].b); 911 ++i; 912 pwl_rgb[i].r = dc_fixpt_sub(dc_fixpt_mul_int( 913 pwl_rgb[i-1].r, 2), pwl_rgb[i-2].r); 914 pwl_rgb[i].g = dc_fixpt_sub(dc_fixpt_mul_int( 915 pwl_rgb[i-1].g, 2), pwl_rgb[i-2].g); 916 pwl_rgb[i].b = dc_fixpt_sub(dc_fixpt_mul_int( 917 pwl_rgb[i-1].b, 2), pwl_rgb[i-2].b); 918 } 919 920 /* todo: all these scale_gamma functions are inherently the same but 921 * take different structures as params or different format for ramp 922 * values. We could probably implement it in a more generic fashion 923 */ 924 static void scale_user_regamma_ramp(struct pwl_float_data *pwl_rgb, 925 const struct regamma_ramp *ramp, 926 struct dividers dividers) 927 { 928 unsigned short max_driver = 0xFFFF; 929 unsigned short max_os = 0xFF00; 930 unsigned short scaler = max_os; 931 uint32_t i; 932 struct pwl_float_data *rgb = pwl_rgb; 933 struct pwl_float_data *rgb_last = rgb + GAMMA_RGB_256_ENTRIES - 1; 934 935 i = 0; 936 do { 937 if (ramp->gamma[i] > max_os || 938 ramp->gamma[i + 256] > max_os || 939 ramp->gamma[i + 512] > max_os) { 940 scaler = max_driver; 941 break; 942 } 943 i++; 944 } while (i != GAMMA_RGB_256_ENTRIES); 945 946 i = 0; 947 do { 948 rgb->r = dc_fixpt_from_fraction( 949 ramp->gamma[i], scaler); 950 rgb->g = dc_fixpt_from_fraction( 951 ramp->gamma[i + 256], scaler); 952 rgb->b = dc_fixpt_from_fraction( 953 ramp->gamma[i + 512], scaler); 954 955 ++rgb; 956 ++i; 957 } while (i != GAMMA_RGB_256_ENTRIES); 958 959 rgb->r = dc_fixpt_mul(rgb_last->r, 960 dividers.divider1); 961 rgb->g = dc_fixpt_mul(rgb_last->g, 962 dividers.divider1); 963 rgb->b = dc_fixpt_mul(rgb_last->b, 964 dividers.divider1); 965 966 ++rgb; 967 968 rgb->r = dc_fixpt_mul(rgb_last->r, 969 dividers.divider2); 970 rgb->g = dc_fixpt_mul(rgb_last->g, 971 dividers.divider2); 972 rgb->b = dc_fixpt_mul(rgb_last->b, 973 dividers.divider2); 974 975 ++rgb; 976 977 rgb->r = dc_fixpt_mul(rgb_last->r, 978 dividers.divider3); 979 rgb->g = dc_fixpt_mul(rgb_last->g, 980 dividers.divider3); 981 rgb->b = dc_fixpt_mul(rgb_last->b, 982 dividers.divider3); 983 } 984 985 /* 986 * RS3+ color transform DDI - 1D LUT adjustment is composed with regamma here 987 * Input is evenly distributed in the output color space as specified in 988 * SetTimings 989 * 990 * Interpolation details: 991 * 1D LUT has 4096 values which give curve correction in 0-1 float range 992 * for evenly spaced points in 0-1 range. lut1D[index] gives correction 993 * for index/4095. 994 * First we find index for which: 995 * index/4095 < regamma_y < (index+1)/4095 => 996 * index < 4095*regamma_y < index + 1 997 * norm_y = 4095*regamma_y, and index is just truncating to nearest integer 998 * lut1 = lut1D[index], lut2 = lut1D[index+1] 999 * 1000 *adjustedY is then linearly interpolating regamma Y between lut1 and lut2 1001 */ 1002 static void apply_lut_1d( 1003 const struct dc_gamma *ramp, 1004 uint32_t num_hw_points, 1005 struct dc_transfer_func_distributed_points *tf_pts) 1006 { 1007 int i = 0; 1008 int color = 0; 1009 struct fixed31_32 *regamma_y; 1010 struct fixed31_32 norm_y; 1011 struct fixed31_32 lut1; 1012 struct fixed31_32 lut2; 1013 const int max_lut_index = 4095; 1014 const struct fixed31_32 max_lut_index_f = 1015 dc_fixpt_from_int(max_lut_index); 1016 int32_t index = 0, index_next = 0; 1017 struct fixed31_32 index_f; 1018 struct fixed31_32 delta_lut; 1019 struct fixed31_32 delta_index; 1020 1021 if (ramp->type != GAMMA_CS_TFM_1D) 1022 return; // this is not expected 1023 1024 for (i = 0; i < num_hw_points; i++) { 1025 for (color = 0; color < 3; color++) { 1026 if (color == 0) 1027 regamma_y = &tf_pts->red[i]; 1028 else if (color == 1) 1029 regamma_y = &tf_pts->green[i]; 1030 else 1031 regamma_y = &tf_pts->blue[i]; 1032 1033 norm_y = dc_fixpt_mul(max_lut_index_f, 1034 *regamma_y); 1035 index = dc_fixpt_floor(norm_y); 1036 index_f = dc_fixpt_from_int(index); 1037 1038 if (index < 0 || index > max_lut_index) 1039 continue; 1040 1041 index_next = (index == max_lut_index) ? index : index+1; 1042 1043 if (color == 0) { 1044 lut1 = ramp->entries.red[index]; 1045 lut2 = ramp->entries.red[index_next]; 1046 } else if (color == 1) { 1047 lut1 = ramp->entries.green[index]; 1048 lut2 = ramp->entries.green[index_next]; 1049 } else { 1050 lut1 = ramp->entries.blue[index]; 1051 lut2 = ramp->entries.blue[index_next]; 1052 } 1053 1054 // we have everything now, so interpolate 1055 delta_lut = dc_fixpt_sub(lut2, lut1); 1056 delta_index = dc_fixpt_sub(norm_y, index_f); 1057 1058 *regamma_y = dc_fixpt_add(lut1, 1059 dc_fixpt_mul(delta_index, delta_lut)); 1060 } 1061 } 1062 } 1063 1064 static void build_evenly_distributed_points( 1065 struct gamma_pixel *points, 1066 uint32_t numberof_points, 1067 struct dividers dividers) 1068 { 1069 struct gamma_pixel *p = points; 1070 struct gamma_pixel *p_last = p + numberof_points - 1; 1071 1072 uint32_t i = 0; 1073 1074 do { 1075 struct fixed31_32 value = dc_fixpt_from_fraction(i, 1076 numberof_points - 1); 1077 1078 p->r = value; 1079 p->g = value; 1080 p->b = value; 1081 1082 ++p; 1083 ++i; 1084 } while (i != numberof_points); 1085 1086 p->r = dc_fixpt_div(p_last->r, dividers.divider1); 1087 p->g = dc_fixpt_div(p_last->g, dividers.divider1); 1088 p->b = dc_fixpt_div(p_last->b, dividers.divider1); 1089 1090 ++p; 1091 1092 p->r = dc_fixpt_div(p_last->r, dividers.divider2); 1093 p->g = dc_fixpt_div(p_last->g, dividers.divider2); 1094 p->b = dc_fixpt_div(p_last->b, dividers.divider2); 1095 1096 ++p; 1097 1098 p->r = dc_fixpt_div(p_last->r, dividers.divider3); 1099 p->g = dc_fixpt_div(p_last->g, dividers.divider3); 1100 p->b = dc_fixpt_div(p_last->b, dividers.divider3); 1101 } 1102 1103 static inline void copy_rgb_regamma_to_coordinates_x( 1104 struct hw_x_point *coordinates_x, 1105 uint32_t hw_points_num, 1106 const struct pwl_float_data_ex *rgb_ex) 1107 { 1108 struct hw_x_point *coords = coordinates_x; 1109 uint32_t i = 0; 1110 const struct pwl_float_data_ex *rgb_regamma = rgb_ex; 1111 1112 while (i <= hw_points_num + 1) { 1113 coords->regamma_y_red = rgb_regamma->r; 1114 coords->regamma_y_green = rgb_regamma->g; 1115 coords->regamma_y_blue = rgb_regamma->b; 1116 1117 ++coords; 1118 ++rgb_regamma; 1119 ++i; 1120 } 1121 } 1122 1123 static bool calculate_interpolated_hardware_curve( 1124 const struct dc_gamma *ramp, 1125 struct pixel_gamma_point *coeff128, 1126 struct pwl_float_data *rgb_user, 1127 const struct hw_x_point *coordinates_x, 1128 const struct gamma_pixel *axis_x, 1129 uint32_t number_of_points, 1130 struct dc_transfer_func_distributed_points *tf_pts) 1131 { 1132 1133 const struct pixel_gamma_point *coeff = coeff128; 1134 uint32_t max_entries = 3 - 1; 1135 1136 uint32_t i = 0; 1137 1138 for (i = 0; i < 3; i++) { 1139 if (!build_custom_gamma_mapping_coefficients_worker( 1140 ramp, coeff128, coordinates_x, axis_x, i, 1141 number_of_points)) 1142 return false; 1143 } 1144 1145 i = 0; 1146 max_entries += ramp->num_entries; 1147 1148 /* TODO: float point case */ 1149 1150 while (i <= number_of_points) { 1151 tf_pts->red[i] = calculate_mapped_value( 1152 rgb_user, coeff, CHANNEL_NAME_RED, max_entries); 1153 tf_pts->green[i] = calculate_mapped_value( 1154 rgb_user, coeff, CHANNEL_NAME_GREEN, max_entries); 1155 tf_pts->blue[i] = calculate_mapped_value( 1156 rgb_user, coeff, CHANNEL_NAME_BLUE, max_entries); 1157 1158 ++coeff; 1159 ++i; 1160 } 1161 1162 return true; 1163 } 1164 1165 /* The "old" interpolation uses a complicated scheme to build an array of 1166 * coefficients while also using an array of 0-255 normalized to 0-1 1167 * Then there's another loop using both of the above + new scaled user ramp 1168 * and we concatenate them. It also searches for points of interpolation and 1169 * uses enums for positions. 1170 * 1171 * This function uses a different approach: 1172 * user ramp is always applied on X with 0/255, 1/255, 2/255, ..., 255/255 1173 * To find index for hwX , we notice the following: 1174 * i/255 <= hwX < (i+1)/255 <=> i <= 255*hwX < i+1 1175 * See apply_lut_1d which is the same principle, but on 4K entry 1D LUT 1176 * 1177 * Once the index is known, combined Y is simply: 1178 * user_ramp(index) + (hwX-index/255)*(user_ramp(index+1) - user_ramp(index) 1179 * 1180 * We should switch to this method in all cases, it's simpler and faster 1181 * ToDo one day - for now this only applies to ADL regamma to avoid regression 1182 * for regular use cases (sRGB and PQ) 1183 */ 1184 static void interpolate_user_regamma(uint32_t hw_points_num, 1185 struct pwl_float_data *rgb_user, 1186 bool apply_degamma, 1187 struct dc_transfer_func_distributed_points *tf_pts) 1188 { 1189 uint32_t i; 1190 uint32_t color = 0; 1191 int32_t index; 1192 int32_t index_next; 1193 struct fixed31_32 *tf_point; 1194 struct fixed31_32 hw_x; 1195 struct fixed31_32 norm_factor = 1196 dc_fixpt_from_int(255); 1197 struct fixed31_32 norm_x; 1198 struct fixed31_32 index_f; 1199 struct fixed31_32 lut1; 1200 struct fixed31_32 lut2; 1201 struct fixed31_32 delta_lut; 1202 struct fixed31_32 delta_index; 1203 1204 i = 0; 1205 /* fixed_pt library has problems handling too small values */ 1206 while (i != 32) { 1207 tf_pts->red[i] = dc_fixpt_zero; 1208 tf_pts->green[i] = dc_fixpt_zero; 1209 tf_pts->blue[i] = dc_fixpt_zero; 1210 ++i; 1211 } 1212 while (i <= hw_points_num + 1) { 1213 for (color = 0; color < 3; color++) { 1214 if (color == 0) 1215 tf_point = &tf_pts->red[i]; 1216 else if (color == 1) 1217 tf_point = &tf_pts->green[i]; 1218 else 1219 tf_point = &tf_pts->blue[i]; 1220 1221 if (apply_degamma) { 1222 if (color == 0) 1223 hw_x = coordinates_x[i].regamma_y_red; 1224 else if (color == 1) 1225 hw_x = coordinates_x[i].regamma_y_green; 1226 else 1227 hw_x = coordinates_x[i].regamma_y_blue; 1228 } else 1229 hw_x = coordinates_x[i].x; 1230 1231 norm_x = dc_fixpt_mul(norm_factor, hw_x); 1232 index = dc_fixpt_floor(norm_x); 1233 if (index < 0 || index > 255) 1234 continue; 1235 1236 index_f = dc_fixpt_from_int(index); 1237 index_next = (index == 255) ? index : index + 1; 1238 1239 if (color == 0) { 1240 lut1 = rgb_user[index].r; 1241 lut2 = rgb_user[index_next].r; 1242 } else if (color == 1) { 1243 lut1 = rgb_user[index].g; 1244 lut2 = rgb_user[index_next].g; 1245 } else { 1246 lut1 = rgb_user[index].b; 1247 lut2 = rgb_user[index_next].b; 1248 } 1249 1250 // we have everything now, so interpolate 1251 delta_lut = dc_fixpt_sub(lut2, lut1); 1252 delta_index = dc_fixpt_sub(norm_x, index_f); 1253 1254 *tf_point = dc_fixpt_add(lut1, 1255 dc_fixpt_mul(delta_index, delta_lut)); 1256 } 1257 ++i; 1258 } 1259 } 1260 1261 static void build_new_custom_resulted_curve( 1262 uint32_t hw_points_num, 1263 struct dc_transfer_func_distributed_points *tf_pts) 1264 { 1265 uint32_t i; 1266 1267 i = 0; 1268 1269 while (i != hw_points_num + 1) { 1270 tf_pts->red[i] = dc_fixpt_clamp( 1271 tf_pts->red[i], dc_fixpt_zero, 1272 dc_fixpt_one); 1273 tf_pts->green[i] = dc_fixpt_clamp( 1274 tf_pts->green[i], dc_fixpt_zero, 1275 dc_fixpt_one); 1276 tf_pts->blue[i] = dc_fixpt_clamp( 1277 tf_pts->blue[i], dc_fixpt_zero, 1278 dc_fixpt_one); 1279 1280 ++i; 1281 } 1282 } 1283 1284 static void apply_degamma_for_user_regamma(struct pwl_float_data_ex *rgb_regamma, 1285 uint32_t hw_points_num) 1286 { 1287 uint32_t i; 1288 1289 struct gamma_coefficients coeff; 1290 struct pwl_float_data_ex *rgb = rgb_regamma; 1291 const struct hw_x_point *coord_x = coordinates_x; 1292 1293 build_coefficients(&coeff, true); 1294 1295 i = 0; 1296 while (i != hw_points_num + 1) { 1297 rgb->r = translate_from_linear_space_ex( 1298 coord_x->x, &coeff, 0); 1299 rgb->g = rgb->r; 1300 rgb->b = rgb->r; 1301 ++coord_x; 1302 ++rgb; 1303 ++i; 1304 } 1305 } 1306 1307 static bool map_regamma_hw_to_x_user( 1308 const struct dc_gamma *ramp, 1309 struct pixel_gamma_point *coeff128, 1310 struct pwl_float_data *rgb_user, 1311 struct hw_x_point *coords_x, 1312 const struct gamma_pixel *axis_x, 1313 const struct pwl_float_data_ex *rgb_regamma, 1314 uint32_t hw_points_num, 1315 struct dc_transfer_func_distributed_points *tf_pts, 1316 bool mapUserRamp) 1317 { 1318 /* setup to spare calculated ideal regamma values */ 1319 1320 int i = 0; 1321 struct hw_x_point *coords = coords_x; 1322 const struct pwl_float_data_ex *regamma = rgb_regamma; 1323 1324 if (mapUserRamp) { 1325 copy_rgb_regamma_to_coordinates_x(coords, 1326 hw_points_num, 1327 rgb_regamma); 1328 1329 calculate_interpolated_hardware_curve( 1330 ramp, coeff128, rgb_user, coords, axis_x, 1331 hw_points_num, tf_pts); 1332 } else { 1333 /* just copy current rgb_regamma into tf_pts */ 1334 while (i <= hw_points_num) { 1335 tf_pts->red[i] = regamma->r; 1336 tf_pts->green[i] = regamma->g; 1337 tf_pts->blue[i] = regamma->b; 1338 1339 ++regamma; 1340 ++i; 1341 } 1342 } 1343 1344 /* this should be named differently, all it does is clamp to 0-1 */ 1345 build_new_custom_resulted_curve(hw_points_num, tf_pts); 1346 1347 return true; 1348 } 1349 1350 #define _EXTRA_POINTS 3 1351 1352 bool mod_color_calculate_regamma_params(struct dc_transfer_func *output_tf, 1353 const struct dc_gamma *ramp, bool mapUserRamp) 1354 { 1355 struct dc_transfer_func_distributed_points *tf_pts = &output_tf->tf_pts; 1356 struct dividers dividers; 1357 1358 struct pwl_float_data *rgb_user = NULL; 1359 struct pwl_float_data_ex *rgb_regamma = NULL; 1360 struct gamma_pixel *axix_x = NULL; 1361 struct pixel_gamma_point *coeff = NULL; 1362 enum dc_transfer_func_predefined tf = TRANSFER_FUNCTION_SRGB; 1363 bool ret = false; 1364 1365 if (output_tf->type == TF_TYPE_BYPASS) 1366 return false; 1367 1368 /* we can use hardcoded curve for plain SRGB TF */ 1369 if (output_tf->type == TF_TYPE_PREDEFINED && 1370 output_tf->tf == TRANSFER_FUNCTION_SRGB && 1371 (!mapUserRamp && ramp->type == GAMMA_RGB_256)) 1372 return true; 1373 1374 output_tf->type = TF_TYPE_DISTRIBUTED_POINTS; 1375 1376 rgb_user = kvcalloc(ramp->num_entries + _EXTRA_POINTS, 1377 sizeof(*rgb_user), 1378 GFP_KERNEL); 1379 if (!rgb_user) 1380 goto rgb_user_alloc_fail; 1381 rgb_regamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, 1382 sizeof(*rgb_regamma), 1383 GFP_KERNEL); 1384 if (!rgb_regamma) 1385 goto rgb_regamma_alloc_fail; 1386 axix_x = kvcalloc(ramp->num_entries + 3, sizeof(*axix_x), 1387 GFP_KERNEL); 1388 if (!axix_x) 1389 goto axix_x_alloc_fail; 1390 coeff = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, sizeof(*coeff), 1391 GFP_KERNEL); 1392 if (!coeff) 1393 goto coeff_alloc_fail; 1394 1395 dividers.divider1 = dc_fixpt_from_fraction(3, 2); 1396 dividers.divider2 = dc_fixpt_from_int(2); 1397 dividers.divider3 = dc_fixpt_from_fraction(5, 2); 1398 1399 tf = output_tf->tf; 1400 1401 build_evenly_distributed_points( 1402 axix_x, 1403 ramp->num_entries, 1404 dividers); 1405 1406 if (ramp->type == GAMMA_RGB_256 && mapUserRamp) 1407 scale_gamma(rgb_user, ramp, dividers); 1408 else if (ramp->type == GAMMA_RGB_FLOAT_1024) 1409 scale_gamma_dx(rgb_user, ramp, dividers); 1410 1411 if (tf == TRANSFER_FUNCTION_PQ) { 1412 tf_pts->end_exponent = 7; 1413 tf_pts->x_point_at_y1_red = 125; 1414 tf_pts->x_point_at_y1_green = 125; 1415 tf_pts->x_point_at_y1_blue = 125; 1416 1417 build_pq(rgb_regamma, 1418 MAX_HW_POINTS, 1419 coordinates_x, 1420 output_tf->sdr_ref_white_level); 1421 } else { 1422 tf_pts->end_exponent = 0; 1423 tf_pts->x_point_at_y1_red = 1; 1424 tf_pts->x_point_at_y1_green = 1; 1425 tf_pts->x_point_at_y1_blue = 1; 1426 1427 build_regamma(rgb_regamma, 1428 MAX_HW_POINTS, 1429 coordinates_x, tf == TRANSFER_FUNCTION_SRGB ? true:false); 1430 } 1431 1432 map_regamma_hw_to_x_user(ramp, coeff, rgb_user, 1433 coordinates_x, axix_x, rgb_regamma, 1434 MAX_HW_POINTS, tf_pts, 1435 (mapUserRamp || ramp->type != GAMMA_RGB_256) && 1436 ramp->type != GAMMA_CS_TFM_1D); 1437 1438 if (ramp->type == GAMMA_CS_TFM_1D) 1439 apply_lut_1d(ramp, MAX_HW_POINTS, tf_pts); 1440 1441 ret = true; 1442 1443 kvfree(coeff); 1444 coeff_alloc_fail: 1445 kvfree(axix_x); 1446 axix_x_alloc_fail: 1447 kvfree(rgb_regamma); 1448 rgb_regamma_alloc_fail: 1449 kvfree(rgb_user); 1450 rgb_user_alloc_fail: 1451 return ret; 1452 } 1453 1454 bool calculate_user_regamma_coeff(struct dc_transfer_func *output_tf, 1455 const struct regamma_lut *regamma) 1456 { 1457 struct gamma_coefficients coeff; 1458 const struct hw_x_point *coord_x = coordinates_x; 1459 uint32_t i = 0; 1460 1461 do { 1462 coeff.a0[i] = dc_fixpt_from_fraction( 1463 regamma->coeff.A0[i], 10000000); 1464 coeff.a1[i] = dc_fixpt_from_fraction( 1465 regamma->coeff.A1[i], 1000); 1466 coeff.a2[i] = dc_fixpt_from_fraction( 1467 regamma->coeff.A2[i], 1000); 1468 coeff.a3[i] = dc_fixpt_from_fraction( 1469 regamma->coeff.A3[i], 1000); 1470 coeff.user_gamma[i] = dc_fixpt_from_fraction( 1471 regamma->coeff.gamma[i], 1000); 1472 1473 ++i; 1474 } while (i != 3); 1475 1476 i = 0; 1477 /* fixed_pt library has problems handling too small values */ 1478 while (i != 32) { 1479 output_tf->tf_pts.red[i] = dc_fixpt_zero; 1480 output_tf->tf_pts.green[i] = dc_fixpt_zero; 1481 output_tf->tf_pts.blue[i] = dc_fixpt_zero; 1482 ++coord_x; 1483 ++i; 1484 } 1485 while (i != MAX_HW_POINTS + 1) { 1486 output_tf->tf_pts.red[i] = translate_from_linear_space_ex( 1487 coord_x->x, &coeff, 0); 1488 output_tf->tf_pts.green[i] = translate_from_linear_space_ex( 1489 coord_x->x, &coeff, 1); 1490 output_tf->tf_pts.blue[i] = translate_from_linear_space_ex( 1491 coord_x->x, &coeff, 2); 1492 ++coord_x; 1493 ++i; 1494 } 1495 1496 // this function just clamps output to 0-1 1497 build_new_custom_resulted_curve(MAX_HW_POINTS, &output_tf->tf_pts); 1498 output_tf->type = TF_TYPE_DISTRIBUTED_POINTS; 1499 1500 return true; 1501 } 1502 1503 bool calculate_user_regamma_ramp(struct dc_transfer_func *output_tf, 1504 const struct regamma_lut *regamma) 1505 { 1506 struct dc_transfer_func_distributed_points *tf_pts = &output_tf->tf_pts; 1507 struct dividers dividers; 1508 1509 struct pwl_float_data *rgb_user = NULL; 1510 struct pwl_float_data_ex *rgb_regamma = NULL; 1511 bool ret = false; 1512 1513 if (regamma == NULL) 1514 return false; 1515 1516 output_tf->type = TF_TYPE_DISTRIBUTED_POINTS; 1517 1518 rgb_user = kcalloc(GAMMA_RGB_256_ENTRIES + _EXTRA_POINTS, 1519 sizeof(*rgb_user), 1520 GFP_KERNEL); 1521 if (!rgb_user) 1522 goto rgb_user_alloc_fail; 1523 1524 rgb_regamma = kcalloc(MAX_HW_POINTS + _EXTRA_POINTS, 1525 sizeof(*rgb_regamma), 1526 GFP_KERNEL); 1527 if (!rgb_regamma) 1528 goto rgb_regamma_alloc_fail; 1529 1530 dividers.divider1 = dc_fixpt_from_fraction(3, 2); 1531 dividers.divider2 = dc_fixpt_from_int(2); 1532 dividers.divider3 = dc_fixpt_from_fraction(5, 2); 1533 1534 scale_user_regamma_ramp(rgb_user, ®amma->ramp, dividers); 1535 1536 if (regamma->flags.bits.applyDegamma == 1) { 1537 apply_degamma_for_user_regamma(rgb_regamma, MAX_HW_POINTS); 1538 copy_rgb_regamma_to_coordinates_x(coordinates_x, 1539 MAX_HW_POINTS, rgb_regamma); 1540 } 1541 1542 interpolate_user_regamma(MAX_HW_POINTS, rgb_user, 1543 regamma->flags.bits.applyDegamma, tf_pts); 1544 1545 // no custom HDR curves! 1546 tf_pts->end_exponent = 0; 1547 tf_pts->x_point_at_y1_red = 1; 1548 tf_pts->x_point_at_y1_green = 1; 1549 tf_pts->x_point_at_y1_blue = 1; 1550 1551 // this function just clamps output to 0-1 1552 build_new_custom_resulted_curve(MAX_HW_POINTS, tf_pts); 1553 1554 ret = true; 1555 1556 kfree(rgb_regamma); 1557 rgb_regamma_alloc_fail: 1558 kvfree(rgb_user); 1559 rgb_user_alloc_fail: 1560 return ret; 1561 } 1562 1563 bool mod_color_calculate_degamma_params(struct dc_transfer_func *input_tf, 1564 const struct dc_gamma *ramp, bool mapUserRamp) 1565 { 1566 struct dc_transfer_func_distributed_points *tf_pts = &input_tf->tf_pts; 1567 struct dividers dividers; 1568 1569 struct pwl_float_data *rgb_user = NULL; 1570 struct pwl_float_data_ex *curve = NULL; 1571 struct gamma_pixel *axix_x = NULL; 1572 struct pixel_gamma_point *coeff = NULL; 1573 enum dc_transfer_func_predefined tf = TRANSFER_FUNCTION_SRGB; 1574 bool ret = false; 1575 1576 if (input_tf->type == TF_TYPE_BYPASS) 1577 return false; 1578 1579 /* we can use hardcoded curve for plain SRGB TF */ 1580 if (input_tf->type == TF_TYPE_PREDEFINED && 1581 input_tf->tf == TRANSFER_FUNCTION_SRGB && 1582 (!mapUserRamp && ramp->type == GAMMA_RGB_256)) 1583 return true; 1584 1585 input_tf->type = TF_TYPE_DISTRIBUTED_POINTS; 1586 1587 rgb_user = kvcalloc(ramp->num_entries + _EXTRA_POINTS, 1588 sizeof(*rgb_user), 1589 GFP_KERNEL); 1590 if (!rgb_user) 1591 goto rgb_user_alloc_fail; 1592 curve = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, sizeof(*curve), 1593 GFP_KERNEL); 1594 if (!curve) 1595 goto curve_alloc_fail; 1596 axix_x = kvcalloc(ramp->num_entries + _EXTRA_POINTS, sizeof(*axix_x), 1597 GFP_KERNEL); 1598 if (!axix_x) 1599 goto axix_x_alloc_fail; 1600 coeff = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, sizeof(*coeff), 1601 GFP_KERNEL); 1602 if (!coeff) 1603 goto coeff_alloc_fail; 1604 1605 dividers.divider1 = dc_fixpt_from_fraction(3, 2); 1606 dividers.divider2 = dc_fixpt_from_int(2); 1607 dividers.divider3 = dc_fixpt_from_fraction(5, 2); 1608 1609 tf = input_tf->tf; 1610 1611 build_evenly_distributed_points( 1612 axix_x, 1613 ramp->num_entries, 1614 dividers); 1615 1616 if (ramp->type == GAMMA_RGB_256 && mapUserRamp) 1617 scale_gamma(rgb_user, ramp, dividers); 1618 else if (ramp->type == GAMMA_RGB_FLOAT_1024) 1619 scale_gamma_dx(rgb_user, ramp, dividers); 1620 1621 if (tf == TRANSFER_FUNCTION_PQ) 1622 build_de_pq(curve, 1623 MAX_HW_POINTS, 1624 coordinates_x); 1625 else 1626 build_degamma(curve, 1627 MAX_HW_POINTS, 1628 coordinates_x, 1629 tf == TRANSFER_FUNCTION_SRGB ? true:false); 1630 1631 tf_pts->end_exponent = 0; 1632 tf_pts->x_point_at_y1_red = 1; 1633 tf_pts->x_point_at_y1_green = 1; 1634 tf_pts->x_point_at_y1_blue = 1; 1635 1636 map_regamma_hw_to_x_user(ramp, coeff, rgb_user, 1637 coordinates_x, axix_x, curve, 1638 MAX_HW_POINTS, tf_pts, 1639 mapUserRamp); 1640 1641 ret = true; 1642 1643 kvfree(coeff); 1644 coeff_alloc_fail: 1645 kvfree(axix_x); 1646 axix_x_alloc_fail: 1647 kvfree(curve); 1648 curve_alloc_fail: 1649 kvfree(rgb_user); 1650 rgb_user_alloc_fail: 1651 1652 return ret; 1653 1654 } 1655 1656 1657 bool mod_color_calculate_curve(enum dc_transfer_func_predefined trans, 1658 struct dc_transfer_func_distributed_points *points) 1659 { 1660 uint32_t i; 1661 bool ret = false; 1662 struct pwl_float_data_ex *rgb_regamma = NULL; 1663 1664 if (trans == TRANSFER_FUNCTION_UNITY || 1665 trans == TRANSFER_FUNCTION_LINEAR) { 1666 points->end_exponent = 0; 1667 points->x_point_at_y1_red = 1; 1668 points->x_point_at_y1_green = 1; 1669 points->x_point_at_y1_blue = 1; 1670 1671 for (i = 0; i <= MAX_HW_POINTS ; i++) { 1672 points->red[i] = coordinates_x[i].x; 1673 points->green[i] = coordinates_x[i].x; 1674 points->blue[i] = coordinates_x[i].x; 1675 } 1676 ret = true; 1677 } else if (trans == TRANSFER_FUNCTION_PQ) { 1678 rgb_regamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, 1679 sizeof(*rgb_regamma), 1680 GFP_KERNEL); 1681 if (!rgb_regamma) 1682 goto rgb_regamma_alloc_fail; 1683 points->end_exponent = 7; 1684 points->x_point_at_y1_red = 125; 1685 points->x_point_at_y1_green = 125; 1686 points->x_point_at_y1_blue = 125; 1687 1688 1689 build_pq(rgb_regamma, 1690 MAX_HW_POINTS, 1691 coordinates_x, 1692 80); 1693 for (i = 0; i <= MAX_HW_POINTS ; i++) { 1694 points->red[i] = rgb_regamma[i].r; 1695 points->green[i] = rgb_regamma[i].g; 1696 points->blue[i] = rgb_regamma[i].b; 1697 } 1698 ret = true; 1699 1700 kvfree(rgb_regamma); 1701 } else if (trans == TRANSFER_FUNCTION_SRGB || 1702 trans == TRANSFER_FUNCTION_BT709) { 1703 rgb_regamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, 1704 sizeof(*rgb_regamma), 1705 GFP_KERNEL); 1706 if (!rgb_regamma) 1707 goto rgb_regamma_alloc_fail; 1708 points->end_exponent = 0; 1709 points->x_point_at_y1_red = 1; 1710 points->x_point_at_y1_green = 1; 1711 points->x_point_at_y1_blue = 1; 1712 1713 build_regamma(rgb_regamma, 1714 MAX_HW_POINTS, 1715 coordinates_x, trans == TRANSFER_FUNCTION_SRGB ? true:false); 1716 for (i = 0; i <= MAX_HW_POINTS ; i++) { 1717 points->red[i] = rgb_regamma[i].r; 1718 points->green[i] = rgb_regamma[i].g; 1719 points->blue[i] = rgb_regamma[i].b; 1720 } 1721 ret = true; 1722 1723 kvfree(rgb_regamma); 1724 } else if (trans == TRANSFER_FUNCTION_HLG || 1725 trans == TRANSFER_FUNCTION_HLG12) { 1726 rgb_regamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, 1727 sizeof(*rgb_regamma), 1728 GFP_KERNEL); 1729 if (!rgb_regamma) 1730 goto rgb_regamma_alloc_fail; 1731 1732 build_hlg_regamma(rgb_regamma, 1733 MAX_HW_POINTS, 1734 coordinates_x, 1735 trans == TRANSFER_FUNCTION_HLG12 ? true:false); 1736 for (i = 0; i <= MAX_HW_POINTS ; i++) { 1737 points->red[i] = rgb_regamma[i].r; 1738 points->green[i] = rgb_regamma[i].g; 1739 points->blue[i] = rgb_regamma[i].b; 1740 } 1741 ret = true; 1742 kvfree(rgb_regamma); 1743 } 1744 rgb_regamma_alloc_fail: 1745 return ret; 1746 } 1747 1748 1749 bool mod_color_calculate_degamma_curve(enum dc_transfer_func_predefined trans, 1750 struct dc_transfer_func_distributed_points *points) 1751 { 1752 uint32_t i; 1753 bool ret = false; 1754 struct pwl_float_data_ex *rgb_degamma = NULL; 1755 1756 if (trans == TRANSFER_FUNCTION_UNITY || 1757 trans == TRANSFER_FUNCTION_LINEAR) { 1758 1759 for (i = 0; i <= MAX_HW_POINTS ; i++) { 1760 points->red[i] = coordinates_x[i].x; 1761 points->green[i] = coordinates_x[i].x; 1762 points->blue[i] = coordinates_x[i].x; 1763 } 1764 ret = true; 1765 } else if (trans == TRANSFER_FUNCTION_PQ) { 1766 rgb_degamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, 1767 sizeof(*rgb_degamma), 1768 GFP_KERNEL); 1769 if (!rgb_degamma) 1770 goto rgb_degamma_alloc_fail; 1771 1772 1773 build_de_pq(rgb_degamma, 1774 MAX_HW_POINTS, 1775 coordinates_x); 1776 for (i = 0; i <= MAX_HW_POINTS ; i++) { 1777 points->red[i] = rgb_degamma[i].r; 1778 points->green[i] = rgb_degamma[i].g; 1779 points->blue[i] = rgb_degamma[i].b; 1780 } 1781 ret = true; 1782 1783 kvfree(rgb_degamma); 1784 } else if (trans == TRANSFER_FUNCTION_SRGB || 1785 trans == TRANSFER_FUNCTION_BT709) { 1786 rgb_degamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, 1787 sizeof(*rgb_degamma), 1788 GFP_KERNEL); 1789 if (!rgb_degamma) 1790 goto rgb_degamma_alloc_fail; 1791 1792 build_degamma(rgb_degamma, 1793 MAX_HW_POINTS, 1794 coordinates_x, trans == TRANSFER_FUNCTION_SRGB ? true:false); 1795 for (i = 0; i <= MAX_HW_POINTS ; i++) { 1796 points->red[i] = rgb_degamma[i].r; 1797 points->green[i] = rgb_degamma[i].g; 1798 points->blue[i] = rgb_degamma[i].b; 1799 } 1800 ret = true; 1801 1802 kvfree(rgb_degamma); 1803 } else if (trans == TRANSFER_FUNCTION_HLG || 1804 trans == TRANSFER_FUNCTION_HLG12) { 1805 rgb_degamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS, 1806 sizeof(*rgb_degamma), 1807 GFP_KERNEL); 1808 if (!rgb_degamma) 1809 goto rgb_degamma_alloc_fail; 1810 1811 build_hlg_degamma(rgb_degamma, 1812 MAX_HW_POINTS, 1813 coordinates_x, 1814 trans == TRANSFER_FUNCTION_HLG12 ? true:false); 1815 for (i = 0; i <= MAX_HW_POINTS ; i++) { 1816 points->red[i] = rgb_degamma[i].r; 1817 points->green[i] = rgb_degamma[i].g; 1818 points->blue[i] = rgb_degamma[i].b; 1819 } 1820 ret = true; 1821 kvfree(rgb_degamma); 1822 } 1823 points->end_exponent = 0; 1824 points->x_point_at_y1_red = 1; 1825 points->x_point_at_y1_green = 1; 1826 points->x_point_at_y1_blue = 1; 1827 1828 rgb_degamma_alloc_fail: 1829 return ret; 1830 } 1831 1832 1833