DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (fddffdeab170)

VCS Links

Line Code
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 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FilterNodeD2D1.h"

#include "Logging.h"

#include "SourceSurfaceD2D1.h"
#include "SourceSurfaceD2D.h"
#include "SourceSurfaceD2DTarget.h"
#include "DrawTargetD2D.h"
#include "DrawTargetD2D1.h"
#include "ExtendInputEffectD2D1.h"

namespace mozilla {
namespace gfx {

D2D1_COLORMATRIX_ALPHA_MODE D2DAlphaMode(uint32_t aMode)
{
  switch (aMode) {
  case ALPHA_MODE_PREMULTIPLIED:
    return D2D1_COLORMATRIX_ALPHA_MODE_PREMULTIPLIED;
  case ALPHA_MODE_STRAIGHT:
    return D2D1_COLORMATRIX_ALPHA_MODE_STRAIGHT;
  default:
    MOZ_CRASH("Unknown enum value!");
  }

  return D2D1_COLORMATRIX_ALPHA_MODE_PREMULTIPLIED;
}

D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE D2DAffineTransformInterpolationMode(Filter aFilter)
{
  switch (aFilter) {
  case Filter::GOOD:
    return D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE_LINEAR;
  case Filter::LINEAR:
    return D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE_LINEAR;
  case Filter::POINT:
    return D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
  default:
    MOZ_CRASH("Unknown enum value!");
  }

  return D2D1_2DAFFINETRANSFORM_INTERPOLATION_MODE_LINEAR;
}

D2D1_BLEND_MODE D2DBlendMode(uint32_t aMode)
{
  switch (aMode) {
  case BLEND_MODE_DARKEN:
    return D2D1_BLEND_MODE_DARKEN;
  case BLEND_MODE_LIGHTEN:
    return D2D1_BLEND_MODE_LIGHTEN;
  case BLEND_MODE_MULTIPLY:
    return D2D1_BLEND_MODE_MULTIPLY;
  case BLEND_MODE_SCREEN:
    return D2D1_BLEND_MODE_SCREEN;
  case BLEND_MODE_OVERLAY:
    return D2D1_BLEND_MODE_OVERLAY;
  case BLEND_MODE_COLOR_DODGE:
    return D2D1_BLEND_MODE_COLOR_DODGE;
  case BLEND_MODE_COLOR_BURN:
    return D2D1_BLEND_MODE_COLOR_BURN;
  case BLEND_MODE_HARD_LIGHT:
    return D2D1_BLEND_MODE_HARD_LIGHT;
  case BLEND_MODE_SOFT_LIGHT:
    return D2D1_BLEND_MODE_SOFT_LIGHT;
  case BLEND_MODE_DIFFERENCE:
    return D2D1_BLEND_MODE_DIFFERENCE;
  case BLEND_MODE_EXCLUSION:
    return D2D1_BLEND_MODE_EXCLUSION;
  case BLEND_MODE_HUE:
    return D2D1_BLEND_MODE_HUE;
  case BLEND_MODE_SATURATION:
    return D2D1_BLEND_MODE_SATURATION;
  case BLEND_MODE_COLOR:
    return D2D1_BLEND_MODE_COLOR;
  case BLEND_MODE_LUMINOSITY:
    return D2D1_BLEND_MODE_LUMINOSITY;

  default:
    MOZ_CRASH("Unknown enum value!");
  }

  return D2D1_BLEND_MODE_DARKEN;
}

D2D1_MORPHOLOGY_MODE D2DMorphologyMode(uint32_t aMode)
{
  switch (aMode) {
  case MORPHOLOGY_OPERATOR_DILATE:
    return D2D1_MORPHOLOGY_MODE_DILATE;
  case MORPHOLOGY_OPERATOR_ERODE:
    return D2D1_MORPHOLOGY_MODE_ERODE;
  }

  MOZ_CRASH("Unknown enum value!");
  return D2D1_MORPHOLOGY_MODE_DILATE;
}

D2D1_TURBULENCE_NOISE D2DTurbulenceNoise(uint32_t aMode)
{
  switch (aMode) {
  case TURBULENCE_TYPE_FRACTAL_NOISE:
    return D2D1_TURBULENCE_NOISE_FRACTAL_SUM;
  case TURBULENCE_TYPE_TURBULENCE:
    return D2D1_TURBULENCE_NOISE_TURBULENCE;
  }

  MOZ_CRASH("Unknown enum value!");
  return D2D1_TURBULENCE_NOISE_TURBULENCE;
}

D2D1_COMPOSITE_MODE D2DFilterCompositionMode(uint32_t aMode)
{
  switch (aMode) {
  case COMPOSITE_OPERATOR_OVER:
    return D2D1_COMPOSITE_MODE_SOURCE_OVER;
  case COMPOSITE_OPERATOR_IN:
    return D2D1_COMPOSITE_MODE_SOURCE_IN;
  case COMPOSITE_OPERATOR_OUT:
    return D2D1_COMPOSITE_MODE_SOURCE_OUT;
  case COMPOSITE_OPERATOR_ATOP:
    return D2D1_COMPOSITE_MODE_SOURCE_ATOP;
  case COMPOSITE_OPERATOR_XOR:
    return D2D1_COMPOSITE_MODE_XOR;
  }

  MOZ_CRASH("Unknown enum value!");
  return D2D1_COMPOSITE_MODE_SOURCE_OVER;
}

D2D1_CHANNEL_SELECTOR D2DChannelSelector(uint32_t aMode)
{
  switch (aMode) {
  case COLOR_CHANNEL_R:
    return D2D1_CHANNEL_SELECTOR_R;
  case COLOR_CHANNEL_G:
    return D2D1_CHANNEL_SELECTOR_G;
  case COLOR_CHANNEL_B:
    return D2D1_CHANNEL_SELECTOR_B;
  case COLOR_CHANNEL_A:
    return D2D1_CHANNEL_SELECTOR_A;
  }

  MOZ_CRASH("Unknown enum value!");
  return D2D1_CHANNEL_SELECTOR_R;
}

already_AddRefed<ID2D1Image> GetImageForSourceSurface(DrawTarget *aDT, SourceSurface *aSurface)
{
  if (aDT->IsTiledDrawTarget() || aDT->IsDualDrawTarget()) {
      MOZ_CRASH("Incompatible draw target type!");
      return nullptr;
  }
  switch (aDT->GetBackendType()) {
    case BackendType::DIRECT2D1_1:
      return static_cast<DrawTargetD2D1*>(aDT)->GetImageForSurface(aSurface, ExtendMode::CLAMP);
    case BackendType::DIRECT2D:
      return static_cast<DrawTargetD2D*>(aDT)->GetImageForSurface(aSurface);
    default:
      MOZ_CRASH("Unknown draw target type!");
      return nullptr;
  }
}

uint32_t ConvertValue(FilterType aType, uint32_t aAttribute, uint32_t aValue)
{
  switch (aType) {
  case FilterType::COLOR_MATRIX:
    if (aAttribute == ATT_COLOR_MATRIX_ALPHA_MODE) {
      aValue = D2DAlphaMode(aValue);
    }
    break;
  case FilterType::TRANSFORM:
    if (aAttribute == ATT_TRANSFORM_FILTER) {
      aValue = D2DAffineTransformInterpolationMode(Filter(aValue));
    }
    break;
  case FilterType::BLEND:
    if (aAttribute == ATT_BLEND_BLENDMODE) {
      aValue = D2DBlendMode(aValue);
    }
    break;
  case FilterType::MORPHOLOGY:
    if (aAttribute == ATT_MORPHOLOGY_OPERATOR) {
      aValue = D2DMorphologyMode(aValue);
    }
    break;
  case FilterType::DISPLACEMENT_MAP:
    if (aAttribute == ATT_DISPLACEMENT_MAP_X_CHANNEL ||
        aAttribute == ATT_DISPLACEMENT_MAP_Y_CHANNEL) {
      aValue = D2DChannelSelector(aValue);
    }
    break;
  case FilterType::TURBULENCE:
    if (aAttribute == ATT_TURBULENCE_TYPE) {
      aValue = D2DTurbulenceNoise(aValue);
    }
    break;
  case FilterType::COMPOSITE:
    if (aAttribute == ATT_COMPOSITE_OPERATOR) {
      aValue = D2DFilterCompositionMode(aValue);
    }
    break;
  }

  return aValue;
}

void ConvertValue(FilterType aType, uint32_t aAttribute, IntSize &aValue)
{
  switch (aType) {
  case FilterType::MORPHOLOGY:
    if (aAttribute == ATT_MORPHOLOGY_RADII) {
      aValue.width *= 2;
      aValue.width += 1;
      aValue.height *= 2;
      aValue.height += 1;
    }
    break;
  }
}

UINT32
GetD2D1InputForInput(FilterType aType, uint32_t aIndex)
{
  return aIndex;
}

#define CONVERT_PROP(moz2dname, d2dname) \
  case ATT_##moz2dname: \
  return D2D1_##d2dname

UINT32
GetD2D1PropForAttribute(FilterType aType, uint32_t aIndex)
{
  switch (aType) {
  case FilterType::COLOR_MATRIX:
    switch (aIndex) {
      CONVERT_PROP(COLOR_MATRIX_MATRIX, COLORMATRIX_PROP_COLOR_MATRIX);
      CONVERT_PROP(COLOR_MATRIX_ALPHA_MODE, COLORMATRIX_PROP_ALPHA_MODE);
    }
    break;
  case FilterType::TRANSFORM:
    switch (aIndex) {
      CONVERT_PROP(TRANSFORM_MATRIX, 2DAFFINETRANSFORM_PROP_TRANSFORM_MATRIX);
      CONVERT_PROP(TRANSFORM_FILTER, 2DAFFINETRANSFORM_PROP_INTERPOLATION_MODE);
    }
  case FilterType::BLEND:
    switch (aIndex) {
      CONVERT_PROP(BLEND_BLENDMODE, BLEND_PROP_MODE);
    }
    break;
  case FilterType::MORPHOLOGY:
    switch (aIndex) {
      CONVERT_PROP(MORPHOLOGY_OPERATOR, MORPHOLOGY_PROP_MODE);
    }
    break;
  case FilterType::FLOOD:
    switch (aIndex) {
      CONVERT_PROP(FLOOD_COLOR, FLOOD_PROP_COLOR);
    }
    break;
  case FilterType::TILE:
    switch (aIndex) {
      CONVERT_PROP(TILE_SOURCE_RECT, TILE_PROP_RECT);
    }
    break;
  case FilterType::TABLE_TRANSFER:
    switch (aIndex) {
      CONVERT_PROP(TABLE_TRANSFER_DISABLE_R, TABLETRANSFER_PROP_RED_DISABLE);
      CONVERT_PROP(TABLE_TRANSFER_DISABLE_G, TABLETRANSFER_PROP_GREEN_DISABLE);
      CONVERT_PROP(TABLE_TRANSFER_DISABLE_B, TABLETRANSFER_PROP_BLUE_DISABLE);
      CONVERT_PROP(TABLE_TRANSFER_DISABLE_A, TABLETRANSFER_PROP_ALPHA_DISABLE);
      CONVERT_PROP(TABLE_TRANSFER_TABLE_R, TABLETRANSFER_PROP_RED_TABLE);
      CONVERT_PROP(TABLE_TRANSFER_TABLE_G, TABLETRANSFER_PROP_GREEN_TABLE);
      CONVERT_PROP(TABLE_TRANSFER_TABLE_B, TABLETRANSFER_PROP_BLUE_TABLE);
      CONVERT_PROP(TABLE_TRANSFER_TABLE_A, TABLETRANSFER_PROP_ALPHA_TABLE);
    }
    break;
  case FilterType::DISCRETE_TRANSFER:
    switch (aIndex) {
      CONVERT_PROP(DISCRETE_TRANSFER_DISABLE_R, DISCRETETRANSFER_PROP_RED_DISABLE);
      CONVERT_PROP(DISCRETE_TRANSFER_DISABLE_G, DISCRETETRANSFER_PROP_GREEN_DISABLE);
      CONVERT_PROP(DISCRETE_TRANSFER_DISABLE_B, DISCRETETRANSFER_PROP_BLUE_DISABLE);
      CONVERT_PROP(DISCRETE_TRANSFER_DISABLE_A, DISCRETETRANSFER_PROP_ALPHA_DISABLE);
      CONVERT_PROP(DISCRETE_TRANSFER_TABLE_R, DISCRETETRANSFER_PROP_RED_TABLE);
      CONVERT_PROP(DISCRETE_TRANSFER_TABLE_G, DISCRETETRANSFER_PROP_GREEN_TABLE);
      CONVERT_PROP(DISCRETE_TRANSFER_TABLE_B, DISCRETETRANSFER_PROP_BLUE_TABLE);
      CONVERT_PROP(DISCRETE_TRANSFER_TABLE_A, DISCRETETRANSFER_PROP_ALPHA_TABLE);
    }
    break;
  case FilterType::LINEAR_TRANSFER:
    switch (aIndex) {
      CONVERT_PROP(LINEAR_TRANSFER_DISABLE_R, LINEARTRANSFER_PROP_RED_DISABLE);
      CONVERT_PROP(LINEAR_TRANSFER_DISABLE_G, LINEARTRANSFER_PROP_GREEN_DISABLE);
      CONVERT_PROP(LINEAR_TRANSFER_DISABLE_B, LINEARTRANSFER_PROP_BLUE_DISABLE);
      CONVERT_PROP(LINEAR_TRANSFER_DISABLE_A, LINEARTRANSFER_PROP_ALPHA_DISABLE);
      CONVERT_PROP(LINEAR_TRANSFER_INTERCEPT_R, LINEARTRANSFER_PROP_RED_Y_INTERCEPT);
      CONVERT_PROP(LINEAR_TRANSFER_INTERCEPT_G, LINEARTRANSFER_PROP_GREEN_Y_INTERCEPT);
      CONVERT_PROP(LINEAR_TRANSFER_INTERCEPT_B, LINEARTRANSFER_PROP_BLUE_Y_INTERCEPT);
      CONVERT_PROP(LINEAR_TRANSFER_INTERCEPT_A, LINEARTRANSFER_PROP_ALPHA_Y_INTERCEPT);
      CONVERT_PROP(LINEAR_TRANSFER_SLOPE_R, LINEARTRANSFER_PROP_RED_SLOPE);
      CONVERT_PROP(LINEAR_TRANSFER_SLOPE_G, LINEARTRANSFER_PROP_GREEN_SLOPE);
      CONVERT_PROP(LINEAR_TRANSFER_SLOPE_B, LINEARTRANSFER_PROP_BLUE_SLOPE);
      CONVERT_PROP(LINEAR_TRANSFER_SLOPE_A, LINEARTRANSFER_PROP_ALPHA_SLOPE);
    }
    break;
  case FilterType::GAMMA_TRANSFER:
    switch (aIndex) {
      CONVERT_PROP(GAMMA_TRANSFER_DISABLE_R, GAMMATRANSFER_PROP_RED_DISABLE);
      CONVERT_PROP(GAMMA_TRANSFER_DISABLE_G, GAMMATRANSFER_PROP_GREEN_DISABLE);
      CONVERT_PROP(GAMMA_TRANSFER_DISABLE_B, GAMMATRANSFER_PROP_BLUE_DISABLE);
      CONVERT_PROP(GAMMA_TRANSFER_DISABLE_A, GAMMATRANSFER_PROP_ALPHA_DISABLE);
      CONVERT_PROP(GAMMA_TRANSFER_AMPLITUDE_R, GAMMATRANSFER_PROP_RED_AMPLITUDE);
      CONVERT_PROP(GAMMA_TRANSFER_AMPLITUDE_G, GAMMATRANSFER_PROP_GREEN_AMPLITUDE);
      CONVERT_PROP(GAMMA_TRANSFER_AMPLITUDE_B, GAMMATRANSFER_PROP_BLUE_AMPLITUDE);
      CONVERT_PROP(GAMMA_TRANSFER_AMPLITUDE_A, GAMMATRANSFER_PROP_ALPHA_AMPLITUDE);
      CONVERT_PROP(GAMMA_TRANSFER_EXPONENT_R, GAMMATRANSFER_PROP_RED_EXPONENT);
      CONVERT_PROP(GAMMA_TRANSFER_EXPONENT_G, GAMMATRANSFER_PROP_GREEN_EXPONENT);
      CONVERT_PROP(GAMMA_TRANSFER_EXPONENT_B, GAMMATRANSFER_PROP_BLUE_EXPONENT);
      CONVERT_PROP(GAMMA_TRANSFER_EXPONENT_A, GAMMATRANSFER_PROP_ALPHA_EXPONENT);
      CONVERT_PROP(GAMMA_TRANSFER_OFFSET_R, GAMMATRANSFER_PROP_RED_OFFSET);
      CONVERT_PROP(GAMMA_TRANSFER_OFFSET_G, GAMMATRANSFER_PROP_GREEN_OFFSET);
      CONVERT_PROP(GAMMA_TRANSFER_OFFSET_B, GAMMATRANSFER_PROP_BLUE_OFFSET);
      CONVERT_PROP(GAMMA_TRANSFER_OFFSET_A, GAMMATRANSFER_PROP_ALPHA_OFFSET);
    }
    break;
  case FilterType::CONVOLVE_MATRIX:
    switch (aIndex) {
      CONVERT_PROP(CONVOLVE_MATRIX_BIAS, CONVOLVEMATRIX_PROP_BIAS);
      CONVERT_PROP(CONVOLVE_MATRIX_KERNEL_MATRIX, CONVOLVEMATRIX_PROP_KERNEL_MATRIX);
      CONVERT_PROP(CONVOLVE_MATRIX_DIVISOR, CONVOLVEMATRIX_PROP_DIVISOR);
      CONVERT_PROP(CONVOLVE_MATRIX_KERNEL_UNIT_LENGTH, CONVOLVEMATRIX_PROP_KERNEL_UNIT_LENGTH);
      CONVERT_PROP(CONVOLVE_MATRIX_PRESERVE_ALPHA, CONVOLVEMATRIX_PROP_PRESERVE_ALPHA);
    }
  case FilterType::DISPLACEMENT_MAP:
    switch (aIndex) {
      CONVERT_PROP(DISPLACEMENT_MAP_SCALE, DISPLACEMENTMAP_PROP_SCALE);
      CONVERT_PROP(DISPLACEMENT_MAP_X_CHANNEL, DISPLACEMENTMAP_PROP_X_CHANNEL_SELECT);
      CONVERT_PROP(DISPLACEMENT_MAP_Y_CHANNEL, DISPLACEMENTMAP_PROP_Y_CHANNEL_SELECT);
    }
    break;
  case FilterType::TURBULENCE:
    switch (aIndex) {
      CONVERT_PROP(TURBULENCE_BASE_FREQUENCY, TURBULENCE_PROP_BASE_FREQUENCY);
      CONVERT_PROP(TURBULENCE_NUM_OCTAVES, TURBULENCE_PROP_NUM_OCTAVES);
      CONVERT_PROP(TURBULENCE_SEED, TURBULENCE_PROP_SEED);
      CONVERT_PROP(TURBULENCE_STITCHABLE, TURBULENCE_PROP_STITCHABLE);
      CONVERT_PROP(TURBULENCE_TYPE, TURBULENCE_PROP_NOISE);
    }
    break;
  case FilterType::ARITHMETIC_COMBINE:
    switch (aIndex) {
      CONVERT_PROP(ARITHMETIC_COMBINE_COEFFICIENTS, ARITHMETICCOMPOSITE_PROP_COEFFICIENTS);
    }
    break;
  case FilterType::COMPOSITE:
    switch (aIndex) {
      CONVERT_PROP(COMPOSITE_OPERATOR, COMPOSITE_PROP_MODE);
    }
    break;
  case FilterType::GAUSSIAN_BLUR:
    switch (aIndex) {
      CONVERT_PROP(GAUSSIAN_BLUR_STD_DEVIATION, GAUSSIANBLUR_PROP_STANDARD_DEVIATION);
    }
    break;
  case FilterType::DIRECTIONAL_BLUR:
    switch (aIndex) {
      CONVERT_PROP(DIRECTIONAL_BLUR_STD_DEVIATION, DIRECTIONALBLUR_PROP_STANDARD_DEVIATION);
      CONVERT_PROP(DIRECTIONAL_BLUR_DIRECTION, DIRECTIONALBLUR_PROP_ANGLE);
    }
    break;
  case FilterType::POINT_DIFFUSE:
    switch (aIndex) {
      CONVERT_PROP(POINT_DIFFUSE_DIFFUSE_CONSTANT, POINTDIFFUSE_PROP_DIFFUSE_CONSTANT);
      CONVERT_PROP(POINT_DIFFUSE_POSITION, POINTDIFFUSE_PROP_LIGHT_POSITION);
      CONVERT_PROP(POINT_DIFFUSE_COLOR, POINTDIFFUSE_PROP_COLOR);
      CONVERT_PROP(POINT_DIFFUSE_SURFACE_SCALE, POINTDIFFUSE_PROP_SURFACE_SCALE);
      CONVERT_PROP(POINT_DIFFUSE_KERNEL_UNIT_LENGTH, POINTDIFFUSE_PROP_KERNEL_UNIT_LENGTH);
    }
    break;
  case FilterType::SPOT_DIFFUSE:
    switch (aIndex) {
      CONVERT_PROP(SPOT_DIFFUSE_DIFFUSE_CONSTANT, SPOTDIFFUSE_PROP_DIFFUSE_CONSTANT);
      CONVERT_PROP(SPOT_DIFFUSE_POINTS_AT, SPOTDIFFUSE_PROP_POINTS_AT);
      CONVERT_PROP(SPOT_DIFFUSE_FOCUS, SPOTDIFFUSE_PROP_FOCUS);
      CONVERT_PROP(SPOT_DIFFUSE_LIMITING_CONE_ANGLE, SPOTDIFFUSE_PROP_LIMITING_CONE_ANGLE);
      CONVERT_PROP(SPOT_DIFFUSE_POSITION, SPOTDIFFUSE_PROP_LIGHT_POSITION);
      CONVERT_PROP(SPOT_DIFFUSE_COLOR, SPOTDIFFUSE_PROP_COLOR);
      CONVERT_PROP(SPOT_DIFFUSE_SURFACE_SCALE, SPOTDIFFUSE_PROP_SURFACE_SCALE);
      CONVERT_PROP(SPOT_DIFFUSE_KERNEL_UNIT_LENGTH, SPOTDIFFUSE_PROP_KERNEL_UNIT_LENGTH);
    }
    break;
  case FilterType::DISTANT_DIFFUSE:
    switch (aIndex) {
      CONVERT_PROP(DISTANT_DIFFUSE_DIFFUSE_CONSTANT, DISTANTDIFFUSE_PROP_DIFFUSE_CONSTANT);
      CONVERT_PROP(DISTANT_DIFFUSE_AZIMUTH, DISTANTDIFFUSE_PROP_AZIMUTH);
      CONVERT_PROP(DISTANT_DIFFUSE_ELEVATION, DISTANTDIFFUSE_PROP_ELEVATION);
      CONVERT_PROP(DISTANT_DIFFUSE_COLOR, DISTANTDIFFUSE_PROP_COLOR);
      CONVERT_PROP(DISTANT_DIFFUSE_SURFACE_SCALE, DISTANTDIFFUSE_PROP_SURFACE_SCALE);
      CONVERT_PROP(DISTANT_DIFFUSE_KERNEL_UNIT_LENGTH, DISTANTDIFFUSE_PROP_KERNEL_UNIT_LENGTH);
    }
    break;
  case FilterType::POINT_SPECULAR:
    switch (aIndex) {
      CONVERT_PROP(POINT_SPECULAR_SPECULAR_CONSTANT, POINTSPECULAR_PROP_SPECULAR_CONSTANT);
      CONVERT_PROP(POINT_SPECULAR_SPECULAR_EXPONENT, POINTSPECULAR_PROP_SPECULAR_EXPONENT);
      CONVERT_PROP(POINT_SPECULAR_POSITION, POINTSPECULAR_PROP_LIGHT_POSITION);
      CONVERT_PROP(POINT_SPECULAR_COLOR, POINTSPECULAR_PROP_COLOR);
      CONVERT_PROP(POINT_SPECULAR_SURFACE_SCALE, POINTSPECULAR_PROP_SURFACE_SCALE);
      CONVERT_PROP(POINT_SPECULAR_KERNEL_UNIT_LENGTH, POINTSPECULAR_PROP_KERNEL_UNIT_LENGTH);
    }
    break;
  case FilterType::SPOT_SPECULAR:
    switch (aIndex) {
      CONVERT_PROP(SPOT_SPECULAR_SPECULAR_CONSTANT, SPOTSPECULAR_PROP_SPECULAR_CONSTANT);
      CONVERT_PROP(SPOT_SPECULAR_SPECULAR_EXPONENT, SPOTSPECULAR_PROP_SPECULAR_EXPONENT);
      CONVERT_PROP(SPOT_SPECULAR_POINTS_AT, SPOTSPECULAR_PROP_POINTS_AT);
      CONVERT_PROP(SPOT_SPECULAR_FOCUS, SPOTSPECULAR_PROP_FOCUS);
      CONVERT_PROP(SPOT_SPECULAR_LIMITING_CONE_ANGLE, SPOTSPECULAR_PROP_LIMITING_CONE_ANGLE);
      CONVERT_PROP(SPOT_SPECULAR_POSITION, SPOTSPECULAR_PROP_LIGHT_POSITION);
      CONVERT_PROP(SPOT_SPECULAR_COLOR, SPOTSPECULAR_PROP_COLOR);
      CONVERT_PROP(SPOT_SPECULAR_SURFACE_SCALE, SPOTSPECULAR_PROP_SURFACE_SCALE);
      CONVERT_PROP(SPOT_SPECULAR_KERNEL_UNIT_LENGTH, SPOTSPECULAR_PROP_KERNEL_UNIT_LENGTH);
    }
    break;
  case FilterType::DISTANT_SPECULAR:
    switch (aIndex) {
      CONVERT_PROP(DISTANT_SPECULAR_SPECULAR_CONSTANT, DISTANTSPECULAR_PROP_SPECULAR_CONSTANT);
      CONVERT_PROP(DISTANT_SPECULAR_SPECULAR_EXPONENT, DISTANTSPECULAR_PROP_SPECULAR_EXPONENT);
      CONVERT_PROP(DISTANT_SPECULAR_AZIMUTH, DISTANTSPECULAR_PROP_AZIMUTH);
      CONVERT_PROP(DISTANT_SPECULAR_ELEVATION, DISTANTSPECULAR_PROP_ELEVATION);
      CONVERT_PROP(DISTANT_SPECULAR_COLOR, DISTANTSPECULAR_PROP_COLOR);
      CONVERT_PROP(DISTANT_SPECULAR_SURFACE_SCALE, DISTANTSPECULAR_PROP_SURFACE_SCALE);
      CONVERT_PROP(DISTANT_SPECULAR_KERNEL_UNIT_LENGTH, DISTANTSPECULAR_PROP_KERNEL_UNIT_LENGTH);
    }
    break;
  case FilterType::CROP:
    switch (aIndex) {
      CONVERT_PROP(CROP_RECT, CROP_PROP_RECT);
    }
    break;
  }

  return UINT32_MAX;
}

bool
GetD2D1PropsForIntSize(FilterType aType, uint32_t aIndex, UINT32 *aPropWidth, UINT32 *aPropHeight)
{
  switch (aType) {
  case FilterType::MORPHOLOGY:
    if (aIndex == ATT_MORPHOLOGY_RADII) {
      *aPropWidth = D2D1_MORPHOLOGY_PROP_WIDTH;
      *aPropHeight = D2D1_MORPHOLOGY_PROP_HEIGHT;
      return true;
    }
    break;
  }
  return false;
}

static inline REFCLSID GetCLDIDForFilterType(FilterType aType)
{
  switch (aType) {
  case FilterType::COLOR_MATRIX:
    return CLSID_D2D1ColorMatrix;
  case FilterType::TRANSFORM:
    return CLSID_D2D12DAffineTransform;
  case FilterType::BLEND:
    return CLSID_D2D1Blend;
  case FilterType::MORPHOLOGY:
    return CLSID_D2D1Morphology;
  case FilterType::FLOOD:
    return CLSID_D2D1Flood;
  case FilterType::TILE:
    return CLSID_D2D1Tile;
  case FilterType::TABLE_TRANSFER:
    return CLSID_D2D1TableTransfer;
  case FilterType::LINEAR_TRANSFER:
    return CLSID_D2D1LinearTransfer;
  case FilterType::DISCRETE_TRANSFER:
    return CLSID_D2D1DiscreteTransfer;
  case FilterType::GAMMA_TRANSFER:
    return CLSID_D2D1GammaTransfer;
  case FilterType::DISPLACEMENT_MAP:
    return CLSID_D2D1DisplacementMap;
  case FilterType::TURBULENCE:
    return CLSID_D2D1Turbulence;
  case FilterType::ARITHMETIC_COMBINE:
    return CLSID_D2D1ArithmeticComposite;
  case FilterType::COMPOSITE:
    return CLSID_D2D1Composite;
  case FilterType::GAUSSIAN_BLUR:
    return CLSID_D2D1GaussianBlur;
  case FilterType::DIRECTIONAL_BLUR:
    return CLSID_D2D1DirectionalBlur;
  case FilterType::POINT_DIFFUSE:
    return CLSID_D2D1PointDiffuse;
  case FilterType::POINT_SPECULAR:
    return CLSID_D2D1PointSpecular;
  case FilterType::SPOT_DIFFUSE:
    return CLSID_D2D1SpotDiffuse;
  case FilterType::SPOT_SPECULAR:
    return CLSID_D2D1SpotSpecular;
  case FilterType::DISTANT_DIFFUSE:
    return CLSID_D2D1DistantDiffuse;
  case FilterType::DISTANT_SPECULAR:
    return CLSID_D2D1DistantSpecular;
  case FilterType::CROP:
    return CLSID_D2D1Crop;
  case FilterType::PREMULTIPLY:
    return CLSID_D2D1Premultiply;
  case FilterType::UNPREMULTIPLY:
    return CLSID_D2D1UnPremultiply;
  }
  return GUID_NULL;
}

static bool
IsTransferFilterType(FilterType aType)
{
  switch (aType) {
    case FilterType::LINEAR_TRANSFER:
    case FilterType::GAMMA_TRANSFER:
    case FilterType::TABLE_TRANSFER:
    case FilterType::DISCRETE_TRANSFER:
      return true;
    default:
      return false;
  }
}

static bool
HasUnboundedOutputRegion(FilterType aType)
{
  if (IsTransferFilterType(aType)) {
    return true;
  }

  switch (aType) {
    case FilterType::COLOR_MATRIX:
    case FilterType::POINT_DIFFUSE:
    case FilterType::SPOT_DIFFUSE:
    case FilterType::DISTANT_DIFFUSE:
    case FilterType::POINT_SPECULAR:
    case FilterType::SPOT_SPECULAR:
    case FilterType::DISTANT_SPECULAR:
      return true;
    default:
      return false;
  }
}

/* static */
already_AddRefed<FilterNode>
FilterNodeD2D1::Create(ID2D1DeviceContext *aDC, FilterType aType)
{
  if (aType == FilterType::CONVOLVE_MATRIX) {
    return MakeAndAddRef<FilterNodeConvolveD2D1>(aDC);
  }

  RefPtr<ID2D1Effect> effect;
  HRESULT hr;

  hr = aDC->CreateEffect(GetCLDIDForFilterType(aType), getter_AddRefs(effect));

  if (FAILED(hr) || !effect) {
    gfxCriticalErrorOnce() << "Failed to create effect for FilterType: " << hexa(hr);
    return nullptr;
  }

  RefPtr<FilterNodeD2D1> filter = new FilterNodeD2D1(effect, aType);

  if (HasUnboundedOutputRegion(aType)) {
    // These filters can produce non-transparent output from transparent
    // input pixels, and we want them to have an unbounded output region.
    filter = new FilterNodeExtendInputAdapterD2D1(aDC, filter, aType);
  }

  if (IsTransferFilterType(aType)) {
    // Component transfer filters should appear to apply on unpremultiplied
    // colors, but the D2D1 effects apply on premultiplied colors.
    filter = new FilterNodePremultiplyAdapterD2D1(aDC, filter, aType);
  }

  return filter.forget();
}

void
FilterNodeD2D1::InitUnmappedProperties()
{
  switch (mType) {
    case FilterType::TRANSFORM:
      mEffect->SetValue(D2D1_2DAFFINETRANSFORM_PROP_BORDER_MODE, D2D1_BORDER_MODE_HARD);
      break;
    default:
      break;
  }
}

void
FilterNodeD2D1::SetInput(uint32_t aIndex, SourceSurface *aSurface)
{
  UINT32 input = GetD2D1InputForInput(mType, aIndex);
  ID2D1Effect* effect = InputEffect();
  MOZ_ASSERT(input < effect->GetInputCount());

  if (mType == FilterType::COMPOSITE) {
    UINT32 inputCount = effect->GetInputCount();

    if (aIndex == inputCount - 1 && aSurface == nullptr) {
      effect->SetInputCount(inputCount - 1);
    } else if (aIndex >= inputCount && aSurface) {
      effect->SetInputCount(aIndex + 1);
    }
  }

  MOZ_ASSERT(input < effect->GetInputCount());

  mInputSurfaces.resize(effect->GetInputCount());
  mInputFilters.resize(effect->GetInputCount());

  // In order to convert aSurface into an ID2D1Image, we need to know what
  // DrawTarget we paint into. However, the same FilterNode object can be
  // used on different DrawTargets, so we need to hold on to the SourceSurface
  // objects and delay the conversion until we're actually painted and know
  // our target DrawTarget.
  // The conversion happens in WillDraw().

  mInputSurfaces[input] = aSurface;
  mInputFilters[input] = nullptr;

  // Clear the existing image from the effect.
  effect->SetInput(input, nullptr);
}

void
FilterNodeD2D1::SetInput(uint32_t aIndex, FilterNode *aFilter)
{
  UINT32 input = GetD2D1InputForInput(mType, aIndex);
  ID2D1Effect* effect = InputEffect();

  if (mType == FilterType::COMPOSITE) {
    UINT32 inputCount = effect->GetInputCount();

    if (aIndex == inputCount - 1 && aFilter == nullptr) {
      effect->SetInputCount(inputCount - 1);
    } else if (aIndex >= inputCount && aFilter) {
      effect->SetInputCount(aIndex + 1);
    }
  }

  MOZ_ASSERT(input < effect->GetInputCount());

  if (aFilter && aFilter->GetBackendType() != FILTER_BACKEND_DIRECT2D1_1) {
    gfxWarning() << "Unknown input FilterNode set on effect.";
    MOZ_ASSERT(0);
    return;
  }

  FilterNodeD2D1* filter = static_cast<FilterNodeD2D1*>(aFilter);

  mInputSurfaces.resize(effect->GetInputCount());
  mInputFilters.resize(effect->GetInputCount());

  // We hold on to the FilterNode object so that we can call WillDraw() on it.
  mInputSurfaces[input] = nullptr;
  mInputFilters[input] = filter;

  if (filter) {
    effect->SetInputEffect(input, filter->OutputEffect());
  }
}

void
FilterNodeD2D1::WillDraw(DrawTarget *aDT)
{
  // Convert input SourceSurfaces into ID2D1Images and set them on the effect.
  for (size_t inputIndex = 0; inputIndex < mInputSurfaces.size(); inputIndex++) {
    if (mInputSurfaces[inputIndex]) {
      ID2D1Effect* effect = InputEffect();
      RefPtr<ID2D1Image> image = GetImageForSourceSurface(aDT, mInputSurfaces[inputIndex]);
      effect->SetInput(inputIndex, image);
    }
  }

  // Call WillDraw() on our input filters.
  for (std::vector<RefPtr<FilterNodeD2D1>>::iterator it = mInputFilters.begin();
       it != mInputFilters.end(); it++) {
    if (*it) {
      (*it)->WillDraw(aDT);
    }
  }
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, uint32_t aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  if (mType == FilterType::TURBULENCE && aIndex == ATT_TURBULENCE_BASE_FREQUENCY) {
    mEffect->SetValue(input, D2D1::Vector2F(FLOAT(aValue), FLOAT(aValue)));
    return;
  } else if (mType == FilterType::DIRECTIONAL_BLUR && aIndex == ATT_DIRECTIONAL_BLUR_DIRECTION) {
    mEffect->SetValue(input, aValue == BLUR_DIRECTION_X ? 0 : 90.0f);
    return;
  }

  mEffect->SetValue(input, ConvertValue(mType, aIndex, aValue));
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, Float aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, aValue);
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Point &aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, D2DPoint(aValue));
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Matrix5x4 &aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, D2DMatrix5x4(aValue));
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Point3D &aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, D2DVector3D(aValue));
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Size &aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, D2D1::Vector2F(aValue.width, aValue.height));
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const IntSize &aValue)
{
  UINT32 widthProp, heightProp;

  if (!GetD2D1PropsForIntSize(mType, aIndex, &widthProp, &heightProp)) {
    return;
  }

  IntSize value = aValue;
  ConvertValue(mType, aIndex, value);

  mEffect->SetValue(widthProp, (UINT)value.width);
  mEffect->SetValue(heightProp, (UINT)value.height);
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Color &aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  switch (mType) {
  case FilterType::POINT_DIFFUSE:
  case FilterType::SPOT_DIFFUSE:
  case FilterType::DISTANT_DIFFUSE:
  case FilterType::POINT_SPECULAR:
  case FilterType::SPOT_SPECULAR:
  case FilterType::DISTANT_SPECULAR:
    mEffect->SetValue(input, D2D1::Vector3F(aValue.r, aValue.g, aValue.b));
	break;
  default:
    mEffect->SetValue(input, D2D1::Vector4F(aValue.r * aValue.a, aValue.g * aValue.a, aValue.b * aValue.a, aValue.a));
  }
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Rect &aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, D2DRect(aValue));
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const IntRect &aValue)
{
  if (mType == FilterType::TURBULENCE) {
    MOZ_ASSERT(aIndex == ATT_TURBULENCE_RECT);

    mEffect->SetValue(D2D1_TURBULENCE_PROP_OFFSET, D2D1::Vector2F(Float(aValue.x), Float(aValue.y)));
    mEffect->SetValue(D2D1_TURBULENCE_PROP_SIZE, D2D1::Vector2F(Float(aValue.width), Float(aValue.height)));
    return;
  }

  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, D2D1::RectF(Float(aValue.x), Float(aValue.y),
                                       Float(aValue.XMost()), Float(aValue.YMost())));
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, bool aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, (BOOL)aValue);
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Float *aValues, uint32_t aSize)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, (BYTE*)aValues, sizeof(Float) * aSize);
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const IntPoint &aValue)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, D2DPoint(aValue));
}

void
FilterNodeD2D1::SetAttribute(uint32_t aIndex, const Matrix &aMatrix)
{
  UINT32 input = GetD2D1PropForAttribute(mType, aIndex);
  MOZ_ASSERT(input < mEffect->GetPropertyCount());

  mEffect->SetValue(input, D2DMatrix(aMatrix));
}

FilterNodeConvolveD2D1::FilterNodeConvolveD2D1(ID2D1DeviceContext *aDC)
  : FilterNodeD2D1(nullptr, FilterType::CONVOLVE_MATRIX)
  , mEdgeMode(EDGE_MODE_DUPLICATE)
{
  // Correctly handling the interaction of edge mode and source rect is a bit
  // tricky with D2D1 effects. We want the edge mode to only apply outside of
  // the source rect (as specified by the ATT_CONVOLVE_MATRIX_SOURCE_RECT
  // attribute). So if our input surface or filter is smaller than the source
  // rect, we need to add transparency around it until we reach the edges of
  // the source rect, and only then do any repeating or edge duplicating.
  // Unfortunately, the border effect does not have a source rect attribute -
  // it only looks at the output rect of its input filter or surface. So we use
  // our custom ExtendInput effect to adjust the output rect of our input.
  // All of this is only necessary when our edge mode is not EDGE_MODE_NONE, so
  // we update the filter chain dynamically in UpdateChain().

  HRESULT hr;
  
  hr = aDC->CreateEffect(CLSID_D2D1ConvolveMatrix, getter_AddRefs(mEffect));

  if (FAILED(hr) || !mEffect) {
    gfxWarning() << "Failed to create ConvolveMatrix filter!";
    return;
  }

  mEffect->SetValue(D2D1_CONVOLVEMATRIX_PROP_BORDER_MODE, D2D1_BORDER_MODE_SOFT);

  hr = aDC->CreateEffect(CLSID_ExtendInputEffect, getter_AddRefs(mExtendInputEffect));

  if (FAILED(hr) || !mExtendInputEffect) {
    gfxWarning() << "Failed to create ConvolveMatrix filter!";
    return;
  }

  hr = aDC->CreateEffect(CLSID_D2D1Border, getter_AddRefs(mBorderEffect));

  if (FAILED(hr) || !mBorderEffect) {
    gfxWarning() << "Failed to create ConvolveMatrix filter!";
    return;
  }

  mBorderEffect->SetInputEffect(0, mExtendInputEffect.get());

  UpdateChain();
  UpdateSourceRect();
}

void
FilterNodeConvolveD2D1::SetInput(uint32_t aIndex, FilterNode *aFilter)
{
  FilterNodeD2D1::SetInput(aIndex, aFilter);

  UpdateChain();
}

void
FilterNodeConvolveD2D1::SetAttribute(uint32_t aIndex, uint32_t aValue)
{
  if (aIndex != ATT_CONVOLVE_MATRIX_EDGE_MODE) {
    return FilterNodeD2D1::SetAttribute(aIndex, aValue);
  }

  mEdgeMode = (ConvolveMatrixEdgeMode)aValue;

  UpdateChain();
}

ID2D1Effect*
FilterNodeConvolveD2D1::InputEffect()
{
  return mEdgeMode == EDGE_MODE_NONE ? mEffect.get() : mExtendInputEffect.get();
}

void
FilterNodeConvolveD2D1::UpdateChain()
{
  // The shape of the filter graph:
  //
  // EDGE_MODE_NONE:
  // input --> convolvematrix
  //
  // EDGE_MODE_DUPLICATE or EDGE_MODE_WRAP:
  // input --> extendinput --> border --> convolvematrix
  //
  // mEffect is convolvematrix.

  if (mEdgeMode != EDGE_MODE_NONE) {
    mEffect->SetInputEffect(0, mBorderEffect.get());
  }

  RefPtr<ID2D1Effect> inputEffect;
  if (mInputFilters.size() > 0 && mInputFilters[0]) {
    inputEffect = mInputFilters[0]->OutputEffect();
  }
  InputEffect()->SetInputEffect(0, inputEffect);

  if (mEdgeMode == EDGE_MODE_DUPLICATE) {
    mBorderEffect->SetValue(D2D1_BORDER_PROP_EDGE_MODE_X, D2D1_BORDER_EDGE_MODE_CLAMP);
    mBorderEffect->SetValue(D2D1_BORDER_PROP_EDGE_MODE_Y, D2D1_BORDER_EDGE_MODE_CLAMP);
  } else if (mEdgeMode == EDGE_MODE_WRAP) {
    mBorderEffect->SetValue(D2D1_BORDER_PROP_EDGE_MODE_X, D2D1_BORDER_EDGE_MODE_WRAP);
    mBorderEffect->SetValue(D2D1_BORDER_PROP_EDGE_MODE_Y, D2D1_BORDER_EDGE_MODE_WRAP);
  }
}

void
FilterNodeConvolveD2D1::SetAttribute(uint32_t aIndex, const IntSize &aValue)
{
  if (aIndex != ATT_CONVOLVE_MATRIX_KERNEL_SIZE) {
    MOZ_ASSERT(false);
    return;
  }

  mKernelSize = aValue;

  mEffect->SetValue(D2D1_CONVOLVEMATRIX_PROP_KERNEL_SIZE_X, aValue.width);
  mEffect->SetValue(D2D1_CONVOLVEMATRIX_PROP_KERNEL_SIZE_Y, aValue.height);

  UpdateOffset();
}

void
FilterNodeConvolveD2D1::SetAttribute(uint32_t aIndex, const IntPoint &aValue)
{
  if (aIndex != ATT_CONVOLVE_MATRIX_TARGET) {
    MOZ_ASSERT(false);
    return;
  }

  mTarget = aValue;

  UpdateOffset();
}

void
FilterNodeConvolveD2D1::SetAttribute(uint32_t aIndex, const IntRect &aValue)
{
  if (aIndex != ATT_CONVOLVE_MATRIX_SOURCE_RECT) {
    MOZ_ASSERT(false);
    return;
  }

  mSourceRect = aValue;

  UpdateSourceRect();
}

void
FilterNodeConvolveD2D1::UpdateOffset()
{
  D2D1_VECTOR_2F vector =
    D2D1::Vector2F((Float(mKernelSize.width) - 1.0f) / 2.0f - Float(mTarget.x),
                   (Float(mKernelSize.height) - 1.0f) / 2.0f - Float(mTarget.y));

  mEffect->SetValue(D2D1_CONVOLVEMATRIX_PROP_KERNEL_OFFSET, vector);
}

void
FilterNodeConvolveD2D1::UpdateSourceRect()
{
  mExtendInputEffect->SetValue(EXTENDINPUT_PROP_OUTPUT_RECT,
    D2D1::Vector4F(Float(mSourceRect.x), Float(mSourceRect.y),
                   Float(mSourceRect.XMost()), Float(mSourceRect.YMost())));
}

FilterNodeExtendInputAdapterD2D1::FilterNodeExtendInputAdapterD2D1(ID2D1DeviceContext *aDC,
                                                                   FilterNodeD2D1 *aFilterNode, FilterType aType)
 : FilterNodeD2D1(aFilterNode->MainEffect(), aType)
 , mWrappedFilterNode(aFilterNode)
{
  // We have an mEffect that looks at the bounds of the input effect, and we
  // want mEffect to regard its input as unbounded. So we take the input,
  // pipe it through an ExtendInput effect (which has an infinite output rect
  // by default), and feed the resulting unbounded composition into mEffect.

  HRESULT hr;

  hr = aDC->CreateEffect(CLSID_ExtendInputEffect, getter_AddRefs(mExtendInputEffect));

  if (FAILED(hr) || !mExtendInputEffect) {
    gfxWarning() << "Failed to create extend input effect for filter: " << hexa(hr);
    return;
  }

  aFilterNode->InputEffect()->SetInputEffect(0, mExtendInputEffect.get());
}

FilterNodePremultiplyAdapterD2D1::FilterNodePremultiplyAdapterD2D1(ID2D1DeviceContext *aDC,
                                                                   FilterNodeD2D1 *aFilterNode, FilterType aType)
 : FilterNodeD2D1(aFilterNode->MainEffect(), aType)
{
  // D2D1 component transfer effects do strange things when it comes to
  // premultiplication.
  // For our purposes we only need the transfer filters to apply straight to
  // unpremultiplied source channels and output unpremultiplied results.
  // However, the D2D1 effects are designed differently: They can apply to both
  // premultiplied and unpremultiplied inputs, and they always premultiply
  // their result - at least in those color channels that have not been
  // disabled.
  // In order to determine whether the input needs to be unpremultiplied as
  // part of the transfer, the effect consults the alpha mode metadata of the
  // input surface or the input effect. We don't have such a concept in Moz2D,
  // and giving Moz2D users different results based on something that cannot be
  // influenced through Moz2D APIs seems like a bad idea.
  // We solve this by applying a premultiply effect to the input before feeding
  // it into the transfer effect. The premultiply effect always premultiplies
  // regardless of any alpha mode metadata on inputs, and it always marks its
  // output as premultiplied so that the transfer effect will unpremultiply
  // consistently. Feeding always-premultiplied input into the transfer effect
  // also avoids another problem that would appear when individual color
  // channels disable the transfer: In that case, the disabled channels would
  // pass through unchanged in their unpremultiplied form and the other
  // channels would be premultiplied, giving a mixed result.
  // But since we now ensure that the input is premultiplied, disabled channels
  // will pass premultiplied values through to the result, which is consistent
  // with the enabled channels.
  // We also add an unpremultiply effect that postprocesses the result of the
  // transfer effect because getting unpremultiplied results from the transfer
  // filters is part of the FilterNode API.
  HRESULT hr;

  hr = aDC->CreateEffect(CLSID_D2D1Premultiply, getter_AddRefs(mPrePremultiplyEffect));

  if (FAILED(hr) || !mPrePremultiplyEffect) {
    gfxWarning() << "Failed to create ComponentTransfer filter!";
    return;
  }

  hr = aDC->CreateEffect(CLSID_D2D1UnPremultiply, getter_AddRefs(mPostUnpremultiplyEffect));

  if (FAILED(hr) || !mPostUnpremultiplyEffect) {
    gfxWarning() << "Failed to create ComponentTransfer filter!";
    return;
  }

  aFilterNode->InputEffect()->SetInputEffect(0, mPrePremultiplyEffect.get());
  mPostUnpremultiplyEffect->SetInputEffect(0, aFilterNode->OutputEffect());
}

}
}