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_D3D11_H
8 #define MOZILLA_GFX_RENDERCOMPOSITOR_D3D11_H
9 
10 #include "mozilla/layers/ScreenshotGrabber.h"
11 #include "mozilla/layers/TextureD3D11.h"
12 #include "mozilla/layers/CompositorD3D11.h"
13 #include "mozilla/webrender/RenderCompositorLayersSWGL.h"
14 
15 namespace mozilla {
16 
17 namespace wr {
18 
19 class SurfaceD3D11SWGL;
20 
21 class RenderCompositorD3D11SWGL : public RenderCompositorLayersSWGL {
22  public:
23   static UniquePtr<RenderCompositor> Create(
24       const RefPtr<widget::CompositorWidget>& aWidget, nsACString& aError);
25 
26   RenderCompositorD3D11SWGL(layers::CompositorD3D11* aCompositor,
27                             const RefPtr<widget::CompositorWidget>& aWidget,
28                             void* aContext);
29   virtual ~RenderCompositorD3D11SWGL();
30 
31   void Pause() override;
32   bool Resume() override;
33 
34   GLenum IsContextLost(bool aForce) override;
35 
CompositorType()36   layers::WebRenderCompositor CompositorType() const override {
37     return layers::WebRenderCompositor::D3D11;
38   }
AsRenderCompositorD3D11SWGL()39   RenderCompositorD3D11SWGL* AsRenderCompositorD3D11SWGL() override {
40     return this;
41   }
42 
43   bool BeginFrame() override;
44 
45   bool MaybeReadback(const gfx::IntSize& aReadbackSize,
46                      const wr::ImageFormat& aReadbackFormat,
47                      const Range<uint8_t>& aReadbackBuffer,
48                      bool* aNeedsYFlip) override;
49 
GetCompositorD3D11()50   layers::CompositorD3D11* GetCompositorD3D11() {
51     return mCompositor->AsCompositorD3D11();
52   }
53 
GetDevice()54   ID3D11Device* GetDevice() { return GetCompositorD3D11()->GetDevice(); }
55 
56  private:
57   already_AddRefed<ID3D11Texture2D> CreateStagingTexture(
58       const gfx::IntSize aSize);
59   already_AddRefed<DataSourceSurface> CreateStagingSurface(
60       const gfx::IntSize aSize);
61 
62   void HandleExternalImage(RenderTextureHost* aExternalImage,
63                            FrameSurface& aFrameSurface) override;
64   UniquePtr<RenderCompositorLayersSWGL::Surface> DoCreateSurface(
65       wr::DeviceIntSize aTileSize, bool aIsOpaque) override;
66   UniquePtr<RenderCompositorLayersSWGL::Tile> DoCreateTile(
67       Surface* aSurface) override;
68 
69   class TileD3D11 : public RenderCompositorLayersSWGL::Tile {
70    public:
71     TileD3D11(layers::DataTextureSourceD3D11* aTexture,
72               ID3D11Texture2D* aStagingTexture,
73               DataSourceSurface* aDataSourceSurface, Surface* aOwner,
74               RenderCompositorD3D11SWGL* aRenderCompositor);
~TileD3D11()75     virtual ~TileD3D11() {}
76 
77     bool Map(wr::DeviceIntRect aDirtyRect, wr::DeviceIntRect aValidRect,
78              void** aData, int32_t* aStride) override;
79     void Unmap(const gfx::IntRect& aDirtyRect) override;
GetTextureSource()80     layers::DataTextureSource* GetTextureSource() override { return mTexture; }
81     bool IsValid() override;
82 
83    private:
84     RefPtr<layers::DataTextureSourceD3D11> mTexture;
85     RefPtr<ID3D11Texture2D> mStagingTexture;
86     RefPtr<DataSourceSurface> mSurface;
87     SurfaceD3D11SWGL* mOwner;
88     RenderCompositorD3D11SWGL* mRenderCompositor;
89   };
90 
91   enum UploadMode {
92     Upload_Immediate,
93     Upload_Staging,
94     Upload_StagingNoBlock,
95     Upload_StagingPooled
96   };
97   UploadMode GetUploadMode();
98   UploadMode mUploadMode = Upload_Staging;
99 
100   RefPtr<ID3D11Texture2D> mCurrentStagingTexture;
101   bool mCurrentStagingTextureIsTemp = false;
102 };
103 
104 class SurfaceD3D11SWGL : public RenderCompositorLayersSWGL::Surface {
105  public:
106   SurfaceD3D11SWGL(wr::DeviceIntSize aTileSize, bool aIsOpaque);
~SurfaceD3D11SWGL()107   virtual ~SurfaceD3D11SWGL() {}
108 
AsSurfaceD3D11SWGL()109   SurfaceD3D11SWGL* AsSurfaceD3D11SWGL() override { return this; }
110 
111   nsTArray<RefPtr<ID3D11Texture2D>> mStagingPool;
112 };
113 
114 }  // namespace wr
115 }  // namespace mozilla
116 
117 #endif
118