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 (b6d82b1a6b02)

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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

// Main header first:
#include "nsCSSFilterInstance.h"

// Keep others in (case-insensitive) order:
#include "gfx2DGlue.h"
#include "gfxUtils.h"
#include "nsIFrame.h"
#include "nsStyleStruct.h"
#include "nsTArray.h"

using namespace mozilla;
using namespace mozilla::gfx;

static float ClampFactor(float aFactor) {
  if (aFactor > 1) {
    return 1;
  }
  if (aFactor < 0) {
    MOZ_ASSERT_UNREACHABLE("A negative value should not have been parsed.");
    return 0;
  }

  return aFactor;
}

nsCSSFilterInstance::nsCSSFilterInstance(
    const StyleFilter& aFilter, nscolor aShadowFallbackColor,
    const nsIntRect& aTargetBoundsInFilterSpace,
    const gfxMatrix& aFrameSpaceInCSSPxToFilterSpaceTransform)
    : mFilter(aFilter),
      mShadowFallbackColor(aShadowFallbackColor),
      mTargetBoundsInFilterSpace(aTargetBoundsInFilterSpace),
      mFrameSpaceInCSSPxToFilterSpaceTransform(
          aFrameSpaceInCSSPxToFilterSpaceTransform) {}

nsresult nsCSSFilterInstance::BuildPrimitives(
    nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
    bool aInputIsTainted) {
  FilterPrimitiveDescription descr =
      CreatePrimitiveDescription(aPrimitiveDescrs, aInputIsTainted);
  nsresult result;
  switch (mFilter.tag) {
    case StyleFilter::Tag::Blur:
      result = SetAttributesForBlur(descr);
      break;
    case StyleFilter::Tag::Brightness:
      result = SetAttributesForBrightness(descr);
      break;
    case StyleFilter::Tag::Contrast:
      result = SetAttributesForContrast(descr);
      break;
    case StyleFilter::Tag::DropShadow:
      result = SetAttributesForDropShadow(descr);
      break;
    case StyleFilter::Tag::Grayscale:
      result = SetAttributesForGrayscale(descr);
      break;
    case StyleFilter::Tag::HueRotate:
      result = SetAttributesForHueRotate(descr);
      break;
    case StyleFilter::Tag::Invert:
      result = SetAttributesForInvert(descr);
      break;
    case StyleFilter::Tag::Opacity:
      result = SetAttributesForOpacity(descr);
      break;
    case StyleFilter::Tag::Saturate:
      result = SetAttributesForSaturate(descr);
      break;
    case StyleFilter::Tag::Sepia:
      result = SetAttributesForSepia(descr);
      break;
    default:
      MOZ_ASSERT_UNREACHABLE("not a valid CSS filter type");
      return NS_ERROR_FAILURE;
  }

  if (NS_FAILED(result)) {
    return result;
  }

  // Compute the primitive's bounds now that we've determined its attributes.
  // Some attributes like blur radius can influence the bounds.
  SetBounds(descr, aPrimitiveDescrs);

  // Add this primitive to the filter chain.
  aPrimitiveDescrs.AppendElement(std::move(descr));
  return NS_OK;
}

FilterPrimitiveDescription nsCSSFilterInstance::CreatePrimitiveDescription(
    const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
    bool aInputIsTainted) {
  FilterPrimitiveDescription descr;
  int32_t inputIndex = GetLastResultIndex(aPrimitiveDescrs);
  descr.SetInputPrimitive(0, inputIndex);
  descr.SetIsTainted(inputIndex < 0 ? aInputIsTainted
                                    : aPrimitiveDescrs[inputIndex].IsTainted());
  descr.SetInputColorSpace(0, ColorSpace::SRGB);
  descr.SetOutputColorSpace(ColorSpace::SRGB);
  return descr;
}

nsresult nsCSSFilterInstance::SetAttributesForBlur(
    FilterPrimitiveDescription& aDescr) {
  const Length& radiusInFrameSpace = mFilter.AsBlur();
  Size radiusInFilterSpace =
      BlurRadiusToFilterSpace(radiusInFrameSpace.ToAppUnits());
  GaussianBlurAttributes atts;
  atts.mStdDeviation = radiusInFilterSpace;
  aDescr.Attributes() = AsVariant(atts);
  return NS_OK;
}

nsresult nsCSSFilterInstance::SetAttributesForBrightness(
    FilterPrimitiveDescription& aDescr) {
  float value = mFilter.AsBrightness();
  float intercept = 0.0f;
  ComponentTransferAttributes atts;

  // Set transfer functions for RGB.
  atts.mTypes[kChannelROrRGB] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_LINEAR;
  atts.mTypes[kChannelG] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN;
  atts.mTypes[kChannelB] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN;
  float slopeIntercept[2];
  slopeIntercept[kComponentTransferSlopeIndex] = value;
  slopeIntercept[kComponentTransferInterceptIndex] = intercept;
  atts.mValues[kChannelROrRGB].AppendElements(slopeIntercept, 2);

  atts.mTypes[kChannelA] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_IDENTITY;

  aDescr.Attributes() = AsVariant(std::move(atts));
  return NS_OK;
}

nsresult nsCSSFilterInstance::SetAttributesForContrast(
    FilterPrimitiveDescription& aDescr) {
  float value = mFilter.AsContrast();
  float intercept = -(0.5 * value) + 0.5;
  ComponentTransferAttributes atts;

  // Set transfer functions for RGB.
  atts.mTypes[kChannelROrRGB] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_LINEAR;
  atts.mTypes[kChannelG] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN;
  atts.mTypes[kChannelB] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN;
  float slopeIntercept[2];
  slopeIntercept[kComponentTransferSlopeIndex] = value;
  slopeIntercept[kComponentTransferInterceptIndex] = intercept;
  atts.mValues[kChannelROrRGB].AppendElements(slopeIntercept, 2);

  atts.mTypes[kChannelA] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_IDENTITY;

  aDescr.Attributes() = AsVariant(std::move(atts));
  return NS_OK;
}

nsresult nsCSSFilterInstance::SetAttributesForDropShadow(
    FilterPrimitiveDescription& aDescr) {
  const auto& shadow = mFilter.AsDropShadow();

  DropShadowAttributes atts;

  // Set drop shadow blur radius.
  Size radiusInFilterSpace = BlurRadiusToFilterSpace(shadow.blur.ToAppUnits());
  atts.mStdDeviation = radiusInFilterSpace;

  // Set offset.
  IntPoint offsetInFilterSpace = OffsetToFilterSpace(
      shadow.horizontal.ToAppUnits(), shadow.vertical.ToAppUnits());
  atts.mOffset = offsetInFilterSpace;

  // Set color. If unspecified, use the CSS color property.
  nscolor shadowColor = shadow.color.CalcColor(mShadowFallbackColor);
  atts.mColor = ToAttributeColor(shadowColor);

  aDescr.Attributes() = AsVariant(std::move(atts));
  return NS_OK;
}

nsresult nsCSSFilterInstance::SetAttributesForGrayscale(
    FilterPrimitiveDescription& aDescr) {
  ColorMatrixAttributes atts;
  // Set color matrix type.
  atts.mType = (uint32_t)SVG_FECOLORMATRIX_TYPE_SATURATE;

  // Set color matrix values.
  float value = 1 - ClampFactor(mFilter.AsGrayscale());
  atts.mValues.AppendElements(&value, 1);

  aDescr.Attributes() = AsVariant(std::move(atts));
  return NS_OK;
}

nsresult nsCSSFilterInstance::SetAttributesForHueRotate(
    FilterPrimitiveDescription& aDescr) {
  ColorMatrixAttributes atts;
  // Set color matrix type.
  atts.mType = (uint32_t)SVG_FECOLORMATRIX_TYPE_HUE_ROTATE;

  // Set color matrix values.
  float value = mFilter.AsHueRotate().ToDegrees();
  atts.mValues.AppendElements(&value, 1);

  aDescr.Attributes() = AsVariant(std::move(atts));
  return NS_OK;
}

nsresult nsCSSFilterInstance::SetAttributesForInvert(
    FilterPrimitiveDescription& aDescr) {
  ComponentTransferAttributes atts;
  float value = ClampFactor(mFilter.AsInvert());

  // Set transfer functions for RGB.
  float invertTableValues[2];
  invertTableValues[0] = value;
  invertTableValues[1] = 1 - value;

  // Set transfer functions for RGB.
  atts.mTypes[kChannelROrRGB] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_TABLE;
  atts.mTypes[kChannelG] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN;
  atts.mTypes[kChannelB] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN;
  atts.mValues[kChannelROrRGB].AppendElements(invertTableValues, 2);

  atts.mTypes[kChannelA] = (uint8_t)SVG_FECOMPONENTTRANSFER_TYPE_IDENTITY;

  aDescr.Attributes() = AsVariant(std::move(atts));
  return NS_OK;
}

nsresult nsCSSFilterInstance::SetAttributesForOpacity(
    FilterPrimitiveDescription& aDescr) {
  OpacityAttributes atts;
  float value = ClampFactor(mFilter.AsOpacity());

  atts.mOpacity = value;
  aDescr.Attributes() = AsVariant(std::move(atts));
  return NS_OK;
}

nsresult nsCSSFilterInstance::SetAttributesForSaturate(
    FilterPrimitiveDescription& aDescr) {
  ColorMatrixAttributes atts;
  // Set color matrix type.
  atts.mType = (uint32_t)SVG_FECOLORMATRIX_TYPE_SATURATE;

  // Set color matrix values.
  float value = mFilter.AsSaturate();
  atts.mValues.AppendElements(&value, 1);

  aDescr.Attributes() = AsVariant(std::move(atts));
  return NS_OK;
}

nsresult nsCSSFilterInstance::SetAttributesForSepia(
    FilterPrimitiveDescription& aDescr) {
  ColorMatrixAttributes atts;
  // Set color matrix type.
  atts.mType = (uint32_t)SVG_FECOLORMATRIX_TYPE_SEPIA;

  // Set color matrix values.
  float value = ClampFactor(mFilter.AsSepia());
  atts.mValues.AppendElements(&value, 1);

  aDescr.Attributes() = AsVariant(std::move(atts));
  return NS_OK;
}

Size nsCSSFilterInstance::BlurRadiusToFilterSpace(nscoord aRadiusInFrameSpace) {
  float radiusInFrameSpaceInCSSPx =
      nsPresContext::AppUnitsToFloatCSSPixels(aRadiusInFrameSpace);

  // Convert the radius to filter space.
  Size radiusInFilterSpace(radiusInFrameSpaceInCSSPx,
                           radiusInFrameSpaceInCSSPx);
  gfxSize frameSpaceInCSSPxToFilterSpaceScale =
      mFrameSpaceInCSSPxToFilterSpaceTransform.ScaleFactors(true);
  radiusInFilterSpace.Scale(frameSpaceInCSSPxToFilterSpaceScale.width,
                            frameSpaceInCSSPxToFilterSpaceScale.height);

  // Check the radius limits.
  if (radiusInFilterSpace.width < 0 || radiusInFilterSpace.height < 0) {
    MOZ_ASSERT_UNREACHABLE(
        "we shouldn't have parsed a negative radius in the "
        "style");
    return Size();
  }

  Float maxStdDeviation = (Float)kMaxStdDeviation;
  radiusInFilterSpace.width =
      std::min(radiusInFilterSpace.width, maxStdDeviation);
  radiusInFilterSpace.height =
      std::min(radiusInFilterSpace.height, maxStdDeviation);

  return radiusInFilterSpace;
}

IntPoint nsCSSFilterInstance::OffsetToFilterSpace(
    nscoord aXOffsetInFrameSpace, nscoord aYOffsetInFrameSpace) {
  gfxPoint offsetInFilterSpace(
      nsPresContext::AppUnitsToFloatCSSPixels(aXOffsetInFrameSpace),
      nsPresContext::AppUnitsToFloatCSSPixels(aYOffsetInFrameSpace));

  // Convert the radius to filter space.
  gfxSize frameSpaceInCSSPxToFilterSpaceScale =
      mFrameSpaceInCSSPxToFilterSpaceTransform.ScaleFactors(true);
  offsetInFilterSpace.x *= frameSpaceInCSSPxToFilterSpaceScale.width;
  offsetInFilterSpace.y *= frameSpaceInCSSPxToFilterSpaceScale.height;

  return IntPoint(int32_t(offsetInFilterSpace.x),
                  int32_t(offsetInFilterSpace.y));
}

Color nsCSSFilterInstance::ToAttributeColor(nscolor aColor) {
  return Color(NS_GET_R(aColor) / 255.0, NS_GET_G(aColor) / 255.0,
               NS_GET_B(aColor) / 255.0, NS_GET_A(aColor) / 255.0);
}

int32_t nsCSSFilterInstance::GetLastResultIndex(
    const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs) {
  uint32_t numPrimitiveDescrs = aPrimitiveDescrs.Length();
  return !numPrimitiveDescrs
             ? FilterPrimitiveDescription::kPrimitiveIndexSourceGraphic
             : numPrimitiveDescrs - 1;
}

void nsCSSFilterInstance::SetBounds(
    FilterPrimitiveDescription& aDescr,
    const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs) {
  int32_t inputIndex = GetLastResultIndex(aPrimitiveDescrs);
  nsIntRect inputBounds =
      (inputIndex < 0) ? mTargetBoundsInFilterSpace
                       : aPrimitiveDescrs[inputIndex].PrimitiveSubregion();

  nsTArray<nsIntRegion> inputExtents;
  inputExtents.AppendElement(inputBounds);

  nsIntRegion outputExtents =
      FilterSupport::PostFilterExtentsForPrimitive(aDescr, inputExtents);
  IntRect outputBounds = outputExtents.GetBounds();

  aDescr.SetPrimitiveSubregion(outputBounds);
  aDescr.SetFilterSpaceBounds(outputBounds);
}