1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "SharedDIBWin.h"
7 #include "gfxAlphaRecovery.h"
8 #include "nsMathUtils.h"
14 static const uint32_t kBytesPerPixel
= 4;
15 static const uint32_t kByteAlign
= 1 << gfxAlphaRecovery::GoodAlignmentLog2();
16 static const uint32_t kHeaderBytes
=
17 (sizeof(BITMAPV4HEADER
) + kByteAlign
- 1) & ~(kByteAlign
- 1);
19 SharedDIBWin::SharedDIBWin() :
26 SharedDIBWin::~SharedDIBWin()
34 if (mSharedHdc
&& mOldObj
)
35 ::SelectObject(mSharedHdc
, mOldObj
);
38 ::DeleteObject(mSharedHdc
);
41 ::DeleteObject(mSharedBmp
);
44 mOldObj
= mSharedBmp
= NULL
;
52 SharedDIBWin::Create(HDC aHdc
, uint32_t aWidth
, uint32_t aHeight
,
57 // create the offscreen shared dib
59 uint32_t size
= SetupBitmapHeader(aWidth
, aHeight
, aTransparent
, &bmih
);
61 nsresult rv
= SharedDIB::Create(size
);
65 if (NS_FAILED(SetupSurface(aHdc
, &bmih
))) {
67 return NS_ERROR_FAILURE
;
74 SharedDIBWin::Attach(Handle aHandle
, uint32_t aWidth
, uint32_t aHeight
,
80 SetupBitmapHeader(aWidth
, aHeight
, aTransparent
, &bmih
);
82 nsresult rv
= SharedDIB::Attach(aHandle
, 0);
86 if (NS_FAILED(SetupSurface(NULL
, &bmih
))) {
88 return NS_ERROR_FAILURE
;
95 SharedDIBWin::SetupBitmapHeader(uint32_t aWidth
, uint32_t aHeight
,
96 bool aTransparent
, BITMAPV4HEADER
*aHeader
)
98 // D3D cannot handle an offscreen memory that pitch (SysMemPitch) is negative.
99 // So we create top-to-bottom DIB.
100 memset((void*)aHeader
, 0, sizeof(BITMAPV4HEADER
));
101 aHeader
->bV4Size
= sizeof(BITMAPV4HEADER
);
102 aHeader
->bV4Width
= aWidth
;
103 aHeader
->bV4Height
= -LONG(aHeight
); // top-to-buttom DIB
104 aHeader
->bV4Planes
= 1;
105 aHeader
->bV4BitCount
= 32;
106 aHeader
->bV4V4Compression
= BI_BITFIELDS
;
107 aHeader
->bV4RedMask
= 0x00FF0000;
108 aHeader
->bV4GreenMask
= 0x0000FF00;
109 aHeader
->bV4BlueMask
= 0x000000FF;
112 aHeader
->bV4AlphaMask
= 0xFF000000;
114 return (kHeaderBytes
+ (-aHeader
->bV4Height
* aHeader
->bV4Width
* kBytesPerPixel
));
118 SharedDIBWin::SetupSurface(HDC aHdc
, BITMAPV4HEADER
*aHdr
)
120 mSharedHdc
= ::CreateCompatibleDC(aHdc
);
123 return NS_ERROR_FAILURE
;
125 mSharedBmp
= ::CreateDIBSection(mSharedHdc
,
132 return NS_ERROR_FAILURE
;
134 mOldObj
= SelectObject(mSharedHdc
, mSharedBmp
);