tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CSSFilterInstance.h (5588B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef LAYOUT_SVG_CSSFILTERINSTANCE_H_
      8 #define LAYOUT_SVG_CSSFILTERINSTANCE_H_
      9 
     10 #include "FilterSupport.h"
     11 #include "gfxMatrix.h"
     12 #include "gfxRect.h"
     13 #include "mozilla/ServoStyleConsts.h"
     14 #include "mozilla/gfx/Point.h"
     15 #include "mozilla/gfx/Types.h"
     16 #include "nsColor.h"
     17 
     18 namespace mozilla {
     19 
     20 /**
     21 * This class helps FilterInstance build its filter graph. It turns a CSS
     22 * filter function (e.g. blur(3px)) from the style system into a
     23 * FilterPrimitiveDescription connected to the filter graph.
     24 */
     25 class CSSFilterInstance {
     26  using sRGBColor = gfx::sRGBColor;
     27  using FilterPrimitiveDescription = gfx::FilterPrimitiveDescription;
     28  using IntPoint = gfx::IntPoint;
     29  using Size = gfx::Size;
     30 
     31 public:
     32  /**
     33   * @param aFilter The CSS filter from the style system. This class stores
     34   *   aFilter by reference, so callers should avoid modifying or deleting
     35   *   aFilter during the lifetime of CSSFilterInstance.
     36   * @param aShadowFallbackColor The color that should be used for
     37   *   drop-shadow() filters that don't specify a shadow color.
     38   * @param aTargetBoundsInFilterSpace The pre-filter ink overflow rect of
     39   *   the frame being filtered, in filter space.
     40   * @param aFrameSpaceInCSSPxToFilterSpaceTransform The transformation from
     41   *   the filtered element's frame space in CSS pixels to filter space.
     42   */
     43  CSSFilterInstance(const StyleFilter& aFilter, nscolor aShadowFallbackColor,
     44                    const nsIntRect& aTargetBoundsInFilterSpace,
     45                    const gfxMatrix& aFrameSpaceInCSSPxToFilterSpaceTransform);
     46 
     47  /**
     48   * Creates at least one new FilterPrimitiveDescription based on the filter
     49   * from the style system. Appends the new FilterPrimitiveDescription(s) to the
     50   * aPrimitiveDescrs list.
     51   * aInputIsTainted describes whether the input to this filter is tainted, i.e.
     52   * whether it contains security-sensitive content. This is needed to propagate
     53   * taintedness to the FilterPrimitive that take tainted inputs. Something
     54   * being tainted means that it contains security sensitive content. The input
     55   * to this filter is the previous filter's output, i.e. the last element in
     56   * aPrimitiveDescrs, or the SourceGraphic input if this is the first filter in
     57   * the filter chain.
     58   */
     59  nsresult BuildPrimitives(
     60      nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
     61      bool aInputIsTainted);
     62 
     63 private:
     64  /**
     65   * Returns a new FilterPrimitiveDescription with its basic properties set up.
     66   * See the comment above BuildPrimitives for the meaning of aInputIsTainted.
     67   */
     68  FilterPrimitiveDescription CreatePrimitiveDescription(
     69      const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
     70      bool aInputIsTainted);
     71 
     72  /**
     73   * Sets aDescr's attributes using the style info in mFilter.
     74   */
     75  nsresult SetAttributesForBlur(FilterPrimitiveDescription& aDescr);
     76  nsresult SetAttributesForBrightness(FilterPrimitiveDescription& aDescr);
     77  nsresult SetAttributesForContrast(FilterPrimitiveDescription& aDescr);
     78  nsresult SetAttributesForDropShadow(FilterPrimitiveDescription& aDescr);
     79  nsresult SetAttributesForGrayscale(FilterPrimitiveDescription& aDescr);
     80  nsresult SetAttributesForHueRotate(FilterPrimitiveDescription& aDescr);
     81  nsresult SetAttributesForInvert(FilterPrimitiveDescription& aDescr);
     82  nsresult SetAttributesForOpacity(FilterPrimitiveDescription& aDescr);
     83  nsresult SetAttributesForSaturate(FilterPrimitiveDescription& aDescr);
     84  nsresult SetAttributesForSepia(FilterPrimitiveDescription& aDescr);
     85 
     86  /**
     87   * Returns the index of the last result in the aPrimitiveDescrs, which we'll
     88   * use as the input to this CSS filter.
     89   */
     90  int32_t GetLastResultIndex(
     91      const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs);
     92 
     93  /**
     94   * Sets aDescr's filter region and primitive subregion to appropriate values
     95   * based on this CSS filter's input and its attributes. For example, a CSS
     96   * blur filter will have bounds equal to its input bounds, inflated by the
     97   * blur extents.
     98   */
     99  void SetBounds(FilterPrimitiveDescription& aDescr,
    100                 const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs);
    101 
    102  /**
    103   * Converts a blur radius in frame space to filter space.
    104   */
    105  Size BlurRadiusToFilterSpace(nscoord aRadiusInFrameSpace);
    106 
    107  /**
    108   * Converts a point defined by a pair of nscoord x, y coordinates from frame
    109   * space to filter space.
    110   */
    111  IntPoint OffsetToFilterSpace(nscoord aXOffsetInFrameSpace,
    112                               nscoord aYOffsetInFrameSpace);
    113 
    114  /**
    115   * The CSS filter originally from the style system.
    116   */
    117  const StyleFilter& mFilter;
    118 
    119  /**
    120   * The color that should be used for drop-shadow() filters that don't
    121   * specify a shadow color.
    122   */
    123  nscolor mShadowFallbackColor;
    124 
    125  /**
    126   * The pre-filter overflow rect of the frame being filtered, in filter space.
    127   * Used for input bounds if this CSS filter is the first in the filter chain.
    128   */
    129  nsIntRect mTargetBoundsInFilterSpace;
    130 
    131  /**
    132   * The transformation from the filtered element's frame space in CSS pixels to
    133   * filter space. Used to transform style values to filter space.
    134   */
    135  gfxMatrix mFrameSpaceInCSSPxToFilterSpaceTransform;
    136 };
    137 
    138 }  // namespace mozilla
    139 
    140 #endif  // LAYOUT_SVG_CSSFILTERINSTANCE_H_