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_DRAWTARGETRECORDING_H_
8 #define MOZILLA_GFX_DRAWTARGETRECORDING_H_
11 #include "DrawEventRecorder.h"
15 class CanvasDrawEventRecorder
;
16 struct RemoteTextureOwnerId
;
21 class DrawTargetRecording
: public DrawTarget
{
23 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetRecording
, override
)
24 DrawTargetRecording(DrawEventRecorder
* aRecorder
, DrawTarget
* aDT
,
25 IntRect aRect
, bool aHasData
= false);
26 DrawTargetRecording(layers::CanvasDrawEventRecorder
* aRecorder
,
28 const layers::RemoteTextureOwnerId
& aTextureOwnerId
,
29 DrawTarget
* aDT
, const IntSize
& aSize
);
31 ~DrawTargetRecording();
33 virtual DrawTargetType
GetType() const override
{
34 return mFinalDT
->GetType();
36 virtual BackendType
GetBackendType() const override
{
37 return BackendType::RECORDING
;
39 virtual bool IsRecording() const override
{ return true; }
41 virtual void Link(const char* aDestination
, const Rect
& aRect
) override
;
42 virtual void Destination(const char* aDestination
,
43 const Point
& aPoint
) override
;
45 virtual already_AddRefed
<SourceSurface
> Snapshot() override
;
46 virtual already_AddRefed
<SourceSurface
> IntoLuminanceSource(
47 LuminanceType aLuminanceType
, float aOpacity
) override
;
49 virtual void DetachAllSnapshots() override
;
51 virtual IntSize
GetSize() const override
{ return mRect
.Size(); }
52 virtual IntRect
GetRect() const override
{ return mRect
; }
54 virtual void Flush() override
;
56 virtual void FlushItem(const IntRect
& aBounds
) override
;
59 * Draw a surface to the draw target. Possibly doing partial drawing or
60 * applying scaling. No sampling happens outside the source.
62 * aSurface Source surface to draw
63 * aDest Destination rectangle that this drawing operation should draw to
64 * aSource Source rectangle in aSurface coordinates, this area of aSurface
65 * will be stretched to the size of aDest.
66 * aOptions General draw options that are applied to the operation
67 * aSurfOptions DrawSurface options that are applied
69 virtual void DrawSurface(
70 SourceSurface
* aSurface
, const Rect
& aDest
, const Rect
& aSource
,
71 const DrawSurfaceOptions
& aSurfOptions
= DrawSurfaceOptions(),
72 const DrawOptions
& aOptions
= DrawOptions()) override
;
74 virtual void DrawDependentSurface(uint64_t aId
, const Rect
& aDest
) override
;
76 virtual void DrawFilter(FilterNode
* aNode
, const Rect
& aSourceRect
,
77 const Point
& aDestPoint
,
78 const DrawOptions
& aOptions
= DrawOptions()) override
;
80 virtual void DrawSurfaceWithShadow(SourceSurface
* aSurface
,
82 const ShadowOptions
& aShadow
,
83 CompositionOp aOperator
) override
;
85 virtual void DrawShadow(const Path
* aPath
, const Pattern
& aPattern
,
86 const ShadowOptions
& aShadow
,
87 const DrawOptions
& aOptions
,
88 const StrokeOptions
* aStrokeOptions
) override
;
91 * Clear a rectangle on the draw target to transparent black. This will
92 * respect the clipping region and transform.
94 * aRect Rectangle to clear
96 virtual void ClearRect(const Rect
& aRect
) override
;
99 * This is essentially a 'memcpy' between two surfaces. It moves a pixel
100 * aligned area from the source surface unscaled directly onto the
101 * drawtarget. This ignores both transform and clip.
103 * aSurface Surface to copy from
104 * aSourceRect Source rectangle to be copied
105 * aDest Destination point to copy the surface to
107 virtual void CopySurface(SourceSurface
* aSurface
, const IntRect
& aSourceRect
,
108 const IntPoint
& aDestination
) override
;
111 * Fill a rectangle on the DrawTarget with a certain source pattern.
113 * aRect Rectangle that forms the mask of this filling operation
114 * aPattern Pattern that forms the source of this filling operation
115 * aOptions Options that are applied to this operation
117 virtual void FillRect(const Rect
& aRect
, const Pattern
& aPattern
,
118 const DrawOptions
& aOptions
= DrawOptions()) override
;
121 * Stroke a rectangle on the DrawTarget with a certain source pattern.
123 * aRect Rectangle that forms the mask of this stroking operation
124 * aPattern Pattern that forms the source of this stroking operation
125 * aOptions Options that are applied to this operation
127 virtual void StrokeRect(const Rect
& aRect
, const Pattern
& aPattern
,
128 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
129 const DrawOptions
& aOptions
= DrawOptions()) override
;
132 * Stroke a line on the DrawTarget with a certain source pattern.
134 * aStart Starting point of the line
135 * aEnd End point of the line
136 * aPattern Pattern that forms the source of this stroking operation
137 * aOptions Options that are applied to this operation
139 virtual void StrokeLine(const Point
& aStart
, const Point
& aEnd
,
140 const Pattern
& aPattern
,
141 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
142 const DrawOptions
& aOptions
= DrawOptions()) override
;
145 * Stroke a path on the draw target with a certain source pattern.
147 * aPath Path that is to be stroked
148 * aPattern Pattern that should be used for the stroke
149 * aStrokeOptions Stroke options used for this operation
150 * aOptions Draw options used for this operation
152 virtual void Stroke(const Path
* aPath
, const Pattern
& aPattern
,
153 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
154 const DrawOptions
& aOptions
= DrawOptions()) override
;
157 * Fill a path on the draw target with a certain source pattern.
159 * aPath Path that is to be filled
160 * aPattern Pattern that should be used for the fill
161 * aOptions Draw options used for this operation
163 virtual void Fill(const Path
* aPath
, const Pattern
& aPattern
,
164 const DrawOptions
& aOptions
= DrawOptions()) override
;
167 * Fill a series of glyphs on the draw target with a certain source pattern.
169 virtual void FillGlyphs(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
170 const Pattern
& aPattern
,
171 const DrawOptions
& aOptions
= DrawOptions()) override
;
174 * Stroke a series of glyphs on the draw target with a certain source pattern.
176 virtual void StrokeGlyphs(
177 ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
, const Pattern
& aPattern
,
178 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
179 const DrawOptions
& aOptions
= DrawOptions()) override
;
182 * This takes a source pattern and a mask, and composites the source pattern
183 * onto the destination surface using the alpha channel of the mask pattern
184 * as a mask for the operation.
186 * aSource Source pattern
188 * aOptions Drawing options
190 virtual void Mask(const Pattern
& aSource
, const Pattern
& aMask
,
191 const DrawOptions
& aOptions
= DrawOptions()) override
;
193 virtual void MaskSurface(
194 const Pattern
& aSource
, SourceSurface
* aMask
, Point aOffset
,
195 const DrawOptions
& aOptions
= DrawOptions()) override
;
198 * Push a clip to the DrawTarget.
200 * aPath The path to clip to
202 virtual void PushClip(const Path
* aPath
) override
;
205 * Push an axis-aligned rectangular clip to the DrawTarget. This rectangle
206 * is specified in user space.
208 * aRect The rect to clip to
210 virtual void PushClipRect(const Rect
& aRect
) override
;
212 /* Pop a clip from the DrawTarget. A pop without a corresponding push will
215 virtual void PopClip() override
;
218 * Push a 'layer' to the DrawTarget, a layer is a temporary surface that all
219 * drawing will be redirected to, this is used for example to support group
220 * opacity or the masking of groups. Clips must be balanced within a layer,
221 * i.e. between a matching PushLayer/PopLayer pair there must be as many
222 * PushClip(Rect) calls as there are PopClip calls.
224 * @param aOpaque Whether the layer will be opaque
225 * @param aOpacity Opacity of the layer
226 * @param aMask Mask applied to the layer
227 * @param aMaskTransform Transform applied to the layer mask
228 * @param aBounds Optional bounds in device space to which the layer is
230 * @param aCopyBackground Whether to copy the background into the layer, this
231 * is only supported when aOpaque is true.
233 virtual void PushLayer(bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
234 const Matrix
& aMaskTransform
,
235 const IntRect
& aBounds
= IntRect(),
236 bool aCopyBackground
= false) override
;
239 * Push a 'layer' to the DrawTarget, a layer is a temporary surface that all
240 * drawing will be redirected to, this is used for example to support group
241 * opacity or the masking of groups. Clips must be balanced within a layer,
242 * i.e. between a matching PushLayer/PopLayer pair there must be as many
243 * PushClip(Rect) calls as there are PopClip calls.
245 * @param aOpaque Whether the layer will be opaque
246 * @param aOpacity Opacity of the layer
247 * @param aMask Mask applied to the layer
248 * @param aMaskTransform Transform applied to the layer mask
249 * @param aBounds Optional bounds in device space to which the layer is
251 * @param aCopyBackground Whether to copy the background into the layer, this
252 * is only supported when aOpaque is true.a
253 * @param aCompositionOp The CompositionOp to use when blending the layer into
256 virtual void PushLayerWithBlend(
257 bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
258 const Matrix
& aMaskTransform
, const IntRect
& aBounds
= IntRect(),
259 bool aCopyBackground
= false,
260 CompositionOp aCompositionOp
= CompositionOp::OP_OVER
) override
;
263 * This balances a call to PushLayer and proceeds to blend the layer back
264 * onto the background. This blend will blend the temporary surface back
265 * onto the target in device space using POINT sampling and operator over.
267 virtual void PopLayer() override
;
270 * Create a SourceSurface optimized for use with this DrawTarget from
271 * existing bitmap data in memory.
273 * The SourceSurface does not take ownership of aData, and may be freed at any
276 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromData(
277 unsigned char* aData
, const IntSize
& aSize
, int32_t aStride
,
278 SurfaceFormat aFormat
) const override
;
281 * Create a SourceSurface optimized for use with this DrawTarget from
282 * an arbitrary other SourceSurface. This may return aSourceSurface or some
283 * other existing surface.
285 virtual already_AddRefed
<SourceSurface
> OptimizeSourceSurface(
286 SourceSurface
* aSurface
) const override
;
289 * Create a SourceSurface for a type of NativeSurface. This may fail if the
290 * draw target does not know how to deal with the type of NativeSurface passed
293 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromNativeSurface(
294 const NativeSurface
& aSurface
) const override
;
297 * Create a DrawTarget whose snapshot is optimized for use with this
300 virtual already_AddRefed
<DrawTarget
> CreateSimilarDrawTarget(
301 const IntSize
& aSize
, SurfaceFormat aFormat
) const override
;
304 * Create a DrawTarget whose backing surface is optimized for use with this
307 virtual already_AddRefed
<DrawTarget
> CreateSimilarDrawTargetWithBacking(
308 const IntSize
& aSize
, SurfaceFormat aFormat
) const override
;
310 bool CanCreateSimilarDrawTarget(const IntSize
& aSize
,
311 SurfaceFormat aFormat
) const override
;
313 * Create a similar DrawTarget whose requested size may be clipped based
314 * on this DrawTarget's rect transformed to the new target's space.
316 virtual RefPtr
<DrawTarget
> CreateClippedDrawTarget(
317 const Rect
& aBounds
, SurfaceFormat aFormat
) override
;
319 virtual already_AddRefed
<DrawTarget
> CreateSimilarDrawTargetForFilter(
320 const IntSize
& aSize
, SurfaceFormat aFormat
, FilterNode
* aFilter
,
321 FilterNode
* aSource
, const Rect
& aSourceRect
,
322 const Point
& aDestPoint
) override
;
324 * Create a path builder with the specified fillmode.
326 * We need the fill mode up front because of Direct2D.
327 * ID2D1SimplifiedGeometrySink requires the fill mode
328 * to be set before calling BeginFigure().
330 virtual already_AddRefed
<PathBuilder
> CreatePathBuilder(
331 FillRule aFillRule
= FillRule::FILL_WINDING
) const override
;
334 * Create a GradientStops object that holds information about a set of
335 * gradient stops, this object is required for linear or radial gradient
336 * patterns to represent the color stops in the gradient.
338 * aStops An array of gradient stops
339 * aNumStops Number of stops in the array aStops
340 * aExtendNone This describes how to extend the stop color outside of the
343 virtual already_AddRefed
<GradientStops
> CreateGradientStops(
344 GradientStop
* aStops
, uint32_t aNumStops
,
345 ExtendMode aExtendMode
= ExtendMode::CLAMP
) const override
;
347 virtual already_AddRefed
<FilterNode
> CreateFilter(FilterType aType
) override
;
350 * Set a transform on the surface, this transform is applied at drawing time
351 * to both the mask and source of the operation.
353 virtual void SetTransform(const Matrix
& aTransform
) override
;
355 virtual void SetPermitSubpixelAA(bool aPermitSubpixelAA
) override
;
357 /* Tries to get a native surface for a DrawTarget, this may fail if the
358 * draw target cannot convert to this surface type.
360 virtual void* GetNativeSurface(NativeSurfaceType aType
) override
{
361 return mFinalDT
->GetNativeSurface(aType
);
364 virtual bool IsCurrentGroupOpaque() override
{
365 return mFinalDT
->IsCurrentGroupOpaque();
368 bool IsDirty() const { return mIsDirty
; }
370 void MarkClean() { mIsDirty
= false; }
374 * Used for creating a DrawTargetRecording for a CreateSimilarDrawTarget call.
376 * @param aDT DrawTargetRecording on which CreateSimilarDrawTarget was called
377 * @param aSize size of the the similar DrawTarget
378 * @param aFormat format of the similar DrawTarget
380 DrawTargetRecording(const DrawTargetRecording
* aDT
, IntRect aRect
,
381 SurfaceFormat aFormat
);
383 Path
* GetPathForPathRecording(const Path
* aPath
) const;
384 already_AddRefed
<PathRecording
> EnsurePathStored(const Path
* aPath
);
385 void EnsurePatternDependenciesStored(const Pattern
& aPattern
);
387 void DrawGlyphs(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
388 const Pattern
& aPattern
,
389 const DrawOptions
& aOptions
= DrawOptions(),
390 const StrokeOptions
* aStrokeOptions
= nullptr);
394 RefPtr
<DrawEventRecorderPrivate
> mRecorder
;
395 RefPtr
<DrawTarget
> mFinalDT
;
399 explicit PushedLayer(bool aOldPermitSubpixelAA
)
400 : mOldPermitSubpixelAA(aOldPermitSubpixelAA
) {}
401 bool mOldPermitSubpixelAA
;
403 std::vector
<PushedLayer
> mPushedLayers
;
405 bool mIsDirty
= false;
409 } // namespace mozilla
411 #endif /* MOZILLA_GFX_DRAWTARGETRECORDING_H_ */