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 ShadowOptions
& aShadow
,
89 CompositionOp aOperator
) override
;
91 virtual void ClearRect(const Rect
& aRect
) override
;
93 virtual void CopySurface(SourceSurface
* aSurface
, const IntRect
& aSourceRect
,
94 const IntPoint
& aDestination
) override
;
95 virtual void CopyRect(const IntRect
& aSourceRect
,
96 const IntPoint
& aDestination
) override
;
98 virtual void FillRect(const Rect
& aRect
, const Pattern
& aPattern
,
99 const DrawOptions
& aOptions
= DrawOptions()) override
;
100 virtual void StrokeRect(const Rect
& aRect
, const Pattern
& aPattern
,
101 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
102 const DrawOptions
& aOptions
= DrawOptions()) override
;
103 virtual void StrokeLine(const Point
& aStart
, const Point
& aEnd
,
104 const Pattern
& aPattern
,
105 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
106 const DrawOptions
& aOptions
= DrawOptions()) override
;
108 virtual void Stroke(const Path
* aPath
, const Pattern
& aPattern
,
109 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
110 const DrawOptions
& aOptions
= DrawOptions()) override
;
112 virtual void Fill(const Path
* aPath
, const Pattern
& aPattern
,
113 const DrawOptions
& aOptions
= DrawOptions()) override
;
115 virtual void FillGlyphs(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
116 const Pattern
& aPattern
,
117 const DrawOptions
& aOptions
) override
;
118 virtual void Mask(const Pattern
& aSource
, const Pattern
& aMask
,
119 const DrawOptions
& aOptions
= DrawOptions()) override
;
120 virtual void MaskSurface(
121 const Pattern
& aSource
, SourceSurface
* aMask
, Point aOffset
,
122 const DrawOptions
& aOptions
= DrawOptions()) override
;
124 virtual bool Draw3DTransformedSurface(SourceSurface
* aSurface
,
125 const Matrix4x4
& aMatrix
) override
;
127 virtual void PushClip(const Path
* aPath
) override
;
128 virtual void PushClipRect(const Rect
& aRect
) override
;
129 virtual void PopClip() override
;
130 virtual void PushLayer(bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
131 const Matrix
& aMaskTransform
,
132 const IntRect
& aBounds
= IntRect(),
133 bool aCopyBackground
= false) override
;
134 virtual void PushLayerWithBlend(
135 bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
136 const Matrix
& aMaskTransform
, const IntRect
& aBounds
= IntRect(),
137 bool aCopyBackground
= false,
138 CompositionOp
= CompositionOp::OP_OVER
) override
;
139 virtual void PopLayer() override
;
141 virtual already_AddRefed
<PathBuilder
> CreatePathBuilder(
142 FillRule aFillRule
= FillRule::FILL_WINDING
) const override
{
143 return PathBuilderCairo::Create(aFillRule
);
146 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromData(
147 unsigned char* aData
, const IntSize
& aSize
, int32_t aStride
,
148 SurfaceFormat aFormat
) const override
;
149 virtual already_AddRefed
<SourceSurface
> OptimizeSourceSurface(
150 SourceSurface
* aSurface
) const override
;
151 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromNativeSurface(
152 const NativeSurface
& aSurface
) const override
;
153 virtual already_AddRefed
<DrawTarget
> CreateSimilarDrawTarget(
154 const IntSize
& aSize
, SurfaceFormat aFormat
) const override
;
155 virtual already_AddRefed
<DrawTarget
> CreateShadowDrawTarget(
156 const IntSize
& aSize
, SurfaceFormat aFormat
, float aSigma
) const override
;
157 virtual RefPtr
<DrawTarget
> CreateClippedDrawTarget(
158 const Rect
& aBounds
, SurfaceFormat aFormat
) override
;
160 virtual already_AddRefed
<GradientStops
> CreateGradientStops(
161 GradientStop
* aStops
, uint32_t aNumStops
,
162 ExtendMode aExtendMode
= ExtendMode::CLAMP
) const override
;
164 virtual already_AddRefed
<FilterNode
> CreateFilter(FilterType aType
) override
;
166 virtual void* GetNativeSurface(NativeSurfaceType aType
) override
;
168 bool Init(cairo_surface_t
* aSurface
, const IntSize
& aSize
,
169 SurfaceFormat
* aFormat
= nullptr);
170 bool Init(const IntSize
& aSize
, SurfaceFormat aFormat
);
171 bool Init(unsigned char* aData
, const IntSize
& aSize
, int32_t aStride
,
172 SurfaceFormat aFormat
);
174 virtual void SetTransform(const Matrix
& aTransform
) override
;
176 virtual void DetachAllSnapshots() override
{ MarkSnapshotIndependent(); }
178 // Call to set up aContext for drawing (with the current transform, etc).
179 // Pass the path you're going to be using if you have one.
180 // Implicitly calls WillChange(aPath).
181 void PrepareForDrawing(cairo_t
* aContext
, const Path
* aPath
= nullptr);
183 static cairo_surface_t
* GetDummySurface();
185 // Cairo hardcodes this as its maximum surface size.
186 static size_t GetMaxSurfaceSize() { return 32766; }
189 // Init cairo surface without doing a cairo_surface_reference() call.
190 bool InitAlreadyReferenced(cairo_surface_t
* aSurface
, const IntSize
& aSize
,
191 SurfaceFormat
* aFormat
= nullptr);
192 enum DrawPatternType
{ DRAW_FILL
, DRAW_STROKE
};
193 void DrawPattern(const Pattern
& aPattern
, const StrokeOptions
& aStrokeOptions
,
194 const DrawOptions
& aOptions
, DrawPatternType aDrawType
,
195 bool aPathBoundsClip
= false);
197 void CopySurfaceInternal(cairo_surface_t
* aSurface
, const IntRect
& aSource
,
198 const IntPoint
& aDest
);
200 Rect
GetUserSpaceClip() const;
202 // Call before you make any changes to the backing surface with which this
203 // context is associated. Pass the path you're going to be using if you have
205 void WillChange(const Path
* aPath
= nullptr);
207 // Call if there is any reason to disassociate the snapshot from this draw
208 // target; for example, because we're going to be destroyed.
209 void MarkSnapshotIndependent();
211 // If the current operator is "source" then clear the destination before we
212 // draw into it, to simulate the effect of an unbounded source operator.
213 void ClearSurfaceForUnboundedSource(const CompositionOp
& aOperator
);
215 // Set the Cairo context font options according to the current draw target
217 void SetFontOptions(cairo_antialias_t aAAMode
= CAIRO_ANTIALIAS_DEFAULT
);
221 cairo_surface_t
* mSurface
;
223 bool mTransformSingular
;
225 uint8_t* mLockedBits
;
227 cairo_font_options_t
* mFontOptions
;
230 PushedLayer(Float aOpacity
, CompositionOp aCompositionOp
,
231 bool aWasPermittingSubpixelAA
)
232 : mOpacity(aOpacity
),
233 mCompositionOp(aCompositionOp
),
234 mMaskPattern(nullptr),
235 mWasPermittingSubpixelAA(aWasPermittingSubpixelAA
) {}
237 CompositionOp mCompositionOp
;
238 cairo_pattern_t
* mMaskPattern
;
239 bool mWasPermittingSubpixelAA
;
241 std::vector
<PushedLayer
> mPushedLayers
;
243 // The latest snapshot of this surface. This needs to be told when this
244 // target is modified. We keep it alive as a cache.
245 RefPtr
<SourceSurfaceCairo
> mSnapshot
;
246 static cairo_surface_t
* mDummySurface
;
250 } // namespace mozilla
252 #endif // _MOZILLA_GFX_DRAWTARGET_CAIRO_H_