Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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/. */
6 #ifndef _MOZILLA_GFX_DRAWTARGET_CAIRO_H_
7 #define _MOZILLA_GFX_DRAWTARGET_CAIRO_H_
9 #include "2D.h"
10 #include "cairo.h"
11 #include "PathCairo.h"
13 #include <vector>
15 namespace mozilla {
16 namespace gfx {
18 class SourceSurfaceCairo;
20 class GradientStopsCairo : public GradientStops
21 {
22 public:
23 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GradientStopsCairo)
24 GradientStopsCairo(GradientStop* aStops, uint32_t aNumStops,
25 ExtendMode aExtendMode)
26 : mExtendMode(aExtendMode)
27 {
28 for (uint32_t i = 0; i < aNumStops; ++i) {
29 mStops.push_back(aStops[i]);
30 }
31 }
33 virtual ~GradientStopsCairo() {}
35 const std::vector<GradientStop>& GetStops() const
36 {
37 return mStops;
38 }
40 ExtendMode GetExtendMode() const
41 {
42 return mExtendMode;
43 }
45 virtual BackendType GetBackendType() const { return BackendType::CAIRO; }
47 private:
48 std::vector<GradientStop> mStops;
49 ExtendMode mExtendMode;
50 };
52 class DrawTargetCairo : public DrawTarget
53 {
54 public:
55 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetCairo)
56 friend class BorrowedCairoContext;
58 DrawTargetCairo();
59 virtual ~DrawTargetCairo();
61 virtual BackendType GetType() const { return BackendType::CAIRO; }
62 virtual TemporaryRef<SourceSurface> Snapshot();
63 virtual IntSize GetSize();
65 virtual void SetPermitSubpixelAA(bool aPermitSubpixelAA);
67 virtual bool LockBits(uint8_t** aData, IntSize* aSize,
68 int32_t* aStride, SurfaceFormat* aFormat);
69 virtual void ReleaseBits(uint8_t* aData);
71 virtual void Flush();
72 virtual void DrawSurface(SourceSurface *aSurface,
73 const Rect &aDest,
74 const Rect &aSource,
75 const DrawSurfaceOptions &aSurfOptions = DrawSurfaceOptions(),
76 const DrawOptions &aOptions = DrawOptions());
77 virtual void DrawFilter(FilterNode *aNode,
78 const Rect &aSourceRect,
79 const Point &aDestPoint,
80 const DrawOptions &aOptions = DrawOptions());
81 virtual void DrawSurfaceWithShadow(SourceSurface *aSurface,
82 const Point &aDest,
83 const Color &aColor,
84 const Point &aOffset,
85 Float aSigma,
86 CompositionOp aOperator);
88 virtual void ClearRect(const Rect &aRect);
90 virtual void CopySurface(SourceSurface *aSurface,
91 const IntRect &aSourceRect,
92 const IntPoint &aDestination);
93 virtual void CopyRect(const IntRect &aSourceRect,
94 const IntPoint &aDestination);
96 virtual void FillRect(const Rect &aRect,
97 const Pattern &aPattern,
98 const DrawOptions &aOptions = DrawOptions());
99 virtual void StrokeRect(const Rect &aRect,
100 const Pattern &aPattern,
101 const StrokeOptions &aStrokeOptions = StrokeOptions(),
102 const DrawOptions &aOptions = DrawOptions());
103 virtual void StrokeLine(const Point &aStart,
104 const Point &aEnd,
105 const Pattern &aPattern,
106 const StrokeOptions &aStrokeOptions = StrokeOptions(),
107 const DrawOptions &aOptions = DrawOptions());
109 virtual void Stroke(const Path *aPath,
110 const Pattern &aPattern,
111 const StrokeOptions &aStrokeOptions = StrokeOptions(),
112 const DrawOptions &aOptions = DrawOptions());
114 virtual void Fill(const Path *aPath,
115 const Pattern &aPattern,
116 const DrawOptions &aOptions = DrawOptions());
118 virtual void FillGlyphs(ScaledFont *aFont,
119 const GlyphBuffer &aBuffer,
120 const Pattern &aPattern,
121 const DrawOptions &aOptions,
122 const GlyphRenderingOptions *aRenderingOptions = nullptr);
123 virtual void Mask(const Pattern &aSource,
124 const Pattern &aMask,
125 const DrawOptions &aOptions = DrawOptions());
126 virtual void MaskSurface(const Pattern &aSource,
127 SourceSurface *aMask,
128 Point aOffset,
129 const DrawOptions &aOptions = DrawOptions());
131 virtual void PushClip(const Path *aPath);
132 virtual void PushClipRect(const Rect &aRect);
133 virtual void PopClip();
135 virtual TemporaryRef<PathBuilder> CreatePathBuilder(FillRule aFillRule = FillRule::FILL_WINDING) const;
137 virtual TemporaryRef<SourceSurface> CreateSourceSurfaceFromData(unsigned char *aData,
138 const IntSize &aSize,
139 int32_t aStride,
140 SurfaceFormat aFormat) const;
141 virtual TemporaryRef<SourceSurface> OptimizeSourceSurface(SourceSurface *aSurface) const;
142 virtual TemporaryRef<SourceSurface>
143 CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const;
144 virtual TemporaryRef<DrawTarget>
145 CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const;
146 virtual TemporaryRef<DrawTarget>
147 CreateShadowDrawTarget(const IntSize &aSize, SurfaceFormat aFormat,
148 float aSigma) const;
150 virtual TemporaryRef<GradientStops>
151 CreateGradientStops(GradientStop *aStops,
152 uint32_t aNumStops,
153 ExtendMode aExtendMode = ExtendMode::CLAMP) const;
155 virtual TemporaryRef<FilterNode> CreateFilter(FilterType aType);
157 virtual void *GetNativeSurface(NativeSurfaceType aType);
159 bool Init(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat = nullptr);
160 bool Init(const IntSize& aSize, SurfaceFormat aFormat);
161 bool Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat);
163 virtual void SetTransform(const Matrix& aTransform);
165 // Call to set up aContext for drawing (with the current transform, etc).
166 // Pass the path you're going to be using if you have one.
167 // Implicitly calls WillChange(aPath).
168 void PrepareForDrawing(cairo_t* aContext, const Path* aPath = nullptr);
170 static cairo_surface_t *GetDummySurface();
172 private: // methods
173 // Init cairo surface without doing a cairo_surface_reference() call.
174 bool InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat = nullptr);
175 enum DrawPatternType { DRAW_FILL, DRAW_STROKE };
176 void DrawPattern(const Pattern& aPattern,
177 const StrokeOptions& aStrokeOptions,
178 const DrawOptions& aOptions,
179 DrawPatternType aDrawType,
180 bool aPathBoundsClip = false);
182 void CopySurfaceInternal(cairo_surface_t* aSurface,
183 const IntRect& aSource,
184 const IntPoint& aDest);
186 Rect GetUserSpaceClip();
188 // Call before you make any changes to the backing surface with which this
189 // context is associated. Pass the path you're going to be using if you have
190 // one.
191 void WillChange(const Path* aPath = nullptr);
193 // Call if there is any reason to disassociate the snapshot from this draw
194 // target; for example, because we're going to be destroyed.
195 void MarkSnapshotIndependent();
197 // If the current operator is "source" then clear the destination before we
198 // draw into it, to simulate the effect of an unbounded source operator.
199 void ClearSurfaceForUnboundedSource(const CompositionOp &aOperator);
200 private: // data
201 cairo_t* mContext;
202 cairo_surface_t* mSurface;
203 IntSize mSize;
205 uint8_t* mLockedBits;
207 // The latest snapshot of this surface. This needs to be told when this
208 // target is modified. We keep it alive as a cache.
209 RefPtr<SourceSurfaceCairo> mSnapshot;
210 static cairo_surface_t *mDummySurface;
211 };
213 }
214 }
216 #endif // _MOZILLA_GFX_DRAWTARGET_CAIRO_H_