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_RENDERCOMPOSITOR_Layers_H
8 #define MOZILLA_GFX_RENDERCOMPOSITOR_Layers_H
10 #include <unordered_map>
12 #include "mozilla/HashFunctions.h"
13 #include "mozilla/layers/Compositor.h"
14 #include "mozilla/layers/ScreenshotGrabber.h"
15 #include "mozilla/webrender/RenderCompositor.h"
16 #include "mozilla/webrender/RenderTextureHost.h"
22 class SurfaceD3D11SWGL
;
24 class RenderCompositorLayersSWGL
: public RenderCompositor
{
26 static UniquePtr
<RenderCompositor
> Create(
27 const RefPtr
<widget::CompositorWidget
>& aWidget
, nsACString
& aError
);
29 RenderCompositorLayersSWGL(layers::Compositor
* aCompositor
,
30 const RefPtr
<widget::CompositorWidget
>& aWidget
,
32 virtual ~RenderCompositorLayersSWGL();
34 void* swgl() const override
{ return mContext
; }
36 bool MakeCurrent() override
;
38 bool BeginFrame() override
;
39 void CancelFrame() override
;
40 RenderedFrameId
EndFrame(const nsTArray
<DeviceIntRect
>& aDirtyRects
) override
;
42 bool SurfaceOriginIsTopLeft() override
{ return true; }
44 LayoutDeviceIntSize
GetBufferSize() override
;
46 // Should we support this?
47 bool SupportsExternalBufferTextures() const override
{ return false; }
49 layers::WebRenderBackend
BackendType() const override
{
50 return layers::WebRenderBackend::SOFTWARE
;
53 bool ShouldUseNativeCompositor() override
{ return true; }
55 void StartCompositing(wr::ColorF aClearColor
,
56 const wr::DeviceIntRect
* aDirtyRects
,
57 size_t aNumDirtyRects
,
58 const wr::DeviceIntRect
* aOpaqueRects
,
59 size_t aNumOpaqueRects
) override
;
61 void CompositorBeginFrame() override
{}
62 void CompositorEndFrame() override
;
63 void Bind(wr::NativeTileId aId
, wr::DeviceIntPoint
* aOffset
, uint32_t* aFboId
,
64 wr::DeviceIntRect aDirtyRect
,
65 wr::DeviceIntRect aValidRect
) override
;
66 void Unbind() override
;
67 bool MapTile(wr::NativeTileId aId
, wr::DeviceIntRect aDirtyRect
,
68 wr::DeviceIntRect aValidRect
, void** aData
,
69 int32_t* aStride
) override
;
70 void UnmapTile() override
;
71 void CreateSurface(wr::NativeSurfaceId aId
, wr::DeviceIntPoint aVirtualOffset
,
72 wr::DeviceIntSize aTileSize
, bool aIsOpaque
) override
;
73 void CreateExternalSurface(wr::NativeSurfaceId aId
, bool aIsOpaque
) override
;
74 void DestroySurface(NativeSurfaceId aId
) override
;
75 void CreateTile(wr::NativeSurfaceId
, int32_t aX
, int32_t aY
) override
;
76 void DestroyTile(wr::NativeSurfaceId
, int32_t aX
, int32_t aY
) override
;
77 void AttachExternalImage(wr::NativeSurfaceId aId
,
78 wr::ExternalImageId aExternalImage
) override
;
79 void AddSurface(wr::NativeSurfaceId aId
,
80 const wr::CompositorSurfaceTransform
& aTransform
,
81 wr::DeviceIntRect aClipRect
,
82 wr::ImageRendering aImageRendering
) override
;
83 void EnableNativeCompositor(bool aEnable
) override
{}
84 void DeInit() override
{}
86 void MaybeRequestAllowFrameRecording(bool aWillRecord
) override
;
87 bool MaybeRecordFrame(layers::CompositionRecorder
& aRecorder
) override
;
88 bool MaybeGrabScreenshot(const gfx::IntSize
& aWindowSize
) override
;
89 bool MaybeProcessScreenshotQueue() override
;
91 // TODO: Screenshots etc
94 TileKey(int32_t aX
, int32_t aY
) : mX(aX
), mY(aY
) {}
100 // Each tile retains a texture, and a DataSourceSurface of the
101 // same size. We draw into the source surface, and then copy the
102 // changed area into the texture.
106 virtual ~Tile() = default;
108 virtual bool Map(wr::DeviceIntRect aDirtyRect
, wr::DeviceIntRect aValidRect
,
109 void** aData
, int32_t* aStride
) = 0;
110 virtual void Unmap(const gfx::IntRect
& aDirtyRect
) = 0;
111 virtual layers::DataTextureSource
* GetTextureSource() = 0;
112 virtual bool IsValid() = 0;
114 gfx::Rect mValidRect
;
117 std::size_t operator()(const TileKey
& aId
) const {
118 return HashGeneric(aId
.mX
, aId
.mY
);
125 explicit Surface(wr::DeviceIntSize aTileSize
, bool aIsOpaque
)
126 : mTileSize(aTileSize
), mIsOpaque(aIsOpaque
) {}
127 virtual ~Surface() {}
129 gfx::IntSize
TileSize() {
130 return gfx::IntSize(mTileSize
.width
, mTileSize
.height
);
132 virtual SurfaceD3D11SWGL
* AsSurfaceD3D11SWGL() { return nullptr; }
134 // External images can change size depending on which image
135 // is attached, so mTileSize will be 0,0 when mIsExternal
137 wr::DeviceIntSize mTileSize
;
139 bool mIsExternal
= false;
140 std::unordered_map
<TileKey
, UniquePtr
<Tile
>, Tile::KeyHashFn
> mTiles
;
141 RefPtr
<RenderTextureHost
> mExternalImage
;
144 std::size_t operator()(const wr::NativeSurfaceId
& aId
) const {
145 return HashGeneric(wr::AsUint64(aId
));
150 static gfx::SamplingFilter
ToSamplingFilter(
151 wr::ImageRendering aImageRendering
);
154 // The set of surfaces added to be composited for the current frame
155 struct FrameSurface
{
156 wr::NativeSurfaceId mId
;
157 gfx::Matrix4x4 mTransform
;
158 gfx::IntRect mClipRect
;
159 gfx::SamplingFilter mFilter
;
162 virtual void HandleExternalImage(RenderTextureHost
* aExternalImage
,
163 FrameSurface
& aFrameSurface
) = 0;
164 virtual UniquePtr
<RenderCompositorLayersSWGL::Surface
> DoCreateSurface(
165 wr::DeviceIntSize aTileSize
, bool aIsOpaque
);
166 virtual UniquePtr
<RenderCompositorLayersSWGL::Tile
> DoCreateTile(
167 Surface
* aSurface
) = 0;
169 RefPtr
<layers::Compositor
> mCompositor
;
170 void* mContext
= nullptr;
172 std::unordered_map
<wr::NativeSurfaceId
, UniquePtr
<Surface
>, Surface::IdHashFn
>
175 // Temporary state held between MapTile and UnmapTile
176 Tile
* mCurrentTile
= nullptr;
177 gfx::IntRect mCurrentTileDirty
;
178 wr::NativeTileId mCurrentTileId
;
180 nsTArray
<FrameSurface
> mFrameSurfaces
;
181 bool mInFrame
= false;
182 bool mCompositingStarted
= false;
184 layers::ScreenshotGrabber mProfilerScreenshotGrabber
;
187 static inline bool operator==(const RenderCompositorLayersSWGL::TileKey
& a0
,
188 const RenderCompositorLayersSWGL::TileKey
& a1
) {
189 return a0
.mX
== a1
.mX
&& a0
.mY
== a1
.mY
;
193 } // namespace mozilla