Files
palemoon27/gfx/layers/D3D9SurfaceImage.cpp
T
roytam1 8f529f64f5 import changes from rmottola/Arctic-Fox:
- Bug 932865 - Add ThreadHangStats for collecting background hang telemetry; r=vladan (2f08a076b)
- Bug 932865 - Add way for telemetry to iterate over active threads; r=froydnj (535615d3d)
- Bug 1128768: Part 3 - Update BHR to allow for hang annotations; r=vladan (0b880a667)
- Bug 935092 - Add ThreadStackHelper to get a thread's pesudo-stack; r=BenWa (1422cfe4d)
- Bug 942488 - Don't report pseudo-stacks without SPS profiler. r=nchen (e160a7a08)
- Bug 946817 - Don't assert mPseudoStack on B2G. r=BenWa (9f846df3b)
- Bug 951431 - Don't get stacks during profiler runs on Linux; r=BenWa (15036e907)
- Bug 978262 - Ignore duplicate frames when getting BHR stack. r=froydnj (964721b1b)
- Bug 985155 - Add signal trampoline on ARM Linux to work around kernel bug. r=snorp (cb8a7846c)
- Bug 995730 - Convert xpcom/threads/ to Gecko style. r=froydnj (fe150404e)
- Bug 1013326 - Distinguish chrome and content scripts in pseudostack; r=snorp (81273c977)
- Bug 1023461 - Remove temporary stack buffer in ThreadStackHelper; r=snorp (cf5a717c2)
- Bug 1023461 - Record filename and line number for chrome JS entries; r=snorp (10c89808f)
- Bug 1022456 - Fix modelines in xpcom/{base,glue,io,string,threads}/. (48dbc0416)
- Bug 1016441 - Switch to using real-time signal in ThreadStackHelper; (2c5f818be)
- Bug 1016629 - b. Use RAII class to assign mStackToFill; r=snorp (769eae130)
- Bug 1016629 - c. Add define for ThreadStackHelper pseudostack support; r=snorp (67def0d2f)
- Bug 1016629 - d. Add and implement GetNativeStack method in ThreadStackHelper; r=snorp r=jseward (46c52f2be)
- Bug 1016629 - e. Implement platform-specific code for filling in context; r=snorp r=jseward (e6a66858b)
- Bug 1016629 - g. Avoid ASan flag when copying stack; r=snorp (0159628b5)
- Bug 1045176 - Unbreak build on non-SPS platforms after bug 1016629. (f1d60d838)
- Bug 1047123 - ThreadStackHelper should use UniquePtr<uint8_t[]>, not ScopedDeleteArray. r=jchen (0e4af313c)
- Bug 1049161 - Fix ThreadStackHelper thread handle permissions on Windows; r=snorp (c05172b1c)
- Bug 1050185 - Make ThreadStackHelper::FillThreadContext Valgrind-friendly. r=nchen (368725774)
- Bug 1050440 - Remove repeated js::RunScript frames in ThreadStackHelper (2a79600b3)
- Bug 1046841 - Fix more style violations in previously touched .cpp files in xpcom/. r=froydnj (02afe2493)
- Bug 1069694 - Remove or move around functions in OldDebugAPI. r=shu (177197302)
- Bug 1069694 - Remove OldDebugAPI from the browser. r=shu (b8c917d42)
- Bug 1100911 - For MacOS builds running on Valgrind, make ThreadStackHelper::GetStack be a no-op. r=nchen. (d99c02e16)
- Bug 1091758 - Report full paths for most chrome scripts; r=snorp (2b72e7878)
- Bug 1109291 - Include better paths for hanging chrome scripts in profile extensions directory; r=snorp r=bsmedberg (1997b9532)
- Bug 1113416 - Don't read stack labels inside hang monitor sighandler; r=nfroyd r=snorp (9688f6069)
- bug 1146027 - more final r=froydnj (7b0f295e5)
- Bug 1164090 - Check for Windows path separator in BHR file name; r=snorp (f014b4d78)
- Bug 1169034 - include <cstdlib> in ThreadStackHelper.cpp to declare correct overload for std::abs; r=jseward (874d4447e)
- Bug 1182996 - Fix and add missing namespace comments. rs=ehsan (054fc00b2)
- Bug 932865 - Collect thread hang stats in BackgroundHangMonitor; (ac80c8e9f)
- minor anticipated fixes to get it compiling (2bd701d15)
2020-02-01 08:07:15 +08:00

277 lines
7.9 KiB
C++

/* -*- 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"
#include "gfx2DGlue.h"
#include "mozilla/layers/TextureD3D9.h"
#include "mozilla/layers/CompositableClient.h"
#include "mozilla/layers/CompositableForwarder.h"
#include "mozilla/gfx/Types.h"
namespace mozilla {
namespace layers {
D3D9SurfaceImage::D3D9SurfaceImage()
: Image(nullptr, ImageFormat::D3D9_RGB32_TEXTURE)
, mSize(0, 0)
, mValid(false)
{}
D3D9SurfaceImage::~D3D9SurfaceImage()
{
if (mTexture) {
gfxWindowsPlatform::sD3D9SurfaceImageUsed -= mSize.width * mSize.height * 4;
}
}
static const GUID sD3D9TextureUsage =
{ 0x631e1338, 0xdc22, 0x497f, { 0xa1, 0xa8, 0xb4, 0xfe, 0x3a, 0xf4, 0x13, 0x4d } };
/* This class get's it's lifetime tied to a D3D texture
* and increments memory usage on construction and decrements
* on destruction */
class TextureMemoryMeasurer9 : public IUnknown
{
public:
TextureMemoryMeasurer9(size_t aMemoryUsed)
{
mMemoryUsed = aMemoryUsed;
gfxWindowsPlatform::sD3D9MemoryUsed += mMemoryUsed;
mRefCnt = 0;
}
~TextureMemoryMeasurer9()
{
gfxWindowsPlatform::sD3D9MemoryUsed -= mMemoryUsed;
}
STDMETHODIMP_(ULONG) AddRef() {
mRefCnt++;
return mRefCnt;
}
STDMETHODIMP QueryInterface(REFIID riid,
void **ppvObject)
{
IUnknown *punk = nullptr;
if (riid == IID_IUnknown) {
punk = this;
}
*ppvObject = punk;
if (punk) {
punk->AddRef();
return S_OK;
} else {
return E_NOINTERFACE;
}
}
STDMETHODIMP_(ULONG) Release() {
int refCnt = --mRefCnt;
if (refCnt == 0) {
delete this;
}
return refCnt;
}
private:
int mRefCnt;
int mMemoryUsed;
};
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.
const nsIntRect& region = aData.mRegion;
RefPtr<IDirect3DTexture9> texture;
HANDLE shareHandle = nullptr;
hr = device->CreateTexture(region.width,
region.height,
1,
D3DUSAGE_RENDERTARGET,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
byRef(texture),
&shareHandle);
NS_ENSURE_TRUE(SUCCEEDED(hr) && shareHandle, hr);
// Track the lifetime of this memory
texture->SetPrivateData(sD3D9TextureUsage, new TextureMemoryMeasurer9(region.width * region.height * 4), sizeof(IUnknown *), D3DSPD_IUNKNOWN);
gfxWindowsPlatform::sD3D9SurfaceImageUsed += region.width * region.height * 4;
// Copy the image onto the texture, preforming YUV -> RGB conversion if necessary.
RefPtr<IDirect3DSurface9> textureSurface;
hr = texture->GetSurfaceLevel(0, byRef(textureSurface));
NS_ENSURE_TRUE(SUCCEEDED(hr), hr);
// Stash the surface description for later use.
textureSurface->GetDesc(&mDesc);
RECT src = { region.x, region.y, region.x+region.width, region.y+region.height };
hr = device->StretchRect(surface, &src, textureSurface, nullptr, D3DTEXF_NONE);
NS_ENSURE_TRUE(SUCCEEDED(hr), hr);
// 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.
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);
mTexture = texture;
mShareHandle = shareHandle;
mSize = gfx::IntSize(region.width, region.height);
mQuery = query;
return S_OK;
}
bool
D3D9SurfaceImage::IsValid()
{
EnsureSynchronized();
return mValid;
}
void
D3D9SurfaceImage::EnsureSynchronized()
{
RefPtr<IDirect3DQuery9> query = mQuery;
if (!query) {
// Not setup, or already synchronized.
return;
}
int iterations = 0;
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;
}
mQuery = nullptr;
}
const D3DSURFACE_DESC&
D3D9SurfaceImage::GetDesc() const
{
return mDesc;
}
gfx::IntSize
D3D9SurfaceImage::GetSize()
{
return mSize;
}
TextureClient*
D3D9SurfaceImage::GetTextureClient(CompositableClient* aClient)
{
EnsureSynchronized();
if (!mTextureClient) {
mTextureClient = SharedTextureClientD3D9::Create(aClient->GetForwarder(),
gfx::SurfaceFormat::B8G8R8X8,
TextureFlags::DEFAULT,
mTexture,
mShareHandle,
mDesc);
}
return mTextureClient;
}
already_AddRefed<gfx::SourceSurface>
D3D9SurfaceImage::GetAsSourceSurface()
{
NS_ENSURE_TRUE(mTexture, nullptr);
HRESULT hr;
RefPtr<gfx::DataSourceSurface> surface = gfx::Factory::CreateDataSourceSurface(mSize, gfx::SurfaceFormat::B8G8R8X8);
if (NS_WARN_IF(!surface)) {
return nullptr;
}
// Ensure that the texture is ready to be used.
EnsureSynchronized();
// Readback the texture from GPU memory into system memory, so that
// we can copy it into the Cairo image. This is expensive.
RefPtr<IDirect3DSurface9> textureSurface;
hr = mTexture->GetSurfaceLevel(0, byRef(textureSurface));
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
RefPtr<IDirect3DDevice9> device;
hr = mTexture->GetDevice(byRef(device));
NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
RefPtr<IDirect3DSurface9> systemMemorySurface;
hr = device->CreateOffscreenPlainSurface(mDesc.Width,
mDesc.Height,
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();
return surface.forget();
}
} // namespace layers
} // namespace mozilla