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/. */
7 #ifndef _MOZILLA_GFX_DRAWTARGETSKIA_H
8 #define _MOZILLA_GFX_DRAWTARGETSKIA_H
14 #ifdef MOZ_WIDGET_COCOA
15 # include <ApplicationServices/ApplicationServices.h>
24 class RefPtrTraits
<SkSurface
> {
26 static void Release(SkSurface
* aSurface
);
27 static void AddRef(SkSurface
* aSurface
);
32 class DataSourceSurface
;
33 class SourceSurfaceSkia
;
34 class BorrowedCGContext
;
36 class DrawTargetSkia
: public DrawTarget
{
38 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetSkia
, override
)
40 virtual ~DrawTargetSkia();
42 virtual DrawTargetType
GetType() const override
;
43 virtual BackendType
GetBackendType() const override
{
44 return BackendType::SKIA
;
46 already_AddRefed
<SourceSurface
> Snapshot(SurfaceFormat aFormat
);
47 virtual already_AddRefed
<SourceSurface
> Snapshot() override
{
48 return Snapshot(mFormat
);
50 already_AddRefed
<SourceSurface
> GetBackingSurface() override
;
51 virtual IntSize
GetSize() const override
{ return mSize
; };
52 virtual bool LockBits(uint8_t** aData
, IntSize
* aSize
, int32_t* aStride
,
53 SurfaceFormat
* aFormat
,
54 IntPoint
* aOrigin
= nullptr) override
;
55 virtual void ReleaseBits(uint8_t* aData
) override
;
56 virtual void Flush() override
;
57 virtual void DrawSurface(
58 SourceSurface
* aSurface
, const Rect
& aDest
, const Rect
& aSource
,
59 const DrawSurfaceOptions
& aSurfOptions
= DrawSurfaceOptions(),
60 const DrawOptions
& aOptions
= DrawOptions()) override
;
61 virtual void DrawFilter(FilterNode
* aNode
, const Rect
& aSourceRect
,
62 const Point
& aDestPoint
,
63 const DrawOptions
& aOptions
= DrawOptions()) override
;
64 virtual void DrawSurfaceWithShadow(SourceSurface
* aSurface
,
66 const ShadowOptions
& aShadow
,
67 CompositionOp aOperator
) override
;
68 virtual void ClearRect(const Rect
& aRect
) override
;
69 virtual void CopySurface(SourceSurface
* aSurface
, const IntRect
& aSourceRect
,
70 const IntPoint
& aDestination
) override
;
71 virtual void FillRect(const Rect
& aRect
, const Pattern
& aPattern
,
72 const DrawOptions
& aOptions
= DrawOptions()) override
;
73 virtual void StrokeRect(const Rect
& aRect
, const Pattern
& aPattern
,
74 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
75 const DrawOptions
& aOptions
= DrawOptions()) override
;
76 virtual void StrokeLine(const Point
& aStart
, const Point
& aEnd
,
77 const Pattern
& aPattern
,
78 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
79 const DrawOptions
& aOptions
= DrawOptions()) override
;
80 virtual void Stroke(const Path
* aPath
, const Pattern
& aPattern
,
81 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
82 const DrawOptions
& aOptions
= DrawOptions()) override
;
83 virtual void Fill(const Path
* aPath
, const Pattern
& aPattern
,
84 const DrawOptions
& aOptions
= DrawOptions()) override
;
86 virtual void FillGlyphs(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
87 const Pattern
& aPattern
,
88 const DrawOptions
& aOptions
= DrawOptions()) override
;
89 virtual void StrokeGlyphs(
90 ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
, const Pattern
& aPattern
,
91 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
92 const DrawOptions
& aOptions
= DrawOptions()) override
;
93 virtual void Mask(const Pattern
& aSource
, const Pattern
& aMask
,
94 const DrawOptions
& aOptions
= DrawOptions()) override
;
95 virtual void MaskSurface(
96 const Pattern
& aSource
, SourceSurface
* aMask
, Point aOffset
,
97 const DrawOptions
& aOptions
= DrawOptions()) override
;
98 virtual bool Draw3DTransformedSurface(SourceSurface
* aSurface
,
99 const Matrix4x4
& aMatrix
) override
;
100 virtual void PushClip(const Path
* aPath
) override
;
101 virtual void PushClipRect(const Rect
& aRect
) override
;
102 virtual void PushDeviceSpaceClipRects(const IntRect
* aRects
,
103 uint32_t aCount
) override
;
104 virtual void PopClip() override
;
105 virtual bool RemoveAllClips() override
;
106 virtual void PushLayer(bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
107 const Matrix
& aMaskTransform
,
108 const IntRect
& aBounds
= IntRect(),
109 bool aCopyBackground
= false) override
;
110 virtual void PushLayerWithBlend(
111 bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
112 const Matrix
& aMaskTransform
, const IntRect
& aBounds
= IntRect(),
113 bool aCopyBackground
= false,
114 CompositionOp aCompositionOp
= CompositionOp::OP_OVER
) override
;
115 virtual void PopLayer() override
;
116 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromData(
117 unsigned char* aData
, const IntSize
& aSize
, int32_t aStride
,
118 SurfaceFormat aFormat
) const override
;
119 virtual already_AddRefed
<SourceSurface
> OptimizeSourceSurface(
120 SourceSurface
* aSurface
) const override
;
121 virtual already_AddRefed
<SourceSurface
> OptimizeSourceSurfaceForUnknownAlpha(
122 SourceSurface
* aSurface
) const override
;
123 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromNativeSurface(
124 const NativeSurface
& aSurface
) const override
;
125 virtual already_AddRefed
<DrawTarget
> CreateSimilarDrawTarget(
126 const IntSize
& aSize
, SurfaceFormat aFormat
) const override
;
127 virtual bool CanCreateSimilarDrawTarget(const IntSize
& aSize
,
128 SurfaceFormat aFormat
) const override
;
129 virtual RefPtr
<DrawTarget
> CreateClippedDrawTarget(
130 const Rect
& aBounds
, SurfaceFormat aFormat
) override
;
132 virtual already_AddRefed
<PathBuilder
> CreatePathBuilder(
133 FillRule aFillRule
= FillRule::FILL_WINDING
) const override
;
135 virtual already_AddRefed
<GradientStops
> CreateGradientStops(
136 GradientStop
* aStops
, uint32_t aNumStops
,
137 ExtendMode aExtendMode
= ExtendMode::CLAMP
) const override
;
138 virtual already_AddRefed
<FilterNode
> CreateFilter(FilterType aType
) override
;
139 virtual void SetTransform(const Matrix
& aTransform
) override
;
140 virtual void* GetNativeSurface(NativeSurfaceType aType
) override
;
141 virtual void DetachAllSnapshots() override
{ MarkChanged(); }
143 bool Init(const IntSize
& aSize
, SurfaceFormat aFormat
);
144 bool Init(unsigned char* aData
, const IntSize
& aSize
, int32_t aStride
,
145 SurfaceFormat aFormat
, bool aUninitialized
= false);
146 bool Init(SkCanvas
* aCanvas
);
147 bool Init(RefPtr
<DataSourceSurface
>&& aSurface
);
149 // Skia assumes that texture sizes fit in 16-bit signed integers.
150 static size_t GetMaxSurfaceSize() { return 32767; }
152 operator std::string() const {
153 std::stringstream stream
;
154 stream
<< "DrawTargetSkia(" << this << ")";
158 Maybe
<IntRect
> GetDeviceClipRect(bool aAllowComplex
= false) const;
160 Maybe
<Rect
> GetGlyphLocalBounds(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
161 const Pattern
& aPattern
,
162 const StrokeOptions
* aStrokeOptions
,
163 const DrawOptions
& aOptions
);
166 friend class SourceSurfaceSkia
;
168 static void ReleaseMappedSkSurface(void* aPixels
, void* aContext
);
172 void DrawGlyphs(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
173 const Pattern
& aPattern
,
174 const StrokeOptions
* aStrokeOptions
= nullptr,
175 const DrawOptions
& aOptions
= DrawOptions());
178 PushedLayer(bool aOldPermitSubpixelAA
, SourceSurface
* aMask
)
179 : mOldPermitSubpixelAA(aOldPermitSubpixelAA
), mMask(aMask
) {}
180 bool mOldPermitSubpixelAA
;
181 RefPtr
<SourceSurface
> mMask
;
183 std::vector
<PushedLayer
> mPushedLayers
;
186 RefPtr
<SkSurface
> mSurface
;
187 SkCanvas
* mCanvas
= nullptr;
188 RefPtr
<DataSourceSurface
> mBackingSurface
;
189 RefPtr
<SourceSurfaceSkia
> mSnapshot
;
190 Mutex mSnapshotLock MOZ_UNANNOTATED
;
192 #ifdef MOZ_WIDGET_COCOA
193 friend class BorrowedCGContext
;
195 CGContextRef
BorrowCGContext(const DrawOptions
& aOptions
);
196 void ReturnCGContext(CGContextRef
);
197 bool FillGlyphsWithCG(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
198 const Pattern
& aPattern
,
199 const DrawOptions
& aOptions
= DrawOptions());
202 CGColorSpaceRef mColorSpace
;
203 uint8_t* mCanvasData
;
210 } // namespace mozilla
212 #endif // _MOZILLA_GFX_SOURCESURFACESKIA_H