Bumping manifests a=b2g-bump
[gecko.git] / gfx / 2d / DrawTargetD2D1.h
blobeac56c97d17666d9d37f83ded6c305fff1d7baff
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_
9 #include "2D.h"
10 #include <d3d11.h>
11 #include <d2d1_1.h>
12 #include "PathD2D.h"
13 #include "HelpersD2D.h"
15 #include <vector>
16 #include <sstream>
18 #ifdef _MSC_VER
19 #include <hash_set>
20 #else
21 #include <unordered_set>
22 #endif
24 struct IDWriteFactory;
26 namespace mozilla {
27 namespace gfx {
29 class SourceSurfaceD2D1;
30 class GradientStopsD2D;
31 class ScaledFontDWrite;
33 const int32_t kLayerCacheSize1 = 5;
35 class DrawTargetD2D1 : public DrawTarget
37 public:
38 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetD2D1)
39 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; }
47 virtual void Flush();
48 virtual void DrawSurface(SourceSurface *aSurface,
49 const Rect &aDest,
50 const Rect &aSource,
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,
58 const Point &aDest,
59 const Color &aColor,
60 const Point &aOffset,
61 Float aSigma,
62 CompositionOp aOperator);
63 virtual void ClearRect(const Rect &aRect);
64 virtual void MaskSurface(const Pattern &aSource,
65 SourceSurface *aMask,
66 Point aOffset,
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,
81 const Point &aEnd,
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,
98 const Pattern &aMask,
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,
106 int32_t aStride,
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,
120 uint32_t aNumStops,
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) {
134 Matrix mat;
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 << ")";
145 return stream.str();
148 static uint64_t mVRAMUsageDT;
149 static uint64_t mVRAMUsageSS;
151 private:
152 friend class SourceSurfaceD2D1;
154 #ifdef _MSC_VER
155 typedef stdext::hash_set<DrawTargetD2D1*> TargetSet;
156 #else
157 typedef std::unordered_set<DrawTargetD2D1*> TargetSet;
158 #endif
160 // This function will mark the surface as changing, and make sure any
161 // copy-on-write snapshots are notified.
162 void MarkChanged();
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);
173 void PopAllClips();
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);
181 IntSize mSize;
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
187 // device space.
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.
197 struct PushedClip
199 D2D1_RECT_F mBounds;
200 union {
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_ */