Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/2d/DrawTargetCairo.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_DRAWTARGET_CAIRO_H_
8
#define _MOZILLA_GFX_DRAWTARGET_CAIRO_H_
9
10
#include "2D.h"
11
#include "cairo.h"
12
#include "PathCairo.h"
13
14
#include <vector>
15
16
namespace mozilla {
17
namespace gfx {
18
19
class SourceSurfaceCairo;
20
21
class GradientStopsCairo : public GradientStops
22
{
23
  public:
24
    MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GradientStopsCairo, override)
25
26
    GradientStopsCairo(GradientStop* aStops, uint32_t aNumStops,
27
                       ExtendMode aExtendMode)
28
     : mExtendMode(aExtendMode)
29
0
    {
30
0
      for (uint32_t i = 0; i < aNumStops; ++i) {
31
0
        mStops.push_back(aStops[i]);
32
0
      }
33
0
    }
34
35
0
    virtual ~GradientStopsCairo() {}
36
37
    const std::vector<GradientStop>& GetStops() const
38
0
    {
39
0
      return mStops;
40
0
    }
41
42
    ExtendMode GetExtendMode() const
43
0
    {
44
0
      return mExtendMode;
45
0
    }
46
47
0
    virtual BackendType GetBackendType() const override { return BackendType::CAIRO; }
48
49
  private:
50
    std::vector<GradientStop> mStops;
51
    ExtendMode mExtendMode;
52
};
53
54
class DrawTargetCairo final : public DrawTarget
55
{
56
public:
57
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetCairo, override)
58
  friend class BorrowedCairoContext;
59
  friend class BorrowedXlibDrawable;
60
61
  DrawTargetCairo();
62
  virtual ~DrawTargetCairo();
63
64
  virtual bool IsValid() const override;
65
  virtual DrawTargetType GetType() const override;
66
0
  virtual BackendType GetBackendType() const override { return BackendType::CAIRO; }
67
  virtual already_AddRefed<SourceSurface> Snapshot() override;
68
  virtual IntSize GetSize() const override;
69
70
  virtual bool IsCurrentGroupOpaque() override;
71
72
  virtual void SetPermitSubpixelAA(bool aPermitSubpixelAA) override;
73
74
  virtual bool LockBits(uint8_t** aData, IntSize* aSize,
75
                        int32_t* aStride, SurfaceFormat* aFormat,
76
                        IntPoint* aOrigin = nullptr) override;
77
  virtual void ReleaseBits(uint8_t* aData) override;
78
79
  virtual void Flush() override;
80
  virtual void DrawSurface(SourceSurface *aSurface,
81
                           const Rect &aDest,
82
                           const Rect &aSource,
83
                           const DrawSurfaceOptions &aSurfOptions = DrawSurfaceOptions(),
84
                           const DrawOptions &aOptions = DrawOptions()) override;
85
  virtual void DrawFilter(FilterNode *aNode,
86
                          const Rect &aSourceRect,
87
                          const Point &aDestPoint,
88
                          const DrawOptions &aOptions = DrawOptions()) override;
89
  virtual void DrawSurfaceWithShadow(SourceSurface *aSurface,
90
                                     const Point &aDest,
91
                                     const Color &aColor,
92
                                     const Point &aOffset,
93
                                     Float aSigma,
94
                                     CompositionOp aOperator) override;
95
96
  virtual void ClearRect(const Rect &aRect) override;
97
98
  virtual void CopySurface(SourceSurface *aSurface,
99
                           const IntRect &aSourceRect,
100
                           const IntPoint &aDestination) override;
101
  virtual void CopyRect(const IntRect &aSourceRect,
102
                        const IntPoint &aDestination) override;
103
104
  virtual void FillRect(const Rect &aRect,
105
                        const Pattern &aPattern,
106
                        const DrawOptions &aOptions = DrawOptions()) override;
107
  virtual void StrokeRect(const Rect &aRect,
108
                          const Pattern &aPattern,
109
                          const StrokeOptions &aStrokeOptions = StrokeOptions(),
110
                          const DrawOptions &aOptions = DrawOptions()) override;
111
  virtual void StrokeLine(const Point &aStart,
112
                          const Point &aEnd,
113
                          const Pattern &aPattern,
114
                          const StrokeOptions &aStrokeOptions = StrokeOptions(),
115
                          const DrawOptions &aOptions = DrawOptions()) override;
116
117
  virtual void Stroke(const Path *aPath,
118
                      const Pattern &aPattern,
119
                      const StrokeOptions &aStrokeOptions = StrokeOptions(),
120
                      const DrawOptions &aOptions = DrawOptions()) override;
121
122
  virtual void Fill(const Path *aPath,
123
                    const Pattern &aPattern,
124
                    const DrawOptions &aOptions = DrawOptions()) override;
125
126
  virtual void FillGlyphs(ScaledFont *aFont,
127
                          const GlyphBuffer &aBuffer,
128
                          const Pattern &aPattern,
129
                          const DrawOptions &aOptions) override;
130
  virtual void Mask(const Pattern &aSource,
131
                    const Pattern &aMask,
132
                    const DrawOptions &aOptions = DrawOptions()) override;
133
  virtual void MaskSurface(const Pattern &aSource,
134
                           SourceSurface *aMask,
135
                           Point aOffset,
136
                           const DrawOptions &aOptions = DrawOptions()) override;
137
138
  virtual bool Draw3DTransformedSurface(SourceSurface* aSurface,
139
                                        const Matrix4x4& aMatrix) override;
140
141
  virtual void PushClip(const Path *aPath) override;
142
  virtual void PushClipRect(const Rect &aRect) override;
143
  virtual void PopClip() override;
144
  virtual void PushLayer(bool aOpaque, Float aOpacity,
145
                         SourceSurface* aMask,
146
                         const Matrix& aMaskTransform,
147
                         const IntRect& aBounds = IntRect(),
148
                         bool aCopyBackground = false) override;
149
  virtual void PopLayer() override;
150
151
  virtual already_AddRefed<PathBuilder> CreatePathBuilder(FillRule aFillRule = FillRule::FILL_WINDING) const override;
152
153
  virtual already_AddRefed<SourceSurface> CreateSourceSurfaceFromData(unsigned char *aData,
154
                                                            const IntSize &aSize,
155
                                                            int32_t aStride,
156
                                                            SurfaceFormat aFormat) const override;
157
  virtual already_AddRefed<SourceSurface> OptimizeSourceSurface(SourceSurface *aSurface) const override;
158
  virtual already_AddRefed<SourceSurface>
159
    CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const override;
160
  virtual already_AddRefed<DrawTarget>
161
    CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const override;
162
  virtual already_AddRefed<DrawTarget>
163
    CreateShadowDrawTarget(const IntSize &aSize, SurfaceFormat aFormat,
164
                           float aSigma) const override;
165
166
  virtual already_AddRefed<GradientStops>
167
    CreateGradientStops(GradientStop *aStops,
168
                        uint32_t aNumStops,
169
                        ExtendMode aExtendMode = ExtendMode::CLAMP) const override;
170
171
  virtual already_AddRefed<FilterNode> CreateFilter(FilterType aType) override;
172
173
  virtual void GetGlyphRasterizationMetrics(ScaledFont *aScaledFont, const uint16_t* aGlyphIndices,
174
                                            uint32_t aNumGlyphs, GlyphMetrics* aGlyphMetrics) override;
175
176
  virtual void *GetNativeSurface(NativeSurfaceType aType) override;
177
178
  bool Init(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat = nullptr);
179
  bool Init(const IntSize& aSize, SurfaceFormat aFormat);
180
  bool Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat);
181
182
  virtual void SetTransform(const Matrix& aTransform) override;
183
184
0
  virtual void DetachAllSnapshots() override { MarkSnapshotIndependent(); }
185
186
  // Call to set up aContext for drawing (with the current transform, etc).
187
  // Pass the path you're going to be using if you have one.
188
  // Implicitly calls WillChange(aPath).
189
  void PrepareForDrawing(cairo_t* aContext, const Path* aPath = nullptr);
190
191
  static cairo_surface_t *GetDummySurface();
192
193
  // Cairo hardcodes this as its maximum surface size.
194
0
  static size_t GetMaxSurfaceSize() {
195
0
    return 32767;
196
0
  }
197
198
private: // methods
199
  // Init cairo surface without doing a cairo_surface_reference() call.
200
  bool InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat = nullptr);
201
  enum DrawPatternType { DRAW_FILL, DRAW_STROKE };
202
  void DrawPattern(const Pattern& aPattern,
203
                   const StrokeOptions& aStrokeOptions,
204
                   const DrawOptions& aOptions,
205
                   DrawPatternType aDrawType,
206
                   bool aPathBoundsClip = false);
207
208
  void CopySurfaceInternal(cairo_surface_t* aSurface,
209
                           const IntRect& aSource,
210
                           const IntPoint& aDest);
211
212
  Rect GetUserSpaceClip();
213
214
  // Call before you make any changes to the backing surface with which this
215
  // context is associated. Pass the path you're going to be using if you have
216
  // one.
217
  void WillChange(const Path* aPath = nullptr);
218
219
  // Call if there is any reason to disassociate the snapshot from this draw
220
  // target; for example, because we're going to be destroyed.
221
  void MarkSnapshotIndependent();
222
223
  // If the current operator is "source" then clear the destination before we
224
  // draw into it, to simulate the effect of an unbounded source operator.
225
  void ClearSurfaceForUnboundedSource(const CompositionOp &aOperator);
226
227
  // Set the Cairo context font options according to the current draw target
228
  // font state.
229
  void SetFontOptions();
230
231
private: // data
232
  cairo_t* mContext;
233
  cairo_surface_t* mSurface;
234
  IntSize mSize;
235
  bool mTransformSingular;
236
237
  uint8_t* mLockedBits;
238
239
  cairo_font_options_t* mFontOptions;
240
241
  struct PushedLayer
242
  {
243
    PushedLayer(Float aOpacity, bool aWasPermittingSubpixelAA)
244
      : mOpacity(aOpacity)
245
      , mMaskPattern(nullptr)
246
      , mWasPermittingSubpixelAA(aWasPermittingSubpixelAA)
247
0
    {}
248
    Float mOpacity;
249
    cairo_pattern_t* mMaskPattern;
250
    bool mWasPermittingSubpixelAA;
251
  };
252
  std::vector<PushedLayer> mPushedLayers;
253
254
  // The latest snapshot of this surface. This needs to be told when this
255
  // target is modified. We keep it alive as a cache.
256
  RefPtr<SourceSurfaceCairo> mSnapshot;
257
  static cairo_surface_t *mDummySurface;
258
};
259
260
} // namespace gfx
261
} // namespace mozilla
262
263
#endif // _MOZILLA_GFX_DRAWTARGET_CAIRO_H_