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