aboutsummaryrefslogtreecommitdiffstats
path: root/vncviewer/Surface_Win32.cxx
blob: 67ba15c5cee20e597b444eeb7dffad0eeccda7e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Copyright 2016 Pierre Ossman for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>

#include <FL/Fl_RGB_Image.H>
#include <FL/x.H>

#include <rdr/Exception.h>

#include "Surface.h"

void Surface::clear(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
  RGBQUAD* out;
  int x, y;

  r = (unsigned)r * a / 255;
  g = (unsigned)g * a / 255;
  b = (unsigned)b * a / 255;

  out = data;
  for (y = 0;y < width();y++) {
    for (x = 0;x < height();x++) {
      out->rgbRed = r;
      out->rgbGreen = g;
      out->rgbBlue = b;
      out->rgbReserved = a;
      out++;
    }
  }
}

void Surface::draw(int src_x, int src_y, int dst_x, int dst_y,
                   int dst_w, int dst_h)
{
  HDC dc;

  dc = CreateCompatibleDC(fl_gc);
  if (!dc)
    throw rdr::Win32Exception("CreateCompatibleDC", GetLastError());

  if (!SelectObject(dc, bitmap))
    throw rdr::Win32Exception("SelectObject", GetLastError());

  if (!BitBlt(fl_gc, dst_x, dst_y, dst_w, dst_h,
              dc, src_x, src_y, SRCCOPY)) {
    // If the desktop we're rendering to is inactive (like when the screen
    // is locked or the UAC is active), then GDI calls will randomly fail.
    // This is completely undocumented so we have no idea how best to deal
    // with it. For now, we've only seen this error and for this function
    // so only ignore this combination.
    if (GetLastError() != ERROR_INVALID_HANDLE)
      throw rdr::Win32Exception("BitBlt", GetLastError());
  }

  DeleteDC(dc);
}

void Surface::draw(Surface* dst, int src_x, int src_y,
                   int dst_x, int dst_y, int dst_w, int dst_h)
{
  HDC origdc, dstdc;

  dstdc = CreateCompatibleDC(nullptr);
  if (!dstdc)
    throw rdr::Win32Exception("CreateCompatibleDC", GetLastError());

  if (!SelectObject(dstdc, dst->bitmap))
    throw rdr::Win32Exception("SelectObject", GetLastError());

  origdc = fl_gc;
  fl_gc = dstdc;
  draw(src_x, src_y, dst_x, dst_y, dst_w, dst_h);
  fl_gc = origdc;

  DeleteDC(dstdc);
}

void Surface::blend(int /*src_x*/, int /*src_y*/,
                    int /*dst_x*/, int /*dst_y*/,
                    int /*dst_w*/, int /*dst_h*/,
                    int /*a*/)
{
  // Compositing doesn't work properly for window DC:s
  assert(false);
}

void Surface::blend(Surface* dst, int src_x, int src_y,
                    int dst_x, int dst_y, int dst_w, int dst_h, int a)
{
  HDC dstdc, srcdc;
  BLENDFUNCTION blend;

  dstdc = CreateCompatibleDC(nullptr);
  if (!dstdc)
    throw rdr::Win32Exception("CreateCompatibleDC", GetLastError());
  srcdc = CreateCompatibleDC(nullptr);
  if (!srcdc)
    throw rdr::Win32Exception("CreateCompatibleDC", GetLastError());

  if (!SelectObject(dstdc, dst->bitmap))
    throw rdr::Win32Exception("SelectObject", GetLastError());
  if (!SelectObject(srcdc, bitmap))
    throw rdr::Win32Exception("SelectObject", GetLastError());

  blend.BlendOp = AC_SRC_OVER;
  blend.BlendFlags = 0;
  blend.SourceConstantAlpha = a;
  blend.AlphaFormat = AC_SRC_ALPHA;

  if (!AlphaBlend(dstdc, dst_x, dst_y, dst_w, dst_h,
                  srcdc, src_x, src_y, dst_w, dst_h, blend)) {
    // If the desktop we're rendering to is inactive (like when the screen
    // is locked or the UAC is active), then GDI calls will randomly fail.
    // This is completely undocumented so we have no idea how best to deal
    // with it. For now, we've only seen this error and for this function
    // so only ignore this combination.
    if (GetLastError() != ERROR_INVALID_HANDLE)
      throw rdr::Win32Exception("BitBlt", GetLastError());
  }

  DeleteDC(srcdc);
  DeleteDC(dstdc);
}

void Surface::alloc()
{
  BITMAPINFOHEADER bih;

  data = new RGBQUAD[width() * height()];

  memset(&bih, 0, sizeof(bih));

  bih.biSize         = sizeof(BITMAPINFOHEADER);
  bih.biBitCount     = 32;
  bih.biPlanes       = 1;
  bih.biWidth        = width();
  bih.biHeight       = -height(); // Negative to get top-down
  bih.biCompression  = BI_RGB;

  bitmap = CreateDIBSection(nullptr, (BITMAPINFO*)&bih,
                            DIB_RGB_COLORS, (void**)&data, nullptr, 0);
  if (!bitmap)
    throw rdr::Win32Exception("CreateDIBSection", GetLastError());
}

void Surface::dealloc()
{
  DeleteObject(bitmap);
}

void Surface::update(const Fl_RGB_Image* image)
{
  const unsigned char* in;
  RGBQUAD* out;
  int x, y;

  assert(image->w() == width());
  assert(image->h() == height());

  // Convert data and pre-multiply alpha
  in = (const unsigned char*)image->data()[0];
  out = data;
  for (y = 0;y < image->w();y++) {
    for (x = 0;x < image->h();x++) {
      switch (image->d()) {
      case 1:
        out->rgbBlue = in[0];
        out->rgbGreen = in[0];
        out->rgbRed = in[0];
        out->rgbReserved = 0xff;
        break;
      case 2:
        out->rgbBlue = (unsigned)in[0] * in[1] / 255;
        out->rgbGreen = (unsigned)in[0] * in[1] / 255;
        out->rgbRed = (unsigned)in[0] * in[1] / 255;
        out->rgbReserved = in[1];
        break;
      case 3:
        out->rgbBlue = in[2];
        out->rgbGreen = in[1];
        out->rgbRed = in[0];
        out->rgbReserved = 0xff;
        break;
      case 4:
        out->rgbBlue = (unsigned)in[2] * in[3] / 255;
        out->rgbGreen = (unsigned)in[1] * in[3] / 255;
        out->rgbRed = (unsigned)in[0] * in[3] / 255;
        out->rgbReserved = in[3];
        break;
      }
      in += image->d();
      out++;
    }
    if (image->ld() != 0)
      in += image->ld() - image->w() * image->d();
  }
}