gfx/src/FilterSupport.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     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/. */
     6 #ifndef __FilterSupport_h
     7 #define __FilterSupport_h
     9 #include "mozilla/Attributes.h"
    10 #include "mozilla/RefPtr.h"
    11 #include "mozilla/TypedEnum.h"
    12 #include "mozilla/gfx/Rect.h"
    13 #include "mozilla/gfx/Matrix.h"
    14 #include "nsClassHashtable.h"
    15 #include "nsTArray.h"
    16 #include "nsRegion.h"
    18 namespace mozilla {
    19 namespace gfx {
    21 // Morphology Operators
    22 const unsigned short SVG_OPERATOR_UNKNOWN = 0;
    23 const unsigned short SVG_OPERATOR_ERODE = 1;
    24 const unsigned short SVG_OPERATOR_DILATE = 2;
    26 // ColorMatrix types
    27 const unsigned short SVG_FECOLORMATRIX_TYPE_UNKNOWN = 0;
    28 const unsigned short SVG_FECOLORMATRIX_TYPE_MATRIX = 1;
    29 const unsigned short SVG_FECOLORMATRIX_TYPE_SATURATE = 2;
    30 const unsigned short SVG_FECOLORMATRIX_TYPE_HUE_ROTATE = 3;
    31 const unsigned short SVG_FECOLORMATRIX_TYPE_LUMINANCE_TO_ALPHA = 4;
    33 // ComponentTransfer types
    34 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN  = 0;
    35 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_IDENTITY = 1;
    36 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_TABLE    = 2;
    37 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_DISCRETE = 3;
    38 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_LINEAR   = 4;
    39 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_GAMMA    = 5;
    41 // Blend Mode Values
    42 const unsigned short SVG_FEBLEND_MODE_UNKNOWN = 0;
    43 const unsigned short SVG_FEBLEND_MODE_NORMAL = 1;
    44 const unsigned short SVG_FEBLEND_MODE_MULTIPLY = 2;
    45 const unsigned short SVG_FEBLEND_MODE_SCREEN = 3;
    46 const unsigned short SVG_FEBLEND_MODE_DARKEN = 4;
    47 const unsigned short SVG_FEBLEND_MODE_LIGHTEN = 5;
    49 // Edge Mode Values
    50 const unsigned short SVG_EDGEMODE_UNKNOWN = 0;
    51 const unsigned short SVG_EDGEMODE_DUPLICATE = 1;
    52 const unsigned short SVG_EDGEMODE_WRAP = 2;
    53 const unsigned short SVG_EDGEMODE_NONE = 3;
    55 // Channel Selectors
    56 const unsigned short SVG_CHANNEL_UNKNOWN = 0;
    57 const unsigned short SVG_CHANNEL_R = 1;
    58 const unsigned short SVG_CHANNEL_G = 2;
    59 const unsigned short SVG_CHANNEL_B = 3;
    60 const unsigned short SVG_CHANNEL_A = 4;
    62 // Turbulence Types
    63 const unsigned short SVG_TURBULENCE_TYPE_UNKNOWN = 0;
    64 const unsigned short SVG_TURBULENCE_TYPE_FRACTALNOISE = 1;
    65 const unsigned short SVG_TURBULENCE_TYPE_TURBULENCE = 2;
    67 // Composite Operators
    68 const unsigned short SVG_FECOMPOSITE_OPERATOR_UNKNOWN = 0;
    69 const unsigned short SVG_FECOMPOSITE_OPERATOR_OVER = 1;
    70 const unsigned short SVG_FECOMPOSITE_OPERATOR_IN = 2;
    71 const unsigned short SVG_FECOMPOSITE_OPERATOR_OUT = 3;
    72 const unsigned short SVG_FECOMPOSITE_OPERATOR_ATOP = 4;
    73 const unsigned short SVG_FECOMPOSITE_OPERATOR_XOR = 5;
    74 const unsigned short SVG_FECOMPOSITE_OPERATOR_ARITHMETIC = 6;
    76 enum AttributeName {
    77   eBlendBlendmode = 0,
    78   eMorphologyRadii,
    79   eMorphologyOperator,
    80   eColorMatrixType,
    81   eColorMatrixValues,
    82   eFloodColor,
    83   eTileSourceRect,
    84   eComponentTransferFunctionR,
    85   eComponentTransferFunctionG,
    86   eComponentTransferFunctionB,
    87   eComponentTransferFunctionA,
    88   eComponentTransferFunctionType,
    89   eComponentTransferFunctionTableValues,
    90   eComponentTransferFunctionSlope,
    91   eComponentTransferFunctionIntercept,
    92   eComponentTransferFunctionAmplitude,
    93   eComponentTransferFunctionExponent,
    94   eComponentTransferFunctionOffset,
    95   eConvolveMatrixKernelSize,
    96   eConvolveMatrixKernelMatrix,
    97   eConvolveMatrixDivisor,
    98   eConvolveMatrixBias,
    99   eConvolveMatrixTarget,
   100   eConvolveMatrixEdgeMode,
   101   eConvolveMatrixKernelUnitLength,
   102   eConvolveMatrixPreserveAlpha,
   103   eOffsetOffset,
   104   eDropShadowStdDeviation,
   105   eDropShadowOffset,
   106   eDropShadowColor,
   107   eDisplacementMapScale,
   108   eDisplacementMapXChannel,
   109   eDisplacementMapYChannel,
   110   eTurbulenceOffset,
   111   eTurbulenceBaseFrequency,
   112   eTurbulenceNumOctaves,
   113   eTurbulenceSeed,
   114   eTurbulenceStitchable,
   115   eTurbulenceType,
   116   eCompositeOperator,
   117   eCompositeCoefficients,
   118   eGaussianBlurStdDeviation,
   119   eLightingLight,
   120   eLightingSurfaceScale,
   121   eLightingKernelUnitLength,
   122   eLightingColor,
   123   eDiffuseLightingDiffuseConstant,
   124   eSpecularLightingSpecularConstant,
   125   eSpecularLightingSpecularExponent,
   126   eLightType,
   127   eLightTypeNone,
   128   eLightTypePoint,
   129   eLightTypeSpot,
   130   eLightTypeDistant,
   131   ePointLightPosition,
   132   eSpotLightPosition,
   133   eSpotLightPointsAt,
   134   eSpotLightFocus,
   135   eSpotLightLimitingConeAngle,
   136   eDistantLightAzimuth,
   137   eDistantLightElevation,
   138   eImageInputIndex,
   139   eImageFilter,
   140   eImageNativeSize,
   141   eImageSubregion,
   142   eImageTransform,
   143   eLastAttributeName
   144 };
   146 class DrawTarget;
   147 class SourceSurface;
   148 class FilterNode;
   149 struct FilterAttribute;
   151 MOZ_BEGIN_ENUM_CLASS(AttributeType)
   152   eBool,
   153   eUint,
   154   eFloat,
   155   eSize,
   156   eIntSize,
   157   eIntPoint,
   158   eMatrix,
   159   eMatrix5x4,
   160   ePoint3D,
   161   eColor,
   162   eAttributeMap,
   163   eFloats,
   164   Max
   165 MOZ_END_ENUM_CLASS(AttributeType)
   167 // A class that stores values of different types, keyed by an attribute name.
   168 // The Get*() methods assert that they're called for the same type that the
   169 // attribute was Set() with.
   170 // AttributeMaps can be nested because AttributeMap is a valid attribute type.
   171 class AttributeMap MOZ_FINAL {
   172 public:
   173   AttributeMap();
   174   AttributeMap(const AttributeMap& aOther);
   175   AttributeMap& operator=(const AttributeMap& aOther);
   176   bool operator==(const AttributeMap& aOther) const;
   177   bool operator!=(const AttributeMap& aOther) const
   178   {
   179     return !(*this == aOther);
   180   }
   181   ~AttributeMap();
   183   void Set(AttributeName aName, bool aValue);
   184   void Set(AttributeName aName, uint32_t aValue);
   185   void Set(AttributeName aName, float aValue);
   186   void Set(AttributeName aName, const Size& aValue);
   187   void Set(AttributeName aName, const IntSize& aValue);
   188   void Set(AttributeName aName, const IntPoint& aValue);
   189   void Set(AttributeName aName, const Matrix& aValue);
   190   void Set(AttributeName aName, const Matrix5x4& aValue);
   191   void Set(AttributeName aName, const Point3D& aValue);
   192   void Set(AttributeName aName, const Color& aValue);
   193   void Set(AttributeName aName, const AttributeMap& aValue);
   194   void Set(AttributeName aName, const float* aValues, int32_t aLength);
   196   bool GetBool(AttributeName aName) const;
   197   uint32_t GetUint(AttributeName aName) const;
   198   float GetFloat(AttributeName aName) const;
   199   Size GetSize(AttributeName aName) const;
   200   IntSize GetIntSize(AttributeName aName) const;
   201   IntPoint GetIntPoint(AttributeName aName) const;
   202   Matrix GetMatrix(AttributeName aName) const;
   203   Matrix5x4 GetMatrix5x4(AttributeName aName) const;
   204   Point3D GetPoint3D(AttributeName aName) const;
   205   Color GetColor(AttributeName aName) const;
   206   AttributeMap GetAttributeMap(AttributeName aName) const;
   207   const nsTArray<float>& GetFloats(AttributeName aName) const;
   209   typedef bool (*AttributeHandleCallback)(AttributeName aName, AttributeType aType, void* aUserData);
   210   void EnumerateRead(AttributeHandleCallback aCallback, void* aUserData) const;
   211   uint32_t Count() const;
   213 private:
   214   mutable nsClassHashtable<nsUint32HashKey, FilterAttribute>  mMap;
   215 };
   217 MOZ_BEGIN_ENUM_CLASS(ColorSpace)
   218   SRGB,
   219   LinearRGB,
   220   Max
   221 MOZ_END_ENUM_CLASS(ColorSpace)
   223 MOZ_BEGIN_ENUM_CLASS(AlphaModel)
   224   Unpremultiplied,
   225   Premultiplied
   226 MOZ_END_ENUM_CLASS(AlphaModel)
   228 class ColorModel {
   229 public:
   230   static ColorModel PremulSRGB()
   231   {
   232     return ColorModel(ColorSpace::SRGB, AlphaModel::Premultiplied);
   233   }
   235   ColorModel(ColorSpace aColorSpace, AlphaModel aAlphaModel) :
   236     mColorSpace(aColorSpace), mAlphaModel(aAlphaModel) {}
   237   ColorModel() :
   238     mColorSpace(ColorSpace::SRGB), mAlphaModel(AlphaModel::Premultiplied) {}
   239   bool operator==(const ColorModel& aOther) const {
   240     return mColorSpace == aOther.mColorSpace &&
   241            mAlphaModel == aOther.mAlphaModel;
   242   }
   244   // Used to index FilterCachedColorModels::mFilterForColorModel.
   245   uint8_t ToIndex() const
   246   {
   247     return (uint8_t(mColorSpace) << 1) + uint8_t(mAlphaModel);
   248   }
   250   ColorSpace mColorSpace;
   251   AlphaModel mAlphaModel;
   252 };
   254 MOZ_BEGIN_ENUM_CLASS(PrimitiveType)
   255   Empty = 0,
   256   Blend,
   257   Morphology,
   258   ColorMatrix,
   259   Flood,
   260   Tile,
   261   ComponentTransfer,
   262   ConvolveMatrix,
   263   Offset,
   264   DisplacementMap,
   265   Turbulence,
   266   Composite,
   267   Merge,
   268   Image,
   269   GaussianBlur,
   270   DropShadow,
   271   DiffuseLighting,
   272   SpecularLighting,
   273   Max
   274 MOZ_END_ENUM_CLASS(PrimitiveType)
   276 /**
   277  * A data structure to carry attributes for a given primitive that's part of a
   278  * filter. Will be serializable via IPDL, so it must not contain complex
   279  * functionality.
   280  * Used as part of a FilterDescription.
   281  */
   282 class FilterPrimitiveDescription MOZ_FINAL {
   283 public:
   284   enum {
   285     kPrimitiveIndexSourceGraphic = -1,
   286     kPrimitiveIndexSourceAlpha = -2,
   287     kPrimitiveIndexFillPaint = -3,
   288     kPrimitiveIndexStrokePaint = -4
   289   };
   291   FilterPrimitiveDescription();
   292   FilterPrimitiveDescription(PrimitiveType aType);
   293   FilterPrimitiveDescription(const FilterPrimitiveDescription& aOther);
   294   FilterPrimitiveDescription& operator=(const FilterPrimitiveDescription& aOther);
   296   PrimitiveType Type() const { return mType; }
   297   void SetType(PrimitiveType aType) { mType = aType; }
   298   const AttributeMap& Attributes() const { return mAttributes; }
   299   AttributeMap& Attributes() { return mAttributes; }
   301   IntRect PrimitiveSubregion() const { return mFilterPrimitiveSubregion; }
   302   bool IsTainted() const { return mIsTainted; }
   304   size_t NumberOfInputs() const { return mInputPrimitives.Length(); }
   305   int32_t InputPrimitiveIndex(size_t aInputIndex) const
   306   {
   307     return aInputIndex < mInputPrimitives.Length() ?
   308       mInputPrimitives[aInputIndex] : 0;
   309   }
   311   ColorSpace InputColorSpace(size_t aInputIndex) const
   312   {
   313     return aInputIndex < mInputColorSpaces.Length() ?
   314       mInputColorSpaces[aInputIndex] : ColorSpace();
   315   }
   317   ColorSpace OutputColorSpace() const { return mOutputColorSpace; }
   319   void SetPrimitiveSubregion(const IntRect& aRect)
   320   {
   321     mFilterPrimitiveSubregion = aRect;
   322   }
   324   void SetIsTainted(bool aIsTainted)
   325   {
   326     mIsTainted = aIsTainted;
   327   }
   329   void SetInputPrimitive(size_t aInputIndex, int32_t aInputPrimitiveIndex)
   330   {
   331     mInputPrimitives.EnsureLengthAtLeast(aInputIndex + 1);
   332     mInputPrimitives[aInputIndex] = aInputPrimitiveIndex;
   333   }
   335   void SetInputColorSpace(size_t aInputIndex, ColorSpace aColorSpace)
   336   {
   337     mInputColorSpaces.EnsureLengthAtLeast(aInputIndex + 1);
   338     mInputColorSpaces[aInputIndex] = aColorSpace;
   339   }
   341   void SetOutputColorSpace(const ColorSpace& aColorSpace)
   342   {
   343     mOutputColorSpace = aColorSpace;
   344   }
   346   bool operator==(const FilterPrimitiveDescription& aOther) const;
   347   bool operator!=(const FilterPrimitiveDescription& aOther) const
   348   {
   349     return !(*this == aOther);
   350   }
   352 private:
   353   PrimitiveType mType;
   354   AttributeMap mAttributes;
   355   nsTArray<int32_t> mInputPrimitives;
   356   IntRect mFilterPrimitiveSubregion;
   357   nsTArray<ColorSpace> mInputColorSpaces;
   358   ColorSpace mOutputColorSpace;
   359   bool mIsTainted;
   360 };
   362 /**
   363  * A data structure that contains one or more FilterPrimitiveDescriptions.
   364  * Designed to be serializable via IPDL, so it must not contain complex
   365  * functionality.
   366  */
   367 struct FilterDescription MOZ_FINAL {
   368   FilterDescription() {}
   369   FilterDescription(const nsTArray<FilterPrimitiveDescription>& aPrimitives,
   370                     const IntRect& aFilterSpaceBounds)
   371    : mPrimitives(aPrimitives)
   372    , mFilterSpaceBounds(aFilterSpaceBounds)
   373   {}
   375   bool operator==(const FilterDescription& aOther) const;
   376   bool operator!=(const FilterDescription& aOther) const
   377   {
   378     return !(*this == aOther);
   379   }
   381   nsTArray<FilterPrimitiveDescription> mPrimitives;
   382   IntRect mFilterSpaceBounds;
   383 };
   385 /**
   386  * The methods of this class are not on FilterDescription because
   387  * FilterDescription is designed as a simple value holder that can be used
   388  * on any thread.
   389  */
   390 class FilterSupport {
   391 public:
   393   /**
   394    * Draw the filter described by aFilter. All rect parameters are in filter
   395    * space coordinates. aRenderRect specifies the part of the filter output
   396    * that will be drawn at (0, 0) into the draw target aDT, subject to the
   397    * current transform on aDT but with no additional scaling.
   398    * The source surfaces must match their corresponding rect in size.
   399    * aAdditionalImages carries the images that are referenced by the
   400    * eImageInputIndex attribute on any image primitives in the filter.
   401    */
   402   static void
   403   RenderFilterDescription(DrawTarget* aDT,
   404                           const FilterDescription& aFilter,
   405                           const Rect& aRenderRect,
   406                           SourceSurface* aSourceGraphic,
   407                           const IntRect& aSourceGraphicRect,
   408                           SourceSurface* aFillPaint,
   409                           const IntRect& aFillPaintRect,
   410                           SourceSurface* aStrokePaint,
   411                           const IntRect& aStrokePaintRect,
   412                           nsTArray<RefPtr<SourceSurface>>& aAdditionalImages);
   414   /**
   415    * Computes the region that changes in the filter output due to a change in
   416    * input.
   417    */
   418   static nsIntRegion
   419   ComputeResultChangeRegion(const FilterDescription& aFilter,
   420                             const nsIntRegion& aSourceGraphicChange,
   421                             const nsIntRegion& aFillPaintChange,
   422                             const nsIntRegion& aStrokePaintChange);
   424   /**
   425    * Computes the regions that need to be supplied in the filter inputs when
   426    * painting aResultNeededRegion of the filter output.
   427    */
   428   static void
   429   ComputeSourceNeededRegions(const FilterDescription& aFilter,
   430                              const nsIntRegion& aResultNeededRegion,
   431                              nsIntRegion& aSourceGraphicNeededRegion,
   432                              nsIntRegion& aFillPaintNeededRegion,
   433                              nsIntRegion& aStrokePaintNeededRegion);
   435   /**
   436    * Computes the size of the filter output.
   437    */
   438   static nsIntRegion
   439   ComputePostFilterExtents(const FilterDescription& aFilter,
   440                            const nsIntRegion& aSourceGraphicExtents);
   442 };
   444 }
   445 }
   447 #endif // __FilterSupport_h

mercurial