Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "SharedDIBWin.h" |
michael@0 | 7 | #include "gfxAlphaRecovery.h" |
michael@0 | 8 | #include "nsMathUtils.h" |
michael@0 | 9 | #include "nsDebug.h" |
michael@0 | 10 | |
michael@0 | 11 | namespace mozilla { |
michael@0 | 12 | namespace gfx { |
michael@0 | 13 | |
michael@0 | 14 | static const uint32_t kBytesPerPixel = 4; |
michael@0 | 15 | static const uint32_t kByteAlign = 1 << gfxAlphaRecovery::GoodAlignmentLog2(); |
michael@0 | 16 | static const uint32_t kHeaderBytes = |
michael@0 | 17 | (sizeof(BITMAPV4HEADER) + kByteAlign - 1) & ~(kByteAlign - 1); |
michael@0 | 18 | |
michael@0 | 19 | SharedDIBWin::SharedDIBWin() : |
michael@0 | 20 | mSharedHdc(nullptr) |
michael@0 | 21 | , mSharedBmp(nullptr) |
michael@0 | 22 | , mOldObj(nullptr) |
michael@0 | 23 | { |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | SharedDIBWin::~SharedDIBWin() |
michael@0 | 27 | { |
michael@0 | 28 | Close(); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | nsresult |
michael@0 | 32 | SharedDIBWin::Close() |
michael@0 | 33 | { |
michael@0 | 34 | if (mSharedHdc && mOldObj) |
michael@0 | 35 | ::SelectObject(mSharedHdc, mOldObj); |
michael@0 | 36 | |
michael@0 | 37 | if (mSharedHdc) |
michael@0 | 38 | ::DeleteObject(mSharedHdc); |
michael@0 | 39 | |
michael@0 | 40 | if (mSharedBmp) |
michael@0 | 41 | ::DeleteObject(mSharedBmp); |
michael@0 | 42 | |
michael@0 | 43 | mSharedHdc = nullptr; |
michael@0 | 44 | mOldObj = mSharedBmp = nullptr; |
michael@0 | 45 | |
michael@0 | 46 | SharedDIB::Close(); |
michael@0 | 47 | |
michael@0 | 48 | return NS_OK; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | nsresult |
michael@0 | 52 | SharedDIBWin::Create(HDC aHdc, uint32_t aWidth, uint32_t aHeight, |
michael@0 | 53 | bool aTransparent) |
michael@0 | 54 | { |
michael@0 | 55 | Close(); |
michael@0 | 56 | |
michael@0 | 57 | // create the offscreen shared dib |
michael@0 | 58 | BITMAPV4HEADER bmih; |
michael@0 | 59 | uint32_t size = SetupBitmapHeader(aWidth, aHeight, aTransparent, &bmih); |
michael@0 | 60 | |
michael@0 | 61 | nsresult rv = SharedDIB::Create(size); |
michael@0 | 62 | if (NS_FAILED(rv)) |
michael@0 | 63 | return rv; |
michael@0 | 64 | |
michael@0 | 65 | if (NS_FAILED(SetupSurface(aHdc, &bmih))) { |
michael@0 | 66 | Close(); |
michael@0 | 67 | return NS_ERROR_FAILURE; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | return NS_OK; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | nsresult |
michael@0 | 74 | SharedDIBWin::Attach(Handle aHandle, uint32_t aWidth, uint32_t aHeight, |
michael@0 | 75 | bool aTransparent) |
michael@0 | 76 | { |
michael@0 | 77 | Close(); |
michael@0 | 78 | |
michael@0 | 79 | BITMAPV4HEADER bmih; |
michael@0 | 80 | SetupBitmapHeader(aWidth, aHeight, aTransparent, &bmih); |
michael@0 | 81 | |
michael@0 | 82 | nsresult rv = SharedDIB::Attach(aHandle, 0); |
michael@0 | 83 | if (NS_FAILED(rv)) |
michael@0 | 84 | return rv; |
michael@0 | 85 | |
michael@0 | 86 | if (NS_FAILED(SetupSurface(nullptr, &bmih))) { |
michael@0 | 87 | Close(); |
michael@0 | 88 | return NS_ERROR_FAILURE; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | return NS_OK; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | uint32_t |
michael@0 | 95 | SharedDIBWin::SetupBitmapHeader(uint32_t aWidth, uint32_t aHeight, |
michael@0 | 96 | bool aTransparent, BITMAPV4HEADER *aHeader) |
michael@0 | 97 | { |
michael@0 | 98 | // D3D cannot handle an offscreen memory that pitch (SysMemPitch) is negative. |
michael@0 | 99 | // So we create top-to-bottom DIB. |
michael@0 | 100 | memset((void*)aHeader, 0, sizeof(BITMAPV4HEADER)); |
michael@0 | 101 | aHeader->bV4Size = sizeof(BITMAPV4HEADER); |
michael@0 | 102 | aHeader->bV4Width = aWidth; |
michael@0 | 103 | aHeader->bV4Height = -LONG(aHeight); // top-to-buttom DIB |
michael@0 | 104 | aHeader->bV4Planes = 1; |
michael@0 | 105 | aHeader->bV4BitCount = 32; |
michael@0 | 106 | aHeader->bV4V4Compression = BI_BITFIELDS; |
michael@0 | 107 | aHeader->bV4RedMask = 0x00FF0000; |
michael@0 | 108 | aHeader->bV4GreenMask = 0x0000FF00; |
michael@0 | 109 | aHeader->bV4BlueMask = 0x000000FF; |
michael@0 | 110 | |
michael@0 | 111 | if (aTransparent) |
michael@0 | 112 | aHeader->bV4AlphaMask = 0xFF000000; |
michael@0 | 113 | |
michael@0 | 114 | return (kHeaderBytes + (-aHeader->bV4Height * aHeader->bV4Width * kBytesPerPixel)); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | nsresult |
michael@0 | 118 | SharedDIBWin::SetupSurface(HDC aHdc, BITMAPV4HEADER *aHdr) |
michael@0 | 119 | { |
michael@0 | 120 | mSharedHdc = ::CreateCompatibleDC(aHdc); |
michael@0 | 121 | |
michael@0 | 122 | if (!mSharedHdc) |
michael@0 | 123 | return NS_ERROR_FAILURE; |
michael@0 | 124 | |
michael@0 | 125 | mSharedBmp = ::CreateDIBSection(mSharedHdc, |
michael@0 | 126 | (BITMAPINFO*)aHdr, |
michael@0 | 127 | DIB_RGB_COLORS, |
michael@0 | 128 | &mBitmapBits, |
michael@0 | 129 | mShMem->handle(), |
michael@0 | 130 | kHeaderBytes); |
michael@0 | 131 | if (!mSharedBmp) |
michael@0 | 132 | return NS_ERROR_FAILURE; |
michael@0 | 133 | |
michael@0 | 134 | mOldObj = SelectObject(mSharedHdc, mSharedBmp); |
michael@0 | 135 | |
michael@0 | 136 | return NS_OK; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | |
michael@0 | 140 | } // gfx |
michael@0 | 141 | } // mozilla |