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/. */
7 #ifndef _MOZILLA_GFX_PATTERNHELPERS_H
8 #define _MOZILLA_GFX_PATTERNHELPERS_H
10 #include "mozilla/Alignment.h"
11 #include "mozilla/gfx/2D.h"
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
28 class GeneralPattern final
{
30 explicit GeneralPattern() = default;
32 GeneralPattern(const GeneralPattern
& aOther
) {}
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
));
47 case PatternType::LINEAR_GRADIENT
:
48 mPattern
= new (mLinearGradientPattern
.addr()) LinearGradientPattern(
49 static_cast<const LinearGradientPattern
&>(aPattern
));
51 case PatternType::RADIAL_GRADIENT
:
52 mPattern
= new (mRadialGradientPattern
.addr()) RadialGradientPattern(
53 static_cast<const RadialGradientPattern
&>(aPattern
));
55 case PatternType::CONIC_GRADIENT
:
56 mPattern
= new (mConicGradientPattern
.addr()) ConicGradientPattern(
57 static_cast<const ConicGradientPattern
&>(aPattern
));
59 case PatternType::SURFACE
:
60 mPattern
= new (mSurfacePattern
.addr())
61 SurfacePattern(static_cast<const SurfacePattern
&>(aPattern
));
64 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Unknown pattern type");
69 ColorPattern
* InitColorPattern(const DeviceColor
& aColor
) {
70 MOZ_ASSERT(!mPattern
);
71 mPattern
= new (mColorPattern
.addr()) ColorPattern(aColor
);
72 return mColorPattern
.addr();
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();
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();
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();
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();
116 Pattern
* GetPattern() { return mPattern
; }
118 const Pattern
* GetPattern() const { return mPattern
; }
120 operator Pattern
&() {
122 MOZ_CRASH("GFX: GeneralPattern not initialized");
129 AlignedStorage2
<ColorPattern
> mColorPattern
;
130 AlignedStorage2
<LinearGradientPattern
> mLinearGradientPattern
;
131 AlignedStorage2
<RadialGradientPattern
> mRadialGradientPattern
;
132 AlignedStorage2
<ConicGradientPattern
> mConicGradientPattern
;
133 AlignedStorage2
<SurfacePattern
> mSurfacePattern
;
135 Pattern
* mPattern
= nullptr;
139 } // namespace mozilla
141 #endif // _MOZILLA_GFX_PATTERNHELPERS_H