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_DRAWTARGETD2D1_H_
7 #define MOZILLA_GFX_DRAWTARGETD2D1_H_
13 #include "HelpersD2D.h"
21 #include <unordered_set>
24 struct IDWriteFactory
;
29 class SourceSurfaceD2D1
;
30 class GradientStopsD2D
;
31 class ScaledFontDWrite
;
33 const int32_t kLayerCacheSize1
= 5;
35 class DrawTargetD2D1
: public DrawTarget
38 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetD2D1
)
40 virtual ~DrawTargetD2D1();
42 virtual DrawTargetType
GetType() const MOZ_OVERRIDE
{ return DrawTargetType::HARDWARE_RASTER
; }
43 virtual BackendType
GetBackendType() const { return BackendType::DIRECT2D1_1
; }
44 virtual TemporaryRef
<SourceSurface
> Snapshot();
45 virtual IntSize
GetSize() { return mSize
; }
48 virtual void DrawSurface(SourceSurface
*aSurface
,
51 const DrawSurfaceOptions
&aSurfOptions
,
52 const DrawOptions
&aOptions
);
53 virtual void DrawFilter(FilterNode
*aNode
,
54 const Rect
&aSourceRect
,
55 const Point
&aDestPoint
,
56 const DrawOptions
&aOptions
= DrawOptions());
57 virtual void DrawSurfaceWithShadow(SourceSurface
*aSurface
,
62 CompositionOp aOperator
);
63 virtual void ClearRect(const Rect
&aRect
);
64 virtual void MaskSurface(const Pattern
&aSource
,
67 const DrawOptions
&aOptions
= DrawOptions());
69 virtual void CopySurface(SourceSurface
*aSurface
,
70 const IntRect
&aSourceRect
,
71 const IntPoint
&aDestination
);
73 virtual void FillRect(const Rect
&aRect
,
74 const Pattern
&aPattern
,
75 const DrawOptions
&aOptions
= DrawOptions());
76 virtual void StrokeRect(const Rect
&aRect
,
77 const Pattern
&aPattern
,
78 const StrokeOptions
&aStrokeOptions
= StrokeOptions(),
79 const DrawOptions
&aOptions
= DrawOptions());
80 virtual void StrokeLine(const Point
&aStart
,
82 const Pattern
&aPattern
,
83 const StrokeOptions
&aStrokeOptions
= StrokeOptions(),
84 const DrawOptions
&aOptions
= DrawOptions());
85 virtual void Stroke(const Path
*aPath
,
86 const Pattern
&aPattern
,
87 const StrokeOptions
&aStrokeOptions
= StrokeOptions(),
88 const DrawOptions
&aOptions
= DrawOptions());
89 virtual void Fill(const Path
*aPath
,
90 const Pattern
&aPattern
,
91 const DrawOptions
&aOptions
= DrawOptions());
92 virtual void FillGlyphs(ScaledFont
*aFont
,
93 const GlyphBuffer
&aBuffer
,
94 const Pattern
&aPattern
,
95 const DrawOptions
&aOptions
= DrawOptions(),
96 const GlyphRenderingOptions
*aRenderingOptions
= nullptr);
97 virtual void Mask(const Pattern
&aSource
,
99 const DrawOptions
&aOptions
= DrawOptions());
100 virtual void PushClip(const Path
*aPath
);
101 virtual void PushClipRect(const Rect
&aRect
);
102 virtual void PopClip();
104 virtual TemporaryRef
<SourceSurface
> CreateSourceSurfaceFromData(unsigned char *aData
,
105 const IntSize
&aSize
,
107 SurfaceFormat aFormat
) const;
108 virtual TemporaryRef
<SourceSurface
> OptimizeSourceSurface(SourceSurface
*aSurface
) const;
110 virtual TemporaryRef
<SourceSurface
>
111 CreateSourceSurfaceFromNativeSurface(const NativeSurface
&aSurface
) const { return nullptr; }
113 virtual TemporaryRef
<DrawTarget
>
114 CreateSimilarDrawTarget(const IntSize
&aSize
, SurfaceFormat aFormat
) const;
116 virtual TemporaryRef
<PathBuilder
> CreatePathBuilder(FillRule aFillRule
= FillRule::FILL_WINDING
) const;
118 virtual TemporaryRef
<GradientStops
>
119 CreateGradientStops(GradientStop
*aStops
,
121 ExtendMode aExtendMode
= ExtendMode::CLAMP
) const;
123 virtual TemporaryRef
<FilterNode
> CreateFilter(FilterType aType
);
125 virtual void *GetNativeSurface(NativeSurfaceType aType
) { return nullptr; }
127 bool Init(const IntSize
&aSize
, SurfaceFormat aFormat
);
128 uint32_t GetByteSize() const;
130 TemporaryRef
<ID2D1Image
> GetImageForSurface(SourceSurface
*aSurface
, Matrix
&aSourceTransform
,
131 ExtendMode aExtendMode
);
133 TemporaryRef
<ID2D1Image
> GetImageForSurface(SourceSurface
*aSurface
, ExtendMode aExtendMode
) {
135 return GetImageForSurface(aSurface
, mat
, aExtendMode
);
138 static ID2D1Factory1
*factory();
139 static void CleanupD2D();
140 static IDWriteFactory
*GetDWriteFactory();
142 operator std::string() const {
143 std::stringstream stream
;
144 stream
<< "DrawTargetD2D 1.1 (" << this << ")";
148 static uint64_t mVRAMUsageDT
;
149 static uint64_t mVRAMUsageSS
;
152 friend class SourceSurfaceD2D1
;
155 typedef stdext::hash_set
<DrawTargetD2D1
*> TargetSet
;
157 typedef std::unordered_set
<DrawTargetD2D1
*> TargetSet
;
160 // This function will mark the surface as changing, and make sure any
161 // copy-on-write snapshots are notified.
163 void PrepareForDrawing(CompositionOp aOp
, const Pattern
&aPattern
);
164 void FinalizeDrawing(CompositionOp aOp
, const Pattern
&aPattern
);
165 void FlushTransformToDC() {
166 if (mTransformDirty
) {
167 mDC
->SetTransform(D2DMatrix(mTransform
));
168 mTransformDirty
= false;
171 void AddDependencyOnSource(SourceSurfaceD2D1
* aSource
);
174 void PushClipsToDC(ID2D1DeviceContext
*aDC
);
175 void PopClipsFromDC(ID2D1DeviceContext
*aDC
);
177 TemporaryRef
<ID2D1Brush
> CreateBrushForPattern(const Pattern
&aPattern
, Float aAlpha
= 1.0f
);
179 void PushD2DLayer(ID2D1DeviceContext
*aDC
, ID2D1Geometry
*aGeometry
, const D2D1_MATRIX_3X2_F
&aTransform
);
183 RefPtr
<ID3D11Device
> mDevice
;
184 RefPtr
<ID3D11Texture2D
> mTexture
;
185 // This is only valid if mCurrentClippedGeometry is non-null. And will
186 // only be the intersection of all pixel-aligned retangular clips. This is in
188 IntRect mCurrentClipBounds
;
189 mutable RefPtr
<ID2D1DeviceContext
> mDC
;
190 RefPtr
<ID2D1Bitmap1
> mBitmap
;
191 RefPtr
<ID2D1Bitmap1
> mTempBitmap
;
193 // We store this to prevent excessive SetTextRenderingParams calls.
194 RefPtr
<IDWriteRenderingParams
> mTextRenderingParams
;
196 // List of pushed clips.
201 // If mPath is non-null, the mTransform member will be used, otherwise
202 // the mIsPixelAligned member is valid.
203 D2D1_MATRIX_3X2_F mTransform
;
204 bool mIsPixelAligned
;
206 RefPtr
<PathD2D
> mPath
;
208 std::vector
<PushedClip
> mPushedClips
;
210 // The latest snapshot of this surface. This needs to be told when this
211 // target is modified. We keep it alive as a cache.
212 RefPtr
<SourceSurfaceD2D1
> mSnapshot
;
213 // A list of targets we need to flush when we're modified.
214 TargetSet mDependentTargets
;
215 // A list of targets which have this object in their mDependentTargets set
216 TargetSet mDependingOnTargets
;
218 // True of the current clip stack is pushed to the main RT.
219 bool mClipsArePushed
;
220 static ID2D1Factory1
*mFactory
;
221 static IDWriteFactory
*mDWriteFactory
;
227 #endif /* MOZILLA_GFX_DRAWTARGETD2D_H_ */