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_H
8 #define MOZILLA_GFX_RENDERCOMPOSITOR_H
10 #include "mozilla/ipc/FileDescriptor.h"
11 #include "mozilla/RefPtr.h"
12 #include "mozilla/UniquePtr.h"
13 #include "mozilla/webrender/WebRenderTypes.h"
25 class CompositionRecorder
;
30 class CompositorWidget
;
35 class RenderCompositorLayersSWGL
;
36 class RenderCompositorD3D11SWGL
;
38 class RenderCompositor
{
40 static UniquePtr
<RenderCompositor
> Create(
41 const RefPtr
<widget::CompositorWidget
>& aWidget
, nsACString
& aError
);
43 RenderCompositor(const RefPtr
<widget::CompositorWidget
>& aWidget
);
44 virtual ~RenderCompositor();
46 virtual bool BeginFrame() = 0;
48 // Optional handler in case the frame was aborted allowing the compositor
49 // to clean up relevant resources if required.
50 virtual void CancelFrame() {}
52 // Called to notify the RenderCompositor that all of the commands for a frame
53 // have been pushed to the queue.
54 // @return a RenderedFrameId for the frame
55 virtual RenderedFrameId
EndFrame(
56 const nsTArray
<DeviceIntRect
>& aDirtyRects
) = 0;
57 // Returns false when waiting gpu tasks is failed.
58 // It might happen when rendering context is lost.
59 virtual bool WaitForGPU() { return true; }
61 // Check for and return the last completed frame.
62 // @return the last (highest) completed RenderedFrameId
63 virtual RenderedFrameId
GetLastCompletedFrameId() {
64 return mLatestRenderFrameId
.Prev();
67 // Update FrameId when WR rendering does not happen.
68 virtual RenderedFrameId
UpdateFrameId() { return GetNextRenderFrameId(); }
70 virtual void Pause() = 0;
71 virtual bool Resume() = 0;
72 // Called when WR rendering is skipped
73 virtual void Update() {}
75 virtual gl::GLContext
* gl() const { return nullptr; }
76 virtual void* swgl() const { return nullptr; }
78 virtual bool MakeCurrent();
80 virtual bool UseANGLE() const { return false; }
82 virtual bool UseDComp() const { return false; }
84 virtual bool UseTripleBuffering() const { return false; }
86 virtual layers::WebRenderBackend
BackendType() const {
87 return layers::WebRenderBackend::HARDWARE
;
89 virtual layers::WebRenderCompositor
CompositorType() const {
90 return layers::WebRenderCompositor::DRAW
;
93 virtual RenderCompositorD3D11SWGL
* AsRenderCompositorD3D11SWGL() {
97 virtual RenderCompositorLayersSWGL
* AsRenderCompositorLayersSWGL() {
101 // True if AttachExternalImage supports being used with an external
102 // image that maps to a RenderBufferTextureHost
103 virtual bool SupportsExternalBufferTextures() const { return false; }
105 virtual LayoutDeviceIntSize
GetBufferSize() = 0;
107 widget::CompositorWidget
* GetWidget() const { return mWidget
; }
109 layers::SyncObjectHost
* GetSyncObject() const { return mSyncObject
.get(); }
111 virtual gfx::DeviceResetReason
IsContextLost(bool aForce
);
113 virtual bool SupportAsyncScreenshot() { return true; }
115 virtual bool ShouldUseNativeCompositor() { return false; }
117 // Interface for wr::Compositor
118 virtual void CompositorBeginFrame() {}
119 virtual void CompositorEndFrame() {}
120 virtual void Bind(wr::NativeTileId aId
, wr::DeviceIntPoint
* aOffset
,
121 uint32_t* aFboId
, wr::DeviceIntRect aDirtyRect
,
122 wr::DeviceIntRect aValidRect
) {}
123 virtual void Unbind() {}
124 virtual bool MapTile(wr::NativeTileId aId
, wr::DeviceIntRect aDirtyRect
,
125 wr::DeviceIntRect aValidRect
, void** aData
,
129 virtual void UnmapTile() {}
130 virtual void CreateSurface(wr::NativeSurfaceId aId
,
131 wr::DeviceIntPoint aVirtualOffset
,
132 wr::DeviceIntSize aTileSize
, bool aIsOpaque
) {}
133 virtual void CreateSwapChainSurface(wr::NativeSurfaceId aId
,
134 wr::DeviceIntSize aSize
, bool aIsOpaque
) {
136 virtual void ResizeSwapChainSurface(wr::NativeSurfaceId aId
,
137 wr::DeviceIntSize aSize
) {}
138 virtual void BindSwapChain(wr::NativeSurfaceId aId
) {}
139 virtual void PresentSwapChain(wr::NativeSurfaceId aId
) {}
140 virtual void CreateExternalSurface(wr::NativeSurfaceId aId
, bool aIsOpaque
) {}
141 virtual void CreateBackdropSurface(wr::NativeSurfaceId aId
,
142 wr::ColorF aColor
) {}
143 virtual void DestroySurface(NativeSurfaceId aId
) {}
144 virtual void CreateTile(wr::NativeSurfaceId
, int32_t aX
, int32_t aY
) {}
145 virtual void DestroyTile(wr::NativeSurfaceId
, int32_t aX
, int32_t aY
) {}
146 virtual void AttachExternalImage(wr::NativeSurfaceId aId
,
147 wr::ExternalImageId aExternalImage
) {}
148 virtual void AddSurface(wr::NativeSurfaceId aId
,
149 const wr::CompositorSurfaceTransform
& aTransform
,
150 wr::DeviceIntRect aClipRect
,
151 wr::ImageRendering aImageRendering
) {}
152 // Called in the middle of a frame after all surfaces have been added but
153 // before tiles are updated to signal that early compositing can start
154 virtual void StartCompositing(wr::ColorF aClearColor
,
155 const wr::DeviceIntRect
* aDirtyRects
,
156 size_t aNumDirtyRects
,
157 const wr::DeviceIntRect
* aOpaqueRects
,
158 size_t aNumOpaqueRects
) {}
159 virtual void EnableNativeCompositor(bool aEnable
) {}
160 virtual void DeInit() {}
161 // Overrides any of the default compositor capabilities for behavior this
162 // compositor might require.
163 virtual void GetCompositorCapabilities(CompositorCapabilities
* aCaps
);
165 virtual void GetWindowVisibility(WindowVisibility
* aVisibility
);
167 // Interface for partial present
168 virtual bool UsePartialPresent() { return false; }
169 virtual bool RequestFullRender() { return false; }
170 virtual uint32_t GetMaxPartialPresentRects() { return 0; }
171 virtual bool ShouldDrawPreviousPartialPresentRegions() { return false; }
172 // Returns the age of the current backbuffer., This should be used, if
173 // ShouldDrawPreviousPartialPresentRegions() returns true, to determine the
174 // region which must be rendered in addition to the current frame's dirty
176 virtual size_t GetBufferAge() const { return 0; }
177 // Allows webrender to specify the total region that will be rendered to this
178 // frame, ie the frame's dirty region and some previous frames' dirty regions,
179 // if applicable (calculated using the buffer age). Must be called before
180 // anything has been rendered to the main framebuffer.
181 virtual void SetBufferDamageRegion(const wr::DeviceIntRect
* aRects
,
184 // Whether the surface origin is top-left.
185 virtual bool SurfaceOriginIsTopLeft() { return false; }
187 // Does readback if wr_renderer_readback() could not get correct WR rendered
188 // result. It could happen when WebRender renders to multiple overlay layers.
189 virtual bool MaybeReadback(const gfx::IntSize
& aReadbackSize
,
190 const wr::ImageFormat
& aReadbackFormat
,
191 const Range
<uint8_t>& aReadbackBuffer
,
195 virtual void MaybeRequestAllowFrameRecording(bool aWillRecord
) {}
196 virtual bool MaybeRecordFrame(layers::CompositionRecorder
& aRecorder
) {
199 virtual bool MaybeGrabScreenshot(const gfx::IntSize
& aWindowSize
) {
202 virtual bool MaybeProcessScreenshotQueue() { return false; }
204 // Returns FileDescriptor of release fence.
205 // Release fence is a fence that is used for waiting until usage/composite of
206 // AHardwareBuffer is ended. The fence is delivered to client side via
207 // ImageBridge. It is used only on android.
208 virtual ipc::FileDescriptor
GetAndResetReleaseFence() {
209 return ipc::FileDescriptor();
212 virtual bool IsPaused() { return false; }
215 // We default this to 2, so that mLatestRenderFrameId.Prev() is always valid.
216 RenderedFrameId mLatestRenderFrameId
= RenderedFrameId
{2};
217 RenderedFrameId
GetNextRenderFrameId() {
218 mLatestRenderFrameId
= mLatestRenderFrameId
.Next();
219 return mLatestRenderFrameId
;
222 RefPtr
<widget::CompositorWidget
> mWidget
;
223 RefPtr
<layers::SyncObjectHost
> mSyncObject
;
227 } // namespace mozilla