blob: 002d4c82c6c3cf135ee2018289e64bbf23656b2c [file] [log] [blame]
Pierre Ossmanb4cb8762011-06-13 13:24:29 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanac13abe2014-02-07 14:46:26 +01002 * Copyright 2011-2014 Pierre Ossman for Cendio AB
Pierre Ossmanb4cb8762011-06-13 13:24:29 +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
Peter Åstrandc359f362011-08-23 12:04:46 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000024#include <stdlib.h>
25
26#include <windows.h>
27
28#include <FL/x.H>
29
30#include <rfb/LogWriter.h>
31#include <rfb/Exception.h>
32
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020033#include "i18n.h"
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000034#include "Win32PixelBuffer.h"
35
36using namespace rfb;
37
Pierre Ossmanac13abe2014-02-07 14:46:26 +010038static rfb::LogWriter vlog("Win32PixelBuffer");
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000039
Pierre Ossmanac13abe2014-02-07 14:46:26 +010040Win32PixelBuffer::Win32PixelBuffer(int width, int height) :
41 PlatformPixelBuffer(rfb::PixelFormat(32, 24, false, true,
42 255, 255, 255, 16, 8, 0),
Pierre Ossman2e5a1062014-01-30 17:57:27 +010043 width, height, NULL, width),
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000044 bitmap(NULL)
45{
46 BITMAPINFOHEADER bih;
47
48 memset(&bih, 0, sizeof(bih));
49
50 bih.biSize = sizeof(BITMAPINFOHEADER);
51 bih.biBitCount = getPF().bpp;
52 bih.biSizeImage = (getPF().bpp / 8) * width * height;
53 bih.biPlanes = 1;
54 bih.biWidth = width;
55 bih.biHeight = -height; // Negative to get top-down
56 bih.biCompression = BI_RGB;
57
58 bitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bih,
59 DIB_RGB_COLORS, (void**)&data, NULL, 0);
Pierre Ossmanfefd9002016-12-17 14:04:39 +010060 if (!bitmap)
61 throw rdr::SystemException("CreateDIBSection", GetLastError());
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000062}
63
64
Pierre Ossmanac13abe2014-02-07 14:46:26 +010065Win32PixelBuffer::~Win32PixelBuffer()
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000066{
67 DeleteObject(bitmap);
68}
69
70
Pierre Ossmanac13abe2014-02-07 14:46:26 +010071void Win32PixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000072{
73 HDC dc;
74
75 dc = CreateCompatibleDC(fl_gc);
76 if (!dc)
Pierre Ossmanfefd9002016-12-17 14:04:39 +010077 throw rdr::SystemException("CreateCompatibleDC", GetLastError());
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000078
79 if (!SelectObject(dc, bitmap))
Pierre Ossmanfefd9002016-12-17 14:04:39 +010080 throw rdr::SystemException("SelectObject", GetLastError());
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000081
Pierre Ossman43332202011-07-15 14:20:30 +000082 if (!BitBlt(fl_gc, x, y, w, h, dc, src_x, src_y, SRCCOPY)) {
83 // If the desktop we're rendering to is inactive (like when the screen
84 // is locked or the UAC is active), then GDI calls will randomly fail.
85 // This is completely undocumented so we have no idea how best to deal
86 // with it. For now, we've only seen this error and for this function
87 // so only ignore this combination.
88 if (GetLastError() != ERROR_INVALID_HANDLE)
Pierre Ossmanfefd9002016-12-17 14:04:39 +010089 throw rdr::SystemException("BitBlt", GetLastError());
Pierre Ossman43332202011-07-15 14:20:30 +000090 }
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000091
92 DeleteDC(dc);
93}