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/DIBSectionBuffer.h> |
| 21 | #include <rfb_win32/DeviceContext.h> |
| 22 | #include <rfb_win32/BitmapInfo.h> |
| 23 | #include <rfb/LogWriter.h> |
| 24 | |
| 25 | using namespace rfb; |
| 26 | using namespace win32; |
| 27 | |
| 28 | static LogWriter vlog("DIBSectionBuffer"); |
| 29 | |
| 30 | |
| 31 | DIBSectionBuffer::DIBSectionBuffer(HWND window_) |
Peter Åstrand | ee3f46a | 2008-12-10 10:29:06 +0000 | [diff] [blame] | 32 | : bitmap(0), window(window_), device(0) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 33 | memset(&format, 0, sizeof(format)); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | DIBSectionBuffer::DIBSectionBuffer(HDC device_) |
| 37 | : bitmap(0), window(0), device(device_) { |
| 38 | memset(&format, 0, sizeof(format)); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | DIBSectionBuffer::~DIBSectionBuffer() { |
| 42 | if (bitmap) |
| 43 | DeleteObject(bitmap); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | void DIBSectionBuffer::setPF(const PixelFormat& pf) { |
| 48 | if (memcmp(&getPF(), &pf, sizeof(pf)) == 0) { |
| 49 | vlog.debug("pixel format unchanged by setPF()"); |
| 50 | return; |
| 51 | } |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame^] | 52 | if (!pf.trueColour) |
| 53 | throw rfb::Exception("palette format not supported"); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 54 | format = pf; |
| 55 | recreateBuffer(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void DIBSectionBuffer::setSize(int w, int h) { |
| 59 | if (width_ == w && height_ == h) { |
| 60 | vlog.debug("size unchanged by setSize()"); |
| 61 | return; |
| 62 | } |
| 63 | width_ = w; |
| 64 | height_ = h; |
| 65 | recreateBuffer(); |
| 66 | } |
| 67 | |
| 68 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 69 | inline void initMaxAndShift(DWORD mask, int* max, int* shift) { |
| 70 | for ((*shift) = 0; (mask & 1) == 0; (*shift)++) mask >>= 1; |
| 71 | (*max) = (rdr::U16)mask; |
| 72 | } |
| 73 | |
| 74 | void DIBSectionBuffer::recreateBuffer() { |
| 75 | HBITMAP new_bitmap = 0; |
| 76 | rdr::U8* new_data = 0; |
| 77 | |
| 78 | if (width_ && height_ && (format.depth != 0)) { |
| 79 | BitmapInfo bi; |
| 80 | memset(&bi, 0, sizeof(bi)); |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame^] | 81 | UINT iUsage = DIB_RGB_COLORS; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 82 | bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 83 | bi.bmiHeader.biBitCount = format.bpp; |
| 84 | bi.bmiHeader.biSizeImage = (format.bpp / 8) * width_ * height_; |
| 85 | bi.bmiHeader.biPlanes = 1; |
| 86 | bi.bmiHeader.biWidth = width_; |
| 87 | bi.bmiHeader.biHeight = -height_; |
| 88 | bi.bmiHeader.biCompression = (format.bpp > 8) ? BI_BITFIELDS : BI_RGB; |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 89 | bi.mask.red = format.pixelFromRGB((rdr::U16)~0, 0, 0); |
| 90 | bi.mask.green = format.pixelFromRGB(0, (rdr::U16)~0, 0); |
| 91 | bi.mask.blue = format.pixelFromRGB(0, 0, (rdr::U16)~0); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 92 | |
| 93 | // Create a DIBSection to draw into |
| 94 | if (device) |
| 95 | new_bitmap = ::CreateDIBSection(device, (BITMAPINFO*)&bi.bmiHeader, iUsage, |
| 96 | (void**)&new_data, NULL, 0); |
| 97 | else |
| 98 | new_bitmap = ::CreateDIBSection(WindowDC(window), (BITMAPINFO*)&bi.bmiHeader, iUsage, |
| 99 | (void**)&new_data, NULL, 0); |
| 100 | |
| 101 | if (!new_bitmap) { |
| 102 | int err = GetLastError(); |
| 103 | throw rdr::SystemException("unable to create DIB section", err); |
| 104 | } |
| 105 | |
| 106 | vlog.debug("recreateBuffer()"); |
| 107 | } else { |
| 108 | vlog.debug("one of area or format not set"); |
| 109 | } |
| 110 | |
| 111 | if (new_bitmap && bitmap) { |
| 112 | vlog.debug("preserving bitmap contents"); |
| 113 | |
| 114 | // Copy the contents across |
| 115 | if (device) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 116 | BitmapDC src_dev(device, bitmap); |
| 117 | BitmapDC dest_dev(device, new_bitmap); |
| 118 | BitBlt(dest_dev, 0, 0, width_, height_, src_dev, 0, 0, SRCCOPY); |
| 119 | } else { |
| 120 | WindowDC wndDC(window); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 121 | BitmapDC src_dev(wndDC, bitmap); |
| 122 | BitmapDC dest_dev(wndDC, new_bitmap); |
| 123 | BitBlt(dest_dev, 0, 0, width_, height_, src_dev, 0, 0, SRCCOPY); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (bitmap) { |
| 128 | // Delete the old bitmap |
| 129 | DeleteObject(bitmap); |
| 130 | bitmap = 0; |
| 131 | data = 0; |
| 132 | } |
| 133 | |
| 134 | if (new_bitmap) { |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 135 | int bpp, depth; |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 136 | int redMax, greenMax, blueMax; |
| 137 | int redShift, greenShift, blueShift; |
| 138 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 139 | // Set up the new bitmap |
| 140 | bitmap = new_bitmap; |
| 141 | data = new_data; |
| 142 | |
| 143 | // Determine the *actual* DIBSection format |
| 144 | DIBSECTION ds; |
| 145 | if (!GetObject(bitmap, sizeof(ds), &ds)) |
| 146 | throw rdr::SystemException("GetObject", GetLastError()); |
| 147 | |
| 148 | // Correct the "stride" of the DIB |
| 149 | // *** This code DWORD aligns each row - is that right??? |
| 150 | stride = width_; |
| 151 | int bytesPerRow = stride * format.bpp/8; |
| 152 | if (bytesPerRow % 4) { |
| 153 | bytesPerRow += 4 - (bytesPerRow % 4); |
| 154 | stride = (bytesPerRow * 8) / format.bpp; |
| 155 | vlog.info("adjusting DIB stride: %d to %d", width_, stride); |
| 156 | } |
| 157 | |
| 158 | // Calculate the PixelFormat for the DIB |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 159 | bpp = depth = ds.dsBm.bmBitsPixel; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 160 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame^] | 161 | // Get the truecolour format used by the DIBSection |
| 162 | initMaxAndShift(ds.dsBitfields[0], &redMax, &redShift); |
| 163 | initMaxAndShift(ds.dsBitfields[1], &greenMax, &greenShift); |
| 164 | initMaxAndShift(ds.dsBitfields[2], &blueMax, &blueShift); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 165 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame^] | 166 | // Calculate the effective depth |
| 167 | depth = 0; |
| 168 | Pixel bits = ds.dsBitfields[0] | ds.dsBitfields[1] | ds.dsBitfields[2]; |
| 169 | while (bits) { |
| 170 | depth++; |
| 171 | bits = bits >> 1; |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 172 | } |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame^] | 173 | if (depth > bpp) |
| 174 | throw Exception("Bad DIBSection format (depth exceeds bpp)"); |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 175 | |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame^] | 176 | format = PixelFormat(bpp, depth, false, true, |
Pierre Ossman | 4d0bc6e | 2014-02-12 13:12:31 +0100 | [diff] [blame] | 177 | redMax, greenMax, blueMax, |
| 178 | redShift, greenShift, blueShift); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 179 | } |
| 180 | } |