blob: cc9bbcad8a6ba75f86c4c102609e1b6b347fb276 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- DeviceFrameBuffer.cxx
20//
21// The DeviceFrameBuffer class encapsulates the pixel data of the system
22// display.
23
24#include <vector>
25#include <rfb_win32/DeviceFrameBuffer.h>
26#include <rfb_win32/DeviceContext.h>
27#include <rfb_win32/OSVersion.h>
28#include <rfb_win32/IconInfo.h>
29#include <rfb/VNCServer.h>
30#include <rfb/LogWriter.h>
31
32using namespace rfb;
33using namespace win32;
34
35static LogWriter vlog("DeviceFrameBuffer");
36
37BoolParameter DeviceFrameBuffer::useCaptureBlt("UseCaptureBlt",
38 "Use a slower capture method that ensures that alpha blended windows appear correctly",
39 true);
40
41
42// -=- DeviceFrameBuffer class
43
44DeviceFrameBuffer::DeviceFrameBuffer(HDC deviceContext, const Rect& wRect)
45 : DIBSectionBuffer(deviceContext), device(deviceContext), cursorBm(deviceContext),
46 ignoreGrabErrors(false)
47{
48
49 // -=- Firstly, let's check that the device has suitable capabilities
50
51 int capabilities = GetDeviceCaps(device, RASTERCAPS);
52 if (!(capabilities & RC_BITBLT)) {
53 throw Exception("device does not support BitBlt");
54 }
55 if (!(capabilities & RC_DI_BITMAP)) {
56 throw Exception("device does not support GetDIBits");
57 }
58 /*
59 if (GetDeviceCaps(device, PLANES) != 1) {
60 throw Exception("device does not support planar displays");
61 }
62 */
63
64 // -=- Get the display dimensions and pixel format
65
66 // Get the display dimensions
67 deviceCoords = DeviceContext::getClipBox(device);
68 if (!wRect.is_empty())
69 deviceCoords = wRect.translate(deviceCoords.tl);
70 int w = deviceCoords.width();
71 int h = deviceCoords.height();
72
73 // We can't handle uneven widths :(
74 if (w % 2) w--;
75
76 // Configure the underlying DIB to match the device
77 DIBSectionBuffer::setPF(DeviceContext::getPF(device));
78 DIBSectionBuffer::setSize(w, h);
79
80 // Configure the cursor buffer
81 cursorBm.setPF(format);
82
83 // Set up a palette if required
84 if (!format.trueColour)
85 updateColourMap();
86}
87
88DeviceFrameBuffer::~DeviceFrameBuffer() {
89}
90
91
92void
93DeviceFrameBuffer::setPF(const PixelFormat &pf) {
94 throw Exception("setPF not supported");
95}
96
97void
98DeviceFrameBuffer::setSize(int w, int h) {
99 throw Exception("setSize not supported");
100}
101
102
103#ifndef CAPTUREBLT
104#define CAPTUREBLT 0x40000000
105#endif
106
107void
108DeviceFrameBuffer::grabRect(const Rect &rect) {
109 BitmapDC tmpDC(device, bitmap);
110
111 // Map the rectangle coords from VNC Desktop-relative to device relative - usually (0,0)
112 Point src = desktopToDevice(rect.tl);
113
114 // Note: Microsoft's documentation lies directly about CAPTUREBLT and claims it works on 98/ME
115 // If you try CAPTUREBLT on 98 then you get blank output...
116 if (!::BitBlt(tmpDC, rect.tl.x, rect.tl.y, rect.width(), rect.height(), device, src.x, src.y,
117 (osVersion.isPlatformNT && useCaptureBlt) ? (CAPTUREBLT | SRCCOPY) : SRCCOPY)) {
118 if (ignoreGrabErrors)
119 vlog.error("BitBlt failed:%ld", GetLastError());
120 else
121 throw rdr::SystemException("BitBlt failed", GetLastError());
122 }
123}
124
125void
126DeviceFrameBuffer::grabRegion(const Region &rgn) {
127 std::vector<Rect> rects;
128 std::vector<Rect>::const_iterator i;
129 rgn.get_rects(&rects);
130 for(i=rects.begin(); i!=rects.end(); i++) {
131 grabRect(*i);
132 }
133 ::GdiFlush();
134}
135
136
137void copyDevicePaletteToDIB(HDC dc, DIBSectionBuffer* dib) {
138 // - Fetch the system palette for the framebuffer
139 PALETTEENTRY syspalette[256];
140 UINT entries = ::GetSystemPaletteEntries(dc, 0, 256, syspalette);
141
142 if (entries == 0) {
143 vlog.info("resorting to standard 16 color palette");
144 for (unsigned int i=0;i<256;i++) {
145 int v = (i%16) >= 8 ? 127 : 255;
146 syspalette[i].peRed = i & 1 ? v : 0;
147 syspalette[i].peGreen = i & 2 ? v : 0;
148 syspalette[i].peBlue = i & 4 ? v : 0;
149 }
150 } else {
151 vlog.info("framebuffer has %u palette entries", entries);
152 }
153
154 // - Update the bitmap's stored copy of the palette
155 for (unsigned int i=0;i<256;i++) {
156 int r, g, b;
157 r = (syspalette[i].peRed << 8) + 0x80;
158 g = (syspalette[i].peGreen << 8) + 0x80;
159 b = (syspalette[i].peBlue << 8) + 0x80;
160 dib->setColour(i, r, g, b);
161 }
162
163 // - Update the DIB section to use the palette
164 dib->refreshPalette();
165}
166
167
168void DeviceFrameBuffer::setCursor(HCURSOR hCursor, VNCServer* server)
169{
170 // - If hCursor is null then there is no cursor - clear the old one
171
172 if (hCursor == 0) {
173 server->setCursor(0, 0, Point(), 0, 0);
174 return;
175 }
176
177 try {
178
179 // - Get the size and other details about the cursor.
180
181 IconInfo iconInfo((HICON)hCursor);
182
183 BITMAP maskInfo;
184 if (!GetObject(iconInfo.hbmMask, sizeof(BITMAP), &maskInfo))
185 throw rdr::SystemException("GetObject() failed", GetLastError());
186 if (maskInfo.bmPlanes != 1)
187 throw rdr::Exception("unsupported multi-plane cursor");
188 if (maskInfo.bmBitsPixel != 1)
189 throw rdr::Exception("unsupported cursor mask format");
190
191 // - Create the cursor pixel buffer and mask storage
192 // NB: The cursor pixel buffer is NOT used here. Instead, we
193 // pass the cursorBm.data pointer directly, to save overhead.
194
195 cursor.setSize(maskInfo.bmWidth, maskInfo.bmHeight);
196 cursor.setPF(format);
197 cursor.hotspot = Point(iconInfo.xHotspot, iconInfo.yHotspot);
198
199 // - Get the AND and XOR masks. There is only an XOR mask if this is not a
200 // colour cursor.
201
202 if (!iconInfo.hbmColor)
203 cursor.setSize(cursor.width(), cursor.height() / 2);
204 rdr::U8Array mask(maskInfo.bmWidthBytes * maskInfo.bmHeight);
205 rdr::U8* xorMask = mask.buf + cursor.height() * maskInfo.bmWidthBytes;
206
207 if (!GetBitmapBits(iconInfo.hbmMask,
208 maskInfo.bmWidthBytes * maskInfo.bmHeight, mask.buf))
209 throw rdr::SystemException("GetBitmapBits failed", GetLastError());
210
211 // Configure the cursor bitmap
212 cursorBm.setSize(cursor.width(), cursor.height());
213
214 // Copy the palette into it if required
215 if (format.bpp <= 8)
216 copyDevicePaletteToDIB(device, &cursorBm);
217
218 // Draw the cursor into the bitmap
219 BitmapDC dc(device, cursorBm.bitmap);
220 if (!DrawIconEx(dc, 0, 0, hCursor, 0, 0, 0, NULL, DI_NORMAL | DI_COMPAT))
221 throw rdr::SystemException("unable to render cursor", GetLastError());
222
223 // Replace any XORed pixels with xorColour, because RFB doesn't support
224 // XORing of cursors. XORing is used for the I-beam cursor, which is most
225 // often used over a white background, but also sometimes over a black
226 // background. We set the XOR'd pixels to black, then draw a white outline
227 // around the whole cursor.
228
229 // *** should we replace any pixels not set in mask to zero, to ensure
230 // that irrelevant data doesn't screw compression?
231
232 bool doOutline = false;
233 if (!iconInfo.hbmColor) {
Pierre Ossman5b39bbe2009-03-10 10:21:11 +0000234 Pixel xorColour = format.pixelFromRGB((rdr::U16)0, (rdr::U16)0, (rdr::U16)0, cursorBm.getColourMap());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000235 for (int y = 0; y < cursor.height(); y++) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000236 for (int x = 0; x < cursor.width(); x++) {
237 int byte = y * maskInfo.bmWidthBytes + x / 8;
238 int bit = 7 - x % 8;
239 if ((mask.buf[byte] & (1 << bit)) && (xorMask[byte] & (1 << bit)))
240 {
241 mask.buf[byte] &= ~(1 << bit);
242
243 switch (format.bpp) {
244 case 8:
245 ((rdr::U8*)cursorBm.data)[y * cursor.width() + x] = xorColour; break;
246 case 16:
247 ((rdr::U16*)cursorBm.data)[y * cursor.width() + x] = xorColour; break;
248 case 32:
249 ((rdr::U32*)cursorBm.data)[y * cursor.width() + x] = xorColour; break;
250 }
251
252 doOutline = true;
253 }
254 }
255 }
256 }
257
258 // Finally invert the AND mask so it's suitable for RFB and pack it into
259 // the minimum number of bytes per row.
260
261 int maskBytesPerRow = (cursor.width() + 7) / 8;
262
263 for (int j = 0; j < cursor.height(); j++) {
264 for (int i = 0; i < maskBytesPerRow; i++)
265 cursor.mask.buf[j * maskBytesPerRow + i]
266 = ~mask.buf[j * maskInfo.bmWidthBytes + i];
267 }
268
269 if (doOutline) {
270 vlog.debug("drawing cursor outline!");
271 memcpy(cursor.data, cursorBm.data, cursor.dataLen());
Pierre Ossman5b39bbe2009-03-10 10:21:11 +0000272 cursor.drawOutline(format.pixelFromRGB((rdr::U16)0xffff, (rdr::U16)0xffff, (rdr::U16)0xffff, cursorBm.getColourMap()));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000273 memcpy(cursorBm.data, cursor.data, cursor.dataLen());
274 }
275
276 server->setCursor(cursor.width(), cursor.height(), cursor.hotspot,
277 cursorBm.data, cursor.mask.buf);
278 } catch (rdr::Exception& e) {
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000279 vlog.error("%s", e.str());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000280 }
281}
282
283
284void
285DeviceFrameBuffer::updateColourMap() {
286 if (!format.trueColour)
287 copyDevicePaletteToDIB(device, this);
288}