blob: 8f9bf024de9fc08f80eef5a528323802d7b2d1f2 [file] [log] [blame]
Pierre Ossmanb4cb8762011-06-13 13:24:29 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
3 *
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 <assert.h>
21#include <stdlib.h>
22
23#include <windows.h>
24
25#include <FL/x.H>
26
27#include <rfb/LogWriter.h>
28#include <rfb/Exception.h>
29
30#include "Win32PixelBuffer.h"
31
32using namespace rfb;
33
34static rfb::LogWriter vlog("PlatformPixelBuffer");
35
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000036PlatformPixelBuffer::PlatformPixelBuffer(int width, int height) :
37 FullFramePixelBuffer(rfb::PixelFormat(32, 24, false, true,
38 255, 255, 255, 16, 8, 0),
39 width, height, NULL, NULL),
40 bitmap(NULL)
41{
42 BITMAPINFOHEADER bih;
43
44 memset(&bih, 0, sizeof(bih));
45
46 bih.biSize = sizeof(BITMAPINFOHEADER);
47 bih.biBitCount = getPF().bpp;
48 bih.biSizeImage = (getPF().bpp / 8) * width * height;
49 bih.biPlanes = 1;
50 bih.biWidth = width;
51 bih.biHeight = -height; // Negative to get top-down
52 bih.biCompression = BI_RGB;
53
54 bitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bih,
55 DIB_RGB_COLORS, (void**)&data, NULL, 0);
56 if (!bitmap) {
57 int err = GetLastError();
58 throw rdr::SystemException("unable to create DIB section", err);
59 }
60}
61
62
63PlatformPixelBuffer::~PlatformPixelBuffer()
64{
65 DeleteObject(bitmap);
66}
67
68
69void PlatformPixelBuffer::draw(int src_x, int src_y, int x, int y, int w, int h)
70{
71 HDC dc;
72
73 dc = CreateCompatibleDC(fl_gc);
74 if (!dc)
75 throw rdr::SystemException("CreateCompatibleDC failed", GetLastError());
76
77 if (!SelectObject(dc, bitmap))
78 throw rdr::SystemException("SelectObject failed", GetLastError());
79
Pierre Ossman43332202011-07-15 14:20:30 +000080 if (!BitBlt(fl_gc, x, y, w, h, dc, src_x, src_y, SRCCOPY)) {
81 // If the desktop we're rendering to is inactive (like when the screen
82 // is locked or the UAC is active), then GDI calls will randomly fail.
83 // This is completely undocumented so we have no idea how best to deal
84 // with it. For now, we've only seen this error and for this function
85 // so only ignore this combination.
86 if (GetLastError() != ERROR_INVALID_HANDLE)
87 throw rdr::SystemException("BitBlt failed", GetLastError());
88 }
Pierre Ossmanb4cb8762011-06-13 13:24:29 +000089
90 DeleteDC(dc);
91}