RenderCompositorLayersSWGL.h (6960B)
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_Layers_H 8 #define MOZILLA_GFX_RENDERCOMPOSITOR_Layers_H 9 10 #include <unordered_map> 11 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" 17 18 namespace mozilla { 19 20 namespace wr { 21 22 class SurfaceD3D11SWGL; 23 24 class RenderCompositorLayersSWGL : public RenderCompositor { 25 public: 26 static UniquePtr<RenderCompositor> Create( 27 const RefPtr<widget::CompositorWidget>& aWidget, nsACString& aError); 28 29 RenderCompositorLayersSWGL(layers::Compositor* aCompositor, 30 const RefPtr<widget::CompositorWidget>& aWidget, 31 void* aContext); 32 virtual ~RenderCompositorLayersSWGL(); 33 34 void* swgl() const override { return mContext; } 35 36 bool MakeCurrent() override; 37 38 bool BeginFrame() override; 39 void CancelFrame() override; 40 RenderedFrameId EndFrame(const nsTArray<DeviceIntRect>& aDirtyRects) override; 41 42 bool SurfaceOriginIsTopLeft() override { return true; } 43 44 LayoutDeviceIntSize GetBufferSize() override; 45 46 // Should we support this? 47 bool SupportsExternalBufferTextures() const override { return false; } 48 49 layers::WebRenderBackend BackendType() const override { 50 return layers::WebRenderBackend::SOFTWARE; 51 } 52 53 bool ShouldUseNativeCompositor() override { return true; } 54 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; 60 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, 83 wr::DeviceIntRect aRoundedClipRect, 84 wr::ClipRadius aClipRadius) override; 85 void EnableNativeCompositor(bool aEnable) override {} 86 void DeInit() override {} 87 88 void MaybeRequestAllowFrameRecording(bool aWillRecord) override; 89 bool MaybeRecordFrame(layers::CompositionRecorder& aRecorder) override; 90 bool MaybeGrabScreenshot(const gfx::IntSize& aWindowSize) override; 91 bool MaybeProcessScreenshotQueue() override; 92 93 // TODO: Screenshots etc 94 95 struct TileKey { 96 TileKey(int32_t aX, int32_t aY) : mX(aX), mY(aY) {} 97 98 int32_t mX; 99 int32_t mY; 100 }; 101 102 // Each tile retains a texture, and a DataSourceSurface of the 103 // same size. We draw into the source surface, and then copy the 104 // changed area into the texture. 105 class Tile { 106 public: 107 Tile() = default; 108 virtual ~Tile() = default; 109 110 virtual bool Map(wr::DeviceIntRect aDirtyRect, wr::DeviceIntRect aValidRect, 111 void** aData, int32_t* aStride) = 0; 112 virtual void Unmap(const gfx::IntRect& aDirtyRect) = 0; 113 virtual layers::DataTextureSource* GetTextureSource() = 0; 114 virtual bool IsValid() = 0; 115 116 gfx::Rect mValidRect; 117 118 struct KeyHashFn { 119 std::size_t operator()(const TileKey& aId) const { 120 return HashGeneric(aId.mX, aId.mY); 121 } 122 }; 123 }; 124 125 class Surface { 126 public: 127 explicit Surface(wr::DeviceIntSize aTileSize, bool aIsOpaque) 128 : mTileSize(aTileSize), mIsOpaque(aIsOpaque) {} 129 virtual ~Surface() {} 130 131 gfx::IntSize TileSize() { 132 return gfx::IntSize(mTileSize.width, mTileSize.height); 133 } 134 virtual SurfaceD3D11SWGL* AsSurfaceD3D11SWGL() { return nullptr; } 135 136 // External images can change size depending on which image 137 // is attached, so mTileSize will be 0,0 when mIsExternal 138 // is true. 139 wr::DeviceIntSize mTileSize; 140 bool mIsOpaque; 141 bool mIsExternal = false; 142 std::unordered_map<TileKey, UniquePtr<Tile>, Tile::KeyHashFn> mTiles; 143 RefPtr<RenderTextureHost> mExternalImage; 144 145 struct IdHashFn { 146 std::size_t operator()(const wr::NativeSurfaceId& aId) const { 147 return HashGeneric(wr::AsUint64(aId)); 148 } 149 }; 150 }; 151 152 static gfx::SamplingFilter ToSamplingFilter( 153 wr::ImageRendering aImageRendering); 154 155 protected: 156 // The set of surfaces added to be composited for the current frame 157 struct FrameSurface { 158 wr::NativeSurfaceId mId; 159 gfx::Matrix4x4 mTransform; 160 gfx::IntRect mClipRect; 161 gfx::SamplingFilter mFilter; 162 gfx::Rect mRoundedClipRect; 163 gfx::RectCornerRadii mRoundedClipRadii; 164 }; 165 166 virtual void HandleExternalImage(RenderTextureHost* aExternalImage, 167 FrameSurface& aFrameSurface) = 0; 168 virtual UniquePtr<RenderCompositorLayersSWGL::Surface> DoCreateSurface( 169 wr::DeviceIntSize aTileSize, bool aIsOpaque); 170 virtual UniquePtr<RenderCompositorLayersSWGL::Tile> DoCreateTile( 171 Surface* aSurface) = 0; 172 173 RefPtr<layers::Compositor> mCompositor; 174 void* mContext = nullptr; 175 176 std::unordered_map<wr::NativeSurfaceId, UniquePtr<Surface>, Surface::IdHashFn> 177 mSurfaces; 178 179 // Temporary state held between MapTile and UnmapTile 180 Tile* mCurrentTile = nullptr; 181 gfx::IntRect mCurrentTileDirty; 182 wr::NativeTileId mCurrentTileId; 183 184 nsTArray<FrameSurface> mFrameSurfaces; 185 bool mInFrame = false; 186 bool mCompositingStarted = false; 187 188 layers::ScreenshotGrabber mProfilerScreenshotGrabber; 189 }; 190 191 static inline bool operator==(const RenderCompositorLayersSWGL::TileKey& a0, 192 const RenderCompositorLayersSWGL::TileKey& a1) { 193 return a0.mX == a1.mX && a0.mY == a1.mY; 194 } 195 196 } // namespace wr 197 } // namespace mozilla 198 199 #endif