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_
12 #include "PathCairo.h"
19 class SourceSurfaceCairo
;
21 class GradientStopsCairo
: public GradientStops
{
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
;
44 std::vector
<GradientStop
> mStops
;
45 ExtendMode mExtendMode
;
48 class DrawTargetCairo final
: public DrawTarget
{
50 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetCairo
, override
)
51 friend class BorrowedXlibDrawable
;
54 virtual ~DrawTargetCairo();
56 virtual bool IsValid() const override
;
57 virtual DrawTargetType
GetType() const override
;
58 virtual BackendType
GetBackendType() const override
{
59 return BackendType::CAIRO
;
62 virtual void Link(const char* aDestination
, const Rect
& aRect
) override
;
63 virtual void Destination(const char* aDestination
,
64 const Point
& aPoint
) override
;
66 virtual already_AddRefed
<SourceSurface
> Snapshot() override
;
67 virtual IntSize
GetSize() const override
;
69 virtual bool IsCurrentGroupOpaque() override
;
71 virtual void SetPermitSubpixelAA(bool aPermitSubpixelAA
) override
;
73 virtual bool LockBits(uint8_t** aData
, IntSize
* aSize
, int32_t* aStride
,
74 SurfaceFormat
* aFormat
,
75 IntPoint
* aOrigin
= nullptr) override
;
76 virtual void ReleaseBits(uint8_t* aData
) override
;
78 virtual void Flush() override
;
79 virtual void DrawSurface(
80 SourceSurface
* aSurface
, const Rect
& aDest
, const Rect
& aSource
,
81 const DrawSurfaceOptions
& aSurfOptions
= DrawSurfaceOptions(),
82 const DrawOptions
& aOptions
= DrawOptions()) override
;
83 virtual void DrawFilter(FilterNode
* aNode
, const Rect
& aSourceRect
,
84 const Point
& aDestPoint
,
85 const DrawOptions
& aOptions
= DrawOptions()) override
;
86 virtual void DrawSurfaceWithShadow(SourceSurface
* aSurface
,
88 const DeviceColor
& aColor
,
89 const Point
& aOffset
, Float aSigma
,
90 CompositionOp aOperator
) override
;
92 virtual void ClearRect(const Rect
& aRect
) override
;
94 virtual void CopySurface(SourceSurface
* aSurface
, const IntRect
& aSourceRect
,
95 const IntPoint
& aDestination
) override
;
96 virtual void CopyRect(const IntRect
& aSourceRect
,
97 const IntPoint
& aDestination
) override
;
99 virtual void FillRect(const Rect
& aRect
, const Pattern
& aPattern
,
100 const DrawOptions
& aOptions
= DrawOptions()) override
;
101 virtual void StrokeRect(const Rect
& aRect
, const Pattern
& aPattern
,
102 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
103 const DrawOptions
& aOptions
= DrawOptions()) override
;
104 virtual void StrokeLine(const Point
& aStart
, const Point
& aEnd
,
105 const Pattern
& aPattern
,
106 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
107 const DrawOptions
& aOptions
= DrawOptions()) override
;
109 virtual void Stroke(const Path
* aPath
, const Pattern
& aPattern
,
110 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
111 const DrawOptions
& aOptions
= DrawOptions()) override
;
113 virtual void Fill(const Path
* aPath
, const Pattern
& aPattern
,
114 const DrawOptions
& aOptions
= DrawOptions()) override
;
116 virtual void FillGlyphs(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
117 const Pattern
& aPattern
,
118 const DrawOptions
& aOptions
) override
;
119 virtual void Mask(const Pattern
& aSource
, const Pattern
& aMask
,
120 const DrawOptions
& aOptions
= DrawOptions()) override
;
121 virtual void MaskSurface(
122 const Pattern
& aSource
, SourceSurface
* aMask
, Point aOffset
,
123 const DrawOptions
& aOptions
= DrawOptions()) override
;
125 virtual bool Draw3DTransformedSurface(SourceSurface
* aSurface
,
126 const Matrix4x4
& aMatrix
) override
;
128 virtual void PushClip(const Path
* aPath
) override
;
129 virtual void PushClipRect(const Rect
& aRect
) override
;
130 virtual void PopClip() override
;
131 virtual void PushLayer(bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
132 const Matrix
& aMaskTransform
,
133 const IntRect
& aBounds
= IntRect(),
134 bool aCopyBackground
= false) override
;
135 virtual void PushLayerWithBlend(
136 bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
137 const Matrix
& aMaskTransform
, const IntRect
& aBounds
= IntRect(),
138 bool aCopyBackground
= false,
139 CompositionOp
= CompositionOp::OP_OVER
) override
;
140 virtual void PopLayer() override
;
142 virtual already_AddRefed
<PathBuilder
> CreatePathBuilder(
143 FillRule aFillRule
= FillRule::FILL_WINDING
) const override
;
145 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromData(
146 unsigned char* aData
, const IntSize
& aSize
, int32_t aStride
,
147 SurfaceFormat aFormat
) const override
;
148 virtual already_AddRefed
<SourceSurface
> OptimizeSourceSurface(
149 SourceSurface
* aSurface
) const override
;
150 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromNativeSurface(
151 const NativeSurface
& aSurface
) const override
;
152 virtual already_AddRefed
<DrawTarget
> CreateSimilarDrawTarget(
153 const IntSize
& aSize
, SurfaceFormat aFormat
) const override
;
154 virtual already_AddRefed
<DrawTarget
> CreateShadowDrawTarget(
155 const IntSize
& aSize
, SurfaceFormat aFormat
, float aSigma
) const override
;
156 virtual RefPtr
<DrawTarget
> CreateClippedDrawTarget(
157 const Rect
& aBounds
, SurfaceFormat aFormat
) override
;
159 virtual already_AddRefed
<GradientStops
> CreateGradientStops(
160 GradientStop
* aStops
, uint32_t aNumStops
,
161 ExtendMode aExtendMode
= ExtendMode::CLAMP
) const override
;
163 virtual already_AddRefed
<FilterNode
> CreateFilter(FilterType aType
) override
;
165 virtual void* GetNativeSurface(NativeSurfaceType aType
) override
;
167 bool Init(cairo_surface_t
* aSurface
, const IntSize
& aSize
,
168 SurfaceFormat
* aFormat
= nullptr);
169 bool Init(const IntSize
& aSize
, SurfaceFormat aFormat
);
170 bool Init(unsigned char* aData
, const IntSize
& aSize
, int32_t aStride
,
171 SurfaceFormat aFormat
);
173 virtual void SetTransform(const Matrix
& aTransform
) override
;
175 virtual void DetachAllSnapshots() override
{ MarkSnapshotIndependent(); }
177 // Call to set up aContext for drawing (with the current transform, etc).
178 // Pass the path you're going to be using if you have one.
179 // Implicitly calls WillChange(aPath).
180 void PrepareForDrawing(cairo_t
* aContext
, const Path
* aPath
= nullptr);
182 static cairo_surface_t
* GetDummySurface();
184 // Cairo hardcodes this as its maximum surface size.
185 static size_t GetMaxSurfaceSize() { return 32766; }
188 // Init cairo surface without doing a cairo_surface_reference() call.
189 bool InitAlreadyReferenced(cairo_surface_t
* aSurface
, const IntSize
& aSize
,
190 SurfaceFormat
* aFormat
= nullptr);
191 enum DrawPatternType
{ DRAW_FILL
, DRAW_STROKE
};
192 void DrawPattern(const Pattern
& aPattern
, const StrokeOptions
& aStrokeOptions
,
193 const DrawOptions
& aOptions
, DrawPatternType aDrawType
,
194 bool aPathBoundsClip
= false);
196 void CopySurfaceInternal(cairo_surface_t
* aSurface
, const IntRect
& aSource
,
197 const IntPoint
& aDest
);
199 Rect
GetUserSpaceClip() const;
201 // Call before you make any changes to the backing surface with which this
202 // context is associated. Pass the path you're going to be using if you have
204 void WillChange(const Path
* aPath
= nullptr);
206 // Call if there is any reason to disassociate the snapshot from this draw
207 // target; for example, because we're going to be destroyed.
208 void MarkSnapshotIndependent();
210 // If the current operator is "source" then clear the destination before we
211 // draw into it, to simulate the effect of an unbounded source operator.
212 void ClearSurfaceForUnboundedSource(const CompositionOp
& aOperator
);
214 // Set the Cairo context font options according to the current draw target
216 void SetFontOptions(cairo_antialias_t aAAMode
= CAIRO_ANTIALIAS_DEFAULT
);
220 cairo_surface_t
* mSurface
;
222 bool mTransformSingular
;
224 uint8_t* mLockedBits
;
226 cairo_font_options_t
* mFontOptions
;
229 PushedLayer(Float aOpacity
, CompositionOp aCompositionOp
,
230 bool aWasPermittingSubpixelAA
)
231 : mOpacity(aOpacity
),
232 mCompositionOp(aCompositionOp
),
233 mMaskPattern(nullptr),
234 mWasPermittingSubpixelAA(aWasPermittingSubpixelAA
) {}
236 CompositionOp mCompositionOp
;
237 cairo_pattern_t
* mMaskPattern
;
238 bool mWasPermittingSubpixelAA
;
240 std::vector
<PushedLayer
> mPushedLayers
;
242 // The latest snapshot of this surface. This needs to be told when this
243 // target is modified. We keep it alive as a cache.
244 RefPtr
<SourceSurfaceCairo
> mSnapshot
;
245 static cairo_surface_t
* mDummySurface
;
249 } // namespace mozilla
251 #endif // _MOZILLA_GFX_DRAWTARGET_CAIRO_H_