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 GFX_D3DSURFACEIMAGE_H
8 #define GFX_D3DSURFACEIMAGE_H
9 
10 #include "mozilla/RefPtr.h"
11 #include "ImageContainer.h"
12 #include "d3d9.h"
13 #include "mozilla/layers/TextureClientRecycleAllocator.h"
14 
15 namespace mozilla {
16 namespace layers {
17 
18 class TextureClient;
19 
20 class D3D9RecycleAllocator : public TextureClientRecycleAllocator {
21  public:
D3D9RecycleAllocator(KnowsCompositor * aAllocator,IDirect3DDevice9 * aDevice)22   D3D9RecycleAllocator(KnowsCompositor* aAllocator, IDirect3DDevice9* aDevice)
23       : TextureClientRecycleAllocator(aAllocator), mDevice(aDevice) {}
24 
25   already_AddRefed<TextureClient> CreateOrRecycleClient(
26       gfx::SurfaceFormat aFormat, const gfx::IntSize& aSize);
27 
28  protected:
29   already_AddRefed<TextureClient> Allocate(
30       gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector,
31       TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) override;
32 
33   RefPtr<IDirect3DDevice9> mDevice;
34 };
35 
36 /**
37  * Wraps a D3D9 texture, shared with the compositor though DXGI.
38  * At the moment it is only used with D3D11 compositing, and the corresponding
39  * TextureHost is DXGITextureHostD3D11.
40  */
41 class DXGID3D9TextureData : public TextureData {
42  public:
43   static DXGID3D9TextureData* Create(gfx::IntSize aSize,
44                                      gfx::SurfaceFormat aFormat,
45                                      TextureFlags aFlags,
46                                      IDirect3DDevice9* aDevice);
47 
48   virtual ~DXGID3D9TextureData();
49 
50   void FillInfo(TextureData::Info& aInfo) const override;
51 
Lock(OpenMode)52   bool Lock(OpenMode) override { return true; }
53 
Unlock()54   void Unlock() override {}
55 
56   bool Serialize(SurfaceDescriptor& aOutDescriptor) override;
57 
Deallocate(LayersIPCChannel * aAllocator)58   void Deallocate(LayersIPCChannel* aAllocator) override {}
59 
GetD3D9Device()60   IDirect3DDevice9* GetD3D9Device() { return mDevice; }
GetD3D9Texture()61   IDirect3DTexture9* GetD3D9Texture() { return mTexture; }
GetShareHandle()62   HANDLE GetShareHandle() const { return mHandle; }
63   already_AddRefed<IDirect3DSurface9> GetD3D9Surface() const;
64 
GetDesc()65   const D3DSURFACE_DESC& GetDesc() const { return mDesc; }
66 
GetSize()67   gfx::IntSize GetSize() const {
68     return gfx::IntSize(mDesc.Width, mDesc.Height);
69   }
70 
71  protected:
72   DXGID3D9TextureData(gfx::SurfaceFormat aFormat, IDirect3DTexture9* aTexture,
73                       HANDLE aHandle, IDirect3DDevice9* aDevice);
74 
75   RefPtr<IDirect3DDevice9> mDevice;
76   RefPtr<IDirect3DTexture9> mTexture;
77   gfx::SurfaceFormat mFormat;
78   HANDLE mHandle;
79   D3DSURFACE_DESC mDesc;
80 };
81 
82 // Image class that wraps a IDirect3DSurface9. This class copies the image
83 // passed into SetData(), so that it can be accessed from other D3D devices.
84 // This class also manages the synchronization of the copy, to ensure the
85 // resource is ready to use.
86 class D3D9SurfaceImage : public Image {
87  public:
88   D3D9SurfaceImage();
89   virtual ~D3D9SurfaceImage();
90 
91   HRESULT AllocateAndCopy(D3D9RecycleAllocator* aAllocator,
92                           IDirect3DSurface9* aSurface,
93                           const gfx::IntRect& aRegion);
94 
95   // Returns the description of the shared surface.
96   gfx::IntSize GetSize() const override;
97 
98   already_AddRefed<gfx::SourceSurface> GetAsSourceSurface() override;
99 
100   TextureClient* GetTextureClient(KnowsCompositor* aKnowsCompositor) override;
101 
102   already_AddRefed<IDirect3DSurface9> GetD3D9Surface() const;
103 
104   HANDLE GetShareHandle() const;
105 
IsValid()106   bool IsValid() const override { return mValid; }
107 
Invalidate()108   void Invalidate() { mValid = false; }
109 
110  private:
111   gfx::IntSize mSize;
112   RefPtr<TextureClient> mTextureClient;
113   RefPtr<IDirect3DTexture9> mTexture;
114   HANDLE mShareHandle;
115   D3DSURFACE_DESC mDesc;
116   bool mValid;
117 };
118 
119 }  // namespace layers
120 }  // namespace mozilla
121 
122 #endif  // GFX_D3DSURFACEIMAGE_H
123