/src/mozilla-central/gfx/2d/DrawTargetTiled.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #ifndef MOZILLA_GFX_DRAWTARGETTILED_H_ |
8 | | #define MOZILLA_GFX_DRAWTARGETTILED_H_ |
9 | | |
10 | | #include "2D.h" |
11 | | |
12 | | #include "mozilla/Vector.h" |
13 | | |
14 | | #include "Filters.h" |
15 | | #include "Logging.h" |
16 | | |
17 | | #include <vector> |
18 | | |
19 | | namespace mozilla { |
20 | | namespace gfx { |
21 | | |
22 | | struct TileInternal : public Tile { |
23 | | TileInternal() |
24 | | : mClippedOut(false) |
25 | 0 | {} |
26 | | |
27 | | explicit TileInternal(const Tile& aOther) |
28 | | : Tile(aOther) |
29 | | , mClippedOut(false) |
30 | 0 | {} |
31 | | |
32 | | bool mClippedOut; |
33 | | }; |
34 | | |
35 | | |
36 | | class DrawTargetTiled : public DrawTarget |
37 | | { |
38 | | public: |
39 | | DrawTargetTiled(); |
40 | | |
41 | | bool Init(const TileSet& mTiles); |
42 | | |
43 | 0 | virtual bool IsTiledDrawTarget() const override { return true; } |
44 | | |
45 | 0 | virtual bool IsCaptureDT() const override { return mTiles[0].mDrawTarget->IsCaptureDT(); } |
46 | 0 | virtual DrawTargetType GetType() const override { return mTiles[0].mDrawTarget->GetType(); } |
47 | 0 | virtual BackendType GetBackendType() const override { return mTiles[0].mDrawTarget->GetBackendType(); } |
48 | | virtual already_AddRefed<SourceSurface> Snapshot() override; |
49 | | virtual void DetachAllSnapshots() override; |
50 | 0 | virtual IntSize GetSize() const override { |
51 | 0 | MOZ_ASSERT(mRect.Width() > 0 && mRect.Height() > 0); |
52 | 0 | return IntSize(mRect.XMost(), mRect.YMost()); |
53 | 0 | } |
54 | 0 | virtual IntRect GetRect() const override { |
55 | 0 | return mRect; |
56 | 0 | } |
57 | | |
58 | | virtual void Flush() override; |
59 | | virtual void DrawSurface(SourceSurface *aSurface, |
60 | | const Rect &aDest, |
61 | | const Rect &aSource, |
62 | | const DrawSurfaceOptions &aSurfOptions, |
63 | | const DrawOptions &aOptions) override; |
64 | | virtual void DrawFilter(FilterNode *aNode, |
65 | | const Rect &aSourceRect, |
66 | | const Point &aDestPoint, |
67 | | const DrawOptions &aOptions = DrawOptions()) override; |
68 | | virtual void DrawSurfaceWithShadow(SourceSurface *aSurface, |
69 | | const Point &aDest, |
70 | | const Color &aColor, |
71 | | const Point &aOffset, |
72 | | Float aSigma, |
73 | 0 | CompositionOp aOperator) override { /* Not implemented */ MOZ_CRASH("GFX: DrawSurfaceWithShadow"); } |
74 | | |
75 | | virtual void ClearRect(const Rect &aRect) override; |
76 | | virtual void MaskSurface(const Pattern &aSource, |
77 | | SourceSurface *aMask, |
78 | | Point aOffset, |
79 | | const DrawOptions &aOptions = DrawOptions()) override; |
80 | | |
81 | | virtual void CopySurface(SourceSurface *aSurface, |
82 | | const IntRect &aSourceRect, |
83 | | const IntPoint &aDestination) override; |
84 | | |
85 | | virtual void FillRect(const Rect &aRect, |
86 | | const Pattern &aPattern, |
87 | | const DrawOptions &aOptions = DrawOptions()) override; |
88 | | virtual void StrokeRect(const Rect &aRect, |
89 | | const Pattern &aPattern, |
90 | | const StrokeOptions &aStrokeOptions = StrokeOptions(), |
91 | | const DrawOptions &aOptions = DrawOptions()) override; |
92 | | virtual void StrokeLine(const Point &aStart, |
93 | | const Point &aEnd, |
94 | | const Pattern &aPattern, |
95 | | const StrokeOptions &aStrokeOptions = StrokeOptions(), |
96 | | const DrawOptions &aOptions = DrawOptions()) override; |
97 | | virtual void Stroke(const Path *aPath, |
98 | | const Pattern &aPattern, |
99 | | const StrokeOptions &aStrokeOptions = StrokeOptions(), |
100 | | const DrawOptions &aOptions = DrawOptions()) override; |
101 | | virtual void Fill(const Path *aPath, |
102 | | const Pattern &aPattern, |
103 | | const DrawOptions &aOptions = DrawOptions()) override; |
104 | | virtual void FillGlyphs(ScaledFont *aFont, |
105 | | const GlyphBuffer &aBuffer, |
106 | | const Pattern &aPattern, |
107 | | const DrawOptions &aOptions = DrawOptions()) override; |
108 | | virtual void Mask(const Pattern &aSource, |
109 | | const Pattern &aMask, |
110 | | const DrawOptions &aOptions = DrawOptions()) override; |
111 | | virtual void PushClip(const Path *aPath) override; |
112 | | virtual void PushClipRect(const Rect &aRect) override; |
113 | | virtual void PopClip() override; |
114 | | virtual void PushLayer(bool aOpaque, Float aOpacity, |
115 | | SourceSurface* aMask, |
116 | | const Matrix& aMaskTransform, |
117 | | const IntRect& aBounds = IntRect(), |
118 | | bool aCopyBackground = false) override; |
119 | | virtual void PushLayerWithBlend(bool aOpaque, Float aOpacity, |
120 | | SourceSurface* aMask, |
121 | | const Matrix& aMaskTransform, |
122 | | const IntRect& aBounds = IntRect(), |
123 | | bool aCopyBackground = false, |
124 | | CompositionOp = CompositionOp::OP_OVER) override; |
125 | | virtual void PopLayer() override; |
126 | | |
127 | | virtual void PadEdges(const IntRegion& aRegion) override; |
128 | | |
129 | | virtual void SetTransform(const Matrix &aTransform) override; |
130 | | |
131 | | virtual void SetPermitSubpixelAA(bool aPermitSubpixelAA) override; |
132 | | |
133 | | virtual already_AddRefed<SourceSurface> CreateSourceSurfaceFromData(unsigned char *aData, |
134 | | const IntSize &aSize, |
135 | | int32_t aStride, |
136 | | SurfaceFormat aFormat) const override |
137 | 0 | { |
138 | 0 | return mTiles[0].mDrawTarget->CreateSourceSurfaceFromData(aData, aSize, aStride, aFormat); |
139 | 0 | } |
140 | | virtual already_AddRefed<SourceSurface> OptimizeSourceSurface(SourceSurface *aSurface) const override |
141 | 0 | { |
142 | 0 | return mTiles[0].mDrawTarget->OptimizeSourceSurface(aSurface); |
143 | 0 | } |
144 | | |
145 | | virtual already_AddRefed<SourceSurface> |
146 | | CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const override |
147 | 0 | { |
148 | 0 | return mTiles[0].mDrawTarget->CreateSourceSurfaceFromNativeSurface(aSurface); |
149 | 0 | } |
150 | | |
151 | | virtual already_AddRefed<DrawTarget> |
152 | | CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const override |
153 | 0 | { |
154 | 0 | return mTiles[0].mDrawTarget->CreateSimilarDrawTarget(aSize, aFormat); |
155 | 0 | } |
156 | | |
157 | | virtual already_AddRefed<PathBuilder> CreatePathBuilder(FillRule aFillRule = FillRule::FILL_WINDING) const override |
158 | 0 | { |
159 | 0 | return mTiles[0].mDrawTarget->CreatePathBuilder(aFillRule); |
160 | 0 | } |
161 | | |
162 | | virtual already_AddRefed<GradientStops> |
163 | | CreateGradientStops(GradientStop *aStops, |
164 | | uint32_t aNumStops, |
165 | | ExtendMode aExtendMode = ExtendMode::CLAMP) const override |
166 | 0 | { |
167 | 0 | return mTiles[0].mDrawTarget->CreateGradientStops(aStops, aNumStops, aExtendMode); |
168 | 0 | } |
169 | | virtual already_AddRefed<FilterNode> CreateFilter(FilterType aType) override |
170 | 0 | { |
171 | 0 | return mTiles[0].mDrawTarget->CreateFilter(aType); |
172 | 0 | } |
173 | | |
174 | | private: |
175 | | std::vector<TileInternal> mTiles; |
176 | | |
177 | | // mClippedOutTilesStack[clipIndex][tileIndex] is true if the tile at |
178 | | // tileIndex has become completely clipped out at the clip stack depth |
179 | | // clipIndex. |
180 | | Vector<std::vector<bool>,8> mClippedOutTilesStack; |
181 | | |
182 | | IntRect mRect; |
183 | | |
184 | | struct PushedLayer |
185 | | { |
186 | | explicit PushedLayer(bool aOldPermitSubpixelAA) |
187 | | : mOldPermitSubpixelAA(aOldPermitSubpixelAA) |
188 | 0 | {} |
189 | | bool mOldPermitSubpixelAA; |
190 | | }; |
191 | | std::vector<PushedLayer> mPushedLayers; |
192 | | }; |
193 | | |
194 | | class SnapshotTiled : public SourceSurface |
195 | | { |
196 | | public: |
197 | | SnapshotTiled(const std::vector<TileInternal>& aTiles, const IntRect& aRect) |
198 | | : mRect(aRect) |
199 | 0 | { |
200 | 0 | for (size_t i = 0; i < aTiles.size(); i++) { |
201 | 0 | mSnapshots.push_back(aTiles[i].mDrawTarget->Snapshot()); |
202 | 0 | mOrigins.push_back(aTiles[i].mTileOrigin); |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | 0 | virtual SurfaceType GetType() const override { return SurfaceType::TILED; } |
207 | 0 | virtual IntSize GetSize() const override { |
208 | 0 | MOZ_ASSERT(mRect.Width() > 0 && mRect.Height() > 0); |
209 | 0 | return IntSize(mRect.XMost(), mRect.YMost()); |
210 | 0 | } |
211 | 0 | virtual IntRect GetRect() const override { |
212 | 0 | return mRect; |
213 | 0 | } |
214 | 0 | virtual SurfaceFormat GetFormat() const override { return mSnapshots[0]->GetFormat(); } |
215 | | |
216 | | virtual already_AddRefed<DataSourceSurface> GetDataSurface() override |
217 | 0 | { |
218 | 0 | RefPtr<DataSourceSurface> surf = Factory::CreateDataSourceSurface(mRect.Size(), GetFormat()); |
219 | 0 | if (!surf) { |
220 | 0 | gfxCriticalError() << "DrawTargetTiled::GetDataSurface failed to allocate surface"; |
221 | 0 | return nullptr; |
222 | 0 | } |
223 | 0 |
|
224 | 0 | DataSourceSurface::MappedSurface mappedSurf; |
225 | 0 | if (!surf->Map(DataSourceSurface::MapType::WRITE, &mappedSurf)) { |
226 | 0 | gfxCriticalError() << "DrawTargetTiled::GetDataSurface failed to map surface"; |
227 | 0 | return nullptr; |
228 | 0 | } |
229 | 0 |
|
230 | 0 | { |
231 | 0 | RefPtr<DrawTarget> dt = |
232 | 0 | Factory::CreateDrawTargetForData(BackendType::CAIRO, mappedSurf.mData, |
233 | 0 | mRect.Size(), mappedSurf.mStride, GetFormat()); |
234 | 0 |
|
235 | 0 | if (!dt) { |
236 | 0 | gfxWarning() << "DrawTargetTiled::GetDataSurface failed in CreateDrawTargetForData"; |
237 | 0 | surf->Unmap(); |
238 | 0 | return nullptr; |
239 | 0 | } |
240 | 0 | for (size_t i = 0; i < mSnapshots.size(); i++) { |
241 | 0 | RefPtr<DataSourceSurface> dataSurf = mSnapshots[i]->GetDataSurface(); |
242 | 0 | dt->CopySurface(dataSurf, IntRect(IntPoint(0, 0), mSnapshots[i]->GetSize()), mOrigins[i] - mRect.TopLeft()); |
243 | 0 | } |
244 | 0 | } |
245 | 0 | surf->Unmap(); |
246 | 0 |
|
247 | 0 | return surf.forget(); |
248 | 0 | } |
249 | | |
250 | | std::vector<RefPtr<SourceSurface>> mSnapshots; |
251 | | std::vector<IntPoint> mOrigins; |
252 | | IntRect mRect; |
253 | | }; |
254 | | |
255 | | } // namespace gfx |
256 | | } // namespace mozilla |
257 | | |
258 | | #endif |