2017-10-28 02:10:06 +03:00
|
|
|
/* -*- 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
|
2013-05-04 14:12:40 +04:00
|
|
|
* 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"
|
2017-03-08 11:17:36 +03:00
|
|
|
#include "gfxWindowsPlatform.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 {
|
|
|
|
|
2017-03-08 11:17:36 +03:00
|
|
|
DXGID3D9TextureData::DXGID3D9TextureData(gfx::SurfaceFormat aFormat,
|
|
|
|
IDirect3DTexture9* aTexture, HANDLE aHandle,
|
|
|
|
IDirect3DDevice9* aDevice)
|
|
|
|
: mDevice(aDevice)
|
|
|
|
, mTexture(aTexture)
|
|
|
|
, mFormat(aFormat)
|
|
|
|
, mHandle(aHandle)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(DXGID3D9TextureData);
|
|
|
|
}
|
|
|
|
|
|
|
|
DXGID3D9TextureData::~DXGID3D9TextureData()
|
|
|
|
{
|
|
|
|
gfxWindowsPlatform::sD3D9SharedTextures -= mDesc.Width * mDesc.Height * 4;
|
|
|
|
MOZ_COUNT_DTOR(DXGID3D9TextureData);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
DXGID3D9TextureData*
|
|
|
|
DXGID3D9TextureData::Create(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
|
|
|
|
TextureFlags aFlags,
|
|
|
|
IDirect3DDevice9* aDevice)
|
|
|
|
{
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
AUTO_PROFILER_LABEL("DXGID3D9TextureData::Create", GRAPHICS);
|
2017-03-08 11:17:36 +03:00
|
|
|
MOZ_ASSERT(aFormat == gfx::SurfaceFormat::B8G8R8A8);
|
|
|
|
if (aFormat != gfx::SurfaceFormat::B8G8R8A8) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<IDirect3DTexture9> texture;
|
|
|
|
HANDLE shareHandle = nullptr;
|
|
|
|
HRESULT hr = aDevice->CreateTexture(aSize.width, aSize.height,
|
|
|
|
1,
|
|
|
|
D3DUSAGE_RENDERTARGET,
|
|
|
|
D3DFMT_A8R8G8B8,
|
|
|
|
D3DPOOL_DEFAULT,
|
|
|
|
getter_AddRefs(texture),
|
|
|
|
&shareHandle);
|
|
|
|
if (FAILED(hr) || !shareHandle) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3DSURFACE_DESC surfaceDesc;
|
|
|
|
hr = texture->GetLevelDesc(0, &surfaceDesc);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
DXGID3D9TextureData* data = new DXGID3D9TextureData(aFormat, texture, shareHandle, aDevice);
|
|
|
|
data->mDesc = surfaceDesc;
|
|
|
|
|
|
|
|
gfxWindowsPlatform::sD3D9SharedTextures += aSize.width * aSize.height * 4;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DXGID3D9TextureData::FillInfo(TextureData::Info& aInfo) const
|
|
|
|
{
|
|
|
|
aInfo.size = GetSize();
|
|
|
|
aInfo.format = mFormat;
|
|
|
|
aInfo.supportsMoz2D = false;
|
|
|
|
aInfo.canExposeMappedData = false;
|
|
|
|
aInfo.hasIntermediateBuffer = false;
|
|
|
|
aInfo.hasSynchronization = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<IDirect3DSurface9>
|
|
|
|
DXGID3D9TextureData::GetD3D9Surface() const
|
|
|
|
{
|
|
|
|
RefPtr<IDirect3DSurface9> textureSurface;
|
|
|
|
HRESULT hr = mTexture->GetSurfaceLevel(0, getter_AddRefs(textureSurface));
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
|
|
|
|
|
|
|
|
return textureSurface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DXGID3D9TextureData::Serialize(SurfaceDescriptor& aOutDescriptor)
|
|
|
|
{
|
|
|
|
aOutDescriptor = SurfaceDescriptorD3D10((WindowsHandle)(mHandle), mFormat, GetSize());
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-07 07:09:08 +04:00
|
|
|
|
2016-03-09 00:34:49 +03:00
|
|
|
D3D9SurfaceImage::D3D9SurfaceImage()
|
2014-04-07 07:09:08 +04:00
|
|
|
: Image(nullptr, ImageFormat::D3D9_RGB32_TEXTURE)
|
|
|
|
, mSize(0, 0)
|
2016-09-21 12:24:43 +03:00
|
|
|
, mShareHandle(0)
|
2016-03-09 00:34:49 +03:00
|
|
|
, mValid(true)
|
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
|
2015-11-17 11:09:00 +03:00
|
|
|
D3D9SurfaceImage::AllocateAndCopy(D3D9RecycleAllocator* aAllocator,
|
|
|
|
IDirect3DSurface9* aSurface,
|
|
|
|
const gfx::IntRect& aRegion)
|
2013-05-04 14:12:40 +04:00
|
|
|
{
|
2015-11-17 11:09:00 +03:00
|
|
|
NS_ENSURE_TRUE(aSurface, E_POINTER);
|
2013-05-04 14:12:40 +04:00
|
|
|
HRESULT hr;
|
2015-11-17 11:09:00 +03:00
|
|
|
RefPtr<IDirect3DSurface9> surface = aSurface;
|
2013-05-04 14:12:40 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDirect3DDevice9> device;
|
2015-10-18 07:40:10 +03:00
|
|
|
hr = surface->GetDevice(getter_AddRefs(device));
|
2013-05-04 14:12:40 +04:00
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), E_FAIL);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDirect3D9> d3d9;
|
2015-10-18 07:40:10 +03:00
|
|
|
hr = device->GetDirect3D(getter_AddRefs(d3d9));
|
2013-05-04 14:12:40 +04:00
|
|
|
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,
|
2016-07-26 02:36:36 +03:00
|
|
|
D3DFMT_A8R8G8B8);
|
2013-05-04 14:12:40 +04:00
|
|
|
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.
|
2016-09-21 12:24:43 +03:00
|
|
|
if (aAllocator) {
|
|
|
|
mTextureClient =
|
|
|
|
aAllocator->CreateOrRecycleClient(gfx::SurfaceFormat::B8G8R8A8, aRegion.Size());
|
|
|
|
if (!mTextureClient) {
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DXGID3D9TextureData* texData =
|
|
|
|
static_cast<DXGID3D9TextureData*>(mTextureClient->GetInternalData());
|
|
|
|
mTexture = texData->GetD3D9Texture();
|
|
|
|
mShareHandle = texData->GetShareHandle();
|
|
|
|
mDesc = texData->GetDesc();
|
|
|
|
} else {
|
|
|
|
hr = device->CreateTexture(aRegion.Size().width, aRegion.Size().height,
|
|
|
|
1,
|
|
|
|
D3DUSAGE_RENDERTARGET,
|
|
|
|
D3DFMT_A8R8G8B8,
|
|
|
|
D3DPOOL_DEFAULT,
|
|
|
|
getter_AddRefs(mTexture),
|
|
|
|
&mShareHandle);
|
|
|
|
if (FAILED(hr) || !mShareHandle) {
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = mTexture->GetLevelDesc(0, &mDesc);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2015-08-11 17:15:34 +03:00
|
|
|
}
|
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.
|
2016-09-21 12:24:43 +03:00
|
|
|
RefPtr<IDirect3DSurface9> textureSurface = GetD3D9Surface();
|
2015-08-11 17:15:34 +03:00
|
|
|
if (!textureSurface) {
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2013-05-04 14:12:40 +04:00
|
|
|
|
2017-12-19 23:48:39 +03:00
|
|
|
RECT src = { aRegion.X(), aRegion.Y(), aRegion.XMost(), aRegion.YMost() };
|
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-11-17 11:09:00 +03:00
|
|
|
mSize = aRegion.Size();
|
2015-03-09 06:18:32 +03:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-21 08:49:43 +03:00
|
|
|
already_AddRefed<IDirect3DSurface9>
|
2017-11-30 21:03:02 +03:00
|
|
|
D3D9SurfaceImage::GetD3D9Surface() const
|
2016-03-21 08:49:43 +03:00
|
|
|
{
|
2016-09-21 12:24:43 +03:00
|
|
|
RefPtr<IDirect3DSurface9> textureSurface;
|
|
|
|
HRESULT hr = mTexture->GetSurfaceLevel(0, getter_AddRefs(textureSurface));
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
|
|
|
|
return textureSurface.forget();
|
2016-03-21 08:49:43 +03:00
|
|
|
}
|
|
|
|
|
2013-05-04 14:12:40 +04:00
|
|
|
const D3DSURFACE_DESC&
|
|
|
|
D3D9SurfaceImage::GetDesc() const
|
|
|
|
{
|
2016-09-21 12:24:43 +03:00
|
|
|
return mDesc;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE
|
|
|
|
D3D9SurfaceImage::GetShareHandle() const
|
|
|
|
{
|
|
|
|
return mShareHandle;
|
2013-05-04 14:12:40 +04:00
|
|
|
}
|
|
|
|
|
2013-12-13 21:32:02 +04:00
|
|
|
gfx::IntSize
|
2017-11-30 21:03:02 +03:00
|
|
|
D3D9SurfaceImage::GetSize() const
|
2013-05-04 14:12:40 +04:00
|
|
|
{
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
2014-04-07 07:09:08 +04:00
|
|
|
TextureClient*
|
2016-09-27 06:22:20 +03:00
|
|
|
D3D9SurfaceImage::GetTextureClient(KnowsCompositor* aForwarder)
|
2014-04-07 07:09:08 +04:00
|
|
|
{
|
2015-08-11 17:15:34 +03:00
|
|
|
MOZ_ASSERT(mTextureClient);
|
2016-09-27 06:22:20 +03:00
|
|
|
MOZ_ASSERT(mTextureClient->GetAllocator() == aForwarder->GetTextureForwarder());
|
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()
|
|
|
|
{
|
2016-09-21 12:24:43 +03:00
|
|
|
if (!mTexture) {
|
2015-10-19 15:46:17 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-01-28 18:14:10 +04:00
|
|
|
|
|
|
|
HRESULT hr;
|
2015-10-18 08:24:48 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readback the texture from GPU memory into system memory, so that
|
|
|
|
// we can copy it into the Cairo image. This is expensive.
|
2016-09-21 12:24:43 +03:00
|
|
|
RefPtr<IDirect3DSurface9> textureSurface = GetD3D9Surface();
|
2015-08-11 17:15:34 +03:00
|
|
|
if (!textureSurface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-01-28 18:14:10 +04:00
|
|
|
|
2016-09-21 12:24:43 +03:00
|
|
|
RefPtr<IDirect3DDevice9> device;
|
|
|
|
hr = textureSurface->GetDevice(getter_AddRefs(device));
|
|
|
|
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
|
2014-01-28 18:14:10 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDirect3DSurface9> systemMemorySurface;
|
2015-08-11 17:15:34 +03:00
|
|
|
hr = device->CreateOffscreenPlainSurface(mSize.width,
|
|
|
|
mSize.height,
|
2016-07-26 02:36:36 +03:00
|
|
|
D3DFMT_A8R8G8B8,
|
2014-01-28 18:14:10 +04:00
|
|
|
D3DPOOL_SYSTEMMEM,
|
2015-10-18 07:40:10 +03:00
|
|
|
getter_AddRefs(systemMemorySurface),
|
2014-01-28 18:14:10 +04:00
|
|
|
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-08-11 17:15:44 +03:00
|
|
|
already_AddRefed<TextureClient>
|
|
|
|
D3D9RecycleAllocator::Allocate(gfx::SurfaceFormat aFormat,
|
|
|
|
gfx::IntSize aSize,
|
|
|
|
BackendSelector aSelector,
|
|
|
|
TextureFlags aTextureFlags,
|
|
|
|
TextureAllocationFlags aAllocFlags)
|
|
|
|
{
|
2015-10-19 15:46:17 +03:00
|
|
|
TextureData* data = DXGID3D9TextureData::Create(aSize, aFormat, aTextureFlags, mDevice);
|
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-27 06:22:20 +03:00
|
|
|
return MakeAndAddRef<TextureClient>(data, aTextureFlags,
|
|
|
|
mSurfaceAllocator->GetTextureForwarder());
|
2015-08-11 17:15:44 +03:00
|
|
|
}
|
|
|
|
|
2015-10-19 15:46:17 +03:00
|
|
|
already_AddRefed<TextureClient>
|
2015-08-11 17:15:44 +03:00
|
|
|
D3D9RecycleAllocator::CreateOrRecycleClient(gfx::SurfaceFormat aFormat,
|
|
|
|
const gfx::IntSize& aSize)
|
|
|
|
{
|
2015-10-19 15:46:17 +03:00
|
|
|
return CreateOrRecycle(aFormat, aSize, BackendSelector::Content,
|
|
|
|
TextureFlags::DEFAULT);
|
2015-08-11 17:15:44 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|