2010-08-06 22:09:18 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2014-04-02 04:32:54 -07:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2013-12-20 08:46:29 -08:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2014-04-02 04:32:54 -07:00
|
|
|
#include "mozilla/RefPtr.h"
|
2013-04-24 11:42:40 -07:00
|
|
|
#include "mozilla/layers/PLayerTransaction.h"
|
2011-07-04 06:15:05 -07:00
|
|
|
#include "gfxSharedImageSurface.h"
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
#include "ImageLayerD3D9.h"
|
2014-09-26 10:06:08 -07:00
|
|
|
#include "PaintedLayerD3D9.h"
|
2011-07-04 06:15:05 -07:00
|
|
|
#include "gfxPlatform.h"
|
2013-12-13 09:32:10 -08:00
|
|
|
#include "gfx2DGlue.h"
|
2010-05-24 08:28:51 -07:00
|
|
|
#include "yuv_convert.h"
|
2013-05-04 03:12:40 -07:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2010-08-26 13:44:53 -07:00
|
|
|
#include "Nv3DVUtils.h"
|
2013-05-04 03:12:40 -07:00
|
|
|
#include "D3D9SurfaceImage.h"
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-12-20 08:46:29 -08:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2012-06-25 19:43:31 -07:00
|
|
|
static inline _D3DFORMAT
|
2014-04-02 04:32:54 -07:00
|
|
|
D3dFormatForSurfaceFormat(SurfaceFormat aFormat)
|
2012-06-25 19:43:31 -07:00
|
|
|
{
|
2014-04-02 04:32:54 -07:00
|
|
|
if (aFormat == SurfaceFormat::A8) {
|
2012-06-25 19:43:31 -07:00
|
|
|
return D3DFMT_A8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return D3DFMT_A8R8G8B8;
|
|
|
|
}
|
|
|
|
|
2010-12-20 14:37:01 -08:00
|
|
|
static already_AddRefed<IDirect3DTexture9>
|
2012-02-15 19:26:19 -08:00
|
|
|
DataToTexture(IDirect3DDevice9 *aDevice,
|
|
|
|
unsigned char *aData,
|
|
|
|
int aStride,
|
2013-12-20 08:46:29 -08:00
|
|
|
const IntSize &aSize,
|
2012-06-25 19:43:31 -07:00
|
|
|
_D3DFORMAT aFormat)
|
2010-12-20 14:37:01 -08:00
|
|
|
{
|
|
|
|
nsRefPtr<IDirect3DTexture9> texture;
|
|
|
|
nsRefPtr<IDirect3DDevice9Ex> deviceEx;
|
2010-12-21 02:52:00 -08:00
|
|
|
aDevice->QueryInterface(IID_IDirect3DDevice9Ex,
|
2010-12-20 14:37:01 -08:00
|
|
|
(void**)getter_AddRefs(deviceEx));
|
|
|
|
|
2012-06-25 19:43:31 -07:00
|
|
|
nsRefPtr<IDirect3DSurface9> surface;
|
|
|
|
D3DLOCKED_RECT lockedRect;
|
2010-12-20 14:37:01 -08:00
|
|
|
if (deviceEx) {
|
|
|
|
// D3D9Ex doesn't support managed textures. We could use dynamic textures
|
|
|
|
// here but since Images are immutable that probably isn't such a great
|
|
|
|
// idea.
|
|
|
|
if (FAILED(aDevice->
|
|
|
|
CreateTexture(aSize.width, aSize.height,
|
2012-06-25 19:43:31 -07:00
|
|
|
1, 0, aFormat, D3DPOOL_DEFAULT,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(texture), nullptr)))
|
2010-12-20 14:37:01 -08:00
|
|
|
{
|
2013-07-20 01:48:55 -07:00
|
|
|
return nullptr;
|
2010-12-20 14:37:01 -08:00
|
|
|
}
|
|
|
|
|
2012-06-25 19:43:31 -07:00
|
|
|
nsRefPtr<IDirect3DTexture9> tmpTexture;
|
2010-12-20 14:37:01 -08:00
|
|
|
if (FAILED(aDevice->
|
2012-06-25 19:43:31 -07:00
|
|
|
CreateTexture(aSize.width, aSize.height,
|
|
|
|
1, 0, aFormat, D3DPOOL_SYSTEMMEM,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(tmpTexture), nullptr)))
|
2010-12-20 14:37:01 -08:00
|
|
|
{
|
2013-07-20 01:48:55 -07:00
|
|
|
return nullptr;
|
2010-12-20 14:37:01 -08:00
|
|
|
}
|
|
|
|
|
2012-06-25 19:43:31 -07:00
|
|
|
tmpTexture->GetSurfaceLevel(0, getter_AddRefs(surface));
|
2013-07-20 01:48:55 -07:00
|
|
|
surface->LockRect(&lockedRect, nullptr, 0);
|
2012-06-25 19:43:31 -07:00
|
|
|
NS_ASSERTION(lockedRect.pBits, "Could not lock surface");
|
2010-12-20 14:37:01 -08:00
|
|
|
} else {
|
|
|
|
if (FAILED(aDevice->
|
|
|
|
CreateTexture(aSize.width, aSize.height,
|
2012-06-25 19:43:31 -07:00
|
|
|
1, 0, aFormat, D3DPOOL_MANAGED,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(texture), nullptr)))
|
2010-12-20 14:37:01 -08:00
|
|
|
{
|
2013-07-20 01:48:55 -07:00
|
|
|
return nullptr;
|
2010-12-20 14:37:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* lock the entire texture */
|
2013-07-20 01:48:55 -07:00
|
|
|
texture->LockRect(0, &lockedRect, nullptr, 0);
|
2012-06-25 19:43:31 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t width = aSize.width;
|
2012-06-25 19:43:31 -07:00
|
|
|
if (aFormat == D3DFMT_A8R8G8B8) {
|
|
|
|
width *= 4;
|
|
|
|
}
|
|
|
|
for (int y = 0; y < aSize.height; y++) {
|
|
|
|
memcpy((char*)lockedRect.pBits + lockedRect.Pitch * y,
|
|
|
|
aData + aStride * y,
|
|
|
|
width);
|
|
|
|
}
|
2010-12-20 14:37:01 -08:00
|
|
|
|
2012-06-25 19:43:31 -07:00
|
|
|
if (deviceEx) {
|
|
|
|
surface->UnlockRect();
|
|
|
|
nsRefPtr<IDirect3DSurface9> dstSurface;
|
|
|
|
texture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2013-07-20 01:48:55 -07:00
|
|
|
aDevice->UpdateSurface(surface, nullptr, dstSurface, nullptr);
|
2012-06-25 19:43:31 -07:00
|
|
|
} else {
|
2010-12-20 14:37:01 -08:00
|
|
|
texture->UnlockRect(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture.forget();
|
|
|
|
}
|
|
|
|
|
2013-05-04 03:12:40 -07:00
|
|
|
static already_AddRefed<IDirect3DTexture9>
|
|
|
|
OpenSharedTexture(const D3DSURFACE_DESC& aDesc,
|
|
|
|
HANDLE aShareHandle,
|
|
|
|
IDirect3DDevice9 *aDevice)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDesc.Format == D3DFMT_X8R8G8B8);
|
|
|
|
|
|
|
|
// Open the frame from DXVA's device in our device using the resource
|
|
|
|
// sharing handle.
|
|
|
|
nsRefPtr<IDirect3DTexture9> sharedTexture;
|
|
|
|
HRESULT hr = aDevice->CreateTexture(aDesc.Width,
|
|
|
|
aDesc.Height,
|
|
|
|
1,
|
|
|
|
D3DUSAGE_RENDERTARGET,
|
|
|
|
D3DFMT_X8R8G8B8,
|
|
|
|
D3DPOOL_DEFAULT,
|
|
|
|
getter_AddRefs(sharedTexture),
|
|
|
|
&aShareHandle);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
NS_WARNING("Failed to open shared texture on our device");
|
|
|
|
}
|
|
|
|
return sharedTexture.forget();
|
|
|
|
}
|
|
|
|
|
2012-02-15 19:26:19 -08:00
|
|
|
static already_AddRefed<IDirect3DTexture9>
|
|
|
|
SurfaceToTexture(IDirect3DDevice9 *aDevice,
|
2014-04-02 04:32:54 -07:00
|
|
|
SourceSurface *aSurface,
|
2013-12-20 08:46:29 -08:00
|
|
|
const IntSize &aSize)
|
2012-02-15 19:26:19 -08:00
|
|
|
{
|
2014-04-02 04:32:54 -07:00
|
|
|
RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface();
|
|
|
|
if (!dataSurface) {
|
|
|
|
return nullptr;
|
2012-02-15 19:26:19 -08:00
|
|
|
}
|
2014-04-02 04:32:54 -07:00
|
|
|
DataSourceSurface::MappedSurface map;
|
|
|
|
if (!dataSurface->Map(DataSourceSurface::MapType::READ, &map)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsRefPtr<IDirect3DTexture9> texture =
|
|
|
|
DataToTexture(aDevice, map.mData, map.mStride, aSize,
|
|
|
|
D3dFormatForSurfaceFormat(dataSurface->GetFormat()));
|
|
|
|
dataSurface->Unmap();
|
|
|
|
return texture.forget();
|
2012-02-15 19:26:19 -08:00
|
|
|
}
|
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
static void AllocateTexturesYCbCr(PlanarYCbCrImage *aImage,
|
|
|
|
IDirect3DDevice9 *aDevice,
|
|
|
|
LayerManagerD3D9 *aManager)
|
2010-05-24 08:28:51 -07:00
|
|
|
{
|
2012-02-06 02:24:47 -08:00
|
|
|
nsAutoPtr<PlanarYCbCrD3D9BackendData> backendData(
|
|
|
|
new PlanarYCbCrD3D9BackendData);
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2013-10-01 17:57:50 -07:00
|
|
|
const PlanarYCbCrData *data = aImage->GetData();
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
D3DLOCKED_RECT lockrectY;
|
|
|
|
D3DLOCKED_RECT lockrectCb;
|
|
|
|
D3DLOCKED_RECT lockrectCr;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t* src;
|
|
|
|
uint8_t* dest;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
nsRefPtr<IDirect3DSurface9> tmpSurfaceY;
|
|
|
|
nsRefPtr<IDirect3DSurface9> tmpSurfaceCb;
|
|
|
|
nsRefPtr<IDirect3DSurface9> tmpSurfaceCr;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
nsRefPtr<IDirect3DDevice9Ex> deviceEx;
|
|
|
|
aDevice->QueryInterface(IID_IDirect3DDevice9Ex,
|
|
|
|
getter_AddRefs(deviceEx));
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
bool isD3D9Ex = deviceEx;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
if (isD3D9Ex) {
|
|
|
|
nsRefPtr<IDirect3DTexture9> tmpYTexture;
|
|
|
|
nsRefPtr<IDirect3DTexture9> tmpCbTexture;
|
|
|
|
nsRefPtr<IDirect3DTexture9> tmpCrTexture;
|
|
|
|
// D3D9Ex does not support the managed pool, could use dynamic textures
|
|
|
|
// here. But since an Image is immutable static textures are probably a
|
|
|
|
// better idea.
|
|
|
|
|
|
|
|
HRESULT hr;
|
2012-08-21 03:18:20 -07:00
|
|
|
hr = aDevice->CreateTexture(data->mYSize.width, data->mYSize.height,
|
2013-11-21 08:40:55 -08:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_DEFAULT,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(backendData->mYTexture), nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 03:18:20 -07:00
|
|
|
hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 08:40:55 -08:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_DEFAULT,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(backendData->mCbTexture), nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 03:18:20 -07:00
|
|
|
hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 08:40:55 -08:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_DEFAULT,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(backendData->mCrTexture), nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 03:18:20 -07:00
|
|
|
hr = aDevice->CreateTexture(data->mYSize.width, data->mYSize.height,
|
2013-11-21 08:40:55 -08:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(tmpYTexture), nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 03:18:20 -07:00
|
|
|
hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 08:40:55 -08:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(tmpCbTexture), nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 03:18:20 -07:00
|
|
|
hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 08:40:55 -08:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(tmpCrTexture), nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
aManager->ReportFailure(NS_LITERAL_CSTRING("PlanarYCbCrImageD3D9::AllocateTextures(): Failed to create texture (isD3D9Ex)"),
|
|
|
|
hr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpYTexture->GetSurfaceLevel(0, getter_AddRefs(tmpSurfaceY));
|
|
|
|
tmpCbTexture->GetSurfaceLevel(0, getter_AddRefs(tmpSurfaceCb));
|
|
|
|
tmpCrTexture->GetSurfaceLevel(0, getter_AddRefs(tmpSurfaceCr));
|
2013-07-20 01:48:55 -07:00
|
|
|
tmpSurfaceY->LockRect(&lockrectY, nullptr, 0);
|
|
|
|
tmpSurfaceCb->LockRect(&lockrectCb, nullptr, 0);
|
|
|
|
tmpSurfaceCr->LockRect(&lockrectCr, nullptr, 0);
|
2012-01-31 18:18:30 -08:00
|
|
|
} else {
|
|
|
|
HRESULT hr;
|
2012-08-21 03:18:20 -07:00
|
|
|
hr = aDevice->CreateTexture(data->mYSize.width, data->mYSize.height,
|
2013-11-21 08:40:55 -08:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_MANAGED,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(backendData->mYTexture), nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 03:18:20 -07:00
|
|
|
aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 08:40:55 -08:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_MANAGED,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(backendData->mCbTexture), nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 03:18:20 -07:00
|
|
|
aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 08:40:55 -08:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_MANAGED,
|
2013-07-20 01:48:55 -07:00
|
|
|
getter_AddRefs(backendData->mCrTexture), nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
aManager->ReportFailure(NS_LITERAL_CSTRING("PlanarYCbCrImageD3D9::AllocateTextures(): Failed to create texture (!isD3D9Ex)"),
|
|
|
|
hr);
|
|
|
|
return;
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
2012-01-31 18:18:30 -08:00
|
|
|
|
|
|
|
/* lock the entire texture */
|
2013-07-20 01:48:55 -07:00
|
|
|
backendData->mYTexture->LockRect(0, &lockrectY, nullptr, 0);
|
|
|
|
backendData->mCbTexture->LockRect(0, &lockrectCb, nullptr, 0);
|
|
|
|
backendData->mCrTexture->LockRect(0, &lockrectCr, nullptr, 0);
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
src = data->mYChannel;
|
2012-01-31 18:18:30 -08:00
|
|
|
//FIX cast
|
2012-08-22 08:56:38 -07:00
|
|
|
dest = (uint8_t*)lockrectY.pBits;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
// copy over data
|
2012-08-21 03:18:20 -07:00
|
|
|
for (int h=0; h<data->mYSize.height; h++) {
|
|
|
|
memcpy(dest, src, data->mYSize.width);
|
2012-01-31 18:18:30 -08:00
|
|
|
dest += lockrectY.Pitch;
|
2012-08-21 03:18:20 -07:00
|
|
|
src += data->mYStride;
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
2012-01-31 18:18:30 -08:00
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
src = data->mCbChannel;
|
2012-01-31 18:18:30 -08:00
|
|
|
//FIX cast
|
2012-08-22 08:56:38 -07:00
|
|
|
dest = (uint8_t*)lockrectCb.pBits;
|
2012-01-31 18:18:30 -08:00
|
|
|
|
|
|
|
// copy over data
|
2012-08-21 03:18:20 -07:00
|
|
|
for (int h=0; h<data->mCbCrSize.height; h++) {
|
|
|
|
memcpy(dest, src, data->mCbCrSize.width);
|
2012-01-31 18:18:30 -08:00
|
|
|
dest += lockrectCb.Pitch;
|
2012-08-21 03:18:20 -07:00
|
|
|
src += data->mCbCrStride;
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
src = data->mCrChannel;
|
2012-01-31 18:18:30 -08:00
|
|
|
//FIX cast
|
2012-08-22 08:56:38 -07:00
|
|
|
dest = (uint8_t*)lockrectCr.pBits;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-01-31 18:18:30 -08:00
|
|
|
// copy over data
|
2012-08-21 03:18:20 -07:00
|
|
|
for (int h=0; h<data->mCbCrSize.height; h++) {
|
|
|
|
memcpy(dest, src, data->mCbCrSize.width);
|
2012-01-31 18:18:30 -08:00
|
|
|
dest += lockrectCr.Pitch;
|
2012-08-21 03:18:20 -07:00
|
|
|
src += data->mCbCrStride;
|
2012-01-31 18:18:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isD3D9Ex) {
|
|
|
|
tmpSurfaceY->UnlockRect();
|
|
|
|
tmpSurfaceCb->UnlockRect();
|
|
|
|
tmpSurfaceCr->UnlockRect();
|
|
|
|
nsRefPtr<IDirect3DSurface9> dstSurface;
|
|
|
|
backendData->mYTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2013-07-20 01:48:55 -07:00
|
|
|
aDevice->UpdateSurface(tmpSurfaceY, nullptr, dstSurface, nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
backendData->mCbTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2013-07-20 01:48:55 -07:00
|
|
|
aDevice->UpdateSurface(tmpSurfaceCb, nullptr, dstSurface, nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
backendData->mCrTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2013-07-20 01:48:55 -07:00
|
|
|
aDevice->UpdateSurface(tmpSurfaceCr, nullptr, dstSurface, nullptr);
|
2012-01-31 18:18:30 -08:00
|
|
|
} else {
|
|
|
|
backendData->mYTexture->UnlockRect(0);
|
|
|
|
backendData->mCbTexture->UnlockRect(0);
|
|
|
|
backendData->mCrTexture->UnlockRect(0);
|
2010-12-20 14:37:30 -08:00
|
|
|
}
|
|
|
|
|
2014-01-23 10:26:41 -08:00
|
|
|
aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, backendData.forget());
|
2010-08-06 22:09:18 -07:00
|
|
|
}
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
Layer*
|
|
|
|
ImageLayerD3D9::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-03-06 13:07:48 -08:00
|
|
|
/*
|
|
|
|
* Returns a texture which backs aImage
|
|
|
|
* Will only work if aImage is a cairo or remote image.
|
2012-07-30 07:20:58 -07:00
|
|
|
* Returns nullptr if unsuccessful.
|
2012-03-06 13:07:48 -08:00
|
|
|
* If successful, aHasAlpha will be set to true if the texture has an
|
|
|
|
* alpha component, false otherwise.
|
|
|
|
*/
|
|
|
|
IDirect3DTexture9*
|
|
|
|
ImageLayerD3D9::GetTexture(Image *aImage, bool& aHasAlpha)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aImage, "Null image.");
|
|
|
|
|
2014-01-30 14:58:49 -08:00
|
|
|
if (aImage->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP) {
|
2012-03-06 13:07:48 -08:00
|
|
|
RemoteBitmapImage *remoteImage =
|
|
|
|
static_cast<RemoteBitmapImage*>(aImage);
|
2013-05-04 03:12:40 -07:00
|
|
|
|
2014-01-23 10:26:41 -08:00
|
|
|
if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) {
|
2012-05-21 02:03:23 -07:00
|
|
|
nsAutoPtr<TextureD3D9BackendData> dat(new TextureD3D9BackendData());
|
2012-06-25 19:43:31 -07:00
|
|
|
dat->mTexture = DataToTexture(device(), remoteImage->mData, remoteImage->mStride, remoteImage->mSize, D3DFMT_A8R8G8B8);
|
2012-03-06 13:07:48 -08:00
|
|
|
if (dat->mTexture) {
|
2014-01-23 10:26:41 -08:00
|
|
|
aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, dat.forget());
|
2012-03-06 13:07:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aHasAlpha = remoteImage->mFormat == RemoteImageData::BGRA32;
|
2014-01-30 14:58:49 -08:00
|
|
|
} else if (aImage->GetFormat() == ImageFormat::CAIRO_SURFACE) {
|
2012-03-06 13:07:48 -08:00
|
|
|
CairoImage *cairoImage =
|
|
|
|
static_cast<CairoImage*>(aImage);
|
|
|
|
|
2014-04-02 04:32:54 -07:00
|
|
|
RefPtr<SourceSurface> surf = cairoImage->GetAsSourceSurface();
|
2014-01-17 08:22:09 -08:00
|
|
|
if (!surf) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-06 13:07:48 -08:00
|
|
|
}
|
|
|
|
|
2014-01-23 10:26:41 -08:00
|
|
|
if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) {
|
2012-05-21 02:03:23 -07:00
|
|
|
nsAutoPtr<TextureD3D9BackendData> dat(new TextureD3D9BackendData());
|
2014-01-17 08:22:09 -08:00
|
|
|
dat->mTexture = SurfaceToTexture(device(), surf, cairoImage->GetSize());
|
2012-03-06 13:07:48 -08:00
|
|
|
if (dat->mTexture) {
|
2014-01-23 10:26:41 -08:00
|
|
|
aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, dat.forget());
|
2012-03-06 13:07:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 04:32:54 -07:00
|
|
|
aHasAlpha = surf->GetFormat() == SurfaceFormat::B8G8R8A8;
|
2014-01-30 14:58:49 -08:00
|
|
|
} else if (aImage->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE) {
|
2014-01-23 10:26:41 -08:00
|
|
|
if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) {
|
2013-05-04 03:12:40 -07:00
|
|
|
// The texture in which the frame is stored belongs to DXVA's D3D9 device.
|
|
|
|
// We need to open it on our device before we can use it.
|
|
|
|
nsAutoPtr<TextureD3D9BackendData> backendData(new TextureD3D9BackendData());
|
|
|
|
D3D9SurfaceImage* image = static_cast<D3D9SurfaceImage*>(aImage);
|
|
|
|
backendData->mTexture = OpenSharedTexture(image->GetDesc(), image->GetShareHandle(), device());
|
|
|
|
if (backendData->mTexture) {
|
2014-01-23 10:26:41 -08:00
|
|
|
aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, backendData.forget());
|
2013-05-04 03:12:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
aHasAlpha = false;
|
2012-03-06 13:07:48 -08:00
|
|
|
} else {
|
|
|
|
NS_WARNING("Inappropriate image type.");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-06 13:07:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureD3D9BackendData *data =
|
2014-01-23 10:26:41 -08:00
|
|
|
static_cast<TextureD3D9BackendData*>(aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9));
|
2012-03-06 13:07:48 -08:00
|
|
|
|
|
|
|
if (!data) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-06 13:07:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDirect3DDevice9> dev;
|
|
|
|
data->mTexture->GetDevice(getter_AddRefs(dev));
|
|
|
|
if (dev != device()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-06 13:07:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return data->mTexture;
|
|
|
|
}
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
void
|
2010-11-08 01:06:15 -08:00
|
|
|
ImageLayerD3D9::RenderLayer()
|
2010-05-24 08:28:51 -07:00
|
|
|
{
|
2012-02-15 19:26:19 -08:00
|
|
|
ImageContainer *container = GetContainer();
|
2012-08-13 03:10:10 -07:00
|
|
|
if (!container || mD3DManager->CompositingDisabled()) {
|
2010-05-24 08:28:51 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-15 19:26:19 -08:00
|
|
|
AutoLockImage autoLock(container);
|
|
|
|
|
|
|
|
Image *image = autoLock.GetImage();
|
2011-01-19 00:27:54 -08:00
|
|
|
if (!image) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-20 14:37:30 -08:00
|
|
|
SetShaderTransformAndOpacity();
|
|
|
|
|
2013-12-13 09:32:02 -08:00
|
|
|
gfx::IntSize size = image->GetSize();
|
2012-03-12 18:41:29 -07:00
|
|
|
|
2014-01-30 14:58:49 -08:00
|
|
|
if (image->GetFormat() == ImageFormat::CAIRO_SURFACE ||
|
|
|
|
image->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP ||
|
|
|
|
image->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE)
|
2010-12-28 17:07:04 -08:00
|
|
|
{
|
2014-01-30 14:58:49 -08:00
|
|
|
NS_ASSERTION(image->GetFormat() != ImageFormat::CAIRO_SURFACE ||
|
2014-04-02 04:33:15 -07:00
|
|
|
!static_cast<CairoImage*>(image)->mSourceSurface ||
|
|
|
|
static_cast<CairoImage*>(image)->mSourceSurface->GetFormat() != SurfaceFormat::A8,
|
2012-06-25 19:43:31 -07:00
|
|
|
"Image layer has alpha image");
|
|
|
|
|
2012-02-15 19:26:19 -08:00
|
|
|
bool hasAlpha = false;
|
2012-03-06 13:07:48 -08:00
|
|
|
nsRefPtr<IDirect3DTexture9> texture = GetTexture(image, hasAlpha);
|
2012-01-31 18:18:30 -08:00
|
|
|
|
2010-12-20 14:37:30 -08:00
|
|
|
device()->SetVertexShaderConstantF(CBvLayerQuad,
|
|
|
|
ShaderConstantRect(0,
|
|
|
|
0,
|
|
|
|
size.width,
|
|
|
|
size.height),
|
|
|
|
1);
|
|
|
|
|
2012-02-15 19:26:19 -08:00
|
|
|
if (hasAlpha) {
|
2012-03-06 13:07:48 -08:00
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER, GetMaskLayer());
|
2010-12-20 14:37:30 -08:00
|
|
|
} else {
|
2012-03-06 13:07:48 -08:00
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBLAYER, GetMaskLayer());
|
2010-12-20 14:37:30 -08:00
|
|
|
}
|
|
|
|
|
2013-10-01 14:01:19 -07:00
|
|
|
if (mFilter == GraphicsFilter::FILTER_NEAREST) {
|
2012-01-31 18:18:30 -08:00
|
|
|
device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
}
|
2012-03-06 13:07:48 -08:00
|
|
|
device()->SetTexture(0, texture);
|
2012-02-15 19:26:19 -08:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
image = nullptr;
|
2012-02-15 19:26:19 -08:00
|
|
|
autoLock.Unlock();
|
|
|
|
|
2010-12-20 14:37:30 -08:00
|
|
|
device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
|
2013-10-01 14:01:19 -07:00
|
|
|
if (mFilter == GraphicsFilter::FILTER_NEAREST) {
|
2012-01-31 18:18:30 -08:00
|
|
|
device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PlanarYCbCrImage *yuvImage =
|
2012-02-15 19:26:19 -08:00
|
|
|
static_cast<PlanarYCbCrImage*>(image);
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-08-21 03:18:20 -07:00
|
|
|
if (!yuvImage->IsValid()) {
|
2012-01-31 18:18:30 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 10:26:41 -08:00
|
|
|
if (!yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) {
|
2012-01-31 18:18:30 -08:00
|
|
|
AllocateTexturesYCbCr(yuvImage, device(), mD3DManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
PlanarYCbCrD3D9BackendData *data =
|
2014-01-23 10:26:41 -08:00
|
|
|
static_cast<PlanarYCbCrD3D9BackendData*>(yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9));
|
2012-01-31 18:18:30 -08:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDirect3DDevice9> dev;
|
|
|
|
data->mYTexture->GetDevice(getter_AddRefs(dev));
|
|
|
|
if (dev != device()) {
|
2010-05-24 08:28:51 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-01 15:24:58 -07:00
|
|
|
device()->SetVertexShaderConstantF(CBvLayerQuad,
|
|
|
|
ShaderConstantRect(0,
|
|
|
|
0,
|
2012-03-12 18:41:29 -07:00
|
|
|
size.width,
|
|
|
|
size.height),
|
2010-10-01 15:24:58 -07:00
|
|
|
1);
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2011-07-04 19:52:34 -07:00
|
|
|
device()->SetVertexShaderConstantF(CBvTextureCoords,
|
|
|
|
ShaderConstantRect(
|
2012-08-21 03:18:20 -07:00
|
|
|
(float)yuvImage->GetData()->mPicX / yuvImage->GetData()->mYSize.width,
|
|
|
|
(float)yuvImage->GetData()->mPicY / yuvImage->GetData()->mYSize.height,
|
|
|
|
(float)yuvImage->GetData()->mPicSize.width / yuvImage->GetData()->mYSize.width,
|
|
|
|
(float)yuvImage->GetData()->mPicSize.height / yuvImage->GetData()->mYSize.height
|
2011-07-04 19:52:34 -07:00
|
|
|
),
|
|
|
|
1);
|
|
|
|
|
2012-03-06 13:07:48 -08:00
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::YCBCRLAYER, GetMaskLayer());
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2011-01-11 20:51:27 -08:00
|
|
|
/*
|
|
|
|
* Send 3d control data and metadata
|
|
|
|
*/
|
|
|
|
if (mD3DManager->GetNv3DVUtils()) {
|
2010-11-02 16:43:29 -07:00
|
|
|
Nv_Stereo_Mode mode;
|
2012-08-21 03:18:20 -07:00
|
|
|
switch (yuvImage->GetData()->mStereoMode) {
|
2014-01-30 14:58:49 -08:00
|
|
|
case StereoMode::LEFT_RIGHT:
|
2010-11-02 16:43:29 -07:00
|
|
|
mode = NV_STEREO_MODE_LEFT_RIGHT;
|
|
|
|
break;
|
2014-01-30 14:58:49 -08:00
|
|
|
case StereoMode::RIGHT_LEFT:
|
2010-11-02 16:43:29 -07:00
|
|
|
mode = NV_STEREO_MODE_RIGHT_LEFT;
|
|
|
|
break;
|
2014-01-30 14:58:49 -08:00
|
|
|
case StereoMode::BOTTOM_TOP:
|
2010-11-02 16:43:29 -07:00
|
|
|
mode = NV_STEREO_MODE_BOTTOM_TOP;
|
|
|
|
break;
|
2014-01-30 14:58:49 -08:00
|
|
|
case StereoMode::TOP_BOTTOM:
|
2010-11-02 16:43:29 -07:00
|
|
|
mode = NV_STEREO_MODE_TOP_BOTTOM;
|
|
|
|
break;
|
2014-01-30 14:58:49 -08:00
|
|
|
case StereoMode::MONO:
|
2011-01-11 20:51:27 -08:00
|
|
|
mode = NV_STEREO_MODE_MONO;
|
|
|
|
break;
|
2010-11-02 16:43:29 -07:00
|
|
|
}
|
|
|
|
|
2011-01-11 20:51:27 -08:00
|
|
|
// Send control data even in mono case so driver knows to leave stereo mode.
|
|
|
|
mD3DManager->GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE);
|
|
|
|
|
2014-01-30 14:58:49 -08:00
|
|
|
if (yuvImage->GetData()->mStereoMode != StereoMode::MONO) {
|
2010-11-02 16:43:29 -07:00
|
|
|
mD3DManager->GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE);
|
|
|
|
|
|
|
|
nsRefPtr<IDirect3DSurface9> renderTarget;
|
|
|
|
device()->GetRenderTarget(0, getter_AddRefs(renderTarget));
|
2012-08-21 03:18:20 -07:00
|
|
|
mD3DManager->GetNv3DVUtils()->SendNv3DVMetaData((unsigned int)yuvImage->GetSize().width,
|
|
|
|
(unsigned int)yuvImage->GetSize().height, (HANDLE)(data->mYTexture), (HANDLE)(renderTarget));
|
2010-11-02 16:43:29 -07:00
|
|
|
}
|
|
|
|
}
|
2010-08-26 13:44:53 -07:00
|
|
|
|
2010-12-29 11:22:18 -08:00
|
|
|
// Linear scaling is default here, adhering to mFilter is difficult since
|
|
|
|
// presumably even with point filtering we'll still want chroma upsampling
|
|
|
|
// to be linear. In the current approach we can't.
|
2012-01-31 18:18:30 -08:00
|
|
|
device()->SetTexture(0, data->mYTexture);
|
|
|
|
device()->SetTexture(1, data->mCbTexture);
|
|
|
|
device()->SetTexture(2, data->mCrTexture);
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
image = nullptr;
|
|
|
|
data = nullptr;
|
2012-02-15 19:26:19 -08:00
|
|
|
autoLock.Unlock();
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
|
|
|
|
|
2011-07-04 19:52:34 -07:00
|
|
|
device()->SetVertexShaderConstantF(CBvTextureCoords,
|
|
|
|
ShaderConstantRect(0, 0, 1.0f, 1.0f), 1);
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
2011-03-23 15:28:57 -07:00
|
|
|
|
|
|
|
GetContainer()->NotifyPaintedImage(image);
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
2012-03-06 13:07:48 -08:00
|
|
|
already_AddRefed<IDirect3DTexture9>
|
2013-12-20 08:46:29 -08:00
|
|
|
ImageLayerD3D9::GetAsTexture(gfx::IntSize* aSize)
|
2012-03-06 13:07:48 -08:00
|
|
|
{
|
|
|
|
if (!GetContainer()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-06 13:07:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoLockImage autoLock(GetContainer());
|
|
|
|
|
|
|
|
Image *image = autoLock.GetImage();
|
|
|
|
|
|
|
|
if (!image) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-06 13:07:48 -08:00
|
|
|
}
|
|
|
|
|
2014-01-30 14:58:49 -08:00
|
|
|
if (image->GetFormat() != ImageFormat::CAIRO_SURFACE &&
|
|
|
|
image->GetFormat() != ImageFormat::REMOTE_IMAGE_BITMAP) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-06 13:07:48 -08:00
|
|
|
}
|
2013-05-04 03:12:40 -07:00
|
|
|
|
2012-03-06 13:07:48 -08:00
|
|
|
bool dontCare;
|
2013-12-31 01:06:12 -08:00
|
|
|
*aSize = image->GetSize();
|
2012-03-06 13:07:48 -08:00
|
|
|
nsRefPtr<IDirect3DTexture9> result = GetTexture(image, dontCare);
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
} /* layers */
|
|
|
|
} /* mozilla */
|