RenderCompositorANGLE.h (6750B)
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_ANGLE_H 8 #define MOZILLA_GFX_RENDERCOMPOSITOR_ANGLE_H 9 10 #include <queue> 11 12 #include "GLTypes.h" 13 #include "mozilla/Maybe.h" 14 #include "mozilla/webrender/RenderCompositor.h" 15 #include "mozilla/webrender/RenderThread.h" 16 17 struct IDXGIDevice; 18 struct IDXGIFactory; 19 struct ID3D11DeviceContext; 20 struct ID3D11Device; 21 struct ID3D11Query; 22 struct IDXGIFactory2; 23 struct IDXGISwapChain; 24 struct IDXGISwapChain1; 25 26 namespace mozilla { 27 namespace gl { 28 class GLLibraryEGL; 29 } // namespace gl 30 31 namespace layers { 32 class FenceD3D11; 33 } // namespace layers 34 35 namespace wr { 36 37 class DCLayerTree; 38 39 class RenderCompositorANGLE final : public RenderCompositor { 40 public: 41 static UniquePtr<RenderCompositor> Create( 42 const RefPtr<widget::CompositorWidget>& aWidget, nsACString& aError); 43 44 explicit RenderCompositorANGLE( 45 const RefPtr<widget::CompositorWidget>& aWidget, 46 RefPtr<gl::GLContext>&& aGL); 47 virtual ~RenderCompositorANGLE(); 48 bool Initialize(nsACString& aError); 49 50 bool BeginFrame() override; 51 RenderedFrameId EndFrame(const nsTArray<DeviceIntRect>& aDirtyRects) final; 52 bool WaitForGPU() override; 53 RenderedFrameId GetLastCompletedFrameId() final; 54 RenderedFrameId UpdateFrameId() final; 55 void Pause() override; 56 bool Resume() override; 57 void Update() override; 58 59 gl::GLContext* gl() const override { return mGL; } 60 61 bool MakeCurrent() override; 62 63 bool UseANGLE() const override { return true; } 64 65 bool UseDComp() const override { return !!mDCLayerTree; } 66 67 bool UseTripleBuffering() const override { return mUseTripleBuffering; } 68 69 layers::WebRenderCompositor CompositorType() const override { 70 if (UseDComp()) { 71 return layers::WebRenderCompositor::DIRECT_COMPOSITION; 72 } 73 return layers::WebRenderCompositor::DRAW; 74 } 75 76 LayoutDeviceIntSize GetBufferSize() override; 77 78 gfx::DeviceResetReason IsContextLost(bool aForce) override; 79 80 bool SurfaceOriginIsTopLeft() override { return true; } 81 82 bool SupportAsyncScreenshot() override; 83 84 bool ShouldUseNativeCompositor() override; 85 86 bool ShouldUseLayerCompositor() const override; 87 bool UseLayerCompositor() const override; 88 89 // Interface for wr::Compositor 90 void CompositorBeginFrame() override; 91 void CompositorEndFrame() override; 92 void Bind(wr::NativeTileId aId, wr::DeviceIntPoint* aOffset, uint32_t* aFboId, 93 wr::DeviceIntRect aDirtyRect, 94 wr::DeviceIntRect aValidRect) override; 95 void Unbind() override; 96 void BindSwapChain(wr::NativeSurfaceId aId, 97 const wr::DeviceIntRect* aDirtyRects, 98 size_t aNumDirtyRects) override; 99 void PresentSwapChain(wr::NativeSurfaceId aId, 100 const wr::DeviceIntRect* aDirtyRects, 101 size_t aNumDirtyRects) override; 102 void CreateSurface(wr::NativeSurfaceId aId, wr::DeviceIntPoint aVirtualOffset, 103 wr::DeviceIntSize aTileSize, bool aIsOpaque) override; 104 void CreateExternalSurface(wr::NativeSurfaceId aId, bool aIsOpaque) override; 105 void DestroySurface(NativeSurfaceId aId) override; 106 void CreateTile(wr::NativeSurfaceId aId, int32_t aX, int32_t aY) override; 107 void DestroyTile(wr::NativeSurfaceId aId, int32_t aX, int32_t aY) override; 108 void AttachExternalImage(wr::NativeSurfaceId aId, 109 wr::ExternalImageId aExternalImage) override; 110 void CreateSwapChainSurface(wr::NativeSurfaceId aId, wr::DeviceIntSize aSize, 111 bool aIsOpaque, 112 bool aNeedsSyncDcompCommit) override; 113 void ResizeSwapChainSurface(wr::NativeSurfaceId aId, 114 wr::DeviceIntSize aSize) override; 115 void AddSurface(wr::NativeSurfaceId aId, 116 const wr::CompositorSurfaceTransform& aTransform, 117 wr::DeviceIntRect aClipRect, 118 wr::ImageRendering aImageRendering, 119 wr::DeviceIntRect aRoundedClipRect, 120 wr::ClipRadius aClipRadius) override; 121 void EnableNativeCompositor(bool aEnable) override; 122 bool EnableAsyncScreenshot() override; 123 void GetCompositorCapabilities(CompositorCapabilities* aCaps) override; 124 void GetWindowProperties(WindowProperties* aProperties) override; 125 126 // Interface for partial present 127 bool UsePartialPresent() override; 128 bool RequestFullRender() override; 129 uint32_t GetMaxPartialPresentRects() override; 130 131 RefPtr<layers::Fence> GetAndResetReleaseFence() override; 132 133 bool MaybeReadback(const gfx::IntSize& aReadbackSize, 134 const wr::ImageFormat& aReadbackFormat, 135 const Range<uint8_t>& aReadbackBuffer, 136 bool* aNeedsYFlip) override; 137 138 protected: 139 bool UseCompositor() const; 140 bool RecreateNonNativeCompositorSwapChain(); 141 void InitializeUsePartialPresent(); 142 void InsertGraphicsCommandsFinishedWaitQuery( 143 RenderedFrameId aRenderedFrameId); 144 bool WaitForPreviousGraphicsCommandsFinishedQuery(bool aWaitAll = false); 145 bool ResizeBufferIfNeeded(); 146 bool CreateEGLSurface(); 147 void DestroyEGLSurface(); 148 ID3D11Device* GetDeviceOfEGLDisplay(nsACString& aError); 149 bool CreateSwapChain(nsACString& aError); 150 void CreateSwapChainForDCompIfPossible(); 151 bool CreateSwapChainForHWND(); 152 RefPtr<IDXGISwapChain1> CreateSwapChainForDComp(bool aUseTripleBuffering); 153 RefPtr<ID3D11Query> GetD3D11Query(); 154 void ReleaseNativeCompositorResources(); 155 HWND GetCompositorHwnd(); 156 bool ShouldUseAlpha() const; 157 158 RefPtr<IDXGIDevice> DXGIDevice(); 159 RefPtr<IDXGIFactory> DXGIFactory(); 160 161 RefPtr<gl::GLContext> mGL; 162 163 EGLConfig mEGLConfig = nullptr; 164 EGLSurface mEGLSurface = nullptr; 165 166 bool mUseTripleBuffering = false; 167 168 RefPtr<ID3D11Device> mDevice; 169 RefPtr<ID3D11DeviceContext> mCtx; 170 RefPtr<IDXGISwapChain> mSwapChain; 171 RefPtr<IDXGISwapChain1> mSwapChain1; 172 173 UniquePtr<DCLayerTree> mDCLayerTree; 174 175 std::queue<std::pair<RenderedFrameId, RefPtr<ID3D11Query>>> 176 mWaitForPresentQueries; 177 RefPtr<ID3D11Query> mRecycledQuery; 178 RenderedFrameId mLastCompletedFrameId; 179 180 Maybe<LayoutDeviceIntSize> mBufferSize; 181 bool mUsePartialPresent = false; 182 bool mFullRender = false; 183 // Used to know a timing of disabling native compositor. 184 bool mDisablingNativeCompositor = false; 185 bool mFirstPresent = true; 186 // Wether we're currently using alpha. 187 bool mSwapChainUsingAlpha = false; 188 RefPtr<layers::FenceD3D11> mFence; 189 }; 190 191 } // namespace wr 192 } // namespace mozilla 193 194 #endif