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 final
: 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 DrawSurfaceDescriptor(
75 const layers::SurfaceDescriptor
& aDesc
,
76 const RefPtr
<layers::Image
>& aImageOfSurfaceDescriptor
, const Rect
& aDest
,
78 const DrawSurfaceOptions
& aSurfOptions
= DrawSurfaceOptions(),
79 const DrawOptions
& aOptions
= DrawOptions()) override
;
81 virtual void DrawDependentSurface(uint64_t aId
, const Rect
& aDest
) override
;
83 virtual void DrawFilter(FilterNode
* aNode
, const Rect
& aSourceRect
,
84 const Point
& aDestPoint
,
85 const DrawOptions
& aOptions
= DrawOptions()) override
;
87 virtual void DrawSurfaceWithShadow(SourceSurface
* aSurface
,
89 const ShadowOptions
& aShadow
,
90 CompositionOp aOperator
) override
;
92 virtual void DrawShadow(const Path
* aPath
, const Pattern
& aPattern
,
93 const ShadowOptions
& aShadow
,
94 const DrawOptions
& aOptions
,
95 const StrokeOptions
* aStrokeOptions
) override
;
98 * Clear a rectangle on the draw target to transparent black. This will
99 * respect the clipping region and transform.
101 * aRect Rectangle to clear
103 virtual void ClearRect(const Rect
& aRect
) override
;
106 * This is essentially a 'memcpy' between two surfaces. It moves a pixel
107 * aligned area from the source surface unscaled directly onto the
108 * drawtarget. This ignores both transform and clip.
110 * aSurface Surface to copy from
111 * aSourceRect Source rectangle to be copied
112 * aDest Destination point to copy the surface to
114 virtual void CopySurface(SourceSurface
* aSurface
, const IntRect
& aSourceRect
,
115 const IntPoint
& aDestination
) override
;
118 * Fill a rectangle on the DrawTarget with a certain source pattern.
120 * aRect Rectangle that forms the mask of this filling operation
121 * aPattern Pattern that forms the source of this filling operation
122 * aOptions Options that are applied to this operation
124 virtual void FillRect(const Rect
& aRect
, const Pattern
& aPattern
,
125 const DrawOptions
& aOptions
= DrawOptions()) override
;
128 * Stroke a rectangle on the DrawTarget with a certain source pattern.
130 * aRect Rectangle that forms the mask of this stroking operation
131 * aPattern Pattern that forms the source of this stroking operation
132 * aOptions Options that are applied to this operation
134 virtual void StrokeRect(const Rect
& aRect
, const Pattern
& aPattern
,
135 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
136 const DrawOptions
& aOptions
= DrawOptions()) override
;
139 * Stroke a line on the DrawTarget with a certain source pattern.
141 * aStart Starting point of the line
142 * aEnd End point of the line
143 * aPattern Pattern that forms the source of this stroking operation
144 * aOptions Options that are applied to this operation
146 virtual void StrokeLine(const Point
& aStart
, const Point
& aEnd
,
147 const Pattern
& aPattern
,
148 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
149 const DrawOptions
& aOptions
= DrawOptions()) override
;
152 * Stroke a path on the draw target with a certain source pattern.
154 * aPath Path that is to be stroked
155 * aPattern Pattern that should be used for the stroke
156 * aStrokeOptions Stroke options used for this operation
157 * aOptions Draw options used for this operation
159 virtual void Stroke(const Path
* aPath
, const Pattern
& aPattern
,
160 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
161 const DrawOptions
& aOptions
= DrawOptions()) override
;
164 * Fill a path on the draw target with a certain source pattern.
166 * aPath Path that is to be filled
167 * aPattern Pattern that should be used for the fill
168 * aOptions Draw options used for this operation
170 virtual void Fill(const Path
* aPath
, const Pattern
& aPattern
,
171 const DrawOptions
& aOptions
= DrawOptions()) override
;
174 * Fill a series of glyphs on the draw target with a certain source pattern.
176 virtual void FillGlyphs(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
177 const Pattern
& aPattern
,
178 const DrawOptions
& aOptions
= DrawOptions()) override
;
181 * Stroke a series of glyphs on the draw target with a certain source pattern.
183 virtual void StrokeGlyphs(
184 ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
, const Pattern
& aPattern
,
185 const StrokeOptions
& aStrokeOptions
= StrokeOptions(),
186 const DrawOptions
& aOptions
= DrawOptions()) override
;
189 * This takes a source pattern and a mask, and composites the source pattern
190 * onto the destination surface using the alpha channel of the mask pattern
191 * as a mask for the operation.
193 * aSource Source pattern
195 * aOptions Drawing options
197 virtual void Mask(const Pattern
& aSource
, const Pattern
& aMask
,
198 const DrawOptions
& aOptions
= DrawOptions()) override
;
200 virtual void MaskSurface(
201 const Pattern
& aSource
, SourceSurface
* aMask
, Point aOffset
,
202 const DrawOptions
& aOptions
= DrawOptions()) override
;
205 * Push a clip to the DrawTarget.
207 * aPath The path to clip to
209 virtual void PushClip(const Path
* aPath
) override
;
212 * Push an axis-aligned rectangular clip to the DrawTarget. This rectangle
213 * is specified in user space.
215 * aRect The rect to clip to
217 virtual void PushClipRect(const Rect
& aRect
) override
;
219 /* Pop a clip from the DrawTarget. A pop without a corresponding push will
222 virtual void PopClip() override
;
225 * Push a 'layer' to the DrawTarget, a layer is a temporary surface that all
226 * drawing will be redirected to, this is used for example to support group
227 * opacity or the masking of groups. Clips must be balanced within a layer,
228 * i.e. between a matching PushLayer/PopLayer pair there must be as many
229 * PushClip(Rect) calls as there are PopClip calls.
231 * @param aOpaque Whether the layer will be opaque
232 * @param aOpacity Opacity of the layer
233 * @param aMask Mask applied to the layer
234 * @param aMaskTransform Transform applied to the layer mask
235 * @param aBounds Optional bounds in device space to which the layer is
237 * @param aCopyBackground Whether to copy the background into the layer, this
238 * is only supported when aOpaque is true.
240 virtual void PushLayer(bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
241 const Matrix
& aMaskTransform
,
242 const IntRect
& aBounds
= IntRect(),
243 bool aCopyBackground
= false) override
;
246 * Push a 'layer' to the DrawTarget, a layer is a temporary surface that all
247 * drawing will be redirected to, this is used for example to support group
248 * opacity or the masking of groups. Clips must be balanced within a layer,
249 * i.e. between a matching PushLayer/PopLayer pair there must be as many
250 * PushClip(Rect) calls as there are PopClip calls.
252 * @param aOpaque Whether the layer will be opaque
253 * @param aOpacity Opacity of the layer
254 * @param aMask Mask applied to the layer
255 * @param aMaskTransform Transform applied to the layer mask
256 * @param aBounds Optional bounds in device space to which the layer is
258 * @param aCopyBackground Whether to copy the background into the layer, this
259 * is only supported when aOpaque is true.a
260 * @param aCompositionOp The CompositionOp to use when blending the layer into
263 virtual void PushLayerWithBlend(
264 bool aOpaque
, Float aOpacity
, SourceSurface
* aMask
,
265 const Matrix
& aMaskTransform
, const IntRect
& aBounds
= IntRect(),
266 bool aCopyBackground
= false,
267 CompositionOp aCompositionOp
= CompositionOp::OP_OVER
) override
;
270 * This balances a call to PushLayer and proceeds to blend the layer back
271 * onto the background. This blend will blend the temporary surface back
272 * onto the target in device space using POINT sampling and operator over.
274 virtual void PopLayer() override
;
277 * Create a SourceSurface optimized for use with this DrawTarget from
278 * existing bitmap data in memory.
280 * The SourceSurface does not take ownership of aData, and may be freed at any
283 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromData(
284 unsigned char* aData
, const IntSize
& aSize
, int32_t aStride
,
285 SurfaceFormat aFormat
) const override
;
288 * Create a SourceSurface optimized for use with this DrawTarget from
289 * an arbitrary other SourceSurface. This may return aSourceSurface or some
290 * other existing surface.
292 virtual already_AddRefed
<SourceSurface
> OptimizeSourceSurface(
293 SourceSurface
* aSurface
) const override
;
296 * Create a SourceSurface for a type of NativeSurface. This may fail if the
297 * draw target does not know how to deal with the type of NativeSurface passed
300 virtual already_AddRefed
<SourceSurface
> CreateSourceSurfaceFromNativeSurface(
301 const NativeSurface
& aSurface
) const override
;
304 * Create a DrawTarget whose snapshot is optimized for use with this
307 virtual already_AddRefed
<DrawTarget
> CreateSimilarDrawTarget(
308 const IntSize
& aSize
, SurfaceFormat aFormat
) const override
;
311 * Create a DrawTarget whose backing surface is optimized for use with this
314 virtual already_AddRefed
<DrawTarget
> CreateSimilarDrawTargetWithBacking(
315 const IntSize
& aSize
, SurfaceFormat aFormat
) const override
;
317 bool CanCreateSimilarDrawTarget(const IntSize
& aSize
,
318 SurfaceFormat aFormat
) const override
;
320 * Create a similar DrawTarget whose requested size may be clipped based
321 * on this DrawTarget's rect transformed to the new target's space.
323 virtual RefPtr
<DrawTarget
> CreateClippedDrawTarget(
324 const Rect
& aBounds
, SurfaceFormat aFormat
) override
;
326 virtual already_AddRefed
<DrawTarget
> CreateSimilarDrawTargetForFilter(
327 const IntSize
& aSize
, SurfaceFormat aFormat
, FilterNode
* aFilter
,
328 FilterNode
* aSource
, const Rect
& aSourceRect
,
329 const Point
& aDestPoint
) override
;
331 * Create a path builder with the specified fillmode.
333 * We need the fill mode up front because of Direct2D.
334 * ID2D1SimplifiedGeometrySink requires the fill mode
335 * to be set before calling BeginFigure().
337 virtual already_AddRefed
<PathBuilder
> CreatePathBuilder(
338 FillRule aFillRule
= FillRule::FILL_WINDING
) const override
;
341 * Create a GradientStops object that holds information about a set of
342 * gradient stops, this object is required for linear or radial gradient
343 * patterns to represent the color stops in the gradient.
345 * aStops An array of gradient stops
346 * aNumStops Number of stops in the array aStops
347 * aExtendNone This describes how to extend the stop color outside of the
350 virtual already_AddRefed
<GradientStops
> CreateGradientStops(
351 GradientStop
* aStops
, uint32_t aNumStops
,
352 ExtendMode aExtendMode
= ExtendMode::CLAMP
) const override
;
354 virtual already_AddRefed
<FilterNode
> CreateFilter(FilterType aType
) override
;
357 * Set a transform on the surface, this transform is applied at drawing time
358 * to both the mask and source of the operation.
360 virtual void SetTransform(const Matrix
& aTransform
) override
;
362 virtual void SetPermitSubpixelAA(bool aPermitSubpixelAA
) override
;
364 /* Tries to get a native surface for a DrawTarget, this may fail if the
365 * draw target cannot convert to this surface type.
367 virtual void* GetNativeSurface(NativeSurfaceType aType
) override
{
368 return mFinalDT
->GetNativeSurface(aType
);
371 virtual bool IsCurrentGroupOpaque() override
{
372 return mFinalDT
->IsCurrentGroupOpaque();
375 bool IsDirty() const { return mIsDirty
; }
377 void MarkClean() { mIsDirty
= false; }
379 void SetOptimizeTransform(bool aOptimizeTransform
) {
380 mOptimizeTransform
= aOptimizeTransform
;
385 * Used for creating a DrawTargetRecording for a CreateSimilarDrawTarget call.
387 * @param aDT DrawTargetRecording on which CreateSimilarDrawTarget was called
388 * @param aSize size of the the similar DrawTarget
389 * @param aFormat format of the similar DrawTarget
391 DrawTargetRecording(const DrawTargetRecording
* aDT
, IntRect aRect
,
392 SurfaceFormat aFormat
);
394 void RecordTransform(const Matrix
& aTransform
) const;
396 void FlushTransform() const {
397 if (mTransformDirty
) {
398 if (!mRecordedTransform
.ExactlyEquals(mTransform
)) {
399 RecordTransform(mTransform
);
401 mTransformDirty
= false;
405 void RecordEvent(const RecordedEvent
& aEvent
) const {
407 mRecorder
->RecordEvent(aEvent
);
410 void RecordEventSelf(const RecordedEvent
& aEvent
) const {
412 mRecorder
->RecordEvent(this, aEvent
);
415 void RecordEventSkipFlushTransform(const RecordedEvent
& aEvent
) const {
416 mRecorder
->RecordEvent(aEvent
);
419 void RecordEventSelfSkipFlushTransform(const RecordedEvent
& aEvent
) const {
420 mRecorder
->RecordEvent(this, aEvent
);
423 Path
* GetPathForPathRecording(const Path
* aPath
) const;
424 already_AddRefed
<PathRecording
> EnsurePathStored(const Path
* aPath
);
425 void EnsurePatternDependenciesStored(const Pattern
& aPattern
);
427 void DrawGlyphs(ScaledFont
* aFont
, const GlyphBuffer
& aBuffer
,
428 const Pattern
& aPattern
,
429 const DrawOptions
& aOptions
= DrawOptions(),
430 const StrokeOptions
* aStrokeOptions
= nullptr);
434 RefPtr
<DrawEventRecorderPrivate
> mRecorder
;
435 RefPtr
<DrawTarget
> mFinalDT
;
439 explicit PushedLayer(bool aOldPermitSubpixelAA
)
440 : mOldPermitSubpixelAA(aOldPermitSubpixelAA
) {}
441 bool mOldPermitSubpixelAA
;
443 std::vector
<PushedLayer
> mPushedLayers
;
445 bool mIsDirty
= false;
446 bool mOptimizeTransform
= false;
448 // Last transform that was used in the recording.
449 mutable Matrix mRecordedTransform
;
453 } // namespace mozilla
455 #endif /* MOZILLA_GFX_DRAWTARGETRECORDING_H_ */