File: D3D11ZeroCopyTextureImage.cpp

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (166 lines) | stat: -rw-r--r-- 6,167 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <d3d11.h>
#include <mfobjects.h>

#include "D3D11ZeroCopyTextureImage.h"
#include "D3D11TextureWrapper.h"
#include "WMF.h"
#include "mozilla/gfx/SourceSurfaceRawData.h"
#include "mozilla/layers/FenceD3D11.h"
#include "mozilla/layers/KnowsCompositor.h"
#include "mozilla/layers/TextureForwarder.h"

namespace mozilla {
namespace layers {

using namespace gfx;

/* static */
RefPtr<IMFSampleWrapper> IMFSampleWrapper::Create(IMFSample* aVideoSample) {
  RefPtr<IMFSampleWrapper> wrapper = new IMFSampleWrapper(aVideoSample);
  return wrapper;
}

IMFSampleWrapper::IMFSampleWrapper(IMFSample* aVideoSample)
    : mVideoSample(aVideoSample) {}

IMFSampleWrapper::~IMFSampleWrapper() {}

void IMFSampleWrapper::ClearVideoSample() { mVideoSample = nullptr; }

D3D11ZeroCopyTextureImage::D3D11ZeroCopyTextureImage(
    ID3D11Texture2D* aTexture, const uint32_t aArrayIndex,
    const gfx::IntSize& aSize, const gfx::IntRect& aRect,
    const gfx::SurfaceFormat aFormat, const gfx::ColorSpace2 aColorSpace,
    const gfx::ColorRange aColorRange, const gfx::ColorDepth aColorDepth)
    : Image(nullptr, ImageFormat::D3D11_TEXTURE_ZERO_COPY),
      mTexture(aTexture),
      mArrayIndex(aArrayIndex),
      mSize(aSize),
      mPictureRect(aRect),
      mFormat(aFormat),
      mColorSpace(aColorSpace),
      mColorRange(aColorRange),
      mColorDepth(aColorDepth) {
  MOZ_ASSERT(XRE_IsGPUProcess());
  MOZ_ASSERT(mFormat == gfx::SurfaceFormat::NV12 ||
             mFormat == gfx::SurfaceFormat::P010 ||
             mFormat == gfx::SurfaceFormat::P016);
}

D3D11ZeroCopyTextureImage::~D3D11ZeroCopyTextureImage() {
  // XXX add shutdown check for fence?
}

void D3D11ZeroCopyTextureImage::AllocateTextureClient(
    KnowsCompositor* aKnowsCompositor, RefPtr<ZeroCopyUsageInfo> aUsageInfo,
    const RefPtr<FenceD3D11> aWriteFence) {
  if (aWriteFence) {
    aWriteFence->IncrementAndSignal();
  }
  mTextureClient = D3D11TextureData::CreateTextureClient(
      mTexture, mArrayIndex, mSize, mFormat, mColorSpace, mColorRange,
      aKnowsCompositor, aUsageInfo, aWriteFence);
  MOZ_ASSERT(mTextureClient);
}

gfx::IntSize D3D11ZeroCopyTextureImage::GetSize() const { return mSize; }

TextureClient* D3D11ZeroCopyTextureImage::GetTextureClient(
    KnowsCompositor* aKnowsCompositor) {
  return mTextureClient;
}

already_AddRefed<gfx::SourceSurface>
D3D11ZeroCopyTextureImage::GetAsSourceSurface() {
  RefPtr<ID3D11Texture2D> src = GetTexture();
  if (!src) {
    gfxWarning() << "Cannot readback from shared texture because no texture is "
                    "available.";
    return nullptr;
  }

  RefPtr<gfx::SourceSurface> sourceSurface =
      gfx::Factory::CreateBGRA8DataSourceSurfaceForD3D11Texture(
          src, mArrayIndex, mColorSpace, mColorRange);

  // There is a case that mSize and size of mTexture are different. In this
  // case, size of sourceSurface is different from mSize.
  if (sourceSurface && sourceSurface->GetSize() != mSize) {
    MOZ_RELEASE_ASSERT(sourceSurface->GetType() == SurfaceType::DATA_ALIGNED);
    RefPtr<gfx::SourceSurfaceAlignedRawData> rawData =
        static_cast<gfx::SourceSurfaceAlignedRawData*>(sourceSurface.get());
    auto data = rawData->GetData();
    auto stride = rawData->Stride();
    auto size = rawData->GetSize();
    auto format = rawData->GetFormat();
    sourceSurface = gfx::Factory::CreateWrappingDataSourceSurface(
        data, stride, Min(size, mSize), format,
        [](void* aClosure) {
          RefPtr<SourceSurfaceAlignedRawData> surface =
              dont_AddRef(static_cast<SourceSurfaceAlignedRawData*>(aClosure));
        },
        rawData.forget().take());
  }
  return sourceSurface.forget();
}

nsresult D3D11ZeroCopyTextureImage::BuildSurfaceDescriptorBuffer(
    SurfaceDescriptorBuffer& aSdBuffer, BuildSdbFlags aFlags,
    const std::function<MemoryOrShmem(uint32_t)>& aAllocate) {
  RefPtr<ID3D11Texture2D> src = GetTexture();
  if (!src) {
    gfxWarning() << "Cannot readback from shared texture because no texture is "
                    "available.";
    return NS_ERROR_FAILURE;
  }

  nsresult rv =
      gfx::Factory::CreateSdbForD3D11Texture(src, mSize, aSdBuffer, aAllocate);
  if (rv != NS_ERROR_NOT_IMPLEMENTED) {
    // TODO(aosmond): We only support BGRA on this path, but depending on
    // aFlags, we may be able to return a YCbCr format without conversion.
    return rv;
  }

  return Image::BuildSurfaceDescriptorBuffer(aSdBuffer, aFlags, aAllocate);
}

ID3D11Texture2D* D3D11ZeroCopyTextureImage::GetTexture() const {
  return mTexture;
}

D3D11TextureIMFSampleImage::D3D11TextureIMFSampleImage(
    IMFSample* aVideoSample, ID3D11Texture2D* aTexture,
    const uint32_t aArrayIndex, const gfx::IntSize& aSize,
    const gfx::IntRect& aRect, const gfx::SurfaceFormat aFormat,
    const gfx::ColorSpace2 aColorSpace, const gfx::ColorRange aColorRange,
    const gfx::ColorDepth aColorDepth)
    : D3D11ZeroCopyTextureImage(aTexture, aArrayIndex, aSize, aRect, aFormat,
                                aColorSpace, aColorRange, aColorDepth),
      mVideoSample(IMFSampleWrapper::Create(aVideoSample)) {
  MOZ_ASSERT(XRE_IsGPUProcess());
}

RefPtr<IMFSampleWrapper> D3D11TextureIMFSampleImage::GetIMFSampleWrapper() {
  return mVideoSample;
}

D3D11TextureAVFrameImage::D3D11TextureAVFrameImage(
    D3D11TextureWrapper* aWrapper, const gfx::IntSize& aSize,
    const gfx::IntRect& aRect, const gfx::ColorSpace2 aColorSpace,
    const gfx::ColorRange aColorRange, const gfx::ColorDepth aColorDepth)
    : D3D11ZeroCopyTextureImage(aWrapper->GetTexture(), aWrapper->mArrayIdx,
                                aSize, aRect, aWrapper->mFormat, aColorSpace,
                                aColorRange, aColorDepth),
      mWrapper(aWrapper) {
  MOZ_ASSERT(XRE_IsGPUProcess());
}

}  // namespace layers
}  // namespace mozilla