|
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_DrawTargetCG_h |
|
7 #define mozilla_gfx_DrawTargetCG_h |
|
8 |
|
9 #include <ApplicationServices/ApplicationServices.h> |
|
10 |
|
11 #include "2D.h" |
|
12 #include "Rect.h" |
|
13 #include "PathCG.h" |
|
14 #include "SourceSurfaceCG.h" |
|
15 #include "GLDefs.h" |
|
16 #include "Tools.h" |
|
17 |
|
18 namespace mozilla { |
|
19 namespace gfx { |
|
20 |
|
21 static inline CGAffineTransform |
|
22 GfxMatrixToCGAffineTransform(Matrix m) |
|
23 { |
|
24 CGAffineTransform t; |
|
25 t.a = m._11; |
|
26 t.b = m._12; |
|
27 t.c = m._21; |
|
28 t.d = m._22; |
|
29 t.tx = m._31; |
|
30 t.ty = m._32; |
|
31 return t; |
|
32 } |
|
33 |
|
34 static inline Rect |
|
35 CGRectToRect(CGRect rect) |
|
36 { |
|
37 return Rect(rect.origin.x, |
|
38 rect.origin.y, |
|
39 rect.size.width, |
|
40 rect.size.height); |
|
41 } |
|
42 |
|
43 static inline Point |
|
44 CGPointToPoint(CGPoint point) |
|
45 { |
|
46 return Point(point.x, point.y); |
|
47 } |
|
48 |
|
49 static inline void |
|
50 SetStrokeOptions(CGContextRef cg, const StrokeOptions &aStrokeOptions) |
|
51 { |
|
52 switch (aStrokeOptions.mLineCap) |
|
53 { |
|
54 case CapStyle::BUTT: |
|
55 CGContextSetLineCap(cg, kCGLineCapButt); |
|
56 break; |
|
57 case CapStyle::ROUND: |
|
58 CGContextSetLineCap(cg, kCGLineCapRound); |
|
59 break; |
|
60 case CapStyle::SQUARE: |
|
61 CGContextSetLineCap(cg, kCGLineCapSquare); |
|
62 break; |
|
63 } |
|
64 |
|
65 switch (aStrokeOptions.mLineJoin) |
|
66 { |
|
67 case JoinStyle::BEVEL: |
|
68 CGContextSetLineJoin(cg, kCGLineJoinBevel); |
|
69 break; |
|
70 case JoinStyle::ROUND: |
|
71 CGContextSetLineJoin(cg, kCGLineJoinRound); |
|
72 break; |
|
73 case JoinStyle::MITER: |
|
74 case JoinStyle::MITER_OR_BEVEL: |
|
75 CGContextSetLineJoin(cg, kCGLineJoinMiter); |
|
76 break; |
|
77 } |
|
78 |
|
79 CGContextSetLineWidth(cg, aStrokeOptions.mLineWidth); |
|
80 CGContextSetMiterLimit(cg, aStrokeOptions.mMiterLimit); |
|
81 |
|
82 // XXX: rename mDashLength to dashLength |
|
83 if (aStrokeOptions.mDashLength > 0) { |
|
84 // we use a regular array instead of a std::vector here because we don't want to leak the <vector> include |
|
85 CGFloat *dashes = new CGFloat[aStrokeOptions.mDashLength]; |
|
86 for (size_t i=0; i<aStrokeOptions.mDashLength; i++) { |
|
87 dashes[i] = aStrokeOptions.mDashPattern[i]; |
|
88 } |
|
89 CGContextSetLineDash(cg, aStrokeOptions.mDashOffset, dashes, aStrokeOptions.mDashLength); |
|
90 delete[] dashes; |
|
91 } |
|
92 } |
|
93 |
|
94 |
|
95 class DrawTargetCG : public DrawTarget |
|
96 { |
|
97 public: |
|
98 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetCG) |
|
99 friend class BorrowedCGContext; |
|
100 DrawTargetCG(); |
|
101 virtual ~DrawTargetCG(); |
|
102 |
|
103 virtual BackendType GetType() const; |
|
104 virtual TemporaryRef<SourceSurface> Snapshot(); |
|
105 |
|
106 virtual void DrawSurface(SourceSurface *aSurface, |
|
107 const Rect &aDest, |
|
108 const Rect &aSource, |
|
109 const DrawSurfaceOptions &aSurfOptions = DrawSurfaceOptions(), |
|
110 const DrawOptions &aOptions = DrawOptions()); |
|
111 virtual void DrawFilter(FilterNode *aNode, |
|
112 const Rect &aSourceRect, |
|
113 const Point &aDestPoint, |
|
114 const DrawOptions &aOptions = DrawOptions()); |
|
115 virtual void MaskSurface(const Pattern &aSource, |
|
116 SourceSurface *aMask, |
|
117 Point aOffset, |
|
118 const DrawOptions &aOptions = DrawOptions()); |
|
119 |
|
120 virtual void FillRect(const Rect &aRect, |
|
121 const Pattern &aPattern, |
|
122 const DrawOptions &aOptions = DrawOptions()); |
|
123 |
|
124 |
|
125 //XXX: why do we take a reference to SurfaceFormat? |
|
126 bool Init(BackendType aType, const IntSize &aSize, SurfaceFormat&); |
|
127 bool Init(BackendType aType, unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat); |
|
128 bool Init(CGContextRef cgContext, const IntSize &aSize); |
|
129 |
|
130 // Flush if using IOSurface context |
|
131 virtual void Flush(); |
|
132 |
|
133 virtual void DrawSurfaceWithShadow(SourceSurface *, const Point &, const Color &, const Point &, Float, CompositionOp); |
|
134 virtual void ClearRect(const Rect &); |
|
135 virtual void CopySurface(SourceSurface *, const IntRect&, const IntPoint&); |
|
136 virtual void StrokeRect(const Rect &, const Pattern &, const StrokeOptions&, const DrawOptions&); |
|
137 virtual void StrokeLine(const Point &, const Point &, const Pattern &, const StrokeOptions &, const DrawOptions &); |
|
138 virtual void Stroke(const Path *, const Pattern &, const StrokeOptions &, const DrawOptions &); |
|
139 virtual void Fill(const Path *, const Pattern &, const DrawOptions &); |
|
140 virtual void FillGlyphs(ScaledFont *, const GlyphBuffer&, const Pattern &, const DrawOptions &, const GlyphRenderingOptions *); |
|
141 virtual void Mask(const Pattern &aSource, |
|
142 const Pattern &aMask, |
|
143 const DrawOptions &aOptions = DrawOptions()); |
|
144 virtual void PushClip(const Path *); |
|
145 virtual void PushClipRect(const Rect &aRect); |
|
146 virtual void PopClip(); |
|
147 virtual TemporaryRef<SourceSurface> CreateSourceSurfaceFromNativeSurface(const NativeSurface&) const { return nullptr;} |
|
148 virtual TemporaryRef<DrawTarget> CreateSimilarDrawTarget(const IntSize &, SurfaceFormat) const; |
|
149 virtual TemporaryRef<PathBuilder> CreatePathBuilder(FillRule) const; |
|
150 virtual TemporaryRef<GradientStops> CreateGradientStops(GradientStop *, uint32_t, |
|
151 ExtendMode aExtendMode = ExtendMode::CLAMP) const; |
|
152 virtual TemporaryRef<FilterNode> CreateFilter(FilterType aType); |
|
153 |
|
154 virtual void *GetNativeSurface(NativeSurfaceType); |
|
155 |
|
156 virtual IntSize GetSize() { return mSize; } |
|
157 |
|
158 |
|
159 /* This is for creating good compatible surfaces */ |
|
160 virtual TemporaryRef<SourceSurface> CreateSourceSurfaceFromData(unsigned char *aData, |
|
161 const IntSize &aSize, |
|
162 int32_t aStride, |
|
163 SurfaceFormat aFormat) const; |
|
164 virtual TemporaryRef<SourceSurface> OptimizeSourceSurface(SourceSurface *aSurface) const; |
|
165 CGContextRef GetCGContext() { |
|
166 return mCg; |
|
167 } |
|
168 private: |
|
169 void MarkChanged(); |
|
170 |
|
171 IntSize mSize; |
|
172 CGColorSpaceRef mColorSpace; |
|
173 CGContextRef mCg; |
|
174 |
|
175 /** |
|
176 * The image buffer, if the buffer is owned by this class. |
|
177 * If the DrawTarget was created for a pre-existing buffer or if the buffer's |
|
178 * lifetime is managed by CoreGraphics, mData will be null. |
|
179 * Data owned by DrawTargetCG will be deallocated in the destructor. |
|
180 */ |
|
181 AlignedArray<uint8_t> mData; |
|
182 |
|
183 RefPtr<SourceSurfaceCGContext> mSnapshot; |
|
184 }; |
|
185 |
|
186 } |
|
187 } |
|
188 |
|
189 #endif |
|
190 |