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 bool SupportsRegionClipping() const { return false; }
127 virtual void *GetNativeSurface(NativeSurfaceType aType
) { return nullptr; }
129 bool Init(const IntSize
&aSize
, SurfaceFormat aFormat
);
130 bool Init(ID3D11Texture2D
* aTexture
, SurfaceFormat aFormat
);
131 uint32_t GetByteSize() const;
133 TemporaryRef
<ID2D1Image
> GetImageForSurface(SourceSurface
*aSurface
, Matrix
&aSourceTransform
,
134 ExtendMode aExtendMode
, const IntRect
* aSourceRect
= nullptr);
136 TemporaryRef
<ID2D1Image
> GetImageForSurface(SourceSurface
*aSurface
, ExtendMode aExtendMode
) {
138 return GetImageForSurface(aSurface
, mat
, aExtendMode
, nullptr);
141 static ID2D1Factory1
*factory();
142 static void CleanupD2D();
143 static IDWriteFactory
*GetDWriteFactory();
145 operator std::string() const {
146 std::stringstream stream
;
147 stream
<< "DrawTargetD2D 1.1 (" << this << ")";
151 static uint64_t mVRAMUsageDT
;
152 static uint64_t mVRAMUsageSS
;
155 friend class SourceSurfaceD2D1
;
158 typedef stdext::hash_set
<DrawTargetD2D1
*> TargetSet
;
160 typedef std::unordered_set
<DrawTargetD2D1
*> TargetSet
;
163 // This function will mark the surface as changing, and make sure any
164 // copy-on-write snapshots are notified.
166 void PrepareForDrawing(CompositionOp aOp
, const Pattern
&aPattern
);
167 void FinalizeDrawing(CompositionOp aOp
, const Pattern
&aPattern
);
168 void FlushTransformToDC() {
169 if (mTransformDirty
) {
170 mDC
->SetTransform(D2DMatrix(mTransform
));
171 mTransformDirty
= false;
174 void AddDependencyOnSource(SourceSurfaceD2D1
* aSource
);
176 // This returns the clipped geometry, in addition it returns aClipBounds which
177 // represents the intersection of all pixel-aligned rectangular clips that
178 // are currently set. The returned clipped geometry must be clipped by these
179 // bounds to correctly reflect the total clip. This is in device space.
180 TemporaryRef
<ID2D1Geometry
> GetClippedGeometry(IntRect
*aClipBounds
);
182 bool GetDeviceSpaceClipRect(D2D1_RECT_F
& aClipRect
, bool& aIsPixelAligned
);
185 void PushClipsToDC(ID2D1DeviceContext
*aDC
);
186 void PopClipsFromDC(ID2D1DeviceContext
*aDC
);
188 TemporaryRef
<ID2D1Brush
> CreateTransparentBlackBrush();
189 TemporaryRef
<ID2D1Brush
> CreateBrushForPattern(const Pattern
&aPattern
, Float aAlpha
= 1.0f
);
191 void PushD2DLayer(ID2D1DeviceContext
*aDC
, ID2D1Geometry
*aGeometry
, const D2D1_MATRIX_3X2_F
&aTransform
);
195 RefPtr
<ID3D11Device
> mDevice
;
196 RefPtr
<ID3D11Texture2D
> mTexture
;
197 RefPtr
<ID2D1Geometry
> mCurrentClippedGeometry
;
198 // This is only valid if mCurrentClippedGeometry is non-null. And will
199 // only be the intersection of all pixel-aligned retangular clips. This is in
201 IntRect mCurrentClipBounds
;
202 mutable RefPtr
<ID2D1DeviceContext
> mDC
;
203 RefPtr
<ID2D1Bitmap1
> mBitmap
;
204 RefPtr
<ID2D1Bitmap1
> mTempBitmap
;
205 RefPtr
<ID2D1Effect
> mBlendEffect
;
207 // We store this to prevent excessive SetTextRenderingParams calls.
208 RefPtr
<IDWriteRenderingParams
> mTextRenderingParams
;
210 // List of pushed clips.
215 // If mPath is non-null, the mTransform member will be used, otherwise
216 // the mIsPixelAligned member is valid.
217 D2D1_MATRIX_3X2_F mTransform
;
218 bool mIsPixelAligned
;
220 RefPtr
<PathD2D
> mPath
;
222 std::vector
<PushedClip
> mPushedClips
;
224 // The latest snapshot of this surface. This needs to be told when this
225 // target is modified. We keep it alive as a cache.
226 RefPtr
<SourceSurfaceD2D1
> mSnapshot
;
227 // A list of targets we need to flush when we're modified.
228 TargetSet mDependentTargets
;
229 // A list of targets which have this object in their mDependentTargets set
230 TargetSet mDependingOnTargets
;
232 // True of the current clip stack is pushed to the main RT.
233 bool mClipsArePushed
;
234 static ID2D1Factory1
*mFactory
;
235 static IDWriteFactory
*mDWriteFactory
;
241 #endif /* MOZILLA_GFX_DRAWTARGETD2D_H_ */