/src/mozilla-central/gfx/2d/DrawTargetSkia.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_SOURCESURFACESKIA_H |
8 | | #define _MOZILLA_GFX_SOURCESURFACESKIA_H |
9 | | |
10 | | #include "skia/include/core/SkCanvas.h" |
11 | | #include "skia/include/core/SkSurface.h" |
12 | | |
13 | | #include "2D.h" |
14 | | #include "HelpersSkia.h" |
15 | | #include "Rect.h" |
16 | | #include "PathSkia.h" |
17 | | #include <sstream> |
18 | | #include <vector> |
19 | | |
20 | | #ifdef MOZ_WIDGET_COCOA |
21 | | #include <ApplicationServices/ApplicationServices.h> |
22 | | #endif |
23 | | |
24 | | namespace mozilla { |
25 | | namespace gfx { |
26 | | |
27 | | class SourceSurfaceSkia; |
28 | | class BorrowedCGContext; |
29 | | |
30 | | class DrawTargetSkia : public DrawTarget |
31 | | { |
32 | | public: |
33 | | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetSkia, override) |
34 | | DrawTargetSkia(); |
35 | | virtual ~DrawTargetSkia(); |
36 | | |
37 | | virtual DrawTargetType GetType() const override; |
38 | 0 | virtual BackendType GetBackendType() const override { return BackendType::SKIA; } |
39 | | virtual already_AddRefed<SourceSurface> Snapshot() override; |
40 | 0 | virtual IntSize GetSize() const override { return mSize; }; |
41 | | virtual bool LockBits(uint8_t** aData, IntSize* aSize, |
42 | | int32_t* aStride, SurfaceFormat* aFormat, |
43 | | IntPoint* aOrigin = nullptr) override; |
44 | | virtual void ReleaseBits(uint8_t* aData) override; |
45 | | virtual void Flush() override; |
46 | | virtual void DrawSurface(SourceSurface *aSurface, |
47 | | const Rect &aDest, |
48 | | const Rect &aSource, |
49 | | const DrawSurfaceOptions &aSurfOptions = DrawSurfaceOptions(), |
50 | | const DrawOptions &aOptions = DrawOptions()) override; |
51 | | virtual void DrawFilter(FilterNode *aNode, |
52 | | const Rect &aSourceRect, |
53 | | const Point &aDestPoint, |
54 | | const DrawOptions &aOptions = DrawOptions()) override; |
55 | | virtual void DrawSurfaceWithShadow(SourceSurface *aSurface, |
56 | | const Point &aDest, |
57 | | const Color &aColor, |
58 | | const Point &aOffset, |
59 | | Float aSigma, |
60 | | CompositionOp aOperator) override; |
61 | | virtual void ClearRect(const Rect &aRect) override; |
62 | | virtual void CopySurface(SourceSurface *aSurface, |
63 | | const IntRect &aSourceRect, |
64 | | const IntPoint &aDestination) override; |
65 | | virtual void FillRect(const Rect &aRect, |
66 | | const Pattern &aPattern, |
67 | | const DrawOptions &aOptions = DrawOptions()) override; |
68 | | virtual void StrokeRect(const Rect &aRect, |
69 | | const Pattern &aPattern, |
70 | | const StrokeOptions &aStrokeOptions = StrokeOptions(), |
71 | | const DrawOptions &aOptions = DrawOptions()) override; |
72 | | virtual void StrokeLine(const Point &aStart, |
73 | | const Point &aEnd, |
74 | | const Pattern &aPattern, |
75 | | const StrokeOptions &aStrokeOptions = StrokeOptions(), |
76 | | const DrawOptions &aOptions = DrawOptions()) override; |
77 | | virtual void Stroke(const Path *aPath, |
78 | | const Pattern &aPattern, |
79 | | const StrokeOptions &aStrokeOptions = StrokeOptions(), |
80 | | const DrawOptions &aOptions = DrawOptions()) override; |
81 | | virtual void Fill(const Path *aPath, |
82 | | const Pattern &aPattern, |
83 | | const DrawOptions &aOptions = DrawOptions()) override; |
84 | | |
85 | | virtual void FillGlyphs(ScaledFont *aFont, |
86 | | const GlyphBuffer &aBuffer, |
87 | | const Pattern &aPattern, |
88 | | const DrawOptions &aOptions = DrawOptions()) override; |
89 | | virtual void StrokeGlyphs(ScaledFont* aFont, |
90 | | const GlyphBuffer& aBuffer, |
91 | | const Pattern& aPattern, |
92 | | const StrokeOptions& aStrokeOptions = StrokeOptions(), |
93 | | const DrawOptions& aOptions = DrawOptions()) override; |
94 | | virtual void Mask(const Pattern &aSource, |
95 | | const Pattern &aMask, |
96 | | const DrawOptions &aOptions = DrawOptions()) override; |
97 | | virtual void MaskSurface(const Pattern &aSource, |
98 | | SourceSurface *aMask, |
99 | | Point aOffset, |
100 | | const DrawOptions &aOptions = DrawOptions()) override; |
101 | | virtual bool Draw3DTransformedSurface(SourceSurface* aSurface, |
102 | | const Matrix4x4& aMatrix) override; |
103 | | virtual void PushClip(const Path *aPath) override; |
104 | | virtual void PushClipRect(const Rect& aRect) override; |
105 | | virtual void PushDeviceSpaceClipRects(const IntRect* aRects, uint32_t aCount) override; |
106 | | virtual void PopClip() override; |
107 | | virtual void PushLayer(bool aOpaque, Float aOpacity, |
108 | | SourceSurface* aMask, |
109 | | const Matrix& aMaskTransform, |
110 | | const IntRect& aBounds = IntRect(), |
111 | | bool aCopyBackground = false) override; |
112 | | virtual void PushLayerWithBlend(bool aOpaque, Float aOpacity, |
113 | | SourceSurface* aMask, |
114 | | const Matrix& aMaskTransform, |
115 | | const IntRect& aBounds = IntRect(), |
116 | | bool aCopyBackground = false, |
117 | | CompositionOp aCompositionOp = CompositionOp::OP_OVER) override; |
118 | | virtual void PopLayer() override; |
119 | | virtual already_AddRefed<SourceSurface> CreateSourceSurfaceFromData(unsigned char *aData, |
120 | | const IntSize &aSize, |
121 | | int32_t aStride, |
122 | | SurfaceFormat aFormat) const override; |
123 | | virtual already_AddRefed<SourceSurface> OptimizeSourceSurface(SourceSurface *aSurface) const override; |
124 | | virtual already_AddRefed<SourceSurface> OptimizeSourceSurfaceForUnknownAlpha(SourceSurface *aSurface) const override; |
125 | | virtual already_AddRefed<SourceSurface> |
126 | | CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const override; |
127 | | virtual already_AddRefed<DrawTarget> |
128 | | CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const override; |
129 | | virtual already_AddRefed<PathBuilder> CreatePathBuilder(FillRule aFillRule = FillRule::FILL_WINDING) const override; |
130 | | virtual already_AddRefed<GradientStops> CreateGradientStops(GradientStop *aStops, uint32_t aNumStops, ExtendMode aExtendMode = ExtendMode::CLAMP) const override; |
131 | | virtual already_AddRefed<FilterNode> CreateFilter(FilterType aType) override; |
132 | | virtual void SetTransform(const Matrix &aTransform) override; |
133 | | virtual void *GetNativeSurface(NativeSurfaceType aType) override; |
134 | 0 | virtual void DetachAllSnapshots() override { MarkChanged(); } |
135 | | |
136 | | bool Init(const IntSize &aSize, SurfaceFormat aFormat); |
137 | | bool Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat, bool aUninitialized = false); |
138 | | bool Init(SkCanvas* aCanvas); |
139 | | |
140 | | #ifdef USE_SKIA_GPU |
141 | | bool InitWithGrContext(GrContext* aGrContext, |
142 | | const IntSize &aSize, |
143 | | SurfaceFormat aFormat, |
144 | | bool aCached); |
145 | | virtual bool |
146 | | InitWithGrContext(GrContext* aGrContext, |
147 | | const IntSize &aSize, |
148 | 0 | SurfaceFormat aFormat) override { |
149 | 0 | return InitWithGrContext(aGrContext, aSize, aFormat, false); |
150 | 0 | } |
151 | | |
152 | | already_AddRefed<SourceSurface> OptimizeGPUSourceSurface(SourceSurface *aSurface) const; |
153 | | #endif |
154 | | |
155 | | // Skia assumes that texture sizes fit in 16-bit signed integers. |
156 | 0 | static size_t GetMaxSurfaceSize() { |
157 | 0 | return 32767; |
158 | 0 | } |
159 | | |
160 | 0 | operator std::string() const { |
161 | 0 | std::stringstream stream; |
162 | 0 | stream << "DrawTargetSkia(" << this << ")"; |
163 | 0 | return stream.str(); |
164 | 0 | } |
165 | | |
166 | | private: |
167 | | friend class SourceSurfaceSkia; |
168 | | |
169 | | void MarkChanged(); |
170 | | |
171 | | bool ShouldLCDRenderText(FontType aFontType, AntialiasMode aAntialiasMode); |
172 | | |
173 | | void DrawGlyphs(ScaledFont* aFont, |
174 | | const GlyphBuffer& aBuffer, |
175 | | const Pattern& aPattern, |
176 | | const StrokeOptions* aStrokeOptions = nullptr, |
177 | | const DrawOptions& aOptions = DrawOptions()); |
178 | | |
179 | | bool UsingSkiaGPU() const; |
180 | | |
181 | | struct PushedLayer |
182 | | { |
183 | | PushedLayer(bool aOldPermitSubpixelAA, |
184 | | SourceSurface* aMask) |
185 | | : mOldPermitSubpixelAA(aOldPermitSubpixelAA), |
186 | | mMask(aMask) |
187 | 0 | {} |
188 | | bool mOldPermitSubpixelAA; |
189 | | RefPtr<SourceSurface> mMask; |
190 | | }; |
191 | | std::vector<PushedLayer> mPushedLayers; |
192 | | |
193 | | #ifdef USE_SKIA_GPU |
194 | | sk_sp<GrContext> mGrContext; |
195 | | #endif |
196 | | |
197 | | IntSize mSize; |
198 | | sk_sp<SkSurface> mSurface; |
199 | | SkCanvas* mCanvas; |
200 | | RefPtr<SourceSurfaceSkia> mSnapshot; |
201 | | Mutex mSnapshotLock; |
202 | | |
203 | | #ifdef MOZ_WIDGET_COCOA |
204 | | friend class BorrowedCGContext; |
205 | | |
206 | | CGContextRef BorrowCGContext(const DrawOptions &aOptions); |
207 | | void ReturnCGContext(CGContextRef); |
208 | | bool FillGlyphsWithCG(ScaledFont* aFont, |
209 | | const GlyphBuffer& aBuffer, |
210 | | const Pattern& aPattern, |
211 | | const DrawOptions& aOptions = DrawOptions()); |
212 | | |
213 | | CGContextRef mCG; |
214 | | CGColorSpaceRef mColorSpace; |
215 | | uint8_t* mCanvasData; |
216 | | IntSize mCGSize; |
217 | | bool mNeedLayer; |
218 | | #endif |
219 | | }; |
220 | | |
221 | | } // namespace gfx |
222 | | } // namespace mozilla |
223 | | |
224 | | #endif // _MOZILLA_GFX_SOURCESURFACESKIA_H |