2010-08-07 09:09:18 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04: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 19:28:51 +04:00
|
|
|
|
2012-07-12 16:51:58 +04:00
|
|
|
#include "ipc/AutoOpenSurface.h"
|
2013-12-20 20:46:29 +04:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2013-04-24 22:42:40 +04:00
|
|
|
#include "mozilla/layers/PLayerTransaction.h"
|
2011-07-04 17:15:05 +04:00
|
|
|
#include "gfxSharedImageSurface.h"
|
|
|
|
|
2010-05-24 19:28:51 +04:00
|
|
|
#include "ImageLayerD3D9.h"
|
2011-07-04 17:15:05 +04:00
|
|
|
#include "ThebesLayerD3D9.h"
|
|
|
|
#include "gfxPlatform.h"
|
2010-05-24 19:28:51 +04:00
|
|
|
#include "gfxImageSurface.h"
|
2013-12-13 21:32:10 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2010-05-24 19:28:51 +04:00
|
|
|
#include "yuv_convert.h"
|
2013-05-04 14:12:40 +04:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2010-08-27 00:44:53 +04:00
|
|
|
#include "Nv3DVUtils.h"
|
2013-05-04 14:12:40 +04:00
|
|
|
#include "D3D9SurfaceImage.h"
|
2010-05-24 19:28:51 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-12-20 20:46:29 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2012-06-26 06:43:31 +04:00
|
|
|
static inline _D3DFORMAT
|
|
|
|
D3dFormatForGfxFormat(gfxImageFormat aFormat)
|
|
|
|
{
|
2014-01-23 22:26:40 +04:00
|
|
|
if (aFormat == gfxImageFormat::A8) {
|
2012-06-26 06:43:31 +04:00
|
|
|
return D3DFMT_A8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return D3DFMT_A8R8G8B8;
|
|
|
|
}
|
|
|
|
|
2010-12-21 01:37:01 +03:00
|
|
|
static already_AddRefed<IDirect3DTexture9>
|
2012-02-16 07:26:19 +04:00
|
|
|
DataToTexture(IDirect3DDevice9 *aDevice,
|
|
|
|
unsigned char *aData,
|
|
|
|
int aStride,
|
2013-12-20 20:46:29 +04:00
|
|
|
const IntSize &aSize,
|
2012-06-26 06:43:31 +04:00
|
|
|
_D3DFORMAT aFormat)
|
2010-12-21 01:37:01 +03:00
|
|
|
{
|
|
|
|
nsRefPtr<IDirect3DTexture9> texture;
|
|
|
|
nsRefPtr<IDirect3DDevice9Ex> deviceEx;
|
2010-12-21 13:52:00 +03:00
|
|
|
aDevice->QueryInterface(IID_IDirect3DDevice9Ex,
|
2010-12-21 01:37:01 +03:00
|
|
|
(void**)getter_AddRefs(deviceEx));
|
|
|
|
|
2012-06-26 06:43:31 +04:00
|
|
|
nsRefPtr<IDirect3DSurface9> surface;
|
|
|
|
D3DLOCKED_RECT lockedRect;
|
2010-12-21 01:37:01 +03: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-26 06:43:31 +04:00
|
|
|
1, 0, aFormat, D3DPOOL_DEFAULT,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(texture), nullptr)))
|
2010-12-21 01:37:01 +03:00
|
|
|
{
|
2013-07-20 12:48:55 +04:00
|
|
|
return nullptr;
|
2010-12-21 01:37:01 +03:00
|
|
|
}
|
|
|
|
|
2012-06-26 06:43:31 +04:00
|
|
|
nsRefPtr<IDirect3DTexture9> tmpTexture;
|
2010-12-21 01:37:01 +03:00
|
|
|
if (FAILED(aDevice->
|
2012-06-26 06:43:31 +04:00
|
|
|
CreateTexture(aSize.width, aSize.height,
|
|
|
|
1, 0, aFormat, D3DPOOL_SYSTEMMEM,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(tmpTexture), nullptr)))
|
2010-12-21 01:37:01 +03:00
|
|
|
{
|
2013-07-20 12:48:55 +04:00
|
|
|
return nullptr;
|
2010-12-21 01:37:01 +03:00
|
|
|
}
|
|
|
|
|
2012-06-26 06:43:31 +04:00
|
|
|
tmpTexture->GetSurfaceLevel(0, getter_AddRefs(surface));
|
2013-07-20 12:48:55 +04:00
|
|
|
surface->LockRect(&lockedRect, nullptr, 0);
|
2012-06-26 06:43:31 +04:00
|
|
|
NS_ASSERTION(lockedRect.pBits, "Could not lock surface");
|
2010-12-21 01:37:01 +03:00
|
|
|
} else {
|
|
|
|
if (FAILED(aDevice->
|
|
|
|
CreateTexture(aSize.width, aSize.height,
|
2012-06-26 06:43:31 +04:00
|
|
|
1, 0, aFormat, D3DPOOL_MANAGED,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(texture), nullptr)))
|
2010-12-21 01:37:01 +03:00
|
|
|
{
|
2013-07-20 12:48:55 +04:00
|
|
|
return nullptr;
|
2010-12-21 01:37:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* lock the entire texture */
|
2013-07-20 12:48:55 +04:00
|
|
|
texture->LockRect(0, &lockedRect, nullptr, 0);
|
2012-06-26 06:43:31 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t width = aSize.width;
|
2012-06-26 06:43:31 +04: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-21 01:37:01 +03:00
|
|
|
|
2012-06-26 06:43:31 +04:00
|
|
|
if (deviceEx) {
|
|
|
|
surface->UnlockRect();
|
|
|
|
nsRefPtr<IDirect3DSurface9> dstSurface;
|
|
|
|
texture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2013-07-20 12:48:55 +04:00
|
|
|
aDevice->UpdateSurface(surface, nullptr, dstSurface, nullptr);
|
2012-06-26 06:43:31 +04:00
|
|
|
} else {
|
2010-12-21 01:37:01 +03:00
|
|
|
texture->UnlockRect(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture.forget();
|
|
|
|
}
|
|
|
|
|
2013-05-04 14:12:40 +04: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-16 07:26:19 +04:00
|
|
|
static already_AddRefed<IDirect3DTexture9>
|
|
|
|
SurfaceToTexture(IDirect3DDevice9 *aDevice,
|
|
|
|
gfxASurface *aSurface,
|
2013-12-20 20:46:29 +04:00
|
|
|
const IntSize &aSize)
|
2012-02-16 07:26:19 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> imageSurface = aSurface->GetAsImageSurface();
|
|
|
|
|
|
|
|
if (!imageSurface) {
|
2013-12-20 20:46:29 +04:00
|
|
|
imageSurface = new gfxImageSurface(ThebesIntSize(aSize),
|
2014-01-23 22:26:40 +04:00
|
|
|
gfxImageFormat::ARGB32);
|
2013-05-04 14:12:40 +04:00
|
|
|
|
2012-02-16 07:26:19 +04:00
|
|
|
nsRefPtr<gfxContext> context = new gfxContext(imageSurface);
|
|
|
|
context->SetSource(aSurface);
|
|
|
|
context->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
context->Paint();
|
|
|
|
}
|
|
|
|
|
2012-06-26 06:43:31 +04:00
|
|
|
return DataToTexture(aDevice, imageSurface->Data(), imageSurface->Stride(),
|
|
|
|
aSize, D3dFormatForGfxFormat(imageSurface->Format()));
|
2012-02-16 07:26:19 +04:00
|
|
|
}
|
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
static void AllocateTexturesYCbCr(PlanarYCbCrImage *aImage,
|
|
|
|
IDirect3DDevice9 *aDevice,
|
|
|
|
LayerManagerD3D9 *aManager)
|
2010-05-24 19:28:51 +04:00
|
|
|
{
|
2012-02-06 14:24:47 +04:00
|
|
|
nsAutoPtr<PlanarYCbCrD3D9BackendData> backendData(
|
|
|
|
new PlanarYCbCrD3D9BackendData);
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2013-10-02 04:57:50 +04:00
|
|
|
const PlanarYCbCrData *data = aImage->GetData();
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
D3DLOCKED_RECT lockrectY;
|
|
|
|
D3DLOCKED_RECT lockrectCb;
|
|
|
|
D3DLOCKED_RECT lockrectCr;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* src;
|
|
|
|
uint8_t* dest;
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
nsRefPtr<IDirect3DSurface9> tmpSurfaceY;
|
|
|
|
nsRefPtr<IDirect3DSurface9> tmpSurfaceCb;
|
|
|
|
nsRefPtr<IDirect3DSurface9> tmpSurfaceCr;
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
nsRefPtr<IDirect3DDevice9Ex> deviceEx;
|
|
|
|
aDevice->QueryInterface(IID_IDirect3DDevice9Ex,
|
|
|
|
getter_AddRefs(deviceEx));
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
bool isD3D9Ex = deviceEx;
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-02-01 06:18:30 +04: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 14:18:20 +04:00
|
|
|
hr = aDevice->CreateTexture(data->mYSize.width, data->mYSize.height,
|
2013-11-21 20:40:55 +04:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_DEFAULT,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(backendData->mYTexture), nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 14:18:20 +04:00
|
|
|
hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 20:40:55 +04:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_DEFAULT,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(backendData->mCbTexture), nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 14:18:20 +04:00
|
|
|
hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 20:40:55 +04:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_DEFAULT,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(backendData->mCrTexture), nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 14:18:20 +04:00
|
|
|
hr = aDevice->CreateTexture(data->mYSize.width, data->mYSize.height,
|
2013-11-21 20:40:55 +04:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(tmpYTexture), nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 14:18:20 +04:00
|
|
|
hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 20:40:55 +04:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(tmpCbTexture), nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 14:18:20 +04:00
|
|
|
hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 20:40:55 +04:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(tmpCrTexture), nullptr);
|
2012-02-01 06:18:30 +04: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 12:48:55 +04:00
|
|
|
tmpSurfaceY->LockRect(&lockrectY, nullptr, 0);
|
|
|
|
tmpSurfaceCb->LockRect(&lockrectCb, nullptr, 0);
|
|
|
|
tmpSurfaceCr->LockRect(&lockrectCr, nullptr, 0);
|
2012-02-01 06:18:30 +04:00
|
|
|
} else {
|
|
|
|
HRESULT hr;
|
2012-08-21 14:18:20 +04:00
|
|
|
hr = aDevice->CreateTexture(data->mYSize.width, data->mYSize.height,
|
2013-11-21 20:40:55 +04:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_MANAGED,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(backendData->mYTexture), nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 14:18:20 +04:00
|
|
|
aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 20:40:55 +04:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_MANAGED,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(backendData->mCbTexture), nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
}
|
|
|
|
if (!FAILED(hr)) {
|
2012-08-21 14:18:20 +04:00
|
|
|
aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height,
|
2013-11-21 20:40:55 +04:00
|
|
|
1, 0, D3DFMT_A8, D3DPOOL_MANAGED,
|
2013-07-20 12:48:55 +04:00
|
|
|
getter_AddRefs(backendData->mCrTexture), nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
}
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
aManager->ReportFailure(NS_LITERAL_CSTRING("PlanarYCbCrImageD3D9::AllocateTextures(): Failed to create texture (!isD3D9Ex)"),
|
|
|
|
hr);
|
|
|
|
return;
|
2010-05-24 19:28:51 +04:00
|
|
|
}
|
2012-02-01 06:18:30 +04:00
|
|
|
|
|
|
|
/* lock the entire texture */
|
2013-07-20 12:48:55 +04: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 19:28:51 +04:00
|
|
|
}
|
|
|
|
|
2012-08-21 14:18:20 +04:00
|
|
|
src = data->mYChannel;
|
2012-02-01 06:18:30 +04:00
|
|
|
//FIX cast
|
2012-08-22 19:56:38 +04:00
|
|
|
dest = (uint8_t*)lockrectY.pBits;
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
// copy over data
|
2012-08-21 14:18:20 +04:00
|
|
|
for (int h=0; h<data->mYSize.height; h++) {
|
|
|
|
memcpy(dest, src, data->mYSize.width);
|
2012-02-01 06:18:30 +04:00
|
|
|
dest += lockrectY.Pitch;
|
2012-08-21 14:18:20 +04:00
|
|
|
src += data->mYStride;
|
2010-05-24 19:28:51 +04:00
|
|
|
}
|
2012-02-01 06:18:30 +04:00
|
|
|
|
2012-08-21 14:18:20 +04:00
|
|
|
src = data->mCbChannel;
|
2012-02-01 06:18:30 +04:00
|
|
|
//FIX cast
|
2012-08-22 19:56:38 +04:00
|
|
|
dest = (uint8_t*)lockrectCb.pBits;
|
2012-02-01 06:18:30 +04:00
|
|
|
|
|
|
|
// copy over data
|
2012-08-21 14:18:20 +04:00
|
|
|
for (int h=0; h<data->mCbCrSize.height; h++) {
|
|
|
|
memcpy(dest, src, data->mCbCrSize.width);
|
2012-02-01 06:18:30 +04:00
|
|
|
dest += lockrectCb.Pitch;
|
2012-08-21 14:18:20 +04:00
|
|
|
src += data->mCbCrStride;
|
2010-05-24 19:28:51 +04:00
|
|
|
}
|
|
|
|
|
2012-08-21 14:18:20 +04:00
|
|
|
src = data->mCrChannel;
|
2012-02-01 06:18:30 +04:00
|
|
|
//FIX cast
|
2012-08-22 19:56:38 +04:00
|
|
|
dest = (uint8_t*)lockrectCr.pBits;
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
// copy over data
|
2012-08-21 14:18:20 +04:00
|
|
|
for (int h=0; h<data->mCbCrSize.height; h++) {
|
|
|
|
memcpy(dest, src, data->mCbCrSize.width);
|
2012-02-01 06:18:30 +04:00
|
|
|
dest += lockrectCr.Pitch;
|
2012-08-21 14:18:20 +04:00
|
|
|
src += data->mCbCrStride;
|
2012-02-01 06:18:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isD3D9Ex) {
|
|
|
|
tmpSurfaceY->UnlockRect();
|
|
|
|
tmpSurfaceCb->UnlockRect();
|
|
|
|
tmpSurfaceCr->UnlockRect();
|
|
|
|
nsRefPtr<IDirect3DSurface9> dstSurface;
|
|
|
|
backendData->mYTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2013-07-20 12:48:55 +04:00
|
|
|
aDevice->UpdateSurface(tmpSurfaceY, nullptr, dstSurface, nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
backendData->mCbTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2013-07-20 12:48:55 +04:00
|
|
|
aDevice->UpdateSurface(tmpSurfaceCb, nullptr, dstSurface, nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
backendData->mCrTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2013-07-20 12:48:55 +04:00
|
|
|
aDevice->UpdateSurface(tmpSurfaceCr, nullptr, dstSurface, nullptr);
|
2012-02-01 06:18:30 +04:00
|
|
|
} else {
|
|
|
|
backendData->mYTexture->UnlockRect(0);
|
|
|
|
backendData->mCbTexture->UnlockRect(0);
|
|
|
|
backendData->mCrTexture->UnlockRect(0);
|
2010-12-21 01:37:30 +03:00
|
|
|
}
|
|
|
|
|
2014-01-23 22:26:41 +04:00
|
|
|
aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, backendData.forget());
|
2010-08-07 09:09:18 +04:00
|
|
|
}
|
|
|
|
|
2010-05-24 19:28:51 +04:00
|
|
|
Layer*
|
|
|
|
ImageLayerD3D9::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-03-07 01:07:48 +04:00
|
|
|
/*
|
|
|
|
* Returns a texture which backs aImage
|
|
|
|
* Will only work if aImage is a cairo or remote image.
|
2012-07-30 18:20:58 +04:00
|
|
|
* Returns nullptr if unsuccessful.
|
2012-03-07 01:07:48 +04: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-31 02:58:49 +04:00
|
|
|
if (aImage->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP) {
|
2012-03-07 01:07:48 +04:00
|
|
|
RemoteBitmapImage *remoteImage =
|
|
|
|
static_cast<RemoteBitmapImage*>(aImage);
|
2013-05-04 14:12:40 +04:00
|
|
|
|
2014-01-23 22:26:41 +04:00
|
|
|
if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) {
|
2012-05-21 13:03:23 +04:00
|
|
|
nsAutoPtr<TextureD3D9BackendData> dat(new TextureD3D9BackendData());
|
2012-06-26 06:43:31 +04:00
|
|
|
dat->mTexture = DataToTexture(device(), remoteImage->mData, remoteImage->mStride, remoteImage->mSize, D3DFMT_A8R8G8B8);
|
2012-03-07 01:07:48 +04:00
|
|
|
if (dat->mTexture) {
|
2014-01-23 22:26:41 +04:00
|
|
|
aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, dat.forget());
|
2012-03-07 01:07:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aHasAlpha = remoteImage->mFormat == RemoteImageData::BGRA32;
|
2014-01-31 02:58:49 +04:00
|
|
|
} else if (aImage->GetFormat() == ImageFormat::CAIRO_SURFACE) {
|
2012-03-07 01:07:48 +04:00
|
|
|
CairoImage *cairoImage =
|
|
|
|
static_cast<CairoImage*>(aImage);
|
|
|
|
|
2014-01-17 20:22:09 +04:00
|
|
|
nsRefPtr<gfxASurface> surf = cairoImage->DeprecatedGetAsSurface();
|
|
|
|
if (!surf) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-03-07 01:07:48 +04:00
|
|
|
}
|
|
|
|
|
2014-01-23 22:26:41 +04:00
|
|
|
if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) {
|
2012-05-21 13:03:23 +04:00
|
|
|
nsAutoPtr<TextureD3D9BackendData> dat(new TextureD3D9BackendData());
|
2014-01-17 20:22:09 +04:00
|
|
|
dat->mTexture = SurfaceToTexture(device(), surf, cairoImage->GetSize());
|
2012-03-07 01:07:48 +04:00
|
|
|
if (dat->mTexture) {
|
2014-01-23 22:26:41 +04:00
|
|
|
aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, dat.forget());
|
2012-03-07 01:07:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:26:40 +04:00
|
|
|
aHasAlpha = surf->GetContentType() == gfxContentType::COLOR_ALPHA;
|
2014-01-31 02:58:49 +04:00
|
|
|
} else if (aImage->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE) {
|
2014-01-23 22:26:41 +04:00
|
|
|
if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) {
|
2013-05-04 14:12:40 +04: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 22:26:41 +04:00
|
|
|
aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, backendData.forget());
|
2013-05-04 14:12:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
aHasAlpha = false;
|
2012-03-07 01:07:48 +04:00
|
|
|
} else {
|
|
|
|
NS_WARNING("Inappropriate image type.");
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-03-07 01:07:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureD3D9BackendData *data =
|
2014-01-23 22:26:41 +04:00
|
|
|
static_cast<TextureD3D9BackendData*>(aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9));
|
2012-03-07 01:07:48 +04:00
|
|
|
|
|
|
|
if (!data) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-03-07 01:07:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDirect3DDevice9> dev;
|
|
|
|
data->mTexture->GetDevice(getter_AddRefs(dev));
|
|
|
|
if (dev != device()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-03-07 01:07:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return data->mTexture;
|
|
|
|
}
|
|
|
|
|
2010-05-24 19:28:51 +04:00
|
|
|
void
|
2010-11-08 12:06:15 +03:00
|
|
|
ImageLayerD3D9::RenderLayer()
|
2010-05-24 19:28:51 +04:00
|
|
|
{
|
2012-02-16 07:26:19 +04:00
|
|
|
ImageContainer *container = GetContainer();
|
2012-08-13 14:10:10 +04:00
|
|
|
if (!container || mD3DManager->CompositingDisabled()) {
|
2010-05-24 19:28:51 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-16 07:26:19 +04:00
|
|
|
AutoLockImage autoLock(container);
|
|
|
|
|
|
|
|
Image *image = autoLock.GetImage();
|
2011-01-19 11:27:54 +03:00
|
|
|
if (!image) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-21 01:37:30 +03:00
|
|
|
SetShaderTransformAndOpacity();
|
|
|
|
|
2013-12-13 21:32:02 +04:00
|
|
|
gfx::IntSize size = image->GetSize();
|
2012-03-13 05:41:29 +04:00
|
|
|
|
2014-01-31 02:58:49 +04:00
|
|
|
if (image->GetFormat() == ImageFormat::CAIRO_SURFACE ||
|
|
|
|
image->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP ||
|
|
|
|
image->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE)
|
2010-12-29 04:07:04 +03:00
|
|
|
{
|
2014-01-31 02:58:49 +04:00
|
|
|
NS_ASSERTION(image->GetFormat() != ImageFormat::CAIRO_SURFACE ||
|
2014-01-24 17:42:12 +04:00
|
|
|
!static_cast<CairoImage*>(image)->mDeprecatedSurface ||
|
|
|
|
static_cast<CairoImage*>(image)->mDeprecatedSurface->GetContentType() != gfxContentType::ALPHA,
|
2012-06-26 06:43:31 +04:00
|
|
|
"Image layer has alpha image");
|
|
|
|
|
2012-02-16 07:26:19 +04:00
|
|
|
bool hasAlpha = false;
|
2012-03-07 01:07:48 +04:00
|
|
|
nsRefPtr<IDirect3DTexture9> texture = GetTexture(image, hasAlpha);
|
2012-02-01 06:18:30 +04:00
|
|
|
|
2010-12-21 01:37:30 +03:00
|
|
|
device()->SetVertexShaderConstantF(CBvLayerQuad,
|
|
|
|
ShaderConstantRect(0,
|
|
|
|
0,
|
|
|
|
size.width,
|
|
|
|
size.height),
|
|
|
|
1);
|
|
|
|
|
2012-02-16 07:26:19 +04:00
|
|
|
if (hasAlpha) {
|
2012-03-07 01:07:48 +04:00
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER, GetMaskLayer());
|
2010-12-21 01:37:30 +03:00
|
|
|
} else {
|
2012-03-07 01:07:48 +04:00
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBLAYER, GetMaskLayer());
|
2010-12-21 01:37:30 +03:00
|
|
|
}
|
|
|
|
|
2013-10-02 01:01:19 +04:00
|
|
|
if (mFilter == GraphicsFilter::FILTER_NEAREST) {
|
2012-02-01 06:18:30 +04:00
|
|
|
device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
|
|
|
|
}
|
2012-03-07 01:07:48 +04:00
|
|
|
device()->SetTexture(0, texture);
|
2012-02-16 07:26:19 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
image = nullptr;
|
2012-02-16 07:26:19 +04:00
|
|
|
autoLock.Unlock();
|
|
|
|
|
2010-12-21 01:37:30 +03:00
|
|
|
device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
|
2013-10-02 01:01:19 +04:00
|
|
|
if (mFilter == GraphicsFilter::FILTER_NEAREST) {
|
2012-02-01 06:18:30 +04:00
|
|
|
device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PlanarYCbCrImage *yuvImage =
|
2012-02-16 07:26:19 +04:00
|
|
|
static_cast<PlanarYCbCrImage*>(image);
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-08-21 14:18:20 +04:00
|
|
|
if (!yuvImage->IsValid()) {
|
2012-02-01 06:18:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:26:41 +04:00
|
|
|
if (!yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) {
|
2012-02-01 06:18:30 +04:00
|
|
|
AllocateTexturesYCbCr(yuvImage, device(), mD3DManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
PlanarYCbCrD3D9BackendData *data =
|
2014-01-23 22:26:41 +04:00
|
|
|
static_cast<PlanarYCbCrD3D9BackendData*>(yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9));
|
2012-02-01 06:18:30 +04:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDirect3DDevice9> dev;
|
|
|
|
data->mYTexture->GetDevice(getter_AddRefs(dev));
|
|
|
|
if (dev != device()) {
|
2010-05-24 19:28:51 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-02 02:24:58 +04:00
|
|
|
device()->SetVertexShaderConstantF(CBvLayerQuad,
|
|
|
|
ShaderConstantRect(0,
|
|
|
|
0,
|
2012-03-13 05:41:29 +04:00
|
|
|
size.width,
|
|
|
|
size.height),
|
2010-10-02 02:24:58 +04:00
|
|
|
1);
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2011-07-05 06:52:34 +04:00
|
|
|
device()->SetVertexShaderConstantF(CBvTextureCoords,
|
|
|
|
ShaderConstantRect(
|
2012-08-21 14:18:20 +04: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-05 06:52:34 +04:00
|
|
|
),
|
|
|
|
1);
|
|
|
|
|
2012-03-07 01:07:48 +04:00
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::YCBCRLAYER, GetMaskLayer());
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2011-01-12 07:51:27 +03:00
|
|
|
/*
|
|
|
|
* Send 3d control data and metadata
|
|
|
|
*/
|
|
|
|
if (mD3DManager->GetNv3DVUtils()) {
|
2010-11-03 02:43:29 +03:00
|
|
|
Nv_Stereo_Mode mode;
|
2012-08-21 14:18:20 +04:00
|
|
|
switch (yuvImage->GetData()->mStereoMode) {
|
2014-01-31 02:58:49 +04:00
|
|
|
case StereoMode::LEFT_RIGHT:
|
2010-11-03 02:43:29 +03:00
|
|
|
mode = NV_STEREO_MODE_LEFT_RIGHT;
|
|
|
|
break;
|
2014-01-31 02:58:49 +04:00
|
|
|
case StereoMode::RIGHT_LEFT:
|
2010-11-03 02:43:29 +03:00
|
|
|
mode = NV_STEREO_MODE_RIGHT_LEFT;
|
|
|
|
break;
|
2014-01-31 02:58:49 +04:00
|
|
|
case StereoMode::BOTTOM_TOP:
|
2010-11-03 02:43:29 +03:00
|
|
|
mode = NV_STEREO_MODE_BOTTOM_TOP;
|
|
|
|
break;
|
2014-01-31 02:58:49 +04:00
|
|
|
case StereoMode::TOP_BOTTOM:
|
2010-11-03 02:43:29 +03:00
|
|
|
mode = NV_STEREO_MODE_TOP_BOTTOM;
|
|
|
|
break;
|
2014-01-31 02:58:49 +04:00
|
|
|
case StereoMode::MONO:
|
2011-01-12 07:51:27 +03:00
|
|
|
mode = NV_STEREO_MODE_MONO;
|
|
|
|
break;
|
2010-11-03 02:43:29 +03:00
|
|
|
}
|
|
|
|
|
2011-01-12 07:51:27 +03: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-31 02:58:49 +04:00
|
|
|
if (yuvImage->GetData()->mStereoMode != StereoMode::MONO) {
|
2010-11-03 02:43:29 +03:00
|
|
|
mD3DManager->GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE);
|
|
|
|
|
|
|
|
nsRefPtr<IDirect3DSurface9> renderTarget;
|
|
|
|
device()->GetRenderTarget(0, getter_AddRefs(renderTarget));
|
2012-08-21 14:18:20 +04:00
|
|
|
mD3DManager->GetNv3DVUtils()->SendNv3DVMetaData((unsigned int)yuvImage->GetSize().width,
|
|
|
|
(unsigned int)yuvImage->GetSize().height, (HANDLE)(data->mYTexture), (HANDLE)(renderTarget));
|
2010-11-03 02:43:29 +03:00
|
|
|
}
|
|
|
|
}
|
2010-08-27 00:44:53 +04:00
|
|
|
|
2010-12-29 22:22:18 +03: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-02-01 06:18:30 +04:00
|
|
|
device()->SetTexture(0, data->mYTexture);
|
|
|
|
device()->SetTexture(1, data->mCbTexture);
|
|
|
|
device()->SetTexture(2, data->mCrTexture);
|
2010-05-24 19:28:51 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
image = nullptr;
|
|
|
|
data = nullptr;
|
2012-02-16 07:26:19 +04:00
|
|
|
autoLock.Unlock();
|
|
|
|
|
2010-05-24 19:28:51 +04:00
|
|
|
device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
|
|
|
|
|
2011-07-05 06:52:34 +04:00
|
|
|
device()->SetVertexShaderConstantF(CBvTextureCoords,
|
|
|
|
ShaderConstantRect(0, 0, 1.0f, 1.0f), 1);
|
2010-05-24 19:28:51 +04:00
|
|
|
}
|
2011-03-24 01:28:57 +03:00
|
|
|
|
|
|
|
GetContainer()->NotifyPaintedImage(image);
|
2010-05-24 19:28:51 +04:00
|
|
|
}
|
|
|
|
|
2012-03-07 01:07:48 +04:00
|
|
|
already_AddRefed<IDirect3DTexture9>
|
2013-12-20 20:46:29 +04:00
|
|
|
ImageLayerD3D9::GetAsTexture(gfx::IntSize* aSize)
|
2012-03-07 01:07:48 +04:00
|
|
|
{
|
|
|
|
if (!GetContainer()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-03-07 01:07:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoLockImage autoLock(GetContainer());
|
|
|
|
|
|
|
|
Image *image = autoLock.GetImage();
|
|
|
|
|
|
|
|
if (!image) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-03-07 01:07:48 +04:00
|
|
|
}
|
|
|
|
|
2014-01-31 02:58:49 +04:00
|
|
|
if (image->GetFormat() != ImageFormat::CAIRO_SURFACE &&
|
|
|
|
image->GetFormat() != ImageFormat::REMOTE_IMAGE_BITMAP) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-03-07 01:07:48 +04:00
|
|
|
}
|
2013-05-04 14:12:40 +04:00
|
|
|
|
2012-03-07 01:07:48 +04:00
|
|
|
bool dontCare;
|
2013-12-31 13:06:12 +04:00
|
|
|
*aSize = image->GetSize();
|
2012-03-07 01:07:48 +04:00
|
|
|
nsRefPtr<IDirect3DTexture9> result = GetTexture(image, dontCare);
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2010-05-24 19:28:51 +04:00
|
|
|
} /* layers */
|
|
|
|
} /* mozilla */
|