Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/gfx/PatternHelpers.h
Line
Count
Source (jump to first uncovered line)
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
29
{
30
public:
31
  explicit GeneralPattern()
32
    : mPattern(nullptr)
33
0
  {}
34
35
  GeneralPattern(const GeneralPattern& aOther)
36
    : mPattern(nullptr)
37
0
  {}
38
39
0
  ~GeneralPattern() {
40
0
    if (mPattern) {
41
0
      mPattern->~Pattern();
42
0
    }
43
0
  }
44
45
0
  Pattern* Init(const Pattern& aPattern) {
46
0
    MOZ_ASSERT(!mPattern);
47
0
    switch (aPattern.GetType()) {
48
0
    case PatternType::COLOR:
49
0
      mPattern = new (mColorPattern.addr())
50
0
        ColorPattern(static_cast<const ColorPattern&>(aPattern));
51
0
      break;
52
0
    case PatternType::LINEAR_GRADIENT:
53
0
      mPattern = new (mLinearGradientPattern.addr())
54
0
        LinearGradientPattern(static_cast<const LinearGradientPattern&>(aPattern));
55
0
      break;
56
0
    case PatternType::RADIAL_GRADIENT:
57
0
      mPattern = new (mRadialGradientPattern.addr())
58
0
        RadialGradientPattern(static_cast<const RadialGradientPattern&>(aPattern));
59
0
      break;
60
0
    case PatternType::SURFACE:
61
0
      mPattern = new (mSurfacePattern.addr())
62
0
        SurfacePattern(static_cast<const SurfacePattern&>(aPattern));
63
0
      break;
64
0
    default:
65
0
      MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Unknown pattern type");
66
0
    }
67
0
    return mPattern;
68
0
  }
69
70
0
  ColorPattern* InitColorPattern(const Color &aColor) {
71
0
    MOZ_ASSERT(!mPattern);
72
0
    mPattern = new (mColorPattern.addr()) ColorPattern(aColor);
73
0
    return mColorPattern.addr();
74
0
  }
75
76
  LinearGradientPattern* InitLinearGradientPattern(const Point &aBegin,
77
                                                   const Point &aEnd,
78
                                                   GradientStops *aStops,
79
0
                                                   const Matrix &aMatrix = Matrix()) {
80
0
    MOZ_ASSERT(!mPattern);
81
0
    mPattern = new (mLinearGradientPattern.addr())
82
0
      LinearGradientPattern(aBegin, aEnd, aStops, aMatrix);
83
0
    return mLinearGradientPattern.addr();
84
0
  }
85
86
  RadialGradientPattern* InitRadialGradientPattern(const Point &aCenter1,
87
                                                   const Point &aCenter2,
88
                                                   Float aRadius1,
89
                                                   Float aRadius2,
90
                                                   GradientStops *aStops,
91
0
                                                   const Matrix &aMatrix = Matrix()) {
92
0
    MOZ_ASSERT(!mPattern);
93
0
    mPattern = new (mRadialGradientPattern.addr())
94
0
      RadialGradientPattern(aCenter1, aCenter2, aRadius1, aRadius2, aStops, aMatrix);
95
0
    return mRadialGradientPattern.addr();
96
0
  }
97
98
  SurfacePattern* InitSurfacePattern(SourceSurface *aSourceSurface,
99
                                     ExtendMode aExtendMode,
100
                                     const Matrix &aMatrix = Matrix(),
101
                                     SamplingFilter aSamplingFilter = SamplingFilter::GOOD,
102
0
                                     const IntRect &aSamplingRect = IntRect()) {
103
0
    MOZ_ASSERT(!mPattern);
104
0
    mPattern = new (mSurfacePattern.addr())
105
0
      SurfacePattern(aSourceSurface, aExtendMode, aMatrix, aSamplingFilter, aSamplingRect);
106
0
    return mSurfacePattern.addr();
107
0
  }
108
109
0
  Pattern* GetPattern() {
110
0
    return mPattern;
111
0
  }
112
113
0
  const Pattern* GetPattern() const {
114
0
    return mPattern;
115
0
  }
116
117
0
  operator Pattern&() {
118
0
    if (!mPattern) {
119
0
      MOZ_CRASH("GFX: GeneralPattern not initialized");
120
0
    }
121
0
    return *mPattern;
122
0
  }
123
124
private:
125
  union {
126
    AlignedStorage2<ColorPattern> mColorPattern;
127
    AlignedStorage2<LinearGradientPattern> mLinearGradientPattern;
128
    AlignedStorage2<RadialGradientPattern> mRadialGradientPattern;
129
    AlignedStorage2<SurfacePattern> mSurfacePattern;
130
  };
131
  Pattern *mPattern;
132
};
133
134
} // namespace gfx
135
} // namespace mozilla
136
137
#endif //  _MOZILLA_GFX_PATTERNHELPERS_H
138