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/. */
11 #include "D3D11TextureIMFSampleImage.h"
13 #include "mozilla/gfx/SourceSurfaceRawData.h"
14 #include "mozilla/layers/KnowsCompositor.h"
15 #include "mozilla/layers/TextureForwarder.h"
23 RefPtr
<IMFSampleWrapper
> IMFSampleWrapper::Create(IMFSample
* aVideoSample
) {
24 RefPtr
<IMFSampleWrapper
> wrapper
= new IMFSampleWrapper(aVideoSample
);
28 IMFSampleWrapper::IMFSampleWrapper(IMFSample
* aVideoSample
)
29 : mVideoSample(aVideoSample
) {}
31 IMFSampleWrapper::~IMFSampleWrapper() {}
33 void IMFSampleWrapper::ClearVideoSample() { mVideoSample
= nullptr; }
35 D3D11TextureIMFSampleImage::D3D11TextureIMFSampleImage(
36 IMFSample
* aVideoSample
, ID3D11Texture2D
* aTexture
, uint32_t aArrayIndex
,
37 const gfx::IntSize
& aSize
, const gfx::IntRect
& aRect
,
38 gfx::ColorSpace2 aColorSpace
, gfx::ColorRange aColorRange
)
39 : Image(nullptr, ImageFormat::D3D11_TEXTURE_IMF_SAMPLE
),
40 mVideoSample(IMFSampleWrapper::Create(aVideoSample
)),
42 mArrayIndex(aArrayIndex
),
45 mColorSpace(aColorSpace
),
46 mColorRange(aColorRange
) {
47 MOZ_ASSERT(XRE_IsGPUProcess());
50 void D3D11TextureIMFSampleImage::AllocateTextureClient(
51 KnowsCompositor
* aKnowsCompositor
, RefPtr
<IMFSampleUsageInfo
> aUsageInfo
) {
52 mTextureClient
= D3D11TextureData::CreateTextureClient(
53 mTexture
, mArrayIndex
, mSize
, gfx::SurfaceFormat::NV12
, mColorSpace
,
54 mColorRange
, aKnowsCompositor
, aUsageInfo
);
55 MOZ_ASSERT(mTextureClient
);
58 gfx::IntSize
D3D11TextureIMFSampleImage::GetSize() const { return mSize
; }
60 TextureClient
* D3D11TextureIMFSampleImage::GetTextureClient(
61 KnowsCompositor
* aKnowsCompositor
) {
62 return mTextureClient
;
65 already_AddRefed
<gfx::SourceSurface
>
66 D3D11TextureIMFSampleImage::GetAsSourceSurface() {
67 RefPtr
<ID3D11Texture2D
> src
= GetTexture();
69 gfxWarning() << "Cannot readback from shared texture because no texture is "
74 RefPtr
<gfx::SourceSurface
> sourceSurface
=
75 gfx::Factory::CreateBGRA8DataSourceSurfaceForD3D11Texture(src
,
78 // There is a case that mSize and size of mTexture are different. In this
79 // case, size of sourceSurface is different from mSize.
80 if (sourceSurface
&& sourceSurface
->GetSize() != mSize
) {
81 MOZ_RELEASE_ASSERT(sourceSurface
->GetType() == SurfaceType::DATA_ALIGNED
);
82 RefPtr
<gfx::SourceSurfaceAlignedRawData
> rawData
=
83 static_cast<gfx::SourceSurfaceAlignedRawData
*>(sourceSurface
.get());
84 auto data
= rawData
->GetData();
85 auto stride
= rawData
->Stride();
86 auto size
= rawData
->GetSize();
87 auto format
= rawData
->GetFormat();
88 sourceSurface
= gfx::Factory::CreateWrappingDataSourceSurface(
89 data
, stride
, Min(size
, mSize
), format
,
91 RefPtr
<SourceSurfaceAlignedRawData
> surface
=
92 dont_AddRef(static_cast<SourceSurfaceAlignedRawData
*>(aClosure
));
94 rawData
.forget().take());
96 return sourceSurface
.forget();
99 nsresult
D3D11TextureIMFSampleImage::BuildSurfaceDescriptorBuffer(
100 SurfaceDescriptorBuffer
& aSdBuffer
, BuildSdbFlags aFlags
,
101 const std::function
<MemoryOrShmem(uint32_t)>& aAllocate
) {
102 RefPtr
<ID3D11Texture2D
> src
= GetTexture();
104 gfxWarning() << "Cannot readback from shared texture because no texture is "
106 return NS_ERROR_FAILURE
;
109 return gfx::Factory::CreateSdbForD3D11Texture(src
, mSize
, aSdBuffer
,
113 ID3D11Texture2D
* D3D11TextureIMFSampleImage::GetTexture() const {
117 RefPtr
<IMFSampleWrapper
> D3D11TextureIMFSampleImage::GetIMFSampleWrapper() {
121 } // namespace layers
122 } // namespace mozilla