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/. */
7 #ifndef MOZILLA_GFX_RENDERD3D11TEXTUREHOST_H
8 #define MOZILLA_GFX_RENDERD3D11TEXTUREHOST_H
13 #include "mozilla/gfx/FileHandleWrapper.h"
14 #include "RenderTextureHostSWGL.h"
16 struct ID3D11Texture2D
;
17 struct IDXGIKeyedMutex
;
27 class RenderDXGITextureHost final
: public RenderTextureHostSWGL
{
29 RenderDXGITextureHost(
30 RefPtr
<gfx::FileHandleWrapper
> aHandle
,
31 Maybe
<layers::GpuProcessTextureId
>& aGpuProcessTextureId
,
32 uint32_t aArrayIndex
, gfx::SurfaceFormat aFormat
, gfx::ColorSpace2
,
33 gfx::ColorRange aColorRange
, gfx::IntSize aSize
, bool aHasKeyedMutex
,
34 gfx::FenceInfo
& aAcquireFenceInfo
,
35 Maybe
<layers::GpuProcessQueryId
>& aGpuProcessQueryId
);
37 wr::WrExternalImage
Lock(uint8_t aChannelIndex
, gl::GLContext
* aGL
) override
;
38 void Unlock() override
;
39 void ClearCachedResources() override
;
41 gfx::IntSize
GetSize(uint8_t aChannelIndex
) const;
42 GLuint
GetGLHandle(uint8_t aChannelIndex
) const;
44 bool SyncObjectNeeded() override
;
46 RenderDXGITextureHost
* AsRenderDXGITextureHost() override
{ return this; }
48 gfx::ColorRange
GetColorRange() const { return mColorRange
; }
50 ID3D11Texture2D
* GetD3D11Texture2DWithGL();
51 ID3D11Texture2D
* GetD3D11Texture2D() { return mTexture
; }
53 // RenderTextureHostSWGL
54 gfx::SurfaceFormat
GetFormat() const override
{ return mFormat
; }
55 gfx::ColorDepth
GetColorDepth() const override
{
56 if (mFormat
== gfx::SurfaceFormat::P010
) {
57 return gfx::ColorDepth::COLOR_10
;
59 if (mFormat
== gfx::SurfaceFormat::P016
) {
60 return gfx::ColorDepth::COLOR_16
;
62 return gfx::ColorDepth::COLOR_8
;
64 size_t GetPlaneCount() const override
;
65 bool MapPlane(RenderCompositor
* aCompositor
, uint8_t aChannelIndex
,
66 PlaneInfo
& aPlaneInfo
) override
;
67 void UnmapPlanes() override
;
68 gfx::YUVRangedColorSpace
GetYUVColorSpace() const override
{
69 return ToYUVRangedColorSpace(ToYUVColorSpace(mColorSpace
), mColorRange
);
72 bool EnsureD3D11Texture2D(ID3D11Device
* aDevice
);
75 size_t Bytes() override
{
78 size_t bpp
= GetPlaneCount() > 1
79 ? (GetColorDepth() == gfx::ColorDepth::COLOR_8
? 1 : 2)
82 for (size_t i
= 0; i
< GetPlaneCount(); i
++) {
83 gfx::IntSize size
= GetSize(i
);
84 bytes
+= size
.width
* size
.height
* bpp
;
89 uint32_t ArrayIndex() const { return mArrayIndex
; }
91 void SetIsSoftwareDecodedVideo() override
{ mIsSoftwareDecodedVideo
= true; }
92 bool IsSoftwareDecodedVideo() override
{ return mIsSoftwareDecodedVideo
; }
94 RefPtr
<ID3D11Query
> GetQuery();
97 virtual ~RenderDXGITextureHost();
99 bool EnsureD3D11Texture2DWithGL();
100 bool EnsureLockable();
102 void DeleteTextureHandle();
104 RefPtr
<gl::GLContext
> mGL
;
106 RefPtr
<gfx::FileHandleWrapper
> mHandle
;
107 Maybe
<layers::GpuProcessTextureId
> mGpuProcessTextureId
;
108 Maybe
<layers::GpuProcessQueryId
> mGpuProcessQueryId
;
109 RefPtr
<ID3D11Texture2D
> mTexture
;
110 uint32_t mArrayIndex
= 0;
111 RefPtr
<IDXGIKeyedMutex
> mKeyedMutex
;
113 // Temporary state between MapPlane and UnmapPlanes.
114 RefPtr
<ID3D11DeviceContext
> mDeviceContext
;
115 RefPtr
<ID3D11Texture2D
> mCpuTexture
;
116 D3D11_MAPPED_SUBRESOURCE mMappedSubresource
;
119 EGLStreamKHR mStream
;
121 // We could use NV12 format for this texture. So, we might have 2 gl texture
122 // handles for Y and CbCr data.
123 GLuint mTextureHandle
[2];
125 bool mIsSoftwareDecodedVideo
= false;
127 RefPtr
<layers::FenceD3D11
> mAcquireFence
;
130 const gfx::SurfaceFormat mFormat
;
131 const gfx::ColorSpace2 mColorSpace
;
132 const gfx::ColorRange mColorRange
;
133 const gfx::IntSize mSize
;
134 const bool mHasKeyedMutex
;
135 const gfx::FenceInfo mAcquireFenceInfo
;
141 class RenderDXGIYCbCrTextureHost final
: public RenderTextureHostSWGL
{
143 explicit RenderDXGIYCbCrTextureHost(
144 RefPtr
<gfx::FileHandleWrapper
> (&aHandles
)[3],
145 gfx::YUVColorSpace aYUVColorSpace
, gfx::ColorDepth aColorDepth
,
146 gfx::ColorRange aColorRange
, gfx::IntSize aSizeY
, gfx::IntSize aSizeCbCr
);
148 RenderDXGIYCbCrTextureHost
* AsRenderDXGIYCbCrTextureHost() override
{
152 wr::WrExternalImage
Lock(uint8_t aChannelIndex
, gl::GLContext
* aGL
) override
;
153 void Unlock() override
;
154 void ClearCachedResources() override
;
156 gfx::IntSize
GetSize(uint8_t aChannelIndex
) const;
157 GLuint
GetGLHandle(uint8_t aChannelIndex
) const;
159 bool SyncObjectNeeded() override
{ return true; }
161 gfx::ColorRange
GetColorRange() const { return mColorRange
; }
163 // RenderTextureHostSWGL
164 gfx::SurfaceFormat
GetFormat() const override
{
165 return gfx::SurfaceFormat::YUV
;
167 gfx::ColorDepth
GetColorDepth() const override
{ return mColorDepth
; }
168 size_t GetPlaneCount() const override
{ return 3; }
169 bool MapPlane(RenderCompositor
* aCompositor
, uint8_t aChannelIndex
,
170 PlaneInfo
& aPlaneInfo
) override
;
171 void UnmapPlanes() override
;
172 gfx::YUVRangedColorSpace
GetYUVColorSpace() const override
{
173 return ToYUVRangedColorSpace(mYUVColorSpace
, GetColorRange());
176 bool EnsureD3D11Texture2D(ID3D11Device
* aDevice
);
179 ID3D11Texture2D
* GetD3D11Texture2D(uint8_t aChannelIndex
) {
180 return mTextures
[aChannelIndex
];
183 size_t Bytes() override
{
186 size_t bpp
= mColorDepth
== gfx::ColorDepth::COLOR_8
? 1 : 2;
188 for (size_t i
= 0; i
< GetPlaneCount(); i
++) {
189 gfx::IntSize size
= GetSize(i
);
190 bytes
+= size
.width
* size
.height
* bpp
;
196 virtual ~RenderDXGIYCbCrTextureHost();
198 bool EnsureLockable();
200 void DeleteTextureHandle();
202 RefPtr
<gl::GLContext
> mGL
;
204 RefPtr
<gfx::FileHandleWrapper
> mHandles
[3];
205 RefPtr
<ID3D11Texture2D
> mTextures
[3];
206 RefPtr
<IDXGIKeyedMutex
> mKeyedMutexs
[3];
208 EGLSurface mSurfaces
[3];
209 EGLStreamKHR mStreams
[3];
211 // The gl handles for Y, Cb and Cr data.
212 GLuint mTextureHandles
[3];
214 // Temporary state between MapPlane and UnmapPlanes.
215 RefPtr
<ID3D11DeviceContext
> mDeviceContext
;
216 RefPtr
<ID3D11Texture2D
> mCpuTexture
[3];
218 gfx::YUVColorSpace mYUVColorSpace
;
219 gfx::ColorDepth mColorDepth
;
220 gfx::ColorRange mColorRange
;
222 gfx::IntSize mSizeCbCr
;
228 } // namespace mozilla
230 #endif // MOZILLA_GFX_RENDERD3D11TEXTUREHOST_H