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/. */
5 
6 #ifndef GFX_PATTERN_H
7 #define GFX_PATTERN_H
8 
9 #include "gfxTypes.h"
10 
11 #include "gfxMatrix.h"
12 #include "mozilla/Alignment.h"
13 #include "mozilla/gfx/2D.h"
14 #include "mozilla/gfx/PatternHelpers.h"
15 #include "nsISupportsImpl.h"
16 #include "nsTArray.h"
17 
18 typedef struct _cairo_pattern cairo_pattern_t;
19 
20 class gfxPattern final {
21   NS_INLINE_DECL_REFCOUNTING(gfxPattern)
22 
23  public:
24   explicit gfxPattern(const mozilla::gfx::Color &aColor);
25   // linear
26   gfxPattern(gfxFloat x0, gfxFloat y0, gfxFloat x1, gfxFloat y1);  // linear
27   gfxPattern(gfxFloat cx0, gfxFloat cy0, gfxFloat radius0, gfxFloat cx1,
28              gfxFloat cy1, gfxFloat radius1);  // radial
29   gfxPattern(mozilla::gfx::SourceSurface *aSurface,
30              const mozilla::gfx::Matrix &aPatternToUserSpace);
31 
32   void AddColorStop(gfxFloat offset, const mozilla::gfx::Color &c);
33   void SetColorStops(mozilla::gfx::GradientStops *aStops);
34 
35   // This should only be called on a cairo pattern that we want to use with
36   // Azure. We will read back the color stops from cairo and try to look
37   // them up in the cache.
38   void CacheColorStops(const mozilla::gfx::DrawTarget *aDT);
39 
40   void SetMatrix(const gfxMatrix &matrix);
41   gfxMatrix GetMatrix() const;
42   gfxMatrix GetInverseMatrix() const;
43 
44   /* Get an Azure Pattern for the current Cairo pattern. aPattern transform
45    * specifies the transform that was set on the DrawTarget when the pattern
46    * was set. When this is nullptr it is assumed the transform is identical
47    * to the current transform.
48    */
49   mozilla::gfx::Pattern *GetPattern(
50       const mozilla::gfx::DrawTarget *aTarget,
51       const mozilla::gfx::Matrix *aOriginalUserToDevice = nullptr);
52   bool IsOpaque();
53 
54   // clamp, repeat, reflect
55   void SetExtend(mozilla::gfx::ExtendMode aExtend);
56 
57   void SetSamplingFilter(mozilla::gfx::SamplingFilter aSamplingFilter);
58   mozilla::gfx::SamplingFilter SamplingFilter() const;
59 
60   /* returns TRUE if it succeeded */
61   bool GetSolidColor(mozilla::gfx::Color &aColorOut);
62 
63  private:
64   // Private destructor, to discourage deletion outside of Release():
~gfxPattern()65   ~gfxPattern() {}
66 
67   mozilla::gfx::GeneralPattern mGfxPattern;
68   RefPtr<mozilla::gfx::SourceSurface> mSourceSurface;
69   mozilla::gfx::Matrix mPatternToUserSpace;
70   RefPtr<mozilla::gfx::GradientStops> mStops;
71   nsTArray<mozilla::gfx::GradientStop> mStopsList;
72   mozilla::gfx::ExtendMode mExtend;
73 };
74 
75 #endif /* GFX_PATTERN_H */
76