SVGFEDisplacementMapElement.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/SVGFEDisplacementMapElement.h"
  6. #include "mozilla/dom/SVGFEDisplacementMapElementBinding.h"
  7. #include "nsSVGFilterInstance.h"
  8. #include "nsSVGUtils.h"
  9. NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(FEDisplacementMap)
  10. using namespace mozilla::gfx;
  11. namespace mozilla {
  12. namespace dom {
  13. JSObject*
  14. SVGFEDisplacementMapElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  15. {
  16. return SVGFEDisplacementMapElementBinding::Wrap(aCx, this, aGivenProto);
  17. }
  18. nsSVGElement::NumberInfo SVGFEDisplacementMapElement::sNumberInfo[1] =
  19. {
  20. { &nsGkAtoms::scale, 0, false },
  21. };
  22. nsSVGEnumMapping SVGFEDisplacementMapElement::sChannelMap[] = {
  23. {&nsGkAtoms::R, SVG_CHANNEL_R},
  24. {&nsGkAtoms::G, SVG_CHANNEL_G},
  25. {&nsGkAtoms::B, SVG_CHANNEL_B},
  26. {&nsGkAtoms::A, SVG_CHANNEL_A},
  27. {nullptr, 0}
  28. };
  29. nsSVGElement::EnumInfo SVGFEDisplacementMapElement::sEnumInfo[2] =
  30. {
  31. { &nsGkAtoms::xChannelSelector,
  32. sChannelMap,
  33. SVG_CHANNEL_A
  34. },
  35. { &nsGkAtoms::yChannelSelector,
  36. sChannelMap,
  37. SVG_CHANNEL_A
  38. }
  39. };
  40. nsSVGElement::StringInfo SVGFEDisplacementMapElement::sStringInfo[3] =
  41. {
  42. { &nsGkAtoms::result, kNameSpaceID_None, true },
  43. { &nsGkAtoms::in, kNameSpaceID_None, true },
  44. { &nsGkAtoms::in2, kNameSpaceID_None, true }
  45. };
  46. //----------------------------------------------------------------------
  47. // nsIDOMNode methods
  48. NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFEDisplacementMapElement)
  49. //----------------------------------------------------------------------
  50. already_AddRefed<SVGAnimatedString>
  51. SVGFEDisplacementMapElement::In1()
  52. {
  53. return mStringAttributes[IN1].ToDOMAnimatedString(this);
  54. }
  55. already_AddRefed<SVGAnimatedString>
  56. SVGFEDisplacementMapElement::In2()
  57. {
  58. return mStringAttributes[IN2].ToDOMAnimatedString(this);
  59. }
  60. already_AddRefed<SVGAnimatedNumber>
  61. SVGFEDisplacementMapElement::Scale()
  62. {
  63. return mNumberAttributes[SCALE].ToDOMAnimatedNumber(this);
  64. }
  65. already_AddRefed<SVGAnimatedEnumeration>
  66. SVGFEDisplacementMapElement::XChannelSelector()
  67. {
  68. return mEnumAttributes[CHANNEL_X].ToDOMAnimatedEnum(this);
  69. }
  70. already_AddRefed<SVGAnimatedEnumeration>
  71. SVGFEDisplacementMapElement::YChannelSelector()
  72. {
  73. return mEnumAttributes[CHANNEL_Y].ToDOMAnimatedEnum(this);
  74. }
  75. FilterPrimitiveDescription
  76. SVGFEDisplacementMapElement::GetPrimitiveDescription(nsSVGFilterInstance* aInstance,
  77. const IntRect& aFilterSubregion,
  78. const nsTArray<bool>& aInputsAreTainted,
  79. nsTArray<RefPtr<SourceSurface>>& aInputImages)
  80. {
  81. if (aInputsAreTainted[1]) {
  82. // If the map is tainted, refuse to apply the effect and act as a
  83. // pass-through filter instead, as required by the spec.
  84. FilterPrimitiveDescription descr(PrimitiveType::Offset);
  85. descr.Attributes().Set(eOffsetOffset, IntPoint(0, 0));
  86. return descr;
  87. }
  88. float scale = aInstance->GetPrimitiveNumber(SVGContentUtils::XY,
  89. &mNumberAttributes[SCALE]);
  90. uint32_t xChannel = mEnumAttributes[CHANNEL_X].GetAnimValue();
  91. uint32_t yChannel = mEnumAttributes[CHANNEL_Y].GetAnimValue();
  92. FilterPrimitiveDescription descr(PrimitiveType::DisplacementMap);
  93. descr.Attributes().Set(eDisplacementMapScale, scale);
  94. descr.Attributes().Set(eDisplacementMapXChannel, xChannel);
  95. descr.Attributes().Set(eDisplacementMapYChannel, yChannel);
  96. return descr;
  97. }
  98. bool
  99. SVGFEDisplacementMapElement::AttributeAffectsRendering(int32_t aNameSpaceID,
  100. nsIAtom* aAttribute) const
  101. {
  102. return SVGFEDisplacementMapElementBase::AttributeAffectsRendering(aNameSpaceID, aAttribute) ||
  103. (aNameSpaceID == kNameSpaceID_None &&
  104. (aAttribute == nsGkAtoms::in ||
  105. aAttribute == nsGkAtoms::in2 ||
  106. aAttribute == nsGkAtoms::scale ||
  107. aAttribute == nsGkAtoms::xChannelSelector ||
  108. aAttribute == nsGkAtoms::yChannelSelector));
  109. }
  110. void
  111. SVGFEDisplacementMapElement::GetSourceImageNames(nsTArray<nsSVGStringInfo>& aSources)
  112. {
  113. aSources.AppendElement(nsSVGStringInfo(&mStringAttributes[IN1], this));
  114. aSources.AppendElement(nsSVGStringInfo(&mStringAttributes[IN2], this));
  115. }
  116. //----------------------------------------------------------------------
  117. // nsSVGElement methods
  118. nsSVGElement::NumberAttributesInfo
  119. SVGFEDisplacementMapElement::GetNumberInfo()
  120. {
  121. return NumberAttributesInfo(mNumberAttributes, sNumberInfo,
  122. ArrayLength(sNumberInfo));
  123. }
  124. nsSVGElement::EnumAttributesInfo
  125. SVGFEDisplacementMapElement::GetEnumInfo()
  126. {
  127. return EnumAttributesInfo(mEnumAttributes, sEnumInfo,
  128. ArrayLength(sEnumInfo));
  129. }
  130. nsSVGElement::StringAttributesInfo
  131. SVGFEDisplacementMapElement::GetStringInfo()
  132. {
  133. return StringAttributesInfo(mStringAttributes, sStringInfo,
  134. ArrayLength(sStringInfo));
  135. }
  136. } // namespace dom
  137. } // namespace mozilla