RenderD3D11TextureHost.h (7167B)
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_RENDERD3D11TEXTUREHOST_H 8 #define MOZILLA_GFX_RENDERD3D11TEXTUREHOST_H 9 10 #include <d3d11.h> 11 12 #include "GLTypes.h" 13 #include "mozilla/gfx/FileHandleWrapper.h" 14 #include "RenderTextureHostSWGL.h" 15 16 struct ID3D11Texture2D; 17 struct IDCompositionTexture; 18 struct IDXGIKeyedMutex; 19 20 namespace mozilla { 21 22 namespace layers { 23 class FenceD3D11; 24 } // namespace layers 25 26 namespace wr { 27 28 class RenderDXGITextureHost final : public RenderTextureHostSWGL { 29 public: 30 RenderDXGITextureHost( 31 const RefPtr<gfx::FileHandleWrapper> aHandle, 32 const Maybe<layers::GpuProcessTextureId>& aGpuProcessTextureId, 33 const uint32_t aArrayIndex, const gfx::SurfaceFormat aFormat, 34 const gfx::ColorSpace2 aColorSpace, const gfx::ColorRange aColorRange, 35 const gfx::IntSize aSize, const bool aHasKeyedMutex, 36 const Maybe<layers::CompositeProcessFencesHolderId>& aFencesHolderId); 37 38 static bool UseDCompositionTextureOverlay(gfx::SurfaceFormat aFormat); 39 40 wr::WrExternalImage Lock(uint8_t aChannelIndex, gl::GLContext* aGL) override; 41 void Unlock() override; 42 void ClearCachedResources() override; 43 44 gfx::IntSize GetSize(uint8_t aChannelIndex) const; 45 GLuint GetGLHandle(uint8_t aChannelIndex) const; 46 47 bool SyncObjectNeeded() override; 48 49 RenderDXGITextureHost* AsRenderDXGITextureHost() override { return this; } 50 51 gfx::ColorRange GetColorRange() const { return mColorRange; } 52 53 ID3D11Texture2D* GetD3D11Texture2DWithGL(); 54 ID3D11Texture2D* GetD3D11Texture2D() { return mTexture; } 55 56 IDCompositionTexture* GetDCompositionTexture(); 57 58 // RenderTextureHostSWGL 59 gfx::SurfaceFormat GetFormat() const override { return mFormat; } 60 gfx::ColorDepth GetColorDepth() const override { 61 if (mFormat == gfx::SurfaceFormat::P010) { 62 return gfx::ColorDepth::COLOR_10; 63 } 64 if (mFormat == gfx::SurfaceFormat::P016) { 65 return gfx::ColorDepth::COLOR_16; 66 } 67 return gfx::ColorDepth::COLOR_8; 68 } 69 size_t GetPlaneCount() const override; 70 bool MapPlane(RenderCompositor* aCompositor, uint8_t aChannelIndex, 71 PlaneInfo& aPlaneInfo) override; 72 void UnmapPlanes() override; 73 gfx::YUVRangedColorSpace GetYUVColorSpace() const override { 74 return ToYUVRangedColorSpace(ToYUVColorSpace(mColorSpace), mColorRange); 75 } 76 77 bool EnsureD3D11Texture2D(ID3D11Device* aDevice); 78 bool LockInternal(); 79 80 size_t Bytes() override { 81 size_t bytes = 0; 82 83 size_t bpp = GetPlaneCount() > 1 84 ? (GetColorDepth() == gfx::ColorDepth::COLOR_8 ? 1 : 2) 85 : 4; 86 87 for (size_t i = 0; i < GetPlaneCount(); i++) { 88 gfx::IntSize size = GetSize(i); 89 bytes += size.width * size.height * bpp; 90 } 91 return bytes; 92 } 93 94 uint32_t ArrayIndex() const { return mArrayIndex; } 95 96 void SetIsSoftwareDecodedVideo() override { mIsSoftwareDecodedVideo = true; } 97 bool IsSoftwareDecodedVideo() override { return mIsSoftwareDecodedVideo; } 98 99 private: 100 virtual ~RenderDXGITextureHost(); 101 102 bool EnsureD3D11Texture2DWithGL(); 103 bool EnsureLockable(); 104 105 void DeleteTextureHandle(); 106 107 RefPtr<gl::GLContext> mGL; 108 109 const RefPtr<gfx::FileHandleWrapper> mHandle; 110 const Maybe<layers::GpuProcessTextureId> mGpuProcessTextureId; 111 RefPtr<ID3D11Texture2D> mTexture; 112 const uint32_t mArrayIndex; 113 RefPtr<IDXGIKeyedMutex> mKeyedMutex; 114 RefPtr<IDCompositionTexture> mDCompositionTexture; 115 116 // Temporary state between MapPlane and UnmapPlanes. 117 RefPtr<ID3D11DeviceContext> mDeviceContext; 118 RefPtr<ID3D11Texture2D> mCpuTexture; 119 D3D11_MAPPED_SUBRESOURCE mMappedSubresource; 120 121 EGLSurface mSurface; 122 EGLStreamKHR mStream; 123 124 // We could use NV12 format for this texture. So, we might have 2 gl texture 125 // handles for Y and CbCr data. 126 GLuint mTextureHandle[2]; 127 128 bool mIsSoftwareDecodedVideo = false; 129 130 public: 131 const gfx::SurfaceFormat mFormat; 132 const gfx::ColorSpace2 mColorSpace; 133 const gfx::ColorRange mColorRange; 134 const gfx::IntSize mSize; 135 const bool mHasKeyedMutex; 136 const Maybe<layers::CompositeProcessFencesHolderId> mFencesHolderId; 137 138 private: 139 bool mLocked; 140 }; 141 142 class RenderDXGIYCbCrTextureHost final : public RenderTextureHostSWGL { 143 public: 144 explicit RenderDXGIYCbCrTextureHost( 145 const RefPtr<gfx::FileHandleWrapper> (&aHandles)[3], 146 const gfx::YUVColorSpace aYUVColorSpace, 147 const gfx::ColorDepth aColorDepth, const gfx::ColorRange aColorRange, 148 const gfx::IntSize aSizeY, const gfx::IntSize aSizeCbCr, 149 const layers::CompositeProcessFencesHolderId aFencesHolderId); 150 151 RenderDXGIYCbCrTextureHost* AsRenderDXGIYCbCrTextureHost() override { 152 return this; 153 } 154 155 wr::WrExternalImage Lock(uint8_t aChannelIndex, gl::GLContext* aGL) override; 156 void Unlock() override; 157 void ClearCachedResources() override; 158 159 gfx::IntSize GetSize(uint8_t aChannelIndex) const; 160 GLuint GetGLHandle(uint8_t aChannelIndex) const; 161 162 bool SyncObjectNeeded() override { return true; } 163 164 gfx::ColorRange GetColorRange() const { return mColorRange; } 165 166 // RenderTextureHostSWGL 167 gfx::SurfaceFormat GetFormat() const override { 168 return gfx::SurfaceFormat::YUV420; 169 } 170 gfx::ColorDepth GetColorDepth() const override { return mColorDepth; } 171 size_t GetPlaneCount() const override { return 3; } 172 bool MapPlane(RenderCompositor* aCompositor, uint8_t aChannelIndex, 173 PlaneInfo& aPlaneInfo) override; 174 void UnmapPlanes() override; 175 gfx::YUVRangedColorSpace GetYUVColorSpace() const override { 176 return ToYUVRangedColorSpace(mYUVColorSpace, GetColorRange()); 177 } 178 179 bool EnsureD3D11Texture2D(ID3D11Device* aDevice); 180 bool LockInternal(); 181 182 ID3D11Texture2D* GetD3D11Texture2D(uint8_t aChannelIndex) { 183 return mTextures[aChannelIndex]; 184 } 185 186 size_t Bytes() override { 187 size_t bytes = 0; 188 189 size_t bpp = mColorDepth == gfx::ColorDepth::COLOR_8 ? 1 : 2; 190 191 for (size_t i = 0; i < GetPlaneCount(); i++) { 192 gfx::IntSize size = GetSize(i); 193 bytes += size.width * size.height * bpp; 194 } 195 return bytes; 196 } 197 198 private: 199 virtual ~RenderDXGIYCbCrTextureHost(); 200 201 bool EnsureLockable(); 202 203 void DeleteTextureHandle(); 204 205 RefPtr<gl::GLContext> mGL; 206 207 RefPtr<gfx::FileHandleWrapper> mHandles[3]; 208 RefPtr<ID3D11Texture2D> mTextures[3]; 209 RefPtr<ID3D11Device> mDevice; 210 211 EGLSurface mSurfaces[3]; 212 EGLStreamKHR mStreams[3]; 213 214 // The gl handles for Y, Cb and Cr data. 215 GLuint mTextureHandles[3]; 216 217 // Temporary state between MapPlane and UnmapPlanes. 218 RefPtr<ID3D11DeviceContext> mDeviceContext; 219 RefPtr<ID3D11Texture2D> mCpuTexture[3]; 220 221 const gfx::YUVColorSpace mYUVColorSpace; 222 const gfx::ColorDepth mColorDepth; 223 const gfx::ColorRange mColorRange; 224 const gfx::IntSize mSizeY; 225 const gfx::IntSize mSizeCbCr; 226 const layers::CompositeProcessFencesHolderId mFencesHolderId; 227 228 bool mLocked = false; 229 }; 230 231 } // namespace wr 232 } // namespace mozilla 233 234 #endif // MOZILLA_GFX_RENDERD3D11TEXTUREHOST_H