2009-12-02 00:05:26 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; 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/. */
|
2009-12-02 00:05:26 +03:00
|
|
|
|
|
|
|
#include "SharedDIBWin.h"
|
2011-02-17 01:43:30 +03:00
|
|
|
#include "gfxAlphaRecovery.h"
|
2009-12-02 00:05:26 +03:00
|
|
|
#include "nsMathUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t kBytesPerPixel = 4;
|
|
|
|
static const uint32_t kByteAlign = 1 << gfxAlphaRecovery::GoodAlignmentLog2();
|
|
|
|
static const uint32_t kHeaderBytes =
|
2011-02-17 01:43:30 +03:00
|
|
|
(sizeof(BITMAPV4HEADER) + kByteAlign - 1) & ~(kByteAlign - 1);
|
2010-10-25 21:57:13 +04:00
|
|
|
|
2009-12-02 00:05:26 +03:00
|
|
|
SharedDIBWin::SharedDIBWin() :
|
2012-07-30 18:20:58 +04:00
|
|
|
mSharedHdc(nullptr)
|
|
|
|
, mSharedBmp(nullptr)
|
|
|
|
, mOldObj(nullptr)
|
2009-12-02 00:05:26 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedDIBWin::~SharedDIBWin()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SharedDIBWin::Close()
|
|
|
|
{
|
|
|
|
if (mSharedHdc && mOldObj)
|
|
|
|
::SelectObject(mSharedHdc, mOldObj);
|
|
|
|
|
|
|
|
if (mSharedHdc)
|
|
|
|
::DeleteObject(mSharedHdc);
|
|
|
|
|
|
|
|
if (mSharedBmp)
|
|
|
|
::DeleteObject(mSharedBmp);
|
|
|
|
|
|
|
|
mSharedHdc = NULL;
|
|
|
|
mOldObj = mSharedBmp = NULL;
|
|
|
|
|
|
|
|
SharedDIB::Close();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
SharedDIBWin::Create(HDC aHdc, uint32_t aWidth, uint32_t aHeight,
|
2010-10-27 17:03:09 +04:00
|
|
|
bool aTransparent)
|
2009-12-02 00:05:26 +03:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
|
|
|
|
// create the offscreen shared dib
|
2010-10-27 17:03:09 +04:00
|
|
|
BITMAPV4HEADER bmih;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size = SetupBitmapHeader(aWidth, aHeight, aTransparent, &bmih);
|
2009-12-02 00:05:26 +03:00
|
|
|
|
|
|
|
nsresult rv = SharedDIB::Create(size);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (NS_FAILED(SetupSurface(aHdc, &bmih))) {
|
|
|
|
Close();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
SharedDIBWin::Attach(Handle aHandle, uint32_t aWidth, uint32_t aHeight,
|
2010-10-27 17:03:09 +04:00
|
|
|
bool aTransparent)
|
2009-12-02 00:05:26 +03:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
|
2010-10-27 17:03:09 +04:00
|
|
|
BITMAPV4HEADER bmih;
|
|
|
|
SetupBitmapHeader(aWidth, aHeight, aTransparent, &bmih);
|
2009-12-02 00:05:26 +03:00
|
|
|
|
|
|
|
nsresult rv = SharedDIB::Attach(aHandle, 0);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (NS_FAILED(SetupSurface(NULL, &bmih))) {
|
|
|
|
Close();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
|
|
|
SharedDIBWin::SetupBitmapHeader(uint32_t aWidth, uint32_t aHeight,
|
2010-10-27 17:03:09 +04:00
|
|
|
bool aTransparent, BITMAPV4HEADER *aHeader)
|
2009-12-02 00:05:26 +03:00
|
|
|
{
|
2010-11-17 08:50:39 +03:00
|
|
|
// D3D cannot handle an offscreen memory that pitch (SysMemPitch) is negative.
|
|
|
|
// So we create top-to-bottom DIB.
|
2010-10-27 17:03:09 +04:00
|
|
|
memset((void*)aHeader, 0, sizeof(BITMAPV4HEADER));
|
|
|
|
aHeader->bV4Size = sizeof(BITMAPV4HEADER);
|
|
|
|
aHeader->bV4Width = aWidth;
|
2010-11-17 08:50:39 +03:00
|
|
|
aHeader->bV4Height = -LONG(aHeight); // top-to-buttom DIB
|
2010-10-27 17:03:09 +04:00
|
|
|
aHeader->bV4Planes = 1;
|
|
|
|
aHeader->bV4BitCount = 32;
|
|
|
|
aHeader->bV4V4Compression = BI_BITFIELDS;
|
|
|
|
aHeader->bV4RedMask = 0x00FF0000;
|
|
|
|
aHeader->bV4GreenMask = 0x0000FF00;
|
|
|
|
aHeader->bV4BlueMask = 0x000000FF;
|
|
|
|
|
|
|
|
if (aTransparent)
|
|
|
|
aHeader->bV4AlphaMask = 0xFF000000;
|
|
|
|
|
2011-02-17 01:43:30 +03:00
|
|
|
return (kHeaderBytes + (-aHeader->bV4Height * aHeader->bV4Width * kBytesPerPixel));
|
2009-12-02 00:05:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-10-27 17:03:09 +04:00
|
|
|
SharedDIBWin::SetupSurface(HDC aHdc, BITMAPV4HEADER *aHdr)
|
2009-12-02 00:05:26 +03:00
|
|
|
{
|
|
|
|
mSharedHdc = ::CreateCompatibleDC(aHdc);
|
|
|
|
|
|
|
|
if (!mSharedHdc)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mSharedBmp = ::CreateDIBSection(mSharedHdc,
|
|
|
|
(BITMAPINFO*)aHdr,
|
|
|
|
DIB_RGB_COLORS,
|
2010-10-25 21:57:13 +04:00
|
|
|
&mBitmapBits,
|
2009-12-02 00:05:26 +03:00
|
|
|
mShMem->handle(),
|
2011-02-17 01:43:30 +03:00
|
|
|
kHeaderBytes);
|
2009-12-02 00:05:26 +03:00
|
|
|
if (!mSharedBmp)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mOldObj = SelectObject(mSharedHdc, mSharedBmp);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // gfx
|
|
|
|
} // mozilla
|