-
Notifications
You must be signed in to change notification settings - Fork 7
/
ULSurface.cpp
353 lines (273 loc) · 10.1 KB
/
ULSurface.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//
// Created by Gyuhwan Park on 10/28/23.
//
#include "ULSurface.hpp"
namespace ulalaca {
ULSurfaceException::ULSurfaceException(const std::string &message):
_message(message)
{
}
const std::string &ULSurfaceException::getMessage() const {
return _message;
}
const char *ULSurfaceException::what() const noexcept {
return _message.c_str();
}
ULSurfaceTransaction::ULSurfaceTransaction() {
}
void ULSurfaceTransaction::setTimestamp(double timestamp) {
_timestamp = timestamp;
}
void ULSurfaceTransaction::addRect(const ULIPCRect &rect) {
_rects.push_back(rect);
}
void ULSurfaceTransaction::addRects(const std::vector<ULIPCRect> &rects) {
_rects.insert(_rects.end(), rects.begin(), rects.end());
}
void ULSurfaceTransaction::setBitmap(std::shared_ptr<uint8_t> bitmap, size_t bitmapSize, int bitmapWidth,
int bitmapHeight) {
_bitmap = std::move(bitmap);
_bitmapSize = bitmapSize;
_bitmapWidth = bitmapWidth;
_bitmapHeight = bitmapHeight;
}
double ULSurfaceTransaction::timestamp() const {
return _timestamp;
}
const std::vector<ULIPCRect> &ULSurfaceTransaction::rects() const {
return _rects;
}
std::shared_ptr<uint8_t> ULSurfaceTransaction::bitmap() const {
return _bitmap;
}
size_t ULSurfaceTransaction::bitmapSize() const {
return _bitmapSize;
}
int ULSurfaceTransaction::bitmapWidth() const {
return _bitmapWidth;
}
int ULSurfaceTransaction::bitmapHeight() const {
return _bitmapHeight;
}
bool ULSurface::isRectOverlaps(const ULIPCRect &a, const ULIPCRect &b) {
int16_t a_x1 = a.x;
int16_t a_x2 = a.x + a.width;
int16_t a_y1 = a.y;
int16_t a_y2 = a.y + a.height;
int16_t b_x1 = b.x;
int16_t b_x2 = b.x + b.width;
int16_t b_y1 = b.y;
int16_t b_y2 = b.y + b.height;
return (
(a_x1 >= b_x1 && a_x1 <= b_x2 && a_y1 >= b_y1 && a_y1 <= b_y2) ||
(a_x2 >= b_x1 && a_x2 <= b_x2 && a_y1 >= b_y1 && a_y1 <= b_y2) ||
(a_x1 >= b_x1 && a_x1 <= b_x2 && a_y2 >= b_y1 && a_y2 <= b_y2) ||
(a_x2 >= b_x1 && a_x2 <= b_x2 && a_y2 >= b_y1 && a_y2 <= b_y2)
);
}
void ULSurface::mergeRect(ULIPCRect &a, const ULIPCRect &b) {
int16_t a_x1 = a.x;
int16_t a_x2 = a.x + a.width;
int16_t a_y1 = a.y;
int16_t a_y2 = a.y + a.height;
int16_t b_x1 = b.x;
int16_t b_x2 = b.x + b.width;
int16_t b_y1 = b.y;
int16_t b_y2 = b.y + b.height;
a.x = std::min(a_x1, b_x1);
a.y = std::min(a_y1, b_y1);
a.width = std::max(a_x2, b_x2) - a.x;
a.height = std::max(a_y2, b_y2) - a.y;
}
std::vector<ULIPCRect> ULSurface::createCopyRects(
const std::vector<ULIPCRect> &drects,
int rectSize,
int surfaceWidth, int surfaceHeight
) {
if (rectSize == RECT_SIZE_BYPASS_CREATE) {
return drects;
}
std::vector<ULIPCRect> blocks;
blocks.reserve((surfaceWidth * surfaceHeight) / (rectSize * rectSize));
/*
* sorry, i'm not good at math. XDD
*/
int mapWidth = std::ceil((float) surfaceWidth / (float) rectSize);
int mapHeight = std::ceil((float) surfaceHeight / (float) rectSize);
int mapSize = mapWidth * mapHeight;
std::unique_ptr<uint8_t> rectMap(new uint8_t[mapSize]);
memset(rectMap.get(), 0x00, mapSize);
for (auto &dirtyRect : drects) {
if (surfaceWidth <= dirtyRect.x ||
surfaceHeight <= dirtyRect.y) {
continue;
}
int mapX1 = (int) std::floor((float) dirtyRect.x / rectSize);
int mapY1 = (int) std::floor((float) dirtyRect.y / rectSize);
int mapX2 = std::min(
(int) std::ceil((float) (dirtyRect.x + dirtyRect.width) / rectSize),
mapWidth
) - 1;
int mapY2 = std::min(
(int) std::ceil((float) (dirtyRect.y + dirtyRect.height) / rectSize),
mapHeight
) - 1;
for (int y = mapY1; y <= mapY2; y++) {
for (int x = mapX1; x <= mapX2; x++) {
rectMap.get()[(y * mapWidth) + x] = 0x01;
}
}
}
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
if (rectMap.get()[(y * mapWidth) + x] == 0x01) {
int rectX = x * rectSize;
int rectY = y * rectSize;
blocks.emplace_back(ULIPCRect{(short) rectX, (short) rectY, (short) rectSize, (short) rectSize});
}
}
}
return std::move(blocks);
}
std::vector<ULIPCRect> ULSurface::cleanupRects(const std::vector<ULIPCRect> &rects) {
std::vector<ULIPCRect> result;
result.reserve(rects.size());
for (const ULIPCRect &rect: rects) {
bool merged = false;
for (ULIPCRect &resultRect: result) {
if (isRectOverlaps(resultRect, rect)) {
mergeRect(resultRect, rect);
merged = true;
break;
}
}
if (!merged) {
result.push_back(rect);
}
}
return std::move(result);
}
std::unique_ptr<short> ULSurface::allocateRectArray(const std::vector<ULIPCRect> &rects) {
static_assert(sizeof(ULIPCRect) == (sizeof(short) * 4));
std::unique_ptr<short> result(new short[rects.size() * 4]);
memcpy(result.get(), rects.data(), sizeof(ULIPCRect) * rects.size());
return std::move(result);
}
ULSurface::ULSurface(XrdpUlalaca *mod):
_mod(mod),
_width(640),
_height(480),
_crectSize(RECT_SIZE_BYPASS_CREATE),
_frameId(0),
_ackFrameId(0),
_updateThreadId(),
__HACK__mstsc_workaround(true)
{
}
int ULSurface::width() const {
return _width;
}
int ULSurface::height() const {
return _height;
}
void ULSurface::setSize(int width, int height) {
_width = width;
_height = height;
}
void ULSurface::setCRectSize(int crectSize) {
_crectSize = crectSize;
}
void ULSurface::setAckFrameId(int ackFrameId) {
_ackFrameId = ackFrameId;
}
bool ULSurface::canUpdate() const {
return _updateThreadId == std::thread::id();
}
void ULSurface::beginUpdate() {
if (_updateThreadId != std::thread::id()) {
throw ULSurfaceException("another thread is updating the surface");
}
_updateThreadId = std::this_thread::get_id();
_mod->server_begin_update(_mod);
}
void ULSurface::endUpdate() {
if (_updateThreadId == std::thread::id()) {
LOG(LOG_LEVEL_WARNING, "endUpdate() called without beginUpdate()");
return;
}
_mod->server_end_update(_mod);
_updateThreadId = std::thread::id();
}
void ULSurface::drawBitmap(int x, int y, int width, int height,
const uint8_t *bitmap, size_t bitmapSize,
int bitmapWidth, int bitmapHeight, int flags) {
std::vector rects {
ULIPCRect { (short) x, (short) y, (short) width, (short) height }
};
this->drawBitmap(rects, bitmap, bitmapSize, bitmapWidth, bitmapHeight, flags);
}
void ULSurface::drawBitmap(const std::vector<ULIPCRect> &rects, const uint8_t *bitmap, size_t bitmapSize,
int bitmapWidth, int bitmapHeight, int flags) {
if (_updateThreadId != std::this_thread::get_id()) {
throw ULSurfaceException("this thread is not holding the update lock");
}
if (rects.empty()) {
return;
}
if (__HACK__mstsc_workaround) {
// we should draw entire screen at least once to make mstsc.exe happy
__HACK__mstsc_workaround = false;
_mod->server_paint_rect(
_mod,
0, 0,
_width, _height,
(char *) bitmap,
bitmapWidth, bitmapHeight,
0, (_frameId++ % INT32_MAX)
);
return;
}
const std::vector<ULIPCRect> &cleanedRects = cleanupRects(rects);
const std::vector<ULIPCRect> ©Rects = createCopyRects(cleanedRects, _crectSize, _width, _height);
std::unique_ptr<short> drects = std::move(allocateRectArray(cleanedRects));
std::unique_ptr<short> crects = std::move(allocateRectArray(copyRects));
_mod->server_paint_rects(
_mod,
cleanedRects.size(), drects.get(),
copyRects.size(), crects.get(),
(char *) bitmap,
bitmapWidth, bitmapHeight,
0, (_frameId++ % INT32_MAX)
);
}
void ULSurface::setForegroundColor(int color) {
_mod->server_set_fgcolor(_mod, color);
}
void ULSurface::fillRect(int x, int y, int width, int height) {
_mod->server_fill_rect(_mod, x, y, width, height);
}
bool ULSurface::submitUpdate(const ULSurfaceTransaction &transaction) {
_pendingRects.insert(
_pendingRects.end(),
transaction.rects().begin(), transaction.rects().end()
);
if (shouldDropFrame(transaction)) {
return false;
}
this->drawBitmap(
_pendingRects,
transaction.bitmap().get(), transaction.bitmapSize(),
transaction.bitmapWidth(), transaction.bitmapHeight(),
0
);
_pendingRects.clear();
return true;
}
bool ULSurface::shouldDropFrame(const ULSurfaceTransaction &transaction) const {
constexpr static const int MAX_FRAME_DELAY = 2;
int fdelta = std::abs(_frameId - _ackFrameId);
return (
fdelta > MAX_FRAME_DELAY
);
}
}