2010-08-06 22:09:18 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2010-05-24 08:28:51 -07:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Corporation code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Bas Schouten <bschouten@mozilla.org>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "ImageLayerD3D9.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "yuv_convert.h"
|
2010-08-26 13:44:53 -07:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "Nv3DVUtils.h"
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
using mozilla::MutexAutoLock;
|
|
|
|
|
2010-12-20 14:37:01 -08:00
|
|
|
static already_AddRefed<IDirect3DTexture9>
|
|
|
|
SurfaceToTexture(IDirect3DDevice9 *aDevice,
|
|
|
|
gfxASurface *aSurface,
|
|
|
|
const gfxIntSize &aSize)
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxImageSurface> imageSurface =
|
|
|
|
new gfxImageSurface(aSize, gfxASurface::ImageFormatARGB32);
|
|
|
|
|
|
|
|
nsRefPtr<gfxContext> context = new gfxContext(imageSurface);
|
|
|
|
|
|
|
|
context->SetSource(aSurface);
|
|
|
|
context->Paint();
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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,
|
|
|
|
1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,
|
|
|
|
getter_AddRefs(texture), NULL)))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<IDirect3DSurface9> surface;
|
|
|
|
if (FAILED(aDevice->
|
|
|
|
CreateOffscreenPlainSurface(aSize.width,
|
|
|
|
aSize.height,
|
|
|
|
D3DFMT_A8R8G8B8,
|
|
|
|
D3DPOOL_SYSTEMMEM,
|
|
|
|
getter_AddRefs(surface),
|
|
|
|
NULL)))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3DLOCKED_RECT lockedRect;
|
|
|
|
surface->LockRect(&lockedRect, NULL, 0);
|
|
|
|
for (int y = 0; y < aSize.height; y++) {
|
|
|
|
memcpy((char*)lockedRect.pBits + lockedRect.Pitch * y,
|
|
|
|
imageSurface->Data() + imageSurface->Stride() * y,
|
|
|
|
aSize.width * 4);
|
|
|
|
}
|
|
|
|
surface->UnlockRect();
|
|
|
|
nsRefPtr<IDirect3DSurface9> dstSurface;
|
|
|
|
texture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
|
|
|
aDevice->UpdateSurface(surface, NULL, dstSurface, NULL);
|
|
|
|
} else {
|
|
|
|
if (FAILED(aDevice->
|
|
|
|
CreateTexture(aSize.width, aSize.height,
|
|
|
|
1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
|
|
|
|
getter_AddRefs(texture), NULL)))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3DLOCKED_RECT lockrect;
|
|
|
|
/* lock the entire texture */
|
|
|
|
texture->LockRect(0, &lockrect, NULL, 0);
|
|
|
|
|
|
|
|
// copy over data. If we don't need to do any swaping we can
|
|
|
|
// use memcpy
|
|
|
|
for (int y = 0; y < aSize.height; y++) {
|
|
|
|
memcpy((char*)lockrect.pBits + lockrect.Pitch * y,
|
|
|
|
imageSurface->Data() + imageSurface->Stride() * y,
|
|
|
|
aSize.width * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
texture->UnlockRect(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return texture.forget();
|
|
|
|
}
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
ImageContainerD3D9::ImageContainerD3D9(LayerManagerD3D9 *aManager)
|
|
|
|
: ImageContainer(aManager)
|
|
|
|
, mActiveImageLock("mozilla.layers.ImageContainerD3D9.mActiveImageLock")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Image>
|
|
|
|
ImageContainerD3D9::CreateImage(const Image::Format *aFormats,
|
|
|
|
PRUint32 aNumFormats)
|
|
|
|
{
|
|
|
|
if (!aNumFormats) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
nsRefPtr<Image> img;
|
|
|
|
if (aFormats[0] == Image::PLANAR_YCBCR) {
|
2010-12-20 14:37:18 -08:00
|
|
|
img = new PlanarYCbCrImageD3D9();
|
2010-05-24 08:28:51 -07:00
|
|
|
} else if (aFormats[0] == Image::CAIRO_SURFACE) {
|
2010-12-20 14:35:31 -08:00
|
|
|
img = new CairoImageD3D9(static_cast<LayerManagerD3D9*>(mManager)->device());
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
return img.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImageContainerD3D9::SetCurrentImage(Image *aImage)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mActiveImageLock);
|
|
|
|
|
|
|
|
mActiveImage = aImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Image>
|
|
|
|
ImageContainerD3D9::GetCurrentImage()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mActiveImageLock);
|
|
|
|
|
|
|
|
nsRefPtr<Image> retval = mActiveImage;
|
|
|
|
return retval.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
ImageContainerD3D9::GetCurrentAsSurface(gfxIntSize *aSize)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mActiveImageLock);
|
|
|
|
if (!mActiveImage) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mActiveImage->GetFormat() == Image::PLANAR_YCBCR) {
|
|
|
|
PlanarYCbCrImageD3D9 *yuvImage =
|
|
|
|
static_cast<PlanarYCbCrImageD3D9*>(mActiveImage.get());
|
|
|
|
if (yuvImage->HasData()) {
|
|
|
|
*aSize = yuvImage->mSize;
|
|
|
|
}
|
|
|
|
} else if (mActiveImage->GetFormat() == Image::CAIRO_SURFACE) {
|
|
|
|
CairoImageD3D9 *cairoImage =
|
|
|
|
static_cast<CairoImageD3D9*>(mActiveImage.get());
|
2010-11-18 10:47:59 -08:00
|
|
|
*aSize = cairoImage->GetSize();
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<ImageD3D9*>(mActiveImage->GetImplData())->GetAsSurface();
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxIntSize
|
|
|
|
ImageContainerD3D9::GetCurrentSize()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mActiveImageLock);
|
|
|
|
if (!mActiveImage) {
|
|
|
|
return gfxIntSize(0,0);
|
|
|
|
}
|
|
|
|
if (mActiveImage->GetFormat() == Image::PLANAR_YCBCR) {
|
|
|
|
PlanarYCbCrImageD3D9 *yuvImage =
|
|
|
|
static_cast<PlanarYCbCrImageD3D9*>(mActiveImage.get());
|
|
|
|
if (!yuvImage->HasData()) {
|
|
|
|
return gfxIntSize(0,0);
|
|
|
|
}
|
|
|
|
return yuvImage->mSize;
|
|
|
|
|
|
|
|
} else if (mActiveImage->GetFormat() == Image::CAIRO_SURFACE) {
|
|
|
|
CairoImageD3D9 *cairoImage =
|
|
|
|
static_cast<CairoImageD3D9*>(mActiveImage.get());
|
2010-11-18 10:47:59 -08:00
|
|
|
return cairoImage->GetSize();
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return gfxIntSize(0,0);
|
|
|
|
}
|
|
|
|
|
2010-08-06 22:09:18 -07:00
|
|
|
PRBool
|
|
|
|
ImageContainerD3D9::SetLayerManager(LayerManager *aManager)
|
|
|
|
{
|
2010-12-20 14:37:30 -08:00
|
|
|
if (aManager->GetBackendType() == LayerManager::LAYERS_D3D9) {
|
|
|
|
mManager = aManager;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-06 22:09:18 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
Layer*
|
|
|
|
ImageLayerD3D9::GetLayer()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-08 01:06:15 -08:00
|
|
|
ImageLayerD3D9::RenderLayer()
|
2010-05-24 08:28:51 -07:00
|
|
|
{
|
|
|
|
if (!GetContainer()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<Image> image = GetContainer()->GetCurrentImage();
|
|
|
|
|
2010-12-20 14:37:30 -08:00
|
|
|
if (Manager() != GetContainer()->Manager()) {
|
|
|
|
GetContainer()->SetLayerManager(Manager());
|
|
|
|
}
|
|
|
|
|
|
|
|
SetShaderTransformAndOpacity();
|
|
|
|
|
|
|
|
if (Manager() != GetContainer()->Manager()) {
|
|
|
|
gfxIntSize size;
|
|
|
|
nsRefPtr<gfxASurface> surface =
|
|
|
|
GetContainer()->GetCurrentAsSurface(&size);
|
|
|
|
nsRefPtr<IDirect3DTexture9> texture =
|
|
|
|
SurfaceToTexture(device(), surface, size);
|
|
|
|
|
|
|
|
device()->SetVertexShaderConstantF(CBvLayerQuad,
|
|
|
|
ShaderConstantRect(0,
|
|
|
|
0,
|
|
|
|
size.width,
|
|
|
|
size.height),
|
|
|
|
1);
|
|
|
|
|
|
|
|
if (surface->GetContentType() == gfxASurface::CONTENT_COLOR_ALPHA) {
|
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER);
|
|
|
|
} else {
|
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBLAYER);
|
|
|
|
}
|
|
|
|
|
|
|
|
device()->SetTexture(0, texture);
|
|
|
|
device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
|
|
|
|
} else if (image->GetFormat() == Image::PLANAR_YCBCR) {
|
2010-05-24 08:28:51 -07:00
|
|
|
PlanarYCbCrImageD3D9 *yuvImage =
|
|
|
|
static_cast<PlanarYCbCrImageD3D9*>(image.get());
|
|
|
|
|
|
|
|
if (!yuvImage->HasData()) {
|
|
|
|
return;
|
|
|
|
}
|
2010-12-20 14:37:18 -08:00
|
|
|
yuvImage->AllocateTextures(device());
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-10-01 15:24:58 -07:00
|
|
|
device()->SetVertexShaderConstantF(CBvLayerQuad,
|
|
|
|
ShaderConstantRect(0,
|
|
|
|
0,
|
|
|
|
yuvImage->mSize.width,
|
|
|
|
yuvImage->mSize.height),
|
|
|
|
1);
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-08-10 16:32:45 -07:00
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::YCBCRLAYER);
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-11-02 16:43:29 -07:00
|
|
|
if (yuvImage->mData.mStereoMode != STEREO_MODE_MONO) {
|
|
|
|
Nv_Stereo_Mode mode;
|
|
|
|
switch (yuvImage->mData.mStereoMode) {
|
|
|
|
case STEREO_MODE_LEFT_RIGHT:
|
|
|
|
mode = NV_STEREO_MODE_LEFT_RIGHT;
|
|
|
|
break;
|
|
|
|
case STEREO_MODE_RIGHT_LEFT:
|
|
|
|
mode = NV_STEREO_MODE_RIGHT_LEFT;
|
|
|
|
break;
|
|
|
|
case STEREO_MODE_BOTTOM_TOP:
|
|
|
|
mode = NV_STEREO_MODE_BOTTOM_TOP;
|
|
|
|
break;
|
|
|
|
case STEREO_MODE_TOP_BOTTOM:
|
|
|
|
mode = NV_STEREO_MODE_TOP_BOTTOM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send 3d control data and metadata
|
|
|
|
*/
|
|
|
|
if (mD3DManager->GetNv3DVUtils()) {
|
|
|
|
mD3DManager->GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE);
|
|
|
|
|
|
|
|
nsRefPtr<IDirect3DSurface9> renderTarget;
|
|
|
|
device()->GetRenderTarget(0, getter_AddRefs(renderTarget));
|
|
|
|
mD3DManager->GetNv3DVUtils()->SendNv3DVMetaData((unsigned int)yuvImage->mSize.width,
|
|
|
|
(unsigned int)yuvImage->mSize.height, (HANDLE)(yuvImage->mYTexture), (HANDLE)(renderTarget));
|
|
|
|
}
|
|
|
|
}
|
2010-08-26 13:44:53 -07:00
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
device()->SetTexture(0, yuvImage->mYTexture);
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
|
|
|
device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
|
|
|
device()->SetTexture(1, yuvImage->mCbTexture);
|
|
|
|
device()->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
|
|
|
device()->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
|
|
|
device()->SetTexture(2, yuvImage->mCrTexture);
|
|
|
|
device()->SetSamplerState(2, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
|
|
|
device()->SetSamplerState(2, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
|
|
|
|
|
|
|
device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
|
|
|
|
|
|
|
|
} else if (image->GetFormat() == Image::CAIRO_SURFACE) {
|
|
|
|
CairoImageD3D9 *cairoImage =
|
|
|
|
static_cast<CairoImageD3D9*>(image.get());
|
|
|
|
|
2010-12-20 14:37:04 -08:00
|
|
|
if (cairoImage->device() != device()) {
|
|
|
|
cairoImage->SetDevice(device());
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-10-01 15:24:58 -07:00
|
|
|
device()->SetVertexShaderConstantF(CBvLayerQuad,
|
|
|
|
ShaderConstantRect(0,
|
|
|
|
0,
|
2010-11-18 10:47:59 -08:00
|
|
|
cairoImage->GetSize().width,
|
|
|
|
cairoImage->GetSize().height),
|
2010-10-01 15:24:58 -07:00
|
|
|
1);
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-12-20 14:37:08 -08:00
|
|
|
if (cairoImage->HasAlpha()) {
|
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER);
|
|
|
|
} else {
|
|
|
|
mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBLAYER);
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-11-18 10:47:59 -08:00
|
|
|
device()->SetTexture(0, cairoImage->GetOrCreateTexture());
|
2010-05-24 08:28:51 -07:00
|
|
|
device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-20 14:37:18 -08:00
|
|
|
PlanarYCbCrImageD3D9::PlanarYCbCrImageD3D9()
|
2010-05-24 08:28:51 -07:00
|
|
|
: PlanarYCbCrImage(static_cast<ImageD3D9*>(this))
|
|
|
|
, mHasData(PR_FALSE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PlanarYCbCrImageD3D9::SetData(const PlanarYCbCrImage::Data &aData)
|
|
|
|
{
|
2010-08-10 16:39:45 -07:00
|
|
|
// XXX - For D3D9Ex we really should just copy to systemmem surfaces here.
|
2010-05-24 08:28:51 -07:00
|
|
|
// For now, we copy the data
|
|
|
|
int width_shift = 0;
|
|
|
|
int height_shift = 0;
|
|
|
|
if (aData.mYSize.width == aData.mCbCrSize.width &&
|
|
|
|
aData.mYSize.height == aData.mCbCrSize.height) {
|
|
|
|
// YV24 format
|
|
|
|
width_shift = 0;
|
|
|
|
height_shift = 0;
|
2010-09-02 20:50:42 -07:00
|
|
|
mType = gfx::YV24;
|
2010-05-24 08:28:51 -07:00
|
|
|
} else if (aData.mYSize.width / 2 == aData.mCbCrSize.width &&
|
|
|
|
aData.mYSize.height == aData.mCbCrSize.height) {
|
|
|
|
// YV16 format
|
|
|
|
width_shift = 1;
|
|
|
|
height_shift = 0;
|
2010-09-02 20:50:42 -07:00
|
|
|
mType = gfx::YV16;
|
2010-05-24 08:28:51 -07:00
|
|
|
} else if (aData.mYSize.width / 2 == aData.mCbCrSize.width &&
|
|
|
|
aData.mYSize.height / 2 == aData.mCbCrSize.height ) {
|
|
|
|
// YV12 format
|
|
|
|
width_shift = 1;
|
|
|
|
height_shift = 1;
|
2010-09-02 20:50:42 -07:00
|
|
|
mType = gfx::YV12;
|
2010-05-24 08:28:51 -07:00
|
|
|
} else {
|
|
|
|
NS_ERROR("YCbCr format not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
mData = aData;
|
|
|
|
mData.mCbCrStride = mData.mCbCrSize.width = aData.mPicSize.width >> width_shift;
|
2010-09-02 20:50:42 -07:00
|
|
|
// Round up the values for width and height to make sure we sample enough data
|
|
|
|
// for the last pixel - See bug 590735
|
|
|
|
if (width_shift && (aData.mPicSize.width & 1)) {
|
|
|
|
mData.mCbCrStride++;
|
|
|
|
mData.mCbCrSize.width++;
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
mData.mCbCrSize.height = aData.mPicSize.height >> height_shift;
|
2010-09-02 20:50:42 -07:00
|
|
|
if (height_shift && (aData.mPicSize.height & 1)) {
|
|
|
|
mData.mCbCrSize.height++;
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
mData.mYSize = aData.mPicSize;
|
|
|
|
mData.mYStride = mData.mYSize.width;
|
2010-05-28 20:27:03 -07:00
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
mBuffer = new PRUint8[mData.mCbCrStride * mData.mCbCrSize.height * 2 +
|
|
|
|
mData.mYStride * mData.mYSize.height];
|
|
|
|
mData.mYChannel = mBuffer;
|
|
|
|
mData.mCbChannel = mData.mYChannel + mData.mYStride * mData.mYSize.height;
|
|
|
|
mData.mCrChannel = mData.mCbChannel + mData.mCbCrStride * mData.mCbCrSize.height;
|
|
|
|
|
|
|
|
int cbcr_x = aData.mPicX >> width_shift;
|
|
|
|
int cbcr_y = aData.mPicY >> height_shift;
|
|
|
|
|
|
|
|
for (int i = 0; i < mData.mYSize.height; i++) {
|
|
|
|
memcpy(mData.mYChannel + i * mData.mYStride,
|
|
|
|
aData.mYChannel + ((aData.mPicY + i) * aData.mYStride) + aData.mPicX,
|
|
|
|
mData.mYStride);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < mData.mCbCrSize.height; i++) {
|
|
|
|
memcpy(mData.mCbChannel + i * mData.mCbCrStride,
|
|
|
|
aData.mCbChannel + ((cbcr_y + i) * aData.mCbCrStride) + cbcr_x,
|
|
|
|
mData.mCbCrStride);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < mData.mCbCrSize.height; i++) {
|
|
|
|
memcpy(mData.mCrChannel + i * mData.mCbCrStride,
|
|
|
|
aData.mCrChannel + ((cbcr_y + i) * aData.mCbCrStride) + cbcr_x,
|
|
|
|
mData.mCbCrStride);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix picture rect to be correct
|
|
|
|
mData.mPicX = mData.mPicY = 0;
|
|
|
|
mSize = aData.mPicSize;
|
|
|
|
|
|
|
|
mHasData = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-20 14:37:18 -08:00
|
|
|
PlanarYCbCrImageD3D9::AllocateTextures(IDirect3DDevice9 *aDevice)
|
2010-05-24 08:28:51 -07:00
|
|
|
{
|
2010-08-10 16:39:45 -07:00
|
|
|
D3DLOCKED_RECT lockrectY;
|
|
|
|
D3DLOCKED_RECT lockrectCb;
|
|
|
|
D3DLOCKED_RECT lockrectCr;
|
2010-05-24 08:28:51 -07:00
|
|
|
PRUint8* src;
|
|
|
|
PRUint8* dest;
|
|
|
|
|
2010-08-10 16:39:45 -07:00
|
|
|
nsRefPtr<IDirect3DSurface9> tmpSurfaceY;
|
|
|
|
nsRefPtr<IDirect3DSurface9> tmpSurfaceCb;
|
|
|
|
nsRefPtr<IDirect3DSurface9> tmpSurfaceCr;
|
|
|
|
|
2010-12-20 14:37:18 -08:00
|
|
|
nsRefPtr<IDirect3DDevice9Ex> deviceEx;
|
2010-12-21 02:52:00 -08:00
|
|
|
aDevice->QueryInterface(IID_IDirect3DDevice9Ex,
|
2010-12-20 14:37:18 -08:00
|
|
|
getter_AddRefs(deviceEx));
|
|
|
|
|
|
|
|
bool isD3D9Ex = deviceEx;
|
|
|
|
|
|
|
|
if (isD3D9Ex) {
|
2010-08-12 11:25:09 -07:00
|
|
|
nsRefPtr<IDirect3DTexture9> tmpYTexture;
|
|
|
|
nsRefPtr<IDirect3DTexture9> tmpCbTexture;
|
|
|
|
nsRefPtr<IDirect3DTexture9> tmpCrTexture;
|
2010-08-10 16:39:45 -07:00
|
|
|
// 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.
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->CreateTexture(mData.mYSize.width, mData.mYSize.height,
|
2010-08-10 16:39:45 -07:00
|
|
|
1, 0, D3DFMT_L8, D3DPOOL_DEFAULT,
|
|
|
|
getter_AddRefs(mYTexture), NULL);
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->CreateTexture(mData.mCbCrSize.width, mData.mCbCrSize.height,
|
2010-08-10 16:39:45 -07:00
|
|
|
1, 0, D3DFMT_L8, D3DPOOL_DEFAULT,
|
|
|
|
getter_AddRefs(mCbTexture), NULL);
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->CreateTexture(mData.mCbCrSize.width, mData.mCbCrSize.height,
|
2010-08-10 16:39:45 -07:00
|
|
|
1, 0, D3DFMT_L8, D3DPOOL_DEFAULT,
|
|
|
|
getter_AddRefs(mCrTexture), NULL);
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->CreateTexture(mData.mYSize.width, mData.mYSize.height,
|
2010-08-12 11:25:09 -07:00
|
|
|
1, 0, D3DFMT_L8, D3DPOOL_SYSTEMMEM,
|
|
|
|
getter_AddRefs(tmpYTexture), NULL);
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->CreateTexture(mData.mCbCrSize.width, mData.mCbCrSize.height,
|
2010-08-12 11:25:09 -07:00
|
|
|
1, 0, D3DFMT_L8, D3DPOOL_SYSTEMMEM,
|
|
|
|
getter_AddRefs(tmpCbTexture), NULL);
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->CreateTexture(mData.mCbCrSize.width, mData.mCbCrSize.height,
|
2010-08-12 11:25:09 -07:00
|
|
|
1, 0, D3DFMT_L8, D3DPOOL_SYSTEMMEM,
|
|
|
|
getter_AddRefs(tmpCrTexture), NULL);
|
|
|
|
tmpYTexture->GetSurfaceLevel(0, getter_AddRefs(tmpSurfaceY));
|
|
|
|
tmpCbTexture->GetSurfaceLevel(0, getter_AddRefs(tmpSurfaceCb));
|
|
|
|
tmpCrTexture->GetSurfaceLevel(0, getter_AddRefs(tmpSurfaceCr));
|
2010-08-10 16:39:45 -07:00
|
|
|
tmpSurfaceY->LockRect(&lockrectY, NULL, 0);
|
|
|
|
tmpSurfaceCb->LockRect(&lockrectCb, NULL, 0);
|
|
|
|
tmpSurfaceCr->LockRect(&lockrectCr, NULL, 0);
|
|
|
|
} else {
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->CreateTexture(mData.mYSize.width, mData.mYSize.height,
|
2010-08-10 16:39:45 -07:00
|
|
|
1, 0, D3DFMT_L8, D3DPOOL_MANAGED,
|
|
|
|
getter_AddRefs(mYTexture), NULL);
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->CreateTexture(mData.mCbCrSize.width, mData.mCbCrSize.height,
|
2010-08-10 16:39:45 -07:00
|
|
|
1, 0, D3DFMT_L8, D3DPOOL_MANAGED,
|
|
|
|
getter_AddRefs(mCbTexture), NULL);
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->CreateTexture(mData.mCbCrSize.width, mData.mCbCrSize.height,
|
2010-08-10 16:39:45 -07:00
|
|
|
1, 0, D3DFMT_L8, D3DPOOL_MANAGED,
|
|
|
|
getter_AddRefs(mCrTexture), NULL);
|
|
|
|
|
|
|
|
/* lock the entire texture */
|
|
|
|
mYTexture->LockRect(0, &lockrectY, NULL, 0);
|
|
|
|
mCbTexture->LockRect(0, &lockrectCb, NULL, 0);
|
|
|
|
mCrTexture->LockRect(0, &lockrectCr, NULL, 0);
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
src = mData.mYChannel;
|
|
|
|
//FIX cast
|
2010-08-10 16:39:45 -07:00
|
|
|
dest = (PRUint8*)lockrectY.pBits;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
// copy over data
|
|
|
|
for (int h=0; h<mData.mYSize.height; h++) {
|
|
|
|
memcpy(dest, src, mData.mYSize.width);
|
2010-08-10 16:39:45 -07:00
|
|
|
dest += lockrectY.Pitch;
|
2010-05-24 08:28:51 -07:00
|
|
|
src += mData.mYStride;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = mData.mCbChannel;
|
|
|
|
//FIX cast
|
2010-08-10 16:39:45 -07:00
|
|
|
dest = (PRUint8*)lockrectCb.pBits;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
// copy over data
|
|
|
|
for (int h=0; h<mData.mCbCrSize.height; h++) {
|
|
|
|
memcpy(dest, src, mData.mCbCrSize.width);
|
2010-08-10 16:39:45 -07:00
|
|
|
dest += lockrectCb.Pitch;
|
2010-05-24 08:28:51 -07:00
|
|
|
src += mData.mCbCrStride;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = mData.mCrChannel;
|
|
|
|
//FIX cast
|
2010-08-10 16:39:45 -07:00
|
|
|
dest = (PRUint8*)lockrectCr.pBits;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
// copy over data
|
|
|
|
for (int h=0; h<mData.mCbCrSize.height; h++) {
|
|
|
|
memcpy(dest, src, mData.mCbCrSize.width);
|
2010-08-10 16:39:45 -07:00
|
|
|
dest += lockrectCr.Pitch;
|
2010-05-24 08:28:51 -07:00
|
|
|
src += mData.mCbCrStride;
|
|
|
|
}
|
|
|
|
|
2010-12-20 14:37:18 -08:00
|
|
|
if (isD3D9Ex) {
|
2010-08-10 16:39:45 -07:00
|
|
|
tmpSurfaceY->UnlockRect();
|
|
|
|
tmpSurfaceCb->UnlockRect();
|
|
|
|
tmpSurfaceCr->UnlockRect();
|
|
|
|
nsRefPtr<IDirect3DSurface9> dstSurface;
|
|
|
|
mYTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->UpdateSurface(tmpSurfaceY, NULL, dstSurface, NULL);
|
2010-08-10 16:39:45 -07:00
|
|
|
mCbTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->UpdateSurface(tmpSurfaceCb, NULL, dstSurface, NULL);
|
2010-08-10 16:39:45 -07:00
|
|
|
mCrTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface));
|
2010-12-20 14:37:18 -08:00
|
|
|
aDevice->UpdateSurface(tmpSurfaceCr, NULL, dstSurface, NULL);
|
2010-08-10 16:39:45 -07:00
|
|
|
} else {
|
|
|
|
mYTexture->UnlockRect(0);
|
|
|
|
mCbTexture->UnlockRect(0);
|
|
|
|
mCrTexture->UnlockRect(0);
|
|
|
|
}
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PlanarYCbCrImageD3D9::FreeTextures()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
PlanarYCbCrImageD3D9::GetAsSurface()
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxImageSurface> imageSurface =
|
|
|
|
new gfxImageSurface(mSize, gfxASurface::ImageFormatRGB24);
|
2010-05-28 20:27:03 -07:00
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
// Convert from YCbCr to RGB now
|
|
|
|
gfx::ConvertYCbCrToRGB32(mData.mYChannel,
|
|
|
|
mData.mCbChannel,
|
|
|
|
mData.mCrChannel,
|
|
|
|
imageSurface->Data(),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
mSize.width,
|
|
|
|
mSize.height,
|
|
|
|
mData.mYStride,
|
|
|
|
mData.mCbCrStride,
|
|
|
|
imageSurface->Stride(),
|
2010-09-02 20:50:42 -07:00
|
|
|
mType);
|
2010-05-24 08:28:51 -07:00
|
|
|
|
|
|
|
return imageSurface.forget().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
CairoImageD3D9::~CairoImageD3D9()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-20 14:37:04 -08:00
|
|
|
void
|
|
|
|
CairoImageD3D9::SetDevice(IDirect3DDevice9 *aDevice)
|
|
|
|
{
|
|
|
|
mTexture = NULL;
|
|
|
|
mDevice = aDevice;
|
|
|
|
}
|
|
|
|
|
2010-05-24 08:28:51 -07:00
|
|
|
void
|
|
|
|
CairoImageD3D9::SetData(const CairoImage::Data &aData)
|
|
|
|
{
|
|
|
|
mSize = aData.mSize;
|
2010-11-18 10:47:59 -08:00
|
|
|
mCachedSurface = aData.mSurface;
|
|
|
|
mTexture = NULL;
|
|
|
|
|
|
|
|
// Try to upload the surface immediately, so that we don't block the
|
|
|
|
// rendering pipeline at paint time.
|
|
|
|
(void) GetOrCreateTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
IDirect3DTexture9*
|
|
|
|
CairoImageD3D9::GetOrCreateTexture()
|
|
|
|
{
|
|
|
|
if (mTexture)
|
|
|
|
return mTexture;
|
2010-05-24 08:28:51 -07:00
|
|
|
|
2010-12-20 14:37:01 -08:00
|
|
|
mTexture = SurfaceToTexture(mDevice, mCachedSurface, mSize);
|
2010-11-18 10:47:59 -08:00
|
|
|
|
2010-12-20 14:37:04 -08:00
|
|
|
// We need to keep our cached surface around in case the device changes.
|
2010-11-18 10:47:59 -08:00
|
|
|
return mTexture;
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
CairoImageD3D9::GetAsSurface()
|
|
|
|
{
|
2010-12-20 14:37:11 -08:00
|
|
|
nsRefPtr<gfxASurface> surface = mCachedSurface;
|
|
|
|
return surface.forget();
|
2010-05-24 08:28:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* layers */
|
|
|
|
} /* mozilla */
|