1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6 #ifndef _MOZILLA_GFX_2D_H
7 #define _MOZILLA_GFX_2D_H
15 // GenericRefCountedBase allows us to hold on to refcounted objects of any type
16 // (contrary to RefCounted<T> which requires knowing the type T) and, in particular,
17 // without having a dependency on that type. This is used for DrawTargetSkia
18 // to be able to hold on to a GLContext.
19 #include "mozilla/GenericRefCounted.h"
21 // This RefPtr class isn't ideal for usage in Azure, as it doesn't allow T**
22 // outparams using the &-operator. But it will have to do as there's no easy
24 #include "mozilla/RefPtr.h"
26 #include "mozilla/DebugOnly.h"
28 #ifdef MOZ_ENABLE_FREETYPE
32 struct _cairo_surface
;
33 typedef _cairo_surface cairo_surface_t
;
35 struct _cairo_scaled_font
;
36 typedef _cairo_scaled_font cairo_scaled_font_t
;
39 struct ID3D10Texture2D
;
42 struct IDWriteRenderingParams
;
48 typedef struct CGContext
*CGContextRef
;
55 class DataSourceSurface
;
57 class DrawEventRecorder
;
60 struct NativeSurface
{
61 NativeSurfaceType mType
;
62 SurfaceFormat mFormat
;
73 * This structure is used to send draw options that are universal to all drawing
77 /// For constructor parameter description, see member data documentation.
78 explicit DrawOptions(Float aAlpha
= 1.0f
,
79 CompositionOp aCompositionOp
= CompositionOp::OP_OVER
,
80 AntialiasMode aAntialiasMode
= AntialiasMode::DEFAULT
)
82 , mCompositionOp(aCompositionOp
)
83 , mAntialiasMode(aAntialiasMode
)
86 Float mAlpha
; /**< Alpha value by which the mask generated by this
87 operation is multiplied. */
88 CompositionOp mCompositionOp
; /**< The operator that indicates how the source and
89 destination patterns are blended. */
90 AntialiasMode mAntialiasMode
; /**< The AntiAlias mode used for this drawing
95 * This structure is used to send stroke options that are used in stroking
98 struct StrokeOptions
{
99 /// For constructor parameter description, see member data documentation.
100 explicit StrokeOptions(Float aLineWidth
= 1.0f
,
101 JoinStyle aLineJoin
= JoinStyle::MITER_OR_BEVEL
,
102 CapStyle aLineCap
= CapStyle::BUTT
,
103 Float aMiterLimit
= 10.0f
,
104 size_t aDashLength
= 0,
105 const Float
* aDashPattern
= 0,
106 Float aDashOffset
= 0.f
)
107 : mLineWidth(aLineWidth
)
108 , mMiterLimit(aMiterLimit
)
109 , mDashPattern(aDashLength
> 0 ? aDashPattern
: 0)
110 , mDashLength(aDashLength
)
111 , mDashOffset(aDashOffset
)
112 , mLineJoin(aLineJoin
)
115 MOZ_ASSERT(aDashLength
== 0 || aDashPattern
);
118 Float mLineWidth
; //!< Width of the stroke in userspace.
119 Float mMiterLimit
; //!< Miter limit in units of linewidth
120 const Float
* mDashPattern
; /**< Series of on/off userspace lengths defining dash.
121 Owned by the caller; must live at least as long as
123 mDashPattern != null <=> mDashLength > 0. */
124 size_t mDashLength
; //!< Number of on/off lengths in mDashPattern.
125 Float mDashOffset
; /**< Userspace offset within mDashPattern at which
127 JoinStyle mLineJoin
; //!< Join style used for joining lines.
128 CapStyle mLineCap
; //!< Cap style used for capping lines.
132 * This structure supplies additional options for calls to DrawSurface.
134 struct DrawSurfaceOptions
{
135 /// For constructor parameter description, see member data documentation.
136 explicit DrawSurfaceOptions(Filter aFilter
= Filter::LINEAR
,
137 SamplingBounds aSamplingBounds
= SamplingBounds::UNBOUNDED
)
139 , mSamplingBounds(aSamplingBounds
)
142 Filter mFilter
; /**< Filter used when resampling source surface
143 region to the destination region. */
144 SamplingBounds mSamplingBounds
; /**< This indicates whether the implementation is
145 allowed to sample pixels outside the source
146 rectangle as specified in DrawSurface on
152 * This class is used to store gradient stops, it can only be used with a
153 * matching DrawTarget. Not adhering to this condition will make a draw call
156 class GradientStops
: public RefCounted
<GradientStops
>
159 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GradientStops
)
160 virtual ~GradientStops() {}
162 virtual BackendType
GetBackendType() const = 0;
169 * This is the base class for 'patterns'. Patterns describe the pixels used as
170 * the source for a masked composition operation that is done by the different
171 * drawing commands. These objects are not backend specific, however for
172 * example the gradient stops on a gradient pattern can be backend specific.
177 virtual ~Pattern() {}
179 virtual PatternType
GetType() const = 0;
185 class ColorPattern
: public Pattern
188 explicit ColorPattern(const Color
&aColor
)
192 virtual PatternType
GetType() const { return PatternType::COLOR
; }
198 * This class is used for Linear Gradient Patterns, the gradient stops are
199 * stored in a separate object and are backend dependent. This class itself
200 * may be used on the stack.
202 class LinearGradientPattern
: public Pattern
205 /// For constructor parameter description, see member data documentation.
206 LinearGradientPattern(const Point
&aBegin
,
208 GradientStops
*aStops
,
209 const Matrix
&aMatrix
= Matrix())
217 virtual PatternType
GetType() const { return PatternType::LINEAR_GRADIENT
; }
219 Point mBegin
; //!< Start of the linear gradient
220 Point mEnd
; /**< End of the linear gradient - NOTE: In the case
221 of a zero length gradient it will act as the
222 color of the last stop. */
223 RefPtr
<GradientStops
> mStops
; /**< GradientStops object for this gradient, this
224 should match the backend type of the draw
225 target this pattern will be used with. */
226 Matrix mMatrix
; /**< A matrix that transforms the pattern into
231 * This class is used for Radial Gradient Patterns, the gradient stops are
232 * stored in a separate object and are backend dependent. This class itself
233 * may be used on the stack.
235 class RadialGradientPattern
: public Pattern
238 /// For constructor parameter description, see member data documentation.
239 RadialGradientPattern(const Point
&aCenter1
,
240 const Point
&aCenter2
,
243 GradientStops
*aStops
,
244 const Matrix
&aMatrix
= Matrix())
254 virtual PatternType
GetType() const { return PatternType::RADIAL_GRADIENT
; }
256 Point mCenter1
; //!< Center of the inner (focal) circle.
257 Point mCenter2
; //!< Center of the outer circle.
258 Float mRadius1
; //!< Radius of the inner (focal) circle.
259 Float mRadius2
; //!< Radius of the outer circle.
260 RefPtr
<GradientStops
> mStops
; /**< GradientStops object for this gradient, this
261 should match the backend type of the draw target
262 this pattern will be used with. */
263 Matrix mMatrix
; //!< A matrix that transforms the pattern into user space
267 * This class is used for Surface Patterns, they wrap a surface and a
268 * repetition mode for the surface. This may be used on the stack.
270 class SurfacePattern
: public Pattern
273 /// For constructor parameter description, see member data documentation.
274 SurfacePattern(SourceSurface
*aSourceSurface
, ExtendMode aExtendMode
,
275 const Matrix
&aMatrix
= Matrix(), Filter aFilter
= Filter::GOOD
)
276 : mSurface(aSourceSurface
)
277 , mExtendMode(aExtendMode
)
282 virtual PatternType
GetType() const { return PatternType::SURFACE
; }
284 RefPtr
<SourceSurface
> mSurface
; //!< Surface to use for drawing
285 ExtendMode mExtendMode
; /**< This determines how the image is extended
286 outside the bounds of the image */
287 Filter mFilter
; //!< Resampling filter for resampling the image.
288 Matrix mMatrix
; //!< Transforms the pattern into user space
292 class DrawTargetCaptureImpl
;
295 * This is the base class for source surfaces. These objects are surfaces
296 * which may be used as a source in a SurfacePattern or a DrawSurface call.
297 * They cannot be drawn to directly.
299 class SourceSurface
: public RefCounted
<SourceSurface
>
302 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurface
)
303 virtual ~SourceSurface() {}
305 virtual SurfaceType
GetType() const = 0;
306 virtual IntSize
GetSize() const = 0;
307 virtual SurfaceFormat
GetFormat() const = 0;
309 /** This returns false if some event has made this source surface invalid for
310 * usage with current DrawTargets. For example in the case of Direct2D this
311 * could return false if we have switched devices since this surface was
314 virtual bool IsValid() const { return true; }
317 * This function will get a DataSourceSurface for this surface, a
318 * DataSourceSurface's data can be accessed directly.
320 virtual TemporaryRef
<DataSourceSurface
> GetDataSurface() = 0;
322 /** Tries to get this SourceSurface's native surface. This will fail if aType
323 * is not the type of this SourceSurface's native surface.
325 virtual void *GetNativeSurface(NativeSurfaceType aType
) {
329 void AddUserData(UserDataKey
*key
, void *userData
, void (*destroy
)(void*)) {
330 mUserData
.Add(key
, userData
, destroy
);
332 void *GetUserData(UserDataKey
*key
) {
333 return mUserData
.Get(key
);
337 friend class DrawTargetCaptureImpl
;
338 friend class StoredPattern
;
340 // This is for internal use, it ensures the SourceSurface's data remains
341 // valid during the lifetime of the SourceSurface.
342 // @todo XXX - We need something better here :(. But we may be able to get rid
343 // of CreateWrappingDataSourceSurface in the future.
344 virtual void GuaranteePersistance() {}
349 class DataSourceSurface
: public SourceSurface
352 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurface
)
359 virtual ~DataSourceSurface()
361 MOZ_ASSERT(!mIsMapped
, "Someone forgot to call Unmap()");
365 struct MappedSurface
{
376 virtual SurfaceType
GetType() const { return SurfaceType::DATA
; }
378 * Get the raw bitmap data of the surface.
379 * Can return null if there was OOM allocating surface data.
381 virtual uint8_t *GetData() = 0;
384 * Stride of the surface, distance in bytes between the start of the image
385 * data belonging to row y and row y+1. This may be negative.
386 * Can return 0 if there was OOM allocating surface data.
388 virtual int32_t Stride() = 0;
390 virtual bool Map(MapType
, MappedSurface
*aMappedSurface
)
392 aMappedSurface
->mData
= GetData();
393 aMappedSurface
->mStride
= Stride();
400 MOZ_ASSERT(mIsMapped
);
405 * Returns a DataSourceSurface with the same data as this one, but
406 * guaranteed to have surface->GetType() == SurfaceType::DATA.
408 virtual TemporaryRef
<DataSourceSurface
> GetDataSurface();
414 /** This is an abstract object that accepts path segments. */
415 class PathSink
: public RefCounted
<PathSink
>
418 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PathSink
)
419 virtual ~PathSink() {}
421 /** Move the current point in the path, any figure currently being drawn will
422 * be considered closed during fill operations, however when stroking the
423 * closing line segment will not be drawn.
425 virtual void MoveTo(const Point
&aPoint
) = 0;
426 /** Add a linesegment to the current figure */
427 virtual void LineTo(const Point
&aPoint
) = 0;
428 /** Add a cubic bezier curve to the current figure */
429 virtual void BezierTo(const Point
&aCP1
,
431 const Point
&aCP3
) = 0;
432 /** Add a quadratic bezier curve to the current figure */
433 virtual void QuadraticBezierTo(const Point
&aCP1
,
434 const Point
&aCP2
) = 0;
435 /** Close the current figure, this will essentially generate a line segment
436 * from the current point to the starting point for the current figure
438 virtual void Close() = 0;
439 /** Add an arc to the current figure */
440 virtual void Arc(const Point
&aOrigin
, float aRadius
, float aStartAngle
,
441 float aEndAngle
, bool aAntiClockwise
= false) = 0;
442 /** Point the current subpath is at - or where the next subpath will start
443 * if there is no active subpath.
445 virtual Point
CurrentPoint() const = 0;
451 /** The path class is used to create (sets of) figures of any shape that can be
452 * filled or stroked to a DrawTarget
454 class Path
: public RefCounted
<Path
>
457 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(Path
)
460 virtual BackendType
GetBackendType() const = 0;
462 /** This returns a PathBuilder object that contains a copy of the contents of
463 * this path and is still writable.
465 virtual TemporaryRef
<PathBuilder
> CopyToBuilder(FillRule aFillRule
= FillRule::FILL_WINDING
) const = 0;
466 virtual TemporaryRef
<PathBuilder
> TransformedCopyToBuilder(const Matrix
&aTransform
,
467 FillRule aFillRule
= FillRule::FILL_WINDING
) const = 0;
469 /** This function checks if a point lies within a path. It allows passing a
470 * transform that will transform the path to the coordinate space in which
473 virtual bool ContainsPoint(const Point
&aPoint
, const Matrix
&aTransform
) const = 0;
476 /** This function checks if a point lies within the stroke of a path using the
477 * specified strokeoptions. It allows passing a transform that will transform
478 * the path to the coordinate space in which aPoint is given.
480 virtual bool StrokeContainsPoint(const StrokeOptions
&aStrokeOptions
,
482 const Matrix
&aTransform
) const = 0;
484 /** This functions gets the bounds of this path. These bounds are not
485 * guaranteed to be tight. A transform may be specified that gives the bounds
486 * after application of the transform.
488 virtual Rect
GetBounds(const Matrix
&aTransform
= Matrix()) const = 0;
490 /** This function gets the bounds of the stroke of this path using the
491 * specified strokeoptions. These bounds are not guaranteed to be tight.
492 * A transform may be specified that gives the bounds after application of
495 virtual Rect
GetStrokedBounds(const StrokeOptions
&aStrokeOptions
,
496 const Matrix
&aTransform
= Matrix()) const = 0;
498 /** Take the contents of this path and stream it to another sink, this works
499 * regardless of the backend that might be used for the destination sink.
501 virtual void StreamToSink(PathSink
*aSink
) const = 0;
503 /** This gets the fillrule this path's builder was created with. This is not
506 virtual FillRule
GetFillRule() const = 0;
508 virtual Float
ComputeLength();
510 virtual Point
ComputePointAtLength(Float aLength
,
511 Point
* aTangent
= nullptr);
515 void EnsureFlattenedPath();
517 RefPtr
<FlattenedPath
> mFlattenedPath
;
520 /** The PathBuilder class allows path creation. Once finish is called on the
521 * pathbuilder it may no longer be written to.
523 class PathBuilder
: public PathSink
526 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(PathBuilder
)
527 /** Finish writing to the path and return a Path object that can be used for
528 * drawing. Future use of the builder results in a crash!
530 virtual TemporaryRef
<Path
> Finish() = 0;
539 /** This class functions as a glyph buffer that can be drawn to a DrawTarget.
540 * @todo XXX - This should probably contain the guts of gfxTextRun in the future as
541 * roc suggested. But for now it's a simple container for a glyph vector.
545 const Glyph
*mGlyphs
; //!< A pointer to a buffer of glyphs. Managed by the caller.
546 uint32_t mNumGlyphs
; //!< Number of glyphs mGlyphs points to.
549 /** This class is an abstraction of a backend/platform specific font object
550 * at a particular size. It is passed into text drawing calls to describe
551 * the font used for the drawing call.
553 class ScaledFont
: public RefCounted
<ScaledFont
>
556 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(ScaledFont
)
557 virtual ~ScaledFont() {}
559 typedef void (*FontFileDataOutput
)(const uint8_t *aData
, uint32_t aLength
, uint32_t aIndex
, Float aGlyphSize
, void *aBaton
);
561 virtual FontType
GetType() const = 0;
563 /** This allows getting a path that describes the outline of a set of glyphs.
564 * A target is passed in so that the guarantee is made the returned path
565 * can be used with any DrawTarget that has the same backend as the one
568 virtual TemporaryRef
<Path
> GetPathForGlyphs(const GlyphBuffer
&aBuffer
, const DrawTarget
*aTarget
) = 0;
570 /** This copies the path describing the glyphs into a PathBuilder. We use this
571 * API rather than a generic API to append paths because it allows easier
572 * implementation in some backends, and more efficient implementation in
575 virtual void CopyGlyphsToBuilder(const GlyphBuffer
&aBuffer
, PathBuilder
*aBuilder
, BackendType aBackendType
, const Matrix
*aTransformHint
= nullptr) = 0;
577 virtual bool GetFontFileData(FontFileDataOutput
, void *) { return false; }
579 void AddUserData(UserDataKey
*key
, void *userData
, void (*destroy
)(void*)) {
580 mUserData
.Add(key
, userData
, destroy
);
582 void *GetUserData(UserDataKey
*key
) {
583 return mUserData
.Get(key
);
592 #ifdef MOZ_ENABLE_FREETYPE
595 * Used to pass the key informatin from a gfxFont into Azure
596 * @todo Should be replaced by a more long term solution, perhaps Bug 738014
606 /** This class is designed to allow passing additional glyph rendering
607 * parameters to the glyph drawing functions. This is an empty wrapper class
608 * merely used to allow holding on to and passing around platform specific
609 * parameters. This is because different platforms have unique rendering
612 class GlyphRenderingOptions
: public RefCounted
<GlyphRenderingOptions
>
615 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GlyphRenderingOptions
)
616 virtual ~GlyphRenderingOptions() {}
618 virtual FontType
GetType() const = 0;
621 GlyphRenderingOptions() {}
624 class DrawTargetCapture
;
626 /** This is the main class used for all the drawing. It is created through the
627 * factory and accepts drawing commands. The results of drawing to a target
628 * may be used either through a Snapshot or by flushing the target and directly
629 * accessing the backing store a DrawTarget was created with.
631 class DrawTarget
: public RefCounted
<DrawTarget
>
634 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTarget
)
635 DrawTarget() : mTransformDirty(false), mPermitSubpixelAA(false) {}
636 virtual ~DrawTarget() {}
638 virtual DrawTargetType
GetType() const = 0;
640 virtual BackendType
GetBackendType() const = 0;
642 * Returns a SourceSurface which is a snapshot of the current contents of the DrawTarget.
643 * Multiple calls to Snapshot() without any drawing operations in between will
644 * normally return the same SourceSurface object.
646 virtual TemporaryRef
<SourceSurface
> Snapshot() = 0;
647 virtual IntSize
GetSize() = 0;
650 * If possible returns the bits to this DrawTarget for direct manipulation. While
651 * the bits is locked any modifications to this DrawTarget is forbidden.
652 * Release takes the original data pointer for safety.
654 virtual bool LockBits(uint8_t** aData
, IntSize
* aSize
,
655 int32_t* aStride
, SurfaceFormat
* aFormat
) { return false; }
656 virtual void ReleaseBits(uint8_t* aData
) {}
658 /** Ensure that the DrawTarget backend has flushed all drawing operations to
659 * this draw target. This must be called before using the backing surface of
660 * this draw target outside of GFX 2D code.
662 virtual void Flush() = 0;
665 * Realize a DrawTargetCapture onto the draw target.
667 * @param aSource Capture DrawTarget to draw
668 * @param aTransform Transform to apply when replaying commands
670 virtual void DrawCapturedDT(DrawTargetCapture
*aCaptureDT
,
671 const Matrix
& aTransform
);
674 * Draw a surface to the draw target. Possibly doing partial drawing or
675 * applying scaling. No sampling happens outside the source.
677 * @param aSurface Source surface to draw
678 * @param aDest Destination rectangle that this drawing operation should draw to
679 * @param aSource Source rectangle in aSurface coordinates, this area of aSurface
680 * will be stretched to the size of aDest.
681 * @param aOptions General draw options that are applied to the operation
682 * @param aSurfOptions DrawSurface options that are applied
684 virtual void DrawSurface(SourceSurface
*aSurface
,
687 const DrawSurfaceOptions
&aSurfOptions
= DrawSurfaceOptions(),
688 const DrawOptions
&aOptions
= DrawOptions()) = 0;
691 * Draw the output of a FilterNode to the DrawTarget.
693 * @param aNode FilterNode to draw
694 * @param aSourceRect Source rectangle in FilterNode space to draw
695 * @param aDestPoint Destination point on the DrawTarget to draw the
696 * SourceRectangle of the filter output to
698 virtual void DrawFilter(FilterNode
*aNode
,
699 const Rect
&aSourceRect
,
700 const Point
&aDestPoint
,
701 const DrawOptions
&aOptions
= DrawOptions()) = 0;
704 * Blend a surface to the draw target with a shadow. The shadow is drawn as a
705 * gaussian blur using a specified sigma. The shadow is clipped to the size
706 * of the input surface, so the input surface should contain a transparent
707 * border the size of the approximate coverage of the blur (3 * aSigma).
708 * NOTE: This function works in device space!
710 * @param aSurface Source surface to draw.
711 * @param aDest Destination point that this drawing operation should draw to.
712 * @param aColor Color of the drawn shadow
713 * @param aOffset Offset of the shadow
714 * @param aSigma Sigma used for the guassian filter kernel
715 * @param aOperator Composition operator used
717 virtual void DrawSurfaceWithShadow(SourceSurface
*aSurface
,
720 const Point
&aOffset
,
722 CompositionOp aOperator
) = 0;
725 * Clear a rectangle on the draw target to transparent black. This will
726 * respect the clipping region and transform.
728 * @param aRect Rectangle to clear
730 virtual void ClearRect(const Rect
&aRect
) = 0;
733 * This is essentially a 'memcpy' between two surfaces. It moves a pixel
734 * aligned area from the source surface unscaled directly onto the
735 * drawtarget. This ignores both transform and clip.
737 * @param aSurface Surface to copy from
738 * @param aSourceRect Source rectangle to be copied
739 * @param aDest Destination point to copy the surface to
741 virtual void CopySurface(SourceSurface
*aSurface
,
742 const IntRect
&aSourceRect
,
743 const IntPoint
&aDestination
) = 0;
746 * Same as CopySurface, except uses itself as the source.
748 * Some backends may be able to optimize this better
749 * than just taking a snapshot and using CopySurface.
751 virtual void CopyRect(const IntRect
&aSourceRect
,
752 const IntPoint
&aDestination
)
754 RefPtr
<SourceSurface
> source
= Snapshot();
755 CopySurface(source
, aSourceRect
, aDestination
);
759 * Fill a rectangle on the DrawTarget with a certain source pattern.
761 * @param aRect Rectangle that forms the mask of this filling operation
762 * @param aPattern Pattern that forms the source of this filling operation
763 * @param aOptions Options that are applied to this operation
765 virtual void FillRect(const Rect
&aRect
,
766 const Pattern
&aPattern
,
767 const DrawOptions
&aOptions
= DrawOptions()) = 0;
770 * Stroke a rectangle on the DrawTarget with a certain source pattern.
772 * @param aRect Rectangle that forms the mask of this stroking operation
773 * @param aPattern Pattern that forms the source of this stroking operation
774 * @param aOptions Options that are applied to this operation
776 virtual void StrokeRect(const Rect
&aRect
,
777 const Pattern
&aPattern
,
778 const StrokeOptions
&aStrokeOptions
= StrokeOptions(),
779 const DrawOptions
&aOptions
= DrawOptions()) = 0;
782 * Stroke a line on the DrawTarget with a certain source pattern.
784 * @param aStart Starting point of the line
785 * @param aEnd End point of the line
786 * @param aPattern Pattern that forms the source of this stroking operation
787 * @param aOptions Options that are applied to this operation
789 virtual void StrokeLine(const Point
&aStart
,
791 const Pattern
&aPattern
,
792 const StrokeOptions
&aStrokeOptions
= StrokeOptions(),
793 const DrawOptions
&aOptions
= DrawOptions()) = 0;
796 * Stroke a path on the draw target with a certain source pattern.
798 * @param aPath Path that is to be stroked
799 * @param aPattern Pattern that should be used for the stroke
800 * @param aStrokeOptions Stroke options used for this operation
801 * @param aOptions Draw options used for this operation
803 virtual void Stroke(const Path
*aPath
,
804 const Pattern
&aPattern
,
805 const StrokeOptions
&aStrokeOptions
= StrokeOptions(),
806 const DrawOptions
&aOptions
= DrawOptions()) = 0;
809 * Fill a path on the draw target with a certain source pattern.
811 * @param aPath Path that is to be filled
812 * @param aPattern Pattern that should be used for the fill
813 * @param aOptions Draw options used for this operation
815 virtual void Fill(const Path
*aPath
,
816 const Pattern
&aPattern
,
817 const DrawOptions
&aOptions
= DrawOptions()) = 0;
820 * Fill a series of clyphs on the draw target with a certain source pattern.
822 virtual void FillGlyphs(ScaledFont
*aFont
,
823 const GlyphBuffer
&aBuffer
,
824 const Pattern
&aPattern
,
825 const DrawOptions
&aOptions
= DrawOptions(),
826 const GlyphRenderingOptions
*aRenderingOptions
= nullptr) = 0;
829 * This takes a source pattern and a mask, and composites the source pattern
830 * onto the destination surface using the alpha channel of the mask pattern
831 * as a mask for the operation.
833 * @param aSource Source pattern
834 * @param aMask Mask pattern
835 * @param aOptions Drawing options
837 virtual void Mask(const Pattern
&aSource
,
838 const Pattern
&aMask
,
839 const DrawOptions
&aOptions
= DrawOptions()) = 0;
842 * This takes a source pattern and a mask, and composites the source pattern
843 * onto the destination surface using the alpha channel of the mask source.
844 * The operation is bound by the extents of the mask.
846 * @param aSource Source pattern
847 * @param aMask Mask surface
848 * @param aOffset a transformed offset that the surface is masked at
849 * @param aOptions Drawing options
851 virtual void MaskSurface(const Pattern
&aSource
,
852 SourceSurface
*aMask
,
854 const DrawOptions
&aOptions
= DrawOptions()) = 0;
857 * Push a clip to the DrawTarget.
859 * @param aPath The path to clip to
861 virtual void PushClip(const Path
*aPath
) = 0;
864 * Push an axis-aligned rectangular clip to the DrawTarget. This rectangle
865 * is specified in user space.
867 * @param aRect The rect to clip to
869 virtual void PushClipRect(const Rect
&aRect
) = 0;
871 /** Pop a clip from the DrawTarget. A pop without a corresponding push will
874 virtual void PopClip() = 0;
877 * Create a SourceSurface optimized for use with this DrawTarget from
878 * existing bitmap data in memory.
880 * The SourceSurface does not take ownership of aData, and may be freed at any time.
882 virtual TemporaryRef
<SourceSurface
> CreateSourceSurfaceFromData(unsigned char *aData
,
883 const IntSize
&aSize
,
885 SurfaceFormat aFormat
) const = 0;
888 * Create a SourceSurface optimized for use with this DrawTarget from an
889 * arbitrary SourceSurface type supported by this backend. This may return
890 * aSourceSurface or some other existing surface.
892 virtual TemporaryRef
<SourceSurface
> OptimizeSourceSurface(SourceSurface
*aSurface
) const = 0;
895 * Create a SourceSurface for a type of NativeSurface. This may fail if the
896 * draw target does not know how to deal with the type of NativeSurface passed
899 virtual TemporaryRef
<SourceSurface
>
900 CreateSourceSurfaceFromNativeSurface(const NativeSurface
&aSurface
) const = 0;
903 * Create a DrawTarget whose snapshot is optimized for use with this DrawTarget.
905 virtual TemporaryRef
<DrawTarget
>
906 CreateSimilarDrawTarget(const IntSize
&aSize
, SurfaceFormat aFormat
) const = 0;
909 * Create a DrawTarget that captures the drawing commands and can be replayed
910 * onto a compatible DrawTarget afterwards.
912 * @param aSize Size of the area this DT will capture.
914 virtual TemporaryRef
<DrawTargetCapture
> CreateCaptureDT(const IntSize
& aSize
);
917 * Create a draw target optimized for drawing a shadow.
919 * Note that aSigma is the blur radius that must be used when we draw the
920 * shadow. Also note that this doesn't affect the size of the allocated
921 * surface, the caller is still responsible for including the shadow area in
924 virtual TemporaryRef
<DrawTarget
>
925 CreateShadowDrawTarget(const IntSize
&aSize
, SurfaceFormat aFormat
,
928 return CreateSimilarDrawTarget(aSize
, aFormat
);
932 * Create a path builder with the specified fillmode.
934 * We need the fill mode up front because of Direct2D.
935 * ID2D1SimplifiedGeometrySink requires the fill mode
936 * to be set before calling BeginFigure().
938 virtual TemporaryRef
<PathBuilder
> CreatePathBuilder(FillRule aFillRule
= FillRule::FILL_WINDING
) const = 0;
941 * Create a GradientStops object that holds information about a set of
942 * gradient stops, this object is required for linear or radial gradient
943 * patterns to represent the color stops in the gradient.
945 * @param aStops An array of gradient stops
946 * @param aNumStops Number of stops in the array aStops
947 * @param aExtendNone This describes how to extend the stop color outside of the
950 virtual TemporaryRef
<GradientStops
>
951 CreateGradientStops(GradientStop
*aStops
,
953 ExtendMode aExtendMode
= ExtendMode::CLAMP
) const = 0;
956 * Create a FilterNode object that can be used to apply a filter to various
959 * @param aType Type of filter node to be created.
961 virtual TemporaryRef
<FilterNode
> CreateFilter(FilterType aType
) = 0;
963 const Matrix
&GetTransform() const { return mTransform
; }
966 * Set a transform on the surface, this transform is applied at drawing time
967 * to both the mask and source of the operation.
969 virtual void SetTransform(const Matrix
&aTransform
)
970 { mTransform
= aTransform
; mTransformDirty
= true; }
972 SurfaceFormat
GetFormat() { return mFormat
; }
974 /** Tries to get a native surface for a DrawTarget, this may fail if the
975 * draw target cannot convert to this surface type.
977 virtual void *GetNativeSurface(NativeSurfaceType aType
) { return nullptr; }
979 virtual bool IsDualDrawTarget() const { return false; }
980 virtual bool IsTiledDrawTarget() const { return false; }
982 void AddUserData(UserDataKey
*key
, void *userData
, void (*destroy
)(void*)) {
983 mUserData
.Add(key
, userData
, destroy
);
985 void *GetUserData(UserDataKey
*key
) {
986 return mUserData
.Get(key
);
989 /** Within this rectangle all pixels will be opaque by the time the result of
990 * this DrawTarget is first used for drawing. Either by the underlying surface
991 * being used as an input to external drawing, or Snapshot() being called.
992 * This rectangle is specified in device space.
994 void SetOpaqueRect(const IntRect
&aRect
) {
998 const IntRect
&GetOpaqueRect() const {
1002 virtual void SetPermitSubpixelAA(bool aPermitSubpixelAA
) {
1003 mPermitSubpixelAA
= aPermitSubpixelAA
;
1006 bool GetPermitSubpixelAA() {
1007 return mPermitSubpixelAA
;
1011 virtual bool InitWithGrContext(GrContext
* aGrContext
,
1012 const IntSize
&aSize
,
1013 SurfaceFormat aFormat
)
1022 IntRect mOpaqueRect
;
1023 bool mTransformDirty
: 1;
1024 bool mPermitSubpixelAA
: 1;
1026 SurfaceFormat mFormat
;
1029 class DrawTargetCapture
: public DrawTarget
1033 class DrawEventRecorder
: public RefCounted
<DrawEventRecorder
>
1036 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorder
)
1037 virtual ~DrawEventRecorder() { }
1042 RefPtr
<DrawTarget
> mDrawTarget
;
1043 IntPoint mTileOrigin
;
1052 class GFX2D_API Factory
1055 static bool HasSSE2();
1057 /** Make sure that the given dimensions don't overflow a 32-bit signed int
1058 * using 4 bytes per pixel; optionally, make sure that either dimension
1059 * doesn't exceed the given limit.
1061 static bool CheckSurfaceSize(const IntSize
&sz
, int32_t limit
= 0);
1063 static TemporaryRef
<DrawTarget
> CreateDrawTargetForCairoSurface(cairo_surface_t
* aSurface
, const IntSize
& aSize
, SurfaceFormat
* aFormat
= nullptr);
1065 static TemporaryRef
<DrawTarget
>
1066 CreateDrawTarget(BackendType aBackend
, const IntSize
&aSize
, SurfaceFormat aFormat
);
1068 static TemporaryRef
<DrawTarget
>
1069 CreateRecordingDrawTarget(DrawEventRecorder
*aRecorder
, DrawTarget
*aDT
);
1071 static TemporaryRef
<DrawTarget
>
1072 CreateDrawTargetForData(BackendType aBackend
, unsigned char* aData
, const IntSize
&aSize
, int32_t aStride
, SurfaceFormat aFormat
);
1074 static TemporaryRef
<ScaledFont
>
1075 CreateScaledFontForNativeFont(const NativeFont
&aNativeFont
, Float aSize
);
1078 * This creates a ScaledFont from TrueType data.
1080 * @param aData Pointer to the data
1081 * @param aSize Size of the TrueType data
1082 * @param aFaceIndex Index of the font face in the truetype data this ScaledFont needs to represent.
1083 * @param aGlyphSize Size of the glyphs in this ScaledFont
1084 * @param aType Type of ScaledFont that should be created.
1086 static TemporaryRef
<ScaledFont
>
1087 CreateScaledFontForTrueTypeData(uint8_t *aData
, uint32_t aSize
, uint32_t aFaceIndex
, Float aGlyphSize
, FontType aType
);
1090 * This creates a scaled font with an associated cairo_scaled_font_t, and
1091 * must be used when using the Cairo backend. The NativeFont and
1092 * cairo_scaled_font_t* parameters must correspond to the same font.
1094 static TemporaryRef
<ScaledFont
>
1095 CreateScaledFontWithCairo(const NativeFont
&aNativeFont
, Float aSize
, cairo_scaled_font_t
* aScaledFont
);
1098 * This creates a simple data source surface for a certain size. It allocates
1099 * new memory for the surface. This memory is freed when the surface is
1100 * destroyed. The caller is responsible for handing the case where nullptr
1101 * is returned. The surface is not zeroed unless requested.
1103 static TemporaryRef
<DataSourceSurface
>
1104 CreateDataSourceSurface(const IntSize
&aSize
, SurfaceFormat aFormat
, bool aZero
= false);
1107 * This creates a simple data source surface for a certain size with a
1108 * specific stride, which must be large enough to fit all pixels.
1109 * It allocates new memory for the surface. This memory is freed when
1110 * the surface is destroyed. The caller is responsible for handling the case
1111 * where nullptr is returned. The surface is not zeroed unless requested.
1113 static TemporaryRef
<DataSourceSurface
>
1114 CreateDataSourceSurfaceWithStride(const IntSize
&aSize
, SurfaceFormat aFormat
, int32_t aStride
, bool aZero
= false);
1117 * This creates a simple data source surface for some existing data. It will
1118 * wrap this data and the data for this source surface. The caller is
1119 * responsible for deallocating the memory only after destruction of the
1122 static TemporaryRef
<DataSourceSurface
>
1123 CreateWrappingDataSourceSurface(uint8_t *aData
, int32_t aStride
,
1124 const IntSize
&aSize
, SurfaceFormat aFormat
);
1126 static TemporaryRef
<DrawEventRecorder
>
1127 CreateEventRecorderForFile(const char *aFilename
);
1129 static void SetGlobalEventRecorder(DrawEventRecorder
*aRecorder
);
1132 static TemporaryRef
<DrawTarget
>
1133 CreateDrawTargetSkiaWithGrContext(GrContext
* aGrContext
,
1134 const IntSize
&aSize
,
1135 SurfaceFormat aFormat
);
1138 static void PurgeAllCaches();
1140 #if defined(USE_SKIA) && defined(MOZ_ENABLE_FREETYPE)
1141 static TemporaryRef
<GlyphRenderingOptions
>
1142 CreateCairoGlyphRenderingOptions(FontHinting aHinting
, bool aAutoHinting
);
1144 static TemporaryRef
<DrawTarget
>
1145 CreateDualDrawTarget(DrawTarget
*targetA
, DrawTarget
*targetB
);
1148 * This creates a new tiled DrawTarget. When a tiled drawtarget is used the
1149 * drawing is distributed over number of tiles which may each hold an
1150 * individual offset. The tiles in the set must each have the same backend
1153 static TemporaryRef
<DrawTarget
> CreateTiledDrawTarget(const TileSet
& aTileSet
);
1156 static TemporaryRef
<DrawTarget
> CreateDrawTargetForCairoCGContext(CGContextRef cg
, const IntSize
& aSize
);
1160 static TemporaryRef
<DrawTarget
> CreateDrawTargetForD3D10Texture(ID3D10Texture2D
*aTexture
, SurfaceFormat aFormat
);
1161 static TemporaryRef
<DrawTarget
>
1162 CreateDualDrawTargetForD3D10Textures(ID3D10Texture2D
*aTextureA
,
1163 ID3D10Texture2D
*aTextureB
,
1164 SurfaceFormat aFormat
);
1166 static void SetDirect3D10Device(ID3D10Device1
*aDevice
);
1167 static ID3D10Device1
*GetDirect3D10Device();
1169 static void SetDirect3D11Device(ID3D11Device
*aDevice
);
1170 static ID3D11Device
*GetDirect3D11Device();
1171 static ID2D1Device
*GetD2D1Device();
1174 static TemporaryRef
<GlyphRenderingOptions
>
1175 CreateDWriteGlyphRenderingOptions(IDWriteRenderingParams
*aParams
);
1177 static uint64_t GetD2DVRAMUsageDrawTarget();
1178 static uint64_t GetD2DVRAMUsageSourceSurface();
1179 static void D2DCleanup();
1182 static ID3D10Device1
*mD3D10Device
;
1184 static ID3D11Device
*mD3D11Device
;
1185 static ID2D1Device
*mD2D1Device
;
1189 static DrawEventRecorder
*mRecorder
;
1195 #endif // _MOZILLA_GFX_2D_H