2013-05-04 14:12:40 +04: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 "D3D9SurfaceImage.h"
|
2013-12-13 21:32:02 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2014-04-07 07:09:08 +04:00
|
|
|
#include "mozilla/layers/TextureD3D9.h"
|
2014-12-22 11:49:00 +03:00
|
|
|
#include "mozilla/layers/CompositableClient.h"
|
|
|
|
#include "mozilla/layers/CompositableForwarder.h"
|
2015-08-11 17:15:34 +03:00
|
|
|
#include "mozilla/layers/ImageBridgeChild.h"
|
2014-04-07 07:09:08 +04:00
|
|
|
#include "mozilla/gfx/Types.h"
|
2013-05-04 14:12:40 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2014-04-07 07:09:08 +04:00
|
|
|
|
|
|
|
D3D9SurfaceImage::D3D9SurfaceImage()
|
|
|
|
: Image(nullptr, ImageFormat::D3D9_RGB32_TEXTURE)
|
|
|
|
, mSize(0, 0)
|
2015-03-12 12:13:23 +03:00
|
|
|
, mValid(false)
|
2014-04-07 07:09:08 +04:00
|
|
|
{}
|
|
|
|
|
2015-02-05 06:20:14 +03:00
|
|
|
D3D9SurfaceImage::~D3D9SurfaceImage()
|
|
|
|
{
|
|
|
|
}
|
2014-04-07 07:09:08 +04:00
|
|
|
|
2013-05-04 14:12:40 +04:00
|
|
|
HRESULT
|
|
|
|
D3D9SurfaceImage::SetData(const Data& aData)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aData.mSurface, E_POINTER);
|
|
|
|
HRESULT hr;
|
|
|
|
RefPtr<IDirect3DSurface9> surface = aData.mSurface;
|
|
|
|
|
|
|
|
RefPtr<IDirect3DDevice9> device;
|
|
|
|
hr = surface->GetDevice(byRef(device));
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), E_FAIL);
|
|
|
|
|
|
|
|
RefPtr<IDirect3D9> d3d9;
|
|
|
|
hr = device->GetDirect3D(byRef(d3d9));
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), E_FAIL);
|
|
|
|
|
|
|
|
D3DSURFACE_DESC desc;
|
|
|
|
surface->GetDesc(&desc);
|
|
|
|
// Ensure we can convert the textures format to RGB conversion
|
|
|
|
// in StretchRect. Fail if we can't.
|
|
|
|
hr = d3d9->CheckDeviceFormatConversion(D3DADAPTER_DEFAULT,
|
|
|
|
D3DDEVTYPE_HAL,
|
|
|
|
desc.Format,
|
|
|
|
D3DFMT_X8R8G8B8);
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), hr);
|
|
|
|
|
|
|
|
// DXVA surfaces aren't created sharable, so we need to copy the surface
|
|
|
|
// to a sharable texture to that it's accessible to the layer manager's
|
|
|
|
// device.
|
2015-05-07 12:07:35 +03:00
|
|
|
const gfx::IntRect& region = aData.mRegion;
|
2015-08-11 17:15:34 +03:00
|
|
|
RefPtr<SharedTextureClientD3D9> textureClient =
|
|
|
|
SharedTextureClientD3D9::Create(layers::ImageBridgeChild::GetSingleton(),
|
|
|
|
gfx::SurfaceFormat::B8G8R8X8,
|
|
|
|
TextureFlags::DEFAULT,
|
|
|
|
device,
|
|
|
|
region.Size());
|
|
|
|
if (!textureClient) {
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2015-02-05 06:20:14 +03:00
|
|
|
|
2013-05-04 14:12:40 +04:00
|
|
|
// Copy the image onto the texture, preforming YUV -> RGB conversion if necessary.
|
2015-08-11 17:15:34 +03:00
|
|
|
RefPtr<IDirect3DSurface9> textureSurface = textureClient->GetD3D9Surface();
|
|
|
|
if (!textureSurface) {
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2013-05-04 14:12:40 +04:00
|
|
|
|
2013-05-20 09:14:13 +04:00
|
|
|
RECT src = { region.x, region.y, region.x+region.width, region.y+region.height };
|
2013-07-20 12:48:55 +04:00
|
|
|
hr = device->StretchRect(surface, &src, textureSurface, nullptr, D3DTEXF_NONE);
|
2013-05-04 14:12:40 +04:00
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), hr);
|
|
|
|
|
2015-03-09 06:18:32 +03:00
|
|
|
// Flush the draw command now, so that by the time we come to draw this
|
|
|
|
// image, we're less likely to need to wait for the draw operation to
|
|
|
|
// complete.
|
2013-05-04 14:12:40 +04:00
|
|
|
RefPtr<IDirect3DQuery9> query;
|
|
|
|
hr = device->CreateQuery(D3DQUERYTYPE_EVENT, byRef(query));
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), hr);
|
|
|
|
hr = query->Issue(D3DISSUE_END);
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), hr);
|
|
|
|
|
2015-08-11 17:15:34 +03:00
|
|
|
mTextureClient = textureClient;
|
|
|
|
mSize = region.Size();
|
2015-03-09 06:18:32 +03:00
|
|
|
mQuery = query;
|
2013-05-04 14:12:40 +04:00
|
|
|
|
2015-03-09 06:18:32 +03:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-12 12:13:23 +03:00
|
|
|
bool
|
|
|
|
D3D9SurfaceImage::IsValid()
|
|
|
|
{
|
|
|
|
EnsureSynchronized();
|
|
|
|
return mValid;
|
|
|
|
}
|
|
|
|
|
2015-03-09 06:18:32 +03:00
|
|
|
void
|
|
|
|
D3D9SurfaceImage::EnsureSynchronized()
|
|
|
|
{
|
|
|
|
RefPtr<IDirect3DQuery9> query = mQuery;
|
|
|
|
if (!query) {
|
|
|
|
// Not setup, or already synchronized.
|
|
|
|
return;
|
|
|
|
}
|
2013-05-04 14:12:40 +04:00
|
|
|
int iterations = 0;
|
2015-03-12 12:13:23 +03:00
|
|
|
while (iterations < 10) {
|
|
|
|
HRESULT hr = query->GetData(nullptr, 0, D3DGETDATA_FLUSH);
|
|
|
|
if (hr == S_FALSE) {
|
|
|
|
Sleep(1);
|
|
|
|
iterations++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (hr == S_OK) {
|
|
|
|
mValid = true;
|
|
|
|
}
|
|
|
|
break;
|
2013-05-04 14:12:40 +04:00
|
|
|
}
|
2015-03-09 06:18:32 +03:00
|
|
|
mQuery = nullptr;
|
2013-05-04 14:12:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const D3DSURFACE_DESC&
|
|
|
|
D3D9SurfaceImage::GetDesc() const
|
|
|
|
{
|
2015-08-11 17:15:34 +03:00
|
|
|
return mTextureClient->GetDesc();
|
2013-05-04 14:12:40 +04:00
|
|
|
}
|
|
|
|
|
2013-12-13 21:32:02 +04:00
|
|
|
gfx::IntSize
|
2013-05-04 14:12:40 +04:00
|
|
|
D3D9SurfaceImage::GetSize()
|
|
|
|
{
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
2014-04-07 07:09:08 +04:00
|
|
|
TextureClient*
|
|
|
|
D3D9SurfaceImage::GetTextureClient(CompositableClient* aClient)
|
|
|
|
{
|
2015-08-11 17:15:34 +03:00
|
|
|
MOZ_ASSERT(mTextureClient);
|
|
|
|
MOZ_ASSERT(mTextureClient->GetAllocator() == aClient->GetForwarder());
|
2015-03-09 06:18:32 +03:00
|
|
|
EnsureSynchronized();
|
2014-04-07 07:09:08 +04:00
|
|
|
return mTextureClient;
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<gfx::SourceSurface>
|
2014-01-28 18:14:10 +04:00
|
|
|
D3D9SurfaceImage::GetAsSourceSurface()
|
|
|
|
{
|
2015-08-11 17:15:34 +03:00
|
|
|
NS_ENSURE_TRUE(mTextureClient, nullptr);
|
2014-01-28 18:14:10 +04:00
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
RefPtr<gfx::DataSourceSurface> surface = gfx::Factory::CreateDataSourceSurface(mSize, gfx::SurfaceFormat::B8G8R8X8);
|
2014-08-27 19:57:43 +04:00
|
|
|
if (NS_WARN_IF(!surface)) {
|
2014-01-28 18:14:10 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-09 06:18:32 +03:00
|
|
|
// Ensure that the texture is ready to be used.
|
|
|
|
EnsureSynchronized();
|
|
|
|
|
2014-01-28 18:14:10 +04:00
|
|
|
// Readback the texture from GPU memory into system memory, so that
|
|
|
|
// we can copy it into the Cairo image. This is expensive.
|
2015-08-11 17:15:34 +03:00
|
|
|
RefPtr<IDirect3DSurface9> textureSurface = mTextureClient->GetD3D9Surface();
|
|
|
|
if (!textureSurface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-01-28 18:14:10 +04:00
|
|
|
|
2015-08-11 17:15:34 +03:00
|
|
|
RefPtr<IDirect3DDevice9> device = mTextureClient->GetD3D9Device();
|
|
|
|
if (!device) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-01-28 18:14:10 +04:00
|
|
|
|
|
|
|
RefPtr<IDirect3DSurface9> systemMemorySurface;
|
2015-08-11 17:15:34 +03:00
|
|
|
hr = device->CreateOffscreenPlainSurface(mSize.width,
|
|
|
|
mSize.height,
|
2014-01-28 18:14:10 +04:00
|
|
|
D3DFMT_X8R8G8B8,
|
|
|
|
D3DPOOL_SYSTEMMEM,
|
|
|
|
byRef(systemMemorySurface),
|
|
|
|
0);
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
|
|
|
|
|
|
|
|
hr = device->GetRenderTargetData(textureSurface, systemMemorySurface);
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
|
|
|
|
|
|
|
|
D3DLOCKED_RECT rect;
|
|
|
|
hr = systemMemorySurface->LockRect(&rect, nullptr, 0);
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
|
|
|
|
|
|
|
|
gfx::DataSourceSurface::MappedSurface mappedSurface;
|
|
|
|
if (!surface->Map(gfx::DataSourceSurface::WRITE, &mappedSurface)) {
|
|
|
|
systemMemorySurface->UnlockRect();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* src = (const unsigned char*)(rect.pBits);
|
|
|
|
const unsigned srcPitch = rect.Pitch;
|
|
|
|
for (int y = 0; y < mSize.height; y++) {
|
|
|
|
memcpy(mappedSurface.mData + mappedSurface.mStride * y,
|
|
|
|
(unsigned char*)(src) + srcPitch * y,
|
|
|
|
mSize.width * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
systemMemorySurface->UnlockRect();
|
|
|
|
surface->Unmap();
|
|
|
|
|
2015-05-01 16:14:16 +03:00
|
|
|
return surface.forget();
|
2014-01-28 18:14:10 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|