2015-03-19 00:17:13 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* 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 "IMFYCbCrImage.h"
|
|
|
|
#include "mozilla/layers/TextureD3D11.h"
|
|
|
|
#include "mozilla/layers/CompositableClient.h"
|
|
|
|
#include "mozilla/layers/CompositableForwarder.h"
|
2016-08-24 01:18:55 +03:00
|
|
|
#include "mozilla/gfx/DeviceManagerDx.h"
|
2015-03-19 00:17:13 +03:00
|
|
|
#include "mozilla/gfx/Types.h"
|
|
|
|
#include "mozilla/layers/TextureClient.h"
|
2015-03-23 05:13:56 +03:00
|
|
|
#include "d3d9.h"
|
2015-03-19 00:17:13 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
IMFYCbCrImage::IMFYCbCrImage(IMFMediaBuffer* aBuffer, IMF2DBuffer* a2DBuffer)
|
2015-11-06 21:55:31 +03:00
|
|
|
: RecyclingPlanarYCbCrImage(nullptr)
|
2015-03-19 00:17:13 +03:00
|
|
|
, mBuffer(aBuffer)
|
|
|
|
, m2DBuffer(a2DBuffer)
|
|
|
|
{}
|
|
|
|
|
|
|
|
IMFYCbCrImage::~IMFYCbCrImage()
|
|
|
|
{
|
|
|
|
if (m2DBuffer) {
|
|
|
|
m2DBuffer->Unlock2D();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mBuffer->Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
static already_AddRefed<IDirect3DTexture9>
|
2015-03-23 05:13:56 +03:00
|
|
|
InitTextures(IDirect3DDevice9* aDevice,
|
|
|
|
const IntSize &aSize,
|
|
|
|
_D3DFORMAT aFormat,
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDirect3DSurface9>& aSurface,
|
2015-03-23 05:13:56 +03:00
|
|
|
HANDLE& aHandle,
|
|
|
|
D3DLOCKED_RECT& aLockedRect)
|
|
|
|
{
|
|
|
|
if (!aDevice) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDirect3DTexture9> result;
|
2015-03-23 05:13:56 +03:00
|
|
|
if (FAILED(aDevice->CreateTexture(aSize.width, aSize.height,
|
|
|
|
1, 0, aFormat, D3DPOOL_DEFAULT,
|
2015-10-18 07:40:10 +03:00
|
|
|
getter_AddRefs(result), &aHandle))) {
|
2015-03-23 05:13:56 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!result) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDirect3DTexture9> tmpTexture;
|
2015-03-23 05:13:56 +03:00
|
|
|
if (FAILED(aDevice->CreateTexture(aSize.width, aSize.height,
|
|
|
|
1, 0, aFormat, D3DPOOL_SYSTEMMEM,
|
2015-10-18 07:40:10 +03:00
|
|
|
getter_AddRefs(tmpTexture), nullptr))) {
|
2015-03-23 05:13:56 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!tmpTexture) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 07:40:10 +03:00
|
|
|
tmpTexture->GetSurfaceLevel(0, getter_AddRefs(aSurface));
|
2015-08-10 16:47:22 +03:00
|
|
|
if (FAILED(aSurface->LockRect(&aLockedRect, nullptr, 0)) ||
|
|
|
|
!aLockedRect.pBits) {
|
2015-03-23 05:13:56 +03:00
|
|
|
NS_WARNING("Could not lock surface");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-01 16:14:16 +03:00
|
|
|
return result.forget();
|
2015-03-23 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:19:11 +03:00
|
|
|
static bool
|
2015-03-23 05:13:56 +03:00
|
|
|
FinishTextures(IDirect3DDevice9* aDevice,
|
|
|
|
IDirect3DTexture9* aTexture,
|
|
|
|
IDirect3DSurface9* aSurface)
|
|
|
|
{
|
|
|
|
if (!aDevice) {
|
2015-06-05 17:56:06 +03:00
|
|
|
return false;
|
2015-03-23 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
2015-06-05 17:19:11 +03:00
|
|
|
HRESULT hr = aSurface->UnlockRect();
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDirect3DSurface9> dstSurface;
|
2015-06-05 17:19:11 +03:00
|
|
|
hr = aTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = aDevice->UpdateSurface(aSurface, nullptr, dstSurface, nullptr);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-03-23 05:13:56 +03:00
|
|
|
}
|
|
|
|
|
2017-02-13 20:20:00 +03:00
|
|
|
DXGIYCbCrTextureData*
|
|
|
|
IMFYCbCrImage::GetD3D11TextureData(Data aData, gfx::IntSize aSize)
|
2015-03-19 00:17:13 +03:00
|
|
|
{
|
2017-02-13 20:20:00 +03:00
|
|
|
HRESULT hr;
|
|
|
|
RefPtr<ID3D10Multithread> mt;
|
2016-01-12 08:08:21 +03:00
|
|
|
|
2017-06-20 21:45:40 +03:00
|
|
|
RefPtr<ID3D11Device> device = gfx::DeviceManagerDx::Get()->GetContentDevice();
|
2017-02-13 20:20:00 +03:00
|
|
|
|
2016-10-07 11:13:32 +03:00
|
|
|
if (!device) {
|
2017-06-20 21:45:40 +03:00
|
|
|
device = gfx::DeviceManagerDx::Get()->GetCompositorDevice();
|
2016-10-07 11:13:32 +03:00
|
|
|
}
|
2015-03-23 05:13:56 +03:00
|
|
|
|
2017-02-02 16:24:57 +03:00
|
|
|
hr = device->QueryInterface((ID3D10Multithread**)getter_AddRefs(mt));
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mt->GetMultithreadProtected()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-19 00:17:13 +03:00
|
|
|
|
2016-10-06 15:32:55 +03:00
|
|
|
if (!gfx::DeviceManagerDx::Get()->CanInitializeKeyedMutexTextures()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-13 20:20:00 +03:00
|
|
|
if (aData.mYStride < 0 || aData.mCbCrStride < 0) {
|
2016-08-02 08:53:06 +03:00
|
|
|
// D3D11 only supports unsigned stride values.
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-19 00:17:13 +03:00
|
|
|
|
2015-06-15 19:30:34 +03:00
|
|
|
CD3D11_TEXTURE2D_DESC newDesc(DXGI_FORMAT_R8_UNORM,
|
2017-02-13 20:20:00 +03:00
|
|
|
aData.mYSize.width, aData.mYSize.height, 1, 1);
|
2015-03-19 00:17:13 +03:00
|
|
|
|
2016-10-07 11:13:32 +03:00
|
|
|
if (device == gfx::DeviceManagerDx::Get()->GetCompositorDevice()) {
|
|
|
|
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
|
|
|
|
} else {
|
|
|
|
newDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
|
|
|
|
}
|
2015-03-19 00:17:13 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID3D11Texture2D> textureY;
|
2017-02-02 16:24:57 +03:00
|
|
|
hr = device->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(textureY));
|
2015-03-19 00:17:13 +03:00
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
|
|
|
|
|
2017-02-13 20:20:00 +03:00
|
|
|
newDesc.Width = aData.mCbCrSize.width;
|
|
|
|
newDesc.Height = aData.mCbCrSize.height;
|
2015-03-19 00:17:13 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID3D11Texture2D> textureCb;
|
2017-02-02 16:24:57 +03:00
|
|
|
hr = device->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(textureCb));
|
2015-03-19 00:17:13 +03:00
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
|
|
|
|
|
2017-02-02 16:24:57 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ID3D11Texture2D> textureCr;
|
2017-02-02 16:24:57 +03:00
|
|
|
hr = device->CreateTexture2D(&newDesc, nullptr, getter_AddRefs(textureCr));
|
2015-03-19 00:17:13 +03:00
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
|
|
|
|
|
2017-02-02 16:24:57 +03:00
|
|
|
// The documentation here seems to suggest using the immediate mode context
|
|
|
|
// on more than one thread is not allowed:
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ff476891(v=vs.85).aspx
|
|
|
|
// The Debug Layer seems to imply it is though. When the ID3D10Multithread
|
|
|
|
// layer is on. The Enter/Leave of the critical section shouldn't even be
|
|
|
|
// required but were added for extra security.
|
|
|
|
|
2015-03-19 00:17:13 +03:00
|
|
|
{
|
2017-05-04 14:54:05 +03:00
|
|
|
AutoLockD3D11Texture lockY(textureY);
|
|
|
|
AutoLockD3D11Texture lockCr(textureCr);
|
|
|
|
AutoLockD3D11Texture lockCb(textureCb);
|
2017-06-20 21:45:40 +03:00
|
|
|
D3D11MTAutoEnter mtAutoEnter(mt.forget());
|
2017-02-02 16:24:57 +03:00
|
|
|
|
|
|
|
RefPtr<ID3D11DeviceContext> ctx;
|
|
|
|
device->GetImmediateContext((ID3D11DeviceContext**)getter_AddRefs(ctx));
|
|
|
|
|
|
|
|
D3D11_BOX box;
|
|
|
|
box.front = box.top = box.left = 0;
|
|
|
|
box.back = 1;
|
2017-02-13 20:20:00 +03:00
|
|
|
box.right = aData.mYSize.width;
|
|
|
|
box.bottom = aData.mYSize.height;
|
|
|
|
ctx->UpdateSubresource(textureY, 0, &box, aData.mYChannel, aData.mYStride, 0);
|
2017-02-02 16:24:57 +03:00
|
|
|
|
2017-02-13 20:20:00 +03:00
|
|
|
box.right = aData.mCbCrSize.width;
|
|
|
|
box.bottom = aData.mCbCrSize.height;
|
|
|
|
ctx->UpdateSubresource(textureCb, 0, &box, aData.mCbChannel, aData.mCbCrStride, 0);
|
|
|
|
ctx->UpdateSubresource(textureCr, 0, &box, aData.mCrChannel, aData.mCbCrStride, 0);
|
2015-03-19 00:17:13 +03:00
|
|
|
}
|
|
|
|
|
2017-05-04 00:23:45 +03:00
|
|
|
return DXGIYCbCrTextureData::Create(textureY, textureCb, textureCr,
|
|
|
|
aSize, aData.mYSize, aData.mCbCrSize);
|
2017-02-13 20:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureClient*
|
|
|
|
IMFYCbCrImage::GetD3D11TextureClient(KnowsCompositor* aForwarder)
|
|
|
|
{
|
|
|
|
DXGIYCbCrTextureData* textureData = GetD3D11TextureData(mData, GetSize());
|
|
|
|
|
|
|
|
if (textureData == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTextureClient = TextureClient::CreateWithData(
|
|
|
|
textureData, TextureFlags::DEFAULT,
|
|
|
|
aForwarder->GetTextureForwarder()
|
|
|
|
);
|
|
|
|
|
|
|
|
return mTextureClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureClient*
|
|
|
|
IMFYCbCrImage::GetTextureClient(KnowsCompositor* aForwarder)
|
|
|
|
{
|
|
|
|
if (mTextureClient) {
|
|
|
|
return mTextureClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ID3D11Device> device =
|
|
|
|
gfx::DeviceManagerDx::Get()->GetContentDevice();
|
|
|
|
if (!device) {
|
|
|
|
device =
|
|
|
|
gfx::DeviceManagerDx::Get()->GetCompositorDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
LayersBackend backend = aForwarder->GetCompositorBackendType();
|
|
|
|
if (!device || backend != LayersBackend::LAYERS_D3D11) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return GetD3D11TextureClient(aForwarder);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|