Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 2 | * Copyright 2014 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | #include <rfb_win32/DeviceContext.h> |
| 21 | #include <rfb_win32/CompatibleBitmap.h> |
| 22 | #include <rfb_win32/BitmapInfo.h> |
| 23 | #include <rdr/Exception.h> |
| 24 | #include <rfb/LogWriter.h> |
| 25 | |
| 26 | using namespace rfb; |
| 27 | using namespace win32; |
| 28 | |
| 29 | |
| 30 | static LogWriter vlog("DeviceContext"); |
| 31 | |
| 32 | PixelFormat DeviceContext::getPF() const { |
| 33 | return getPF(dc); |
| 34 | } |
| 35 | |
| 36 | PixelFormat DeviceContext::getPF(HDC dc) { |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 37 | bool trueColour, bigEndian; |
| 38 | int bpp, depth; |
| 39 | int redMax, greenMax, blueMax; |
| 40 | int redShift, greenShift, blueShift; |
| 41 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 42 | CompatibleBitmap bitmap(dc, 1, 1); |
| 43 | |
| 44 | // -=- Get the bitmap format information |
| 45 | BitmapInfo bi; |
| 46 | memset(&bi, 0, sizeof(bi)); |
| 47 | bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 48 | bi.bmiHeader.biBitCount = 0; |
| 49 | if (!::GetDIBits(dc, bitmap, 0, 1, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS)) { |
| 50 | throw rdr::SystemException("unable to determine device pixel format", GetLastError()); |
| 51 | } |
| 52 | if (!::GetDIBits(dc, bitmap, 0, 1, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS)) { |
| 53 | throw rdr::SystemException("unable to determine pixel shifts/palette", GetLastError()); |
| 54 | } |
| 55 | |
| 56 | // Set the initial format information |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 57 | trueColour = bi.bmiHeader.biBitCount > 8; |
| 58 | bigEndian = 0; |
| 59 | bpp = bi.bmiHeader.biBitCount; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 60 | |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 61 | if (trueColour) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 62 | DWORD rMask=0, gMask=0, bMask=0; |
| 63 | |
| 64 | // Which true colour format is the DIB section using? |
| 65 | switch (bi.bmiHeader.biCompression) { |
| 66 | case BI_RGB: |
| 67 | // Default RGB layout |
| 68 | switch (bi.bmiHeader.biBitCount) { |
| 69 | case 16: |
| 70 | // RGB 555 - High Colour |
| 71 | vlog.info("16-bit High Colour"); |
| 72 | rMask = 0x7c00; |
| 73 | bMask = 0x001f; |
| 74 | gMask = 0x03e0; |
| 75 | break; |
| 76 | case 24: |
| 77 | case 32: |
| 78 | // RGB 888 - True Colour |
| 79 | vlog.info("24/32-bit High Colour"); |
| 80 | rMask = 0xff0000; |
| 81 | gMask = 0x00ff00; |
| 82 | bMask = 0x0000ff; |
| 83 | break; |
| 84 | default: |
| 85 | vlog.error("bits per pixel %u not supported", bi.bmiHeader.biBitCount); |
| 86 | throw rdr::Exception("unknown bits per pixel specified"); |
| 87 | }; |
| 88 | break; |
| 89 | case BI_BITFIELDS: |
| 90 | // Custom RGB layout |
| 91 | rMask = bi.mask.red; |
| 92 | gMask = bi.mask.green; |
| 93 | bMask = bi.mask.blue; |
| 94 | vlog.info("%lu-bit BitFields: (%lx, %lx, %lx)", |
| 95 | bi.bmiHeader.biBitCount, rMask, gMask, bMask); |
| 96 | break; |
| 97 | }; |
| 98 | |
| 99 | // Convert the data we just retrieved |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 100 | initMaxAndShift(rMask, &redMax, &redShift); |
| 101 | initMaxAndShift(gMask, &greenMax, &greenShift); |
| 102 | initMaxAndShift(bMask, &blueMax, &blueShift); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 103 | |
| 104 | // Calculate the depth from the colour shifts |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 105 | depth = 0; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 106 | Pixel bits = rMask | gMask | bMask; |
| 107 | while (bits) { |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 108 | depth++; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 109 | bits = bits >> 1; |
| 110 | } |
| 111 | |
| 112 | // Check that the depth & bpp are valid |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 113 | if (depth > bpp) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 114 | vlog.error("depth exceeds bits per pixel!"); |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 115 | bpp = depth; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Correct the bits-per-pixel to something we're happy with |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 119 | if (bpp <= 16) |
| 120 | bpp = 16; |
| 121 | else if (bpp <= 32) |
| 122 | bpp = 32; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 123 | } else { |
| 124 | // Palettised format - depth reflects number of colours, |
| 125 | // but bits-per-pixel is ALWAYS 8 |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 126 | depth = bpp; |
| 127 | if (bpp < 8) |
| 128 | bpp = 8; |
| 129 | vlog.info("%d-colour palettised", 1<<depth); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Adam Tkac | 9c8076b | 2011-02-21 12:40:30 +0000 | [diff] [blame] | 132 | |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 133 | return PixelFormat(bpp, depth, bigEndian, trueColour, |
| 134 | redMax, greenMax, blueMax, |
| 135 | redShift, greenShift, blueShift); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | Rect DeviceContext::getClipBox() const { |
| 139 | return getClipBox(dc); |
| 140 | } |
| 141 | |
| 142 | Rect DeviceContext::getClipBox(HDC dc) { |
| 143 | // Get the display dimensions |
| 144 | RECT cr; |
| 145 | if (!GetClipBox(dc, &cr)) |
| 146 | throw rdr::SystemException("GetClipBox", GetLastError()); |
| 147 | return Rect(cr.left, cr.top, cr.right, cr.bottom); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | DeviceDC::DeviceDC(const TCHAR* deviceName) { |
| 152 | dc = ::CreateDC(_T("DISPLAY"), deviceName, NULL, NULL); |
| 153 | if (!dc) |
| 154 | throw rdr::SystemException("failed to create DeviceDC", GetLastError()); |
| 155 | } |
| 156 | |
| 157 | DeviceDC::~DeviceDC() { |
| 158 | if (dc) |
| 159 | DeleteDC(dc); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | WindowDC::WindowDC(HWND wnd) : hwnd(wnd) { |
| 164 | dc = GetDC(wnd); |
| 165 | if (!dc) |
| 166 | throw rdr::SystemException("GetDC failed", GetLastError()); |
| 167 | } |
| 168 | |
| 169 | WindowDC::~WindowDC() { |
| 170 | if (dc) |
| 171 | ReleaseDC(hwnd, dc); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | CompatibleDC::CompatibleDC(HDC existing) { |
| 176 | dc = CreateCompatibleDC(existing); |
| 177 | if (!dc) |
| 178 | throw rdr::SystemException("CreateCompatibleDC failed", GetLastError()); |
| 179 | } |
| 180 | |
| 181 | CompatibleDC::~CompatibleDC() { |
| 182 | if (dc) |
| 183 | DeleteDC(dc); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | BitmapDC::BitmapDC(HDC hdc, HBITMAP hbitmap) : CompatibleDC(hdc){ |
| 188 | oldBitmap = (HBITMAP)SelectObject(dc, hbitmap); |
| 189 | if (!oldBitmap) |
| 190 | throw rdr::SystemException("SelectObject to CompatibleDC failed", |
| 191 | GetLastError()); |
| 192 | } |
| 193 | |
| 194 | BitmapDC::~BitmapDC() { |
| 195 | SelectObject(dc, oldBitmap); |
| 196 | } |