tor-browser

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

FilterNodeWebgl.h (13275B)


      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 MOZILLA_GFX_FILTERNODEWEBGL_H_
      8 #define MOZILLA_GFX_FILTERNODEWEBGL_H_
      9 
     10 #include <vector>
     11 
     12 #include "mozilla/gfx/Filters.h"
     13 #include "mozilla/gfx/PatternHelpers.h"
     14 
     15 namespace mozilla::gfx {
     16 
     17 class DrawTargetWebgl;
     18 class FilterNodeSoftware;
     19 
     20 /**
     21 * FilterNodeWebgl wraps a FilterNodeSoftware for most operations that
     22 * are not yet accelerated. To provide acceleration, this must be subclassed
     23 * to override an optimized implementation for particular operations.
     24 */
     25 class FilterNodeWebgl : public FilterNode {
     26 public:
     27  ~FilterNodeWebgl() override;
     28 
     29  FilterBackend GetBackendType() override { return FILTER_BACKEND_WEBGL; }
     30 
     31  FilterType GetType() const { return mType; }
     32 
     33  bool ReserveInputIndex(uint32_t aIndex);
     34  bool SetInputAccel(uint32_t aIndex, SourceSurface* aSurface);
     35  bool SetInputSoftware(uint32_t aIndex, SourceSurface* aSurface);
     36  void SetInput(uint32_t aIndex, SourceSurface* aSurface) override;
     37  void SetInput(uint32_t aIndex, FilterNode* aFilter) override;
     38  void SetAttribute(uint32_t aIndex, bool) override;
     39  void SetAttribute(uint32_t aIndex, uint32_t) override;
     40  void SetAttribute(uint32_t aIndex, Float) override;
     41  void SetAttribute(uint32_t aIndex, const Size&) override;
     42  void SetAttribute(uint32_t aIndex, const IntSize&) override;
     43  void SetAttribute(uint32_t aIndex, const IntPoint&) override;
     44  void SetAttribute(uint32_t aIndex, const Rect&) override;
     45  void SetAttribute(uint32_t aIndex, const IntRect&) override;
     46  void SetAttribute(uint32_t aIndex, const Point&) override;
     47  void SetAttribute(uint32_t aIndex, const Matrix&) override;
     48  void SetAttribute(uint32_t aIndex, const Matrix5x4&) override;
     49  void SetAttribute(uint32_t aIndex, const Point3D&) override;
     50  void SetAttribute(uint32_t aIndex, const DeviceColor&) override;
     51  void SetAttribute(uint32_t aIndex, const Float* aValues,
     52                    uint32_t aSize) override;
     53 
     54  IntRect MapRectToSource(const IntRect& aRect, const IntRect& aMax,
     55                          FilterNode* aSourceNode) override;
     56 
     57  // Draw the root of a filter chain. Any filter drawing originates here.
     58  virtual void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
     59                    const Point& aDestPoint, const DrawOptions& aOptions,
     60                    FilterNodeWebgl* aParent = nullptr);
     61  // Speculatively draw part of a filter chain, only if it can be accelerated.
     62  // On success, it return true. Instead of falling back, nothing is drawn and
     63  // it returns false.
     64  virtual bool DrawAccel(DrawTargetWebgl* aDT, const Rect& aSourceRect,
     65                         const Point& aDestPoint, const DrawOptions& aOptions,
     66                         FilterNodeWebgl* aParent = nullptr) {
     67    return false;
     68  }
     69  // Draw a child filter and return a surface that can be processed by a parent
     70  // filter. This will try to accelerate the filter if possible, but implements
     71  // a fallback if not.
     72  virtual already_AddRefed<SourceSurface> DrawChild(
     73      FilterNodeWebgl* aParent, DrawTargetWebgl* aDT, const Rect& aSourceRect,
     74      const DrawOptions& aOptions, Point& aSurfaceOffset, DeviceColor& aColor);
     75 
     76  // This handles deferred filter inputs that should not be resolved to surfaces
     77  // until right before drawing. Accelerated filters must ensure they call this
     78  // to resolve these inputs before using them for drawing.
     79  virtual void ResolveInputs(DrawTargetWebgl* aDT, bool aAccel,
     80                             FilterNodeWebgl* aParent = nullptr) {}
     81 
     82  // Recursively resolve all inputs in the filter chain.
     83  void ResolveAllInputs(DrawTargetWebgl* aDT, FilterNodeWebgl* aParent);
     84 
     85 protected:
     86  std::vector<RefPtr<FilterNodeWebgl>> mInputFilters;
     87  std::vector<RefPtr<SourceSurface>> mInputSurfaces;
     88  uint32_t mInputMask = 0;
     89  FilterType mType;
     90  RefPtr<FilterNodeSoftware> mSoftwareFilter;
     91 
     92  friend class DrawTargetWebgl;
     93 
     94  explicit FilterNodeWebgl(FilterType aType);
     95 
     96  static already_AddRefed<FilterNodeWebgl> Create(FilterType aType);
     97 
     98  size_t NumberOfSetInputs() const {
     99    return std::max(mInputSurfaces.size(), mInputFilters.size());
    100  }
    101 
    102  virtual int32_t InputIndex(uint32_t aInputEnumIndex) const;
    103 
    104  IntRect MapInputRectToSource(uint32_t aInputEnumIndex, const IntRect& aRect,
    105                               const IntRect& aMax, FilterNode* aSourceNode);
    106 };
    107 
    108 class FilterNodeCropWebgl : public FilterNodeWebgl {
    109 public:
    110  FilterNodeCropWebgl() : FilterNodeWebgl(FilterType::CROP) {}
    111 
    112  void SetAttribute(uint32_t aIndex, const Rect& aValue) override;
    113  IntRect MapRectToSource(const IntRect& aRect, const IntRect& aMax,
    114                          FilterNode* aSourceNode) override;
    115 
    116  void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
    117            const Point& aDestPoint, const DrawOptions& aOptions,
    118            FilterNodeWebgl* aParent) override;
    119  bool DrawAccel(DrawTargetWebgl* aDT, const Rect& aSourceRect,
    120                 const Point& aDestPoint, const DrawOptions& aOptions,
    121                 FilterNodeWebgl* aParent) override;
    122  already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
    123                                            DrawTargetWebgl* aDT,
    124                                            const Rect& aSourceRect,
    125                                            const DrawOptions& aOptions,
    126                                            Point& aSurfaceOffset,
    127                                            DeviceColor& aColor) override;
    128 
    129 private:
    130  IntRect mCropRect;
    131 
    132  int32_t InputIndex(uint32_t aInputEnumIndex) const override;
    133 };
    134 
    135 class FilterNodeTransformWebgl : public FilterNodeWebgl {
    136 public:
    137  FilterNodeTransformWebgl() : FilterNodeWebgl(FilterType::TRANSFORM) {}
    138 
    139  void SetAttribute(uint32_t aIndex, uint32_t aValue) override;
    140  void SetAttribute(uint32_t aIndex, const Matrix& aValue) override;
    141  IntRect MapRectToSource(const IntRect& aRect, const IntRect& aMax,
    142                          FilterNode* aSourceNode) override;
    143 
    144  void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
    145            const Point& aDestPoint, const DrawOptions& aOptions,
    146            FilterNodeWebgl* aParent) override;
    147  already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
    148                                            DrawTargetWebgl* aDT,
    149                                            const Rect& aSourceRect,
    150                                            const DrawOptions& aOptions,
    151                                            Point& aSurfaceOffset,
    152                                            DeviceColor& aColor) override;
    153 
    154 protected:
    155  Matrix mMatrix;
    156  SamplingFilter mSamplingFilter = SamplingFilter::GOOD;
    157 
    158  int32_t InputIndex(uint32_t aInputEnumIndex) const override;
    159 };
    160 
    161 class FilterNodeDeferInputWebgl : public FilterNodeTransformWebgl {
    162 public:
    163  FilterNodeDeferInputWebgl(RefPtr<Path> aPath, const Pattern& aPattern,
    164                            const IntRect& aSourceRect,
    165                            const Matrix& aDestTransform,
    166                            const DrawOptions& aOptions,
    167                            const StrokeOptions* aStrokeOptions);
    168 
    169  void ResolveInputs(DrawTargetWebgl* aDT, bool aAccel,
    170                     FilterNodeWebgl* aParent) override;
    171 
    172  void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
    173            const Point& aDestPoint, const DrawOptions& aOptions,
    174            FilterNodeWebgl* aParent) override;
    175  already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
    176                                            DrawTargetWebgl* aDT,
    177                                            const Rect& aSourceRect,
    178                                            const DrawOptions& aOptions,
    179                                            Point& aSurfaceOffset,
    180                                            DeviceColor& aColor) override;
    181 
    182 private:
    183  RefPtr<Path> mPath;
    184  GeneralPattern mPattern;
    185  IntRect mSourceRect;
    186  Matrix mDestTransform;
    187  DrawOptions mOptions;
    188  Maybe<StrokeOptions> mStrokeOptions;
    189  UniquePtr<Float[]> mDashPatternStorage;
    190 };
    191 
    192 class FilterNodeGaussianBlurWebgl : public FilterNodeWebgl {
    193 public:
    194  FilterNodeGaussianBlurWebgl() : FilterNodeWebgl(FilterType::GAUSSIAN_BLUR) {}
    195 
    196  void SetAttribute(uint32_t aIndex, float aValue) override;
    197  IntRect MapRectToSource(const IntRect& aRect, const IntRect& aMax,
    198                          FilterNode* aSourceNode) override;
    199 
    200  void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
    201            const Point& aDestPoint, const DrawOptions& aOptions,
    202            FilterNodeWebgl* aParent) override;
    203 
    204 private:
    205  float mStdDeviation = 0;
    206 
    207  int32_t InputIndex(uint32_t aInputEnumIndex) const override;
    208 };
    209 
    210 class FilterNodeColorMatrixWebgl : public FilterNodeWebgl {
    211 public:
    212  FilterNodeColorMatrixWebgl() : FilterNodeWebgl(FilterType::COLOR_MATRIX) {}
    213 
    214  void SetAttribute(uint32_t aIndex, const Matrix5x4& aValue) override;
    215  void SetAttribute(uint32_t aIndex, uint32_t aValue) override;
    216 
    217  bool DrawAccel(DrawTargetWebgl* aDT, const Rect& aSourceRect,
    218                 const Point& aDestPoint, const DrawOptions& aOptions,
    219                 FilterNodeWebgl* aParent) override;
    220 
    221 protected:
    222  Matrix5x4 mMatrix;
    223  AlphaMode mAlphaMode;
    224 
    225  int32_t InputIndex(uint32_t aInputEnumIndex) const override;
    226 };
    227 
    228 class FilterNodeComponentTransferWebgl : public FilterNodeWebgl {
    229 public:
    230  explicit FilterNodeComponentTransferWebgl(FilterType aType)
    231      : FilterNodeWebgl(aType),
    232        mDisableR(true),
    233        mDisableG(true),
    234        mDisableB(true),
    235        mDisableA(true) {}
    236 
    237  void SetAttribute(uint32_t aIndex, bool aValue) override;
    238 
    239  bool DrawAccel(DrawTargetWebgl* aDT, const Rect& aSourceRect,
    240                 const Point& aDestPoint, const DrawOptions& aOptions,
    241                 FilterNodeWebgl* aParent) override;
    242 
    243  virtual bool ToColorMatrix(Matrix5x4& aMatrix) const { return false; }
    244 
    245 protected:
    246  bool mDisableR : 1;
    247  bool mDisableG : 1;
    248  bool mDisableB : 1;
    249  bool mDisableA : 1;
    250 
    251  int32_t InputIndex(uint32_t aInputEnumIndex) const override;
    252 };
    253 
    254 class FilterNodeLinearTransferWebgl : public FilterNodeComponentTransferWebgl {
    255 public:
    256  FilterNodeLinearTransferWebgl()
    257      : FilterNodeComponentTransferWebgl(FilterType::LINEAR_TRANSFER) {}
    258 
    259  using FilterNodeComponentTransferWebgl::SetAttribute;
    260  void SetAttribute(uint32_t aIndex, Float aValue) override;
    261 
    262  bool ToColorMatrix(Matrix5x4& aMatrix) const override;
    263 
    264 protected:
    265  DeviceColor mSlope;
    266  DeviceColor mIntercept;
    267 };
    268 
    269 class FilterNodeTableTransferWebgl : public FilterNodeComponentTransferWebgl {
    270 public:
    271  FilterNodeTableTransferWebgl()
    272      : FilterNodeComponentTransferWebgl(FilterType::TABLE_TRANSFER) {}
    273 
    274  using FilterNodeComponentTransferWebgl::SetAttribute;
    275  void SetAttribute(uint32_t aIndex, const Float* aValues,
    276                    uint32_t aSize) override;
    277 
    278  bool ToColorMatrix(Matrix5x4& aMatrix) const override;
    279 
    280 protected:
    281  std::vector<Float> mTableR;
    282  std::vector<Float> mTableG;
    283  std::vector<Float> mTableB;
    284  std::vector<Float> mTableA;
    285 };
    286 
    287 class FilterNodePremultiplyWebgl : public FilterNodeWebgl {
    288 public:
    289  FilterNodePremultiplyWebgl() : FilterNodeWebgl(FilterType::PREMULTIPLY) {}
    290 
    291  void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
    292            const Point& aDestPoint, const DrawOptions& aOptions,
    293            FilterNodeWebgl* aParent) override;
    294 
    295 protected:
    296  int32_t InputIndex(uint32_t aInputEnumIndex) const override;
    297 };
    298 
    299 class FilterNodeUnpremultiplyWebgl : public FilterNodeWebgl {
    300 public:
    301  FilterNodeUnpremultiplyWebgl() : FilterNodeWebgl(FilterType::UNPREMULTIPLY) {}
    302 
    303  already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
    304                                            DrawTargetWebgl* aDT,
    305                                            const Rect& aSourceRect,
    306                                            const DrawOptions& aOptions,
    307                                            Point& aSurfaceOffset,
    308                                            DeviceColor& aColor) override;
    309 
    310 protected:
    311  int32_t InputIndex(uint32_t aInputEnumIndex) const override;
    312 };
    313 
    314 class FilterNodeOpacityWebgl : public FilterNodeWebgl {
    315 public:
    316  FilterNodeOpacityWebgl() : FilterNodeWebgl(FilterType::OPACITY) {}
    317 
    318  void SetAttribute(uint32_t aIndex, Float aValue) override;
    319 
    320  void Draw(DrawTargetWebgl* aDT, const Rect& aSourceRect,
    321            const Point& aDestPoint, const DrawOptions& aOptions,
    322            FilterNodeWebgl* aParent) override;
    323  already_AddRefed<SourceSurface> DrawChild(FilterNodeWebgl* aParent,
    324                                            DrawTargetWebgl* aDT,
    325                                            const Rect& aSourceRect,
    326                                            const DrawOptions& aOptions,
    327                                            Point& aSurfaceOffset,
    328                                            DeviceColor& aColor) override;
    329 
    330 protected:
    331  Float mValue = 1.0f;
    332 
    333  int32_t InputIndex(uint32_t aInputEnumIndex) const override;
    334 };
    335 
    336 }  // namespace mozilla::gfx
    337 
    338 #endif /* MOZILLA_GFX_FILTERNODEWEBGL_H_ */