tor-browser

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

PatternHelpers.h (5173B)


      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_PATTERNHELPERS_H
      8 #define _MOZILLA_GFX_PATTERNHELPERS_H
      9 
     10 #include "mozilla/Alignment.h"
     11 #include "mozilla/gfx/2D.h"
     12 
     13 namespace mozilla {
     14 namespace gfx {
     15 
     16 /**
     17 * This class is used to allow general pattern creation functions to return
     18 * any type of pattern via an out-paramater without allocating a pattern
     19 * instance on the free-store (an instance of this class being created on the
     20 * stack before passing it in to the creation function). Without this class
     21 * writing pattern creation functions would be a pain since Pattern objects are
     22 * not reference counted, making lifetime management of instances created on
     23 * the free-store and returned from a creation function hazardous. Besides
     24 * that, in the case that ColorPattern's are expected to be common, it is
     25 * particularly desirable to avoid the overhead of allocating on the
     26 * free-store.
     27 */
     28 class GeneralPattern final {
     29 public:
     30  explicit GeneralPattern() = default;
     31 
     32  GeneralPattern(const GeneralPattern& aOther) {}
     33 
     34  ~GeneralPattern() {
     35    if (mPattern) {
     36      mPattern->~Pattern();
     37    }
     38  }
     39 
     40  Pattern* Init(const Pattern& aPattern) {
     41    MOZ_ASSERT(!mPattern);
     42    switch (aPattern.GetType()) {
     43      case PatternType::COLOR:
     44        mPattern = new (mColorPattern.addr())
     45            ColorPattern(static_cast<const ColorPattern&>(aPattern));
     46        break;
     47      case PatternType::LINEAR_GRADIENT:
     48        mPattern = new (mLinearGradientPattern.addr()) LinearGradientPattern(
     49            static_cast<const LinearGradientPattern&>(aPattern));
     50        break;
     51      case PatternType::RADIAL_GRADIENT:
     52        mPattern = new (mRadialGradientPattern.addr()) RadialGradientPattern(
     53            static_cast<const RadialGradientPattern&>(aPattern));
     54        break;
     55      case PatternType::CONIC_GRADIENT:
     56        mPattern = new (mConicGradientPattern.addr()) ConicGradientPattern(
     57            static_cast<const ConicGradientPattern&>(aPattern));
     58        break;
     59      case PatternType::SURFACE:
     60        mPattern = new (mSurfacePattern.addr())
     61            SurfacePattern(static_cast<const SurfacePattern&>(aPattern));
     62        break;
     63      default:
     64        MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Unknown pattern type");
     65    }
     66    return mPattern;
     67  }
     68 
     69  ColorPattern* InitColorPattern(const DeviceColor& aColor) {
     70    MOZ_ASSERT(!mPattern);
     71    mPattern = new (mColorPattern.addr()) ColorPattern(aColor);
     72    return mColorPattern.addr();
     73  }
     74 
     75  LinearGradientPattern* InitLinearGradientPattern(
     76      const Point& aBegin, const Point& aEnd,
     77      already_AddRefed<GradientStops> aStops,
     78      const Matrix& aMatrix = Matrix()) {
     79    MOZ_ASSERT(!mPattern);
     80    mPattern = new (mLinearGradientPattern.addr())
     81        LinearGradientPattern(aBegin, aEnd, std::move(aStops), aMatrix);
     82    return mLinearGradientPattern.addr();
     83  }
     84 
     85  RadialGradientPattern* InitRadialGradientPattern(
     86      const Point& aCenter1, const Point& aCenter2, Float aRadius1,
     87      Float aRadius2, already_AddRefed<GradientStops> aStops,
     88      const Matrix& aMatrix = Matrix()) {
     89    MOZ_ASSERT(!mPattern);
     90    mPattern = new (mRadialGradientPattern.addr()) RadialGradientPattern(
     91        aCenter1, aCenter2, aRadius1, aRadius2, std::move(aStops), aMatrix);
     92    return mRadialGradientPattern.addr();
     93  }
     94 
     95  ConicGradientPattern* InitConicGradientPattern(
     96      const Point& aCenter, Float aAngle, Float aStartOffset, Float aEndOffset,
     97      already_AddRefed<GradientStops> aStops,
     98      const Matrix& aMatrix = Matrix()) {
     99    MOZ_ASSERT(!mPattern);
    100    mPattern = new (mConicGradientPattern.addr()) ConicGradientPattern(
    101        aCenter, aAngle, aStartOffset, aEndOffset, std::move(aStops), aMatrix);
    102    return mConicGradientPattern.addr();
    103  }
    104 
    105  SurfacePattern* InitSurfacePattern(
    106      SourceSurface* aSourceSurface, ExtendMode aExtendMode,
    107      const Matrix& aMatrix = Matrix(),
    108      SamplingFilter aSamplingFilter = SamplingFilter::GOOD,
    109      const IntRect& aSamplingRect = IntRect()) {
    110    MOZ_ASSERT(!mPattern);
    111    mPattern = new (mSurfacePattern.addr()) SurfacePattern(
    112        aSourceSurface, aExtendMode, aMatrix, aSamplingFilter, aSamplingRect);
    113    return mSurfacePattern.addr();
    114  }
    115 
    116  Pattern* GetPattern() { return mPattern; }
    117 
    118  const Pattern* GetPattern() const { return mPattern; }
    119 
    120  operator Pattern&() {
    121    if (!mPattern) {
    122      MOZ_CRASH("GFX: GeneralPattern not initialized");
    123    }
    124    return *mPattern;
    125  }
    126 
    127 private:
    128  union {
    129    AlignedStorage2<ColorPattern> mColorPattern;
    130    AlignedStorage2<LinearGradientPattern> mLinearGradientPattern;
    131    AlignedStorage2<RadialGradientPattern> mRadialGradientPattern;
    132    AlignedStorage2<ConicGradientPattern> mConicGradientPattern;
    133    AlignedStorage2<SurfacePattern> mSurfacePattern;
    134  };
    135  Pattern* mPattern = nullptr;
    136 };
    137 
    138 }  // namespace gfx
    139 }  // namespace mozilla
    140 
    141 #endif  //  _MOZILLA_GFX_PATTERNHELPERS_H