Bug 1919083 - [ci] Enable os-integration variant for more suites, r=jmaher
[gecko.git] / gfx / layers / D3D11TextureIMFSampleImage.cpp
blobc965b6580d9ed0fea14d5b13a88de2bd1ff8fc02
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 #include <d3d11.h>
8 #include <memory>
9 #include <mfobjects.h>
11 #include "D3D11TextureIMFSampleImage.h"
12 #include "WMF.h"
13 #include "mozilla/gfx/SourceSurfaceRawData.h"
14 #include "mozilla/layers/KnowsCompositor.h"
15 #include "mozilla/layers/TextureForwarder.h"
17 namespace mozilla {
18 namespace layers {
20 using namespace gfx;
22 /* static */
23 RefPtr<IMFSampleWrapper> IMFSampleWrapper::Create(IMFSample* aVideoSample) {
24 RefPtr<IMFSampleWrapper> wrapper = new IMFSampleWrapper(aVideoSample);
25 return wrapper;
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)),
41 mTexture(aTexture),
42 mArrayIndex(aArrayIndex),
43 mSize(aSize),
44 mPictureRect(aRect),
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();
68 if (!src) {
69 gfxWarning() << "Cannot readback from shared texture because no texture is "
70 "available.";
71 return nullptr;
74 RefPtr<gfx::SourceSurface> sourceSurface =
75 gfx::Factory::CreateBGRA8DataSourceSurfaceForD3D11Texture(src,
76 mArrayIndex);
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,
90 [](void* aClosure) {
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();
103 if (!src) {
104 gfxWarning() << "Cannot readback from shared texture because no texture is "
105 "available.";
106 return NS_ERROR_FAILURE;
109 return gfx::Factory::CreateSdbForD3D11Texture(src, mSize, aSdBuffer,
110 aAllocate);
113 ID3D11Texture2D* D3D11TextureIMFSampleImage::GetTexture() const {
114 return mTexture;
117 RefPtr<IMFSampleWrapper> D3D11TextureIMFSampleImage::GetIMFSampleWrapper() {
118 return mVideoSample;
121 } // namespace layers
122 } // namespace mozilla