blob: 48718165c26d401e05b836962a6a7ac285ebb57b [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
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020034#include "i18n.h"
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000035#include "Win32PixelBuffer.h"
36
37using namespace rfb;
38
Pierre Ossmanac13abe2014-02-07 14:46:26 +010039static rfb::LogWriter vlog("Win32PixelBuffer");
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000040
Pierre Ossmanac13abe2014-02-07 14:46:26 +010041Win32PixelBuffer::Win32PixelBuffer(int width, int height) :
42 PlatformPixelBuffer(rfb::PixelFormat(32, 24, false, true,
43 255, 255, 255, 16, 8, 0),
Pierre Ossman2e5a1062014-01-30 17:57:27 +010044 width, height, NULL, width),
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000045 bitmap(NULL)
46{
47 BITMAPINFOHEADER bih;
48
49 memset(&bih, 0, sizeof(bih));
50
51 bih.biSize = sizeof(BITMAPINFOHEADER);
52 bih.biBitCount = getPF().bpp;
53 bih.biSizeImage = (getPF().bpp / 8) * width * height;
54 bih.biPlanes = 1;
55 bih.biWidth = width;
56 bih.biHeight = -height; // Negative to get top-down
57 bih.biCompression = BI_RGB;
58
59 bitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bih,
60 DIB_RGB_COLORS, (void**)&data, NULL, 0);
61 if (!bitmap) {
62 int err = GetLastError();
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020063 throw rdr::SystemException(_("unable to create DIB section"), err);
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000064 }
65}
66
67
Pierre Ossmanac13abe2014-02-07 14:46:26 +010068Win32PixelBuffer::~Win32PixelBuffer()
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000069{
70 DeleteObject(bitmap);
71}
72
73
Pierre Ossmanac13abe2014-02-07 14:46:26 +010074void Win32PixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000075{
76 HDC dc;
77
78 dc = CreateCompatibleDC(fl_gc);
79 if (!dc)
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020080 throw rdr::SystemException(_("CreateCompatibleDC failed"), GetLastError());
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000081
82 if (!SelectObject(dc, bitmap))
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020083 throw rdr::SystemException(_("SelectObject failed"), GetLastError());
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000084
Pierre Ossman43332202011-07-15 14:20:30 +000085 if (!BitBlt(fl_gc, x, y, w, h, dc, src_x, src_y, SRCCOPY)) {
86 // If the desktop we're rendering to is inactive (like when the screen
87 // is locked or the UAC is active), then GDI calls will randomly fail.
88 // This is completely undocumented so we have no idea how best to deal
89 // with it. For now, we've only seen this error and for this function
90 // so only ignore this combination.
91 if (GetLastError() != ERROR_INVALID_HANDLE)
Pierre Ossman8ca4c1d2014-09-22 12:54:26 +020092 throw rdr::SystemException(_("BitBlt failed"), GetLastError());
Pierre Ossman43332202011-07-15 14:20:30 +000093 }
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000094
95 DeleteDC(dc);
96}