tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RenderCompositor.h (9650B)


      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/. */
      6 
      7 #ifndef MOZILLA_GFX_RENDERCOMPOSITOR_H
      8 #define MOZILLA_GFX_RENDERCOMPOSITOR_H
      9 
     10 #include "mozilla/RefPtr.h"
     11 #include "mozilla/UniquePtr.h"
     12 #include "mozilla/layers/Fence.h"
     13 #include "mozilla/webrender/WebRenderTypes.h"
     14 #include "Units.h"
     15 
     16 #include "GLTypes.h"
     17 
     18 namespace mozilla {
     19 
     20 namespace gl {
     21 class GLContext;
     22 }
     23 
     24 namespace layers {
     25 class CompositionRecorder;
     26 class SyncObjectHost;
     27 }  // namespace layers
     28 
     29 namespace widget {
     30 class CompositorWidget;
     31 }
     32 
     33 namespace wr {
     34 
     35 class RenderCompositorLayersSWGL;
     36 class RenderCompositorD3D11SWGL;
     37 
     38 class RenderCompositor {
     39 public:
     40  static UniquePtr<RenderCompositor> Create(
     41      const RefPtr<widget::CompositorWidget>& aWidget, nsACString& aError);
     42 
     43  RenderCompositor(const RefPtr<widget::CompositorWidget>& aWidget);
     44  virtual ~RenderCompositor();
     45 
     46  virtual bool BeginFrame() = 0;
     47 
     48  // Optional handler in case the frame was aborted allowing the compositor
     49  // to clean up relevant resources if required.
     50  virtual void CancelFrame() {}
     51 
     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; }
     60 
     61  // On platforms where putting the frame onto the screen involves work in other
     62  // processes, wait until those other processes have completed that work.
     63  // Specifically, on macOS, we have to send surfaces to the parent process and
     64  // it will put them into CALayers, and we want to wait until that's done.
     65  virtual void WaitUntilPresentationFlushed() {}
     66 
     67  // Check for and return the last completed frame.
     68  // @return the last (highest) completed RenderedFrameId
     69  virtual RenderedFrameId GetLastCompletedFrameId() {
     70    return mLatestRenderFrameId.Prev();
     71  }
     72 
     73  // Update FrameId when WR rendering does not happen.
     74  virtual RenderedFrameId UpdateFrameId() { return GetNextRenderFrameId(); }
     75 
     76  virtual void Pause() = 0;
     77  virtual bool Resume() = 0;
     78  // Called when WR rendering is skipped
     79  virtual void Update() {}
     80 
     81  virtual gl::GLContext* gl() const { return nullptr; }
     82  virtual void* swgl() const { return nullptr; }
     83 
     84  virtual bool MakeCurrent();
     85 
     86  virtual bool UseANGLE() const { return false; }
     87 
     88  virtual bool UseDComp() const { return false; }
     89 
     90  virtual bool UseTripleBuffering() const { return false; }
     91 
     92  virtual layers::WebRenderBackend BackendType() const {
     93    return layers::WebRenderBackend::HARDWARE;
     94  }
     95  virtual layers::WebRenderCompositor CompositorType() const {
     96    return layers::WebRenderCompositor::DRAW;
     97  }
     98 
     99  virtual RenderCompositorD3D11SWGL* AsRenderCompositorD3D11SWGL() {
    100    return nullptr;
    101  }
    102 
    103  virtual RenderCompositorLayersSWGL* AsRenderCompositorLayersSWGL() {
    104    return nullptr;
    105  }
    106 
    107  // True if AttachExternalImage supports being used with an external
    108  // image that maps to a RenderBufferTextureHost
    109  virtual bool SupportsExternalBufferTextures() const { return false; }
    110 
    111  virtual LayoutDeviceIntSize GetBufferSize() = 0;
    112 
    113  widget::CompositorWidget* GetWidget() const { return mWidget; }
    114 
    115  layers::SyncObjectHost* GetSyncObject() const { return mSyncObject.get(); }
    116 
    117  virtual gfx::DeviceResetReason IsContextLost(bool aForce);
    118 
    119  virtual bool SupportAsyncScreenshot() { return true; }
    120 
    121  virtual bool ShouldUseNativeCompositor() { return false; }
    122 
    123  virtual bool ShouldUseLayerCompositor() const { return false; }
    124 
    125  virtual bool UseLayerCompositor() const { return false; }
    126 
    127  virtual bool EnableAsyncScreenshot() { return false; }
    128 
    129  // Interface for wr::Compositor
    130  virtual void CompositorBeginFrame() {}
    131  virtual void CompositorEndFrame() {}
    132  virtual void Bind(wr::NativeTileId aId, wr::DeviceIntPoint* aOffset,
    133                    uint32_t* aFboId, wr::DeviceIntRect aDirtyRect,
    134                    wr::DeviceIntRect aValidRect) {}
    135  virtual void Unbind() {}
    136  virtual bool MapTile(wr::NativeTileId aId, wr::DeviceIntRect aDirtyRect,
    137                       wr::DeviceIntRect aValidRect, void** aData,
    138                       int32_t* aStride) {
    139    return false;
    140  }
    141  virtual void UnmapTile() {}
    142  virtual void CreateSurface(wr::NativeSurfaceId aId,
    143                             wr::DeviceIntPoint aVirtualOffset,
    144                             wr::DeviceIntSize aTileSize, bool aIsOpaque) {}
    145  virtual void CreateSwapChainSurface(wr::NativeSurfaceId aId,
    146                                      wr::DeviceIntSize aSize, bool aIsOpaque,
    147                                      bool aNeedsSyncDcompCommit) {}
    148  virtual void ResizeSwapChainSurface(wr::NativeSurfaceId aId,
    149                                      wr::DeviceIntSize aSize) {}
    150  virtual void BindSwapChain(wr::NativeSurfaceId aId,
    151                             const wr::DeviceIntRect* aDirtyRects,
    152                             size_t aNumDirtyRects) {}
    153  virtual void PresentSwapChain(wr::NativeSurfaceId aId,
    154                                const wr::DeviceIntRect* aDirtyRects,
    155                                size_t aNumDirtyRects) {}
    156  virtual void CreateExternalSurface(wr::NativeSurfaceId aId, bool aIsOpaque) {}
    157  virtual void CreateBackdropSurface(wr::NativeSurfaceId aId,
    158                                     wr::ColorF aColor) {}
    159  virtual void DestroySurface(NativeSurfaceId aId) {}
    160  virtual void CreateTile(wr::NativeSurfaceId, int32_t aX, int32_t aY) {}
    161  virtual void DestroyTile(wr::NativeSurfaceId, int32_t aX, int32_t aY) {}
    162  virtual void AttachExternalImage(wr::NativeSurfaceId aId,
    163                                   wr::ExternalImageId aExternalImage) {}
    164  virtual void AddSurface(wr::NativeSurfaceId aId,
    165                          const wr::CompositorSurfaceTransform& aTransform,
    166                          wr::DeviceIntRect aClipRect,
    167                          wr::ImageRendering aImageRendering,
    168                          wr::DeviceIntRect aRoundedClipRect,
    169                          wr::ClipRadius aClipRadius) {}
    170  // Called in the middle of a frame after all surfaces have been added but
    171  // before tiles are updated to signal that early compositing can start
    172  virtual void StartCompositing(wr::ColorF aClearColor,
    173                                const wr::DeviceIntRect* aDirtyRects,
    174                                size_t aNumDirtyRects,
    175                                const wr::DeviceIntRect* aOpaqueRects,
    176                                size_t aNumOpaqueRects) {}
    177  virtual void EnableNativeCompositor(bool aEnable) {}
    178  virtual void DeInit() {}
    179  // Overrides any of the default compositor capabilities for behavior this
    180  // compositor might require.
    181  virtual void GetCompositorCapabilities(CompositorCapabilities* aCaps);
    182 
    183  virtual void GetWindowVisibility(WindowVisibility* aVisibility);
    184  virtual void GetWindowProperties(WindowProperties* aProperties);
    185 
    186  // Interface for partial present
    187  virtual bool UsePartialPresent() { return false; }
    188  virtual bool RequestFullRender() { return false; }
    189  virtual uint32_t GetMaxPartialPresentRects() { return 0; }
    190  virtual bool ShouldDrawPreviousPartialPresentRegions() { return false; }
    191  // Returns the age of the current backbuffer., This should be used, if
    192  // ShouldDrawPreviousPartialPresentRegions() returns true, to determine the
    193  // region which must be rendered in addition to the current frame's dirty
    194  // rect.
    195  virtual size_t GetBufferAge() const { return 0; }
    196  // Allows webrender to specify the total region that will be rendered to this
    197  // frame, ie the frame's dirty region and some previous frames' dirty regions,
    198  // if applicable (calculated using the buffer age). Must be called before
    199  // anything has been rendered to the main framebuffer.
    200  virtual void SetBufferDamageRegion(const wr::DeviceIntRect* aRects,
    201                                     size_t aNumRects) {}
    202 
    203  // Whether the surface origin is top-left.
    204  virtual bool SurfaceOriginIsTopLeft() { return false; }
    205 
    206  // Does readback if wr_renderer_readback() could not get correct WR rendered
    207  // result. It could happen when WebRender renders to multiple overlay layers.
    208  virtual bool MaybeReadback(const gfx::IntSize& aReadbackSize,
    209                             const wr::ImageFormat& aReadbackFormat,
    210                             const Range<uint8_t>& aReadbackBuffer,
    211                             bool* aNeedsYFlip) {
    212    return false;
    213  }
    214  virtual void MaybeRequestAllowFrameRecording(bool aWillRecord) {}
    215  virtual bool MaybeRecordFrame(layers::CompositionRecorder& aRecorder) {
    216    return false;
    217  }
    218  virtual bool MaybeGrabScreenshot(const gfx::IntSize& aWindowSize) {
    219    return false;
    220  }
    221  virtual bool MaybeProcessScreenshotQueue() { return false; }
    222 
    223  virtual RefPtr<layers::Fence> GetAndResetReleaseFence() { return nullptr; }
    224 
    225  virtual bool IsPaused() { return false; }
    226 
    227 protected:
    228  // We default this to 2, so that mLatestRenderFrameId.Prev() is always valid.
    229  RenderedFrameId mLatestRenderFrameId = RenderedFrameId{2};
    230  RenderedFrameId GetNextRenderFrameId() {
    231    mLatestRenderFrameId = mLatestRenderFrameId.Next();
    232    return mLatestRenderFrameId;
    233  }
    234 
    235  RefPtr<widget::CompositorWidget> mWidget;
    236  RefPtr<layers::SyncObjectHost> mSyncObject;
    237 };
    238 
    239 }  // namespace wr
    240 }  // namespace mozilla
    241 
    242 #endif