Bug 1686668 [wpt PR 27185] - Update wpt metadata, a=testonly
[gecko.git] / gfx / 2d / DrawTargetCairo.h
blob36c56c83a81b6171e10b1470c7774864a71606f9
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_DRAWTARGET_CAIRO_H_
8 #define _MOZILLA_GFX_DRAWTARGET_CAIRO_H_
10 #include "2D.h"
11 #include "cairo.h"
12 #include "PathCairo.h"
14 #include <vector>
16 namespace mozilla {
17 namespace gfx {
19 class SourceSurfaceCairo;
21 class GradientStopsCairo : public GradientStops {
22 public:
23 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GradientStopsCairo, override)
25 GradientStopsCairo(GradientStop* aStops, uint32_t aNumStops,
26 ExtendMode aExtendMode)
27 : mExtendMode(aExtendMode) {
28 for (uint32_t i = 0; i < aNumStops; ++i) {
29 mStops.push_back(aStops[i]);
33 virtual ~GradientStopsCairo() = default;
35 const std::vector<GradientStop>& GetStops() const { return mStops; }
37 ExtendMode GetExtendMode() const { return mExtendMode; }
39 virtual BackendType GetBackendType() const override {
40 return BackendType::CAIRO;
43 private:
44 std::vector<GradientStop> mStops;
45 ExtendMode mExtendMode;
48 class DrawTargetCairo final : public DrawTarget {
49 public:
50 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetCairo, override)
51 friend class BorrowedCairoContext;
52 friend class BorrowedXlibDrawable;
54 DrawTargetCairo();
55 virtual ~DrawTargetCairo();
57 virtual bool IsValid() const override;
58 virtual DrawTargetType GetType() const override;
59 virtual BackendType GetBackendType() const override {
60 return BackendType::CAIRO;
62 virtual already_AddRefed<SourceSurface> Snapshot() override;
63 virtual IntSize GetSize() const override;
65 virtual bool IsCurrentGroupOpaque() override;
67 virtual void SetPermitSubpixelAA(bool aPermitSubpixelAA) override;
69 virtual bool LockBits(uint8_t** aData, IntSize* aSize, int32_t* aStride,
70 SurfaceFormat* aFormat,
71 IntPoint* aOrigin = nullptr) override;
72 virtual void ReleaseBits(uint8_t* aData) override;
74 virtual void Flush() override;
75 virtual void DrawSurface(
76 SourceSurface* aSurface, const Rect& aDest, const Rect& aSource,
77 const DrawSurfaceOptions& aSurfOptions = DrawSurfaceOptions(),
78 const DrawOptions& aOptions = DrawOptions()) override;
79 virtual void DrawFilter(FilterNode* aNode, const Rect& aSourceRect,
80 const Point& aDestPoint,
81 const DrawOptions& aOptions = DrawOptions()) override;
82 virtual void DrawSurfaceWithShadow(SourceSurface* aSurface,
83 const Point& aDest,
84 const DeviceColor& aColor,
85 const Point& aOffset, Float aSigma,
86 CompositionOp aOperator) override;
88 virtual void ClearRect(const Rect& aRect) override;
90 virtual void CopySurface(SourceSurface* aSurface, const IntRect& aSourceRect,
91 const IntPoint& aDestination) override;
92 virtual void CopyRect(const IntRect& aSourceRect,
93 const IntPoint& aDestination) override;
95 virtual void FillRect(const Rect& aRect, const Pattern& aPattern,
96 const DrawOptions& aOptions = DrawOptions()) override;
97 virtual void StrokeRect(const Rect& aRect, const Pattern& aPattern,
98 const StrokeOptions& aStrokeOptions = StrokeOptions(),
99 const DrawOptions& aOptions = DrawOptions()) override;
100 virtual void StrokeLine(const Point& aStart, const Point& aEnd,
101 const Pattern& aPattern,
102 const StrokeOptions& aStrokeOptions = StrokeOptions(),
103 const DrawOptions& aOptions = DrawOptions()) override;
105 virtual void Stroke(const Path* aPath, const Pattern& aPattern,
106 const StrokeOptions& aStrokeOptions = StrokeOptions(),
107 const DrawOptions& aOptions = DrawOptions()) override;
109 virtual void Fill(const Path* aPath, const Pattern& aPattern,
110 const DrawOptions& aOptions = DrawOptions()) override;
112 virtual void FillGlyphs(ScaledFont* aFont, const GlyphBuffer& aBuffer,
113 const Pattern& aPattern,
114 const DrawOptions& aOptions) override;
115 virtual void Mask(const Pattern& aSource, const Pattern& aMask,
116 const DrawOptions& aOptions = DrawOptions()) override;
117 virtual void MaskSurface(
118 const Pattern& aSource, SourceSurface* aMask, Point aOffset,
119 const DrawOptions& aOptions = DrawOptions()) override;
121 virtual bool Draw3DTransformedSurface(SourceSurface* aSurface,
122 const Matrix4x4& aMatrix) override;
124 virtual void PushClip(const Path* aPath) override;
125 virtual void PushClipRect(const Rect& aRect) override;
126 virtual void PopClip() override;
127 virtual void PushLayer(bool aOpaque, Float aOpacity, SourceSurface* aMask,
128 const Matrix& aMaskTransform,
129 const IntRect& aBounds = IntRect(),
130 bool aCopyBackground = false) override;
131 virtual void PopLayer() override;
133 virtual already_AddRefed<PathBuilder> CreatePathBuilder(
134 FillRule aFillRule = FillRule::FILL_WINDING) const override;
136 virtual already_AddRefed<SourceSurface> CreateSourceSurfaceFromData(
137 unsigned char* aData, const IntSize& aSize, int32_t aStride,
138 SurfaceFormat aFormat) const override;
139 virtual already_AddRefed<SourceSurface> OptimizeSourceSurface(
140 SourceSurface* aSurface) const override;
141 virtual already_AddRefed<SourceSurface> CreateSourceSurfaceFromNativeSurface(
142 const NativeSurface& aSurface) const override;
143 virtual already_AddRefed<DrawTarget> CreateSimilarDrawTarget(
144 const IntSize& aSize, SurfaceFormat aFormat) const override;
145 virtual already_AddRefed<DrawTarget> CreateShadowDrawTarget(
146 const IntSize& aSize, SurfaceFormat aFormat, float aSigma) const override;
147 virtual RefPtr<DrawTarget> CreateClippedDrawTarget(
148 const Rect& aBounds, SurfaceFormat aFormat) override;
150 virtual already_AddRefed<GradientStops> CreateGradientStops(
151 GradientStop* aStops, uint32_t aNumStops,
152 ExtendMode aExtendMode = ExtendMode::CLAMP) const override;
154 virtual already_AddRefed<FilterNode> CreateFilter(FilterType aType) override;
156 virtual void* GetNativeSurface(NativeSurfaceType aType) override;
158 bool Init(cairo_surface_t* aSurface, const IntSize& aSize,
159 SurfaceFormat* aFormat = nullptr);
160 bool Init(const IntSize& aSize, SurfaceFormat aFormat);
161 bool Init(unsigned char* aData, const IntSize& aSize, int32_t aStride,
162 SurfaceFormat aFormat);
164 virtual void SetTransform(const Matrix& aTransform) override;
166 virtual void DetachAllSnapshots() override { MarkSnapshotIndependent(); }
168 // Call to set up aContext for drawing (with the current transform, etc).
169 // Pass the path you're going to be using if you have one.
170 // Implicitly calls WillChange(aPath).
171 void PrepareForDrawing(cairo_t* aContext, const Path* aPath = nullptr);
173 static cairo_surface_t* GetDummySurface();
175 // Cairo hardcodes this as its maximum surface size.
176 static size_t GetMaxSurfaceSize() { return 32766; }
178 private: // methods
179 // Init cairo surface without doing a cairo_surface_reference() call.
180 bool InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize,
181 SurfaceFormat* aFormat = nullptr);
182 enum DrawPatternType { DRAW_FILL, DRAW_STROKE };
183 void DrawPattern(const Pattern& aPattern, const StrokeOptions& aStrokeOptions,
184 const DrawOptions& aOptions, DrawPatternType aDrawType,
185 bool aPathBoundsClip = false);
187 void CopySurfaceInternal(cairo_surface_t* aSurface, const IntRect& aSource,
188 const IntPoint& aDest);
190 Rect GetUserSpaceClip() const;
192 // Call before you make any changes to the backing surface with which this
193 // context is associated. Pass the path you're going to be using if you have
194 // one.
195 void WillChange(const Path* aPath = nullptr);
197 // Call if there is any reason to disassociate the snapshot from this draw
198 // target; for example, because we're going to be destroyed.
199 void MarkSnapshotIndependent();
201 // If the current operator is "source" then clear the destination before we
202 // draw into it, to simulate the effect of an unbounded source operator.
203 void ClearSurfaceForUnboundedSource(const CompositionOp& aOperator);
205 // Set the Cairo context font options according to the current draw target
206 // font state.
207 void SetFontOptions(cairo_antialias_t aAAMode = CAIRO_ANTIALIAS_DEFAULT);
209 private: // data
210 cairo_t* mContext;
211 cairo_surface_t* mSurface;
212 IntSize mSize;
213 bool mTransformSingular;
215 uint8_t* mLockedBits;
217 cairo_font_options_t* mFontOptions;
219 struct PushedLayer {
220 PushedLayer(Float aOpacity, bool aWasPermittingSubpixelAA)
221 : mOpacity(aOpacity),
222 mMaskPattern(nullptr),
223 mWasPermittingSubpixelAA(aWasPermittingSubpixelAA) {}
224 Float mOpacity;
225 cairo_pattern_t* mMaskPattern;
226 bool mWasPermittingSubpixelAA;
228 std::vector<PushedLayer> mPushedLayers;
230 // The latest snapshot of this surface. This needs to be told when this
231 // target is modified. We keep it alive as a cache.
232 RefPtr<SourceSurfaceCairo> mSnapshot;
233 static cairo_surface_t* mDummySurface;
236 } // namespace gfx
237 } // namespace mozilla
239 #endif // _MOZILLA_GFX_DRAWTARGET_CAIRO_H_