blob: 9fb041451713596af79eb97d406a1ecac2e96990 [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 <assert.h>
25#include <stdlib.h>
26
27#include <windows.h>
28
29#include <FL/x.H>
30
31#include <rfb/LogWriter.h>
32#include <rfb/Exception.h>
33
34#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);
60 if (!bitmap) {
61 int err = GetLastError();
62 throw rdr::SystemException("unable to create DIB section", err);
63 }
64}
65
66
Pierre Ossmanac13abe2014-02-07 14:46:26 +010067Win32PixelBuffer::~Win32PixelBuffer()
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000068{
69 DeleteObject(bitmap);
70}
71
72
Pierre Ossmanac13abe2014-02-07 14:46:26 +010073void Win32PixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000074{
75 HDC dc;
76
77 dc = CreateCompatibleDC(fl_gc);
78 if (!dc)
79 throw rdr::SystemException("CreateCompatibleDC failed", GetLastError());
80
81 if (!SelectObject(dc, bitmap))
82 throw rdr::SystemException("SelectObject failed", GetLastError());
83
Pierre Ossman43332202011-07-15 14:20:30 +000084 if (!BitBlt(fl_gc, x, y, w, h, dc, src_x, src_y, SRCCOPY)) {
85 // If the desktop we're rendering to is inactive (like when the screen
86 // is locked or the UAC is active), then GDI calls will randomly fail.
87 // This is completely undocumented so we have no idea how best to deal
88 // with it. For now, we've only seen this error and for this function
89 // so only ignore this combination.
90 if (GetLastError() != ERROR_INVALID_HANDLE)
91 throw rdr::SystemException("BitBlt failed", GetLastError());
92 }
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000093
94 DeleteDC(dc);
95}