blob: e2b0d641be61ae04348841e0b7f2b23a7fa7394e [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +01002 * Copyright 2014 Pierre Ossman for Cendio AB
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00003 *
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
25using namespace rfb;
26using namespace win32;
27
28static LogWriter vlog("DIBSectionBuffer");
29
30
31DIBSectionBuffer::DIBSectionBuffer(HWND window_)
Peter Åstrandee3f46a2008-12-10 10:29:06 +000032 : bitmap(0), window(window_), device(0) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000033 memset(&format, 0, sizeof(format));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000034}
35
36DIBSectionBuffer::DIBSectionBuffer(HDC device_)
37 : bitmap(0), window(0), device(device_) {
38 memset(&format, 0, sizeof(format));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000039}
40
41DIBSectionBuffer::~DIBSectionBuffer() {
42 if (bitmap)
43 DeleteObject(bitmap);
44}
45
46
47void 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 Ossmanb6b4dc62014-01-20 15:05:21 +010052 if (!pf.trueColour)
53 throw rfb::Exception("palette format not supported");
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000054 format = pf;
55 recreateBuffer();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000056}
57
58void 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 Kaplinsky729598c2006-05-25 05:12:25 +000069inline 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
74void 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 Ossmanb6b4dc62014-01-20 15:05:21 +010081 UINT iUsage = DIB_RGB_COLORS;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000082 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 Ossman4d0bc6e2014-02-12 13:12:31 +010089 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 Kaplinsky729598c2006-05-25 05:12:25 +000092
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 Kaplinsky729598c2006-05-25 05:12:25 +0000116 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 Kaplinsky729598c2006-05-25 05:12:25 +0000121 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 Ossman4d0bc6e2014-02-12 13:12:31 +0100135 int bpp, depth;
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100136 int redMax, greenMax, blueMax;
137 int redShift, greenShift, blueShift;
138
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000139 // 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 Ossman4d0bc6e2014-02-12 13:12:31 +0100159 bpp = depth = ds.dsBm.bmBitsPixel;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000160
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100161 // 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 Kaplinsky729598c2006-05-25 05:12:25 +0000165
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100166 // 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 Ossman4d0bc6e2014-02-12 13:12:31 +0100172 }
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100173 if (depth > bpp)
174 throw Exception("Bad DIBSection format (depth exceeds bpp)");
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100175
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100176 format = PixelFormat(bpp, depth, false, true,
Pierre Ossman4d0bc6e2014-02-12 13:12:31 +0100177 redMax, greenMax, blueMax,
178 redShift, greenShift, blueShift);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000179 }
180}