Bug 1521243 - Show a warning for invalid declarations and filter icon for overridden...
[gecko.git] / gfx / layers / D3D11ShareHandleImage.cpp
blobbe44f326d60356a5daecb6bb5675be86cd0faaa6
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 "WMF.h"
8 #include "D3D11ShareHandleImage.h"
9 #include "gfxImageSurface.h"
10 #include "gfxWindowsPlatform.h"
11 #include "mozilla/layers/TextureClient.h"
12 #include "mozilla/layers/TextureD3D11.h"
13 #include "mozilla/layers/CompositableClient.h"
14 #include "mozilla/layers/CompositableForwarder.h"
15 #include "mozilla/gfx/DeviceManagerDx.h"
16 #include "d3d11.h"
17 #include "gfxPrefs.h"
18 #include "DXVA2Manager.h"
19 #include <memory>
21 namespace mozilla {
22 namespace layers {
24 using namespace gfx;
26 D3D11ShareHandleImage::D3D11ShareHandleImage(const gfx::IntSize& aSize,
27 const gfx::IntRect& aRect,
28 const GUID& aSourceFormat)
29 : Image(nullptr, ImageFormat::D3D11_SHARE_HANDLE_TEXTURE),
30 mSize(aSize),
31 mPictureRect(aRect),
32 mSourceFormat(aSourceFormat)
36 bool D3D11ShareHandleImage::AllocateTexture(D3D11RecycleAllocator* aAllocator,
37 ID3D11Device* aDevice) {
38 if (aAllocator) {
39 if (mSourceFormat == MFVideoFormat_NV12 &&
40 gfxPrefs::PDMWMFUseNV12Format() &&
41 gfx::DeviceManagerDx::Get()->CanUseNV12()) {
42 mTextureClient =
43 aAllocator->CreateOrRecycleClient(gfx::SurfaceFormat::NV12, mSize);
44 } else if (((mSourceFormat == MFVideoFormat_P010 &&
45 gfx::DeviceManagerDx::Get()->CanUseP010()) ||
46 (mSourceFormat == MFVideoFormat_P016 &&
47 gfx::DeviceManagerDx::Get()->CanUseP016())) &&
48 gfxPrefs::PDMWMFUseNV12Format()) {
49 mTextureClient = aAllocator->CreateOrRecycleClient(
50 mSourceFormat == MFVideoFormat_P010 ? gfx::SurfaceFormat::P010
51 : gfx::SurfaceFormat::P016,
52 mSize);
53 } else {
54 mTextureClient = aAllocator->CreateOrRecycleClient(
55 gfx::SurfaceFormat::B8G8R8A8, mSize);
57 if (mTextureClient) {
58 mTexture =
59 static_cast<D3D11TextureData*>(mTextureClient->GetInternalData())
60 ->GetD3D11Texture();
61 return true;
63 return false;
64 } else {
65 MOZ_ASSERT(aDevice);
66 CD3D11_TEXTURE2D_DESC newDesc(
67 DXGI_FORMAT_B8G8R8A8_UNORM, mSize.width, mSize.height, 1, 1,
68 D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
69 newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
71 HRESULT hr =
72 aDevice->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(mTexture));
73 return SUCCEEDED(hr);
77 gfx::IntSize D3D11ShareHandleImage::GetSize() const { return mSize; }
79 TextureClient* D3D11ShareHandleImage::GetTextureClient(
80 KnowsCompositor* aForwarder) {
81 return mTextureClient;
84 already_AddRefed<gfx::SourceSurface>
85 D3D11ShareHandleImage::GetAsSourceSurface() {
86 RefPtr<ID3D11Texture2D> texture = GetTexture();
87 if (!texture) {
88 gfxWarning() << "Cannot readback from shared texture because no texture is "
89 "available.";
90 return nullptr;
93 RefPtr<ID3D11Device> device;
94 texture->GetDevice(getter_AddRefs(device));
96 D3D11_TEXTURE2D_DESC desc;
97 texture->GetDesc(&desc);
99 HRESULT hr;
101 if (desc.Format != DXGI_FORMAT_B8G8R8A8_UNORM) {
102 nsAutoCString error;
103 std::unique_ptr<DXVA2Manager> manager(
104 DXVA2Manager::CreateD3D11DXVA(nullptr, error, device));
106 if (!manager) {
107 gfxWarning() << "Failed to create DXVA2 manager!";
108 return nullptr;
111 RefPtr<ID3D11Texture2D> outTexture;
113 hr = manager->CopyToBGRATexture(texture, mSourceFormat,
114 getter_AddRefs(outTexture));
116 if (FAILED(hr)) {
117 gfxWarning() << "Failed to copy to BGRA texture.";
118 return nullptr;
121 texture = outTexture;
122 texture->GetDesc(&desc);
125 CD3D11_TEXTURE2D_DESC softDesc(desc.Format, desc.Width, desc.Height);
126 softDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
127 softDesc.BindFlags = 0;
128 softDesc.MiscFlags = 0;
129 softDesc.MipLevels = 1;
130 softDesc.Usage = D3D11_USAGE_STAGING;
132 RefPtr<ID3D11Texture2D> softTexture;
133 hr = device->CreateTexture2D(
134 &softDesc, NULL,
135 static_cast<ID3D11Texture2D**>(getter_AddRefs(softTexture)));
137 if (FAILED(hr)) {
138 NS_WARNING("Failed to create 2D staging texture.");
139 return nullptr;
142 RefPtr<ID3D11DeviceContext> context;
143 device->GetImmediateContext(getter_AddRefs(context));
144 if (!context) {
145 gfxCriticalError() << "Failed to get immediate context.";
146 return nullptr;
149 RefPtr<IDXGIKeyedMutex> mutex;
150 hr = texture->QueryInterface((IDXGIKeyedMutex**)getter_AddRefs(mutex));
152 if (SUCCEEDED(hr) && mutex) {
153 hr = mutex->AcquireSync(0, 2000);
154 if (hr != S_OK) {
155 NS_WARNING("Acquire sync didn't manage to return within 2 seconds.");
158 context->CopyResource(softTexture, texture);
159 if (SUCCEEDED(hr) && mutex) {
160 mutex->ReleaseSync(0);
163 RefPtr<gfx::DataSourceSurface> surface =
164 gfx::Factory::CreateDataSourceSurface(mSize,
165 gfx::SurfaceFormat::B8G8R8A8);
166 if (NS_WARN_IF(!surface)) {
167 return nullptr;
170 gfx::DataSourceSurface::MappedSurface mappedSurface;
171 if (!surface->Map(gfx::DataSourceSurface::WRITE, &mappedSurface)) {
172 return nullptr;
175 D3D11_MAPPED_SUBRESOURCE map;
176 hr = context->Map(softTexture, 0, D3D11_MAP_READ, 0, &map);
177 if (!SUCCEEDED(hr)) {
178 surface->Unmap();
179 return nullptr;
182 for (int y = 0; y < mSize.height; y++) {
183 memcpy(mappedSurface.mData + mappedSurface.mStride * y,
184 (unsigned char*)(map.pData) + map.RowPitch * y, mSize.width * 4);
187 context->Unmap(softTexture, 0);
188 surface->Unmap();
190 return surface.forget();
193 ID3D11Texture2D* D3D11ShareHandleImage::GetTexture() const { return mTexture; }
195 already_AddRefed<TextureClient> D3D11RecycleAllocator::Allocate(
196 gfx::SurfaceFormat aFormat, gfx::IntSize aSize, BackendSelector aSelector,
197 TextureFlags aTextureFlags, TextureAllocationFlags aAllocFlags) {
198 return CreateD3D11TextureClientWithDevice(
199 aSize, aFormat, aTextureFlags, aAllocFlags, mDevice,
200 mSurfaceAllocator->GetTextureForwarder());
203 already_AddRefed<TextureClient> D3D11RecycleAllocator::CreateOrRecycleClient(
204 gfx::SurfaceFormat aFormat, const gfx::IntSize& aSize) {
205 // When CompositorDevice or ContentDevice is updated,
206 // we could not reuse old D3D11Textures. It could cause video flickering.
207 RefPtr<ID3D11Device> device = gfx::DeviceManagerDx::Get()->GetImageDevice();
208 if (!!mImageDevice && mImageDevice != device) {
209 ShrinkToMinimumSize();
211 mImageDevice = device;
213 TextureAllocationFlags allocFlags = TextureAllocationFlags::ALLOC_DEFAULT;
214 if (gfxPrefs::PDMWMFUseSyncTexture() ||
215 mDevice == DeviceManagerDx::Get()->GetCompositorDevice()) {
216 // If our device is the compositor device, we don't need any synchronization
217 // in practice.
218 allocFlags = TextureAllocationFlags::ALLOC_MANUAL_SYNCHRONIZATION;
220 RefPtr<TextureClient> textureClient =
221 CreateOrRecycle(aFormat, aSize, BackendSelector::Content,
222 layers::TextureFlags::DEFAULT, allocFlags);
223 return textureClient.forget();
226 } // namespace layers
227 } // namespace mozilla