Kitxuuu commited on
Commit
12fb50f
·
verified ·
1 Parent(s): 6f1881a

Add files using upload-large-folder tool

Browse files
Files changed (50) hide show
  1. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/CMakeLists.txt +82 -0
  2. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_channels.hpp +29 -0
  3. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_disp.hpp +82 -0
  4. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_kbd.cpp +579 -0
  5. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_kbd.hpp +77 -0
  6. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_window.hpp +64 -0
  7. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/CMakeLists.txt +75 -0
  8. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/res/sdl3_resource_manager.cpp +43 -0
  9. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/res/sdl3_resource_manager.hpp +38 -0
  10. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_button.hpp +27 -0
  11. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_buttons.cpp +109 -0
  12. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_buttons.hpp +38 -0
  13. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_connection_dialog.cpp +537 -0
  14. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_connection_dialog.hpp +132 -0
  15. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_dialogs.cpp +620 -0
  16. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_dialogs.hpp +53 -0
  17. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_input.cpp +178 -0
  18. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_input_widgets.cpp +295 -0
  19. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_input_widgets.hpp +44 -0
  20. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_select.cpp +69 -0
  21. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_select.hpp +46 -0
  22. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_selectlist.hpp +41 -0
  23. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_widget.cpp +320 -0
  24. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_widget.hpp +88 -0
  25. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/test/TestSDLDialogs.cpp +99 -0
  26. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/man/CMakeLists.txt +8 -0
  27. local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/man/sdl3-freerdp.1.in +15 -0
  28. local-test-freerdp-delta-01/afc-freerdp/client/SDL/common/CMakeLists.txt +27 -0
  29. local-test-freerdp-delta-01/afc-freerdp/client/SDL/common/scoped_guard.hpp +56 -0
  30. local-test-freerdp-delta-01/afc-freerdp/client/SDL/common/sdl_prefs.cpp +199 -0
  31. local-test-freerdp-full-01/afc-freerdp/client/X11/cli/xfreerdp.c +120 -0
  32. local-test-freerdp-full-01/afc-freerdp/client/X11/resource/lock.xbm +11 -0
  33. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/audio.c +285 -0
  34. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/dsp.h +46 -0
  35. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/dsp_fdk_aac.c +154 -0
  36. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/dsp_fdk_impl.h +45 -0
  37. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/planar.c +1797 -0
  38. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/progressive.c +2681 -0
  39. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/rfx_decode.h +33 -0
  40. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/rfx_dwt.c +230 -0
  41. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/rfx_quantization.h +31 -0
  42. local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/rfx_types.h +184 -0
  43. local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/CMakeLists.txt +34 -0
  44. local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/bitmap.c +678 -0
  45. local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/brush.h +53 -0
  46. local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/dc.c +258 -0
  47. local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/gfx.c +1967 -0
  48. local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/line.c +316 -0
  49. local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/shape.c +299 -0
  50. local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/video.c +210 -0
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/CMakeLists.txt ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FreeRDP: A Remote Desktop Protocol Implementation
2
+ # FreeRDP SDL Client
3
+ #
4
+ # Copyright 2022 Armin Novak <[email protected]>
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ set(MODULE_NAME "sdl2-freerdp")
18
+
19
+ find_package(SDL2 REQUIRED)
20
+
21
+ find_package(Threads REQUIRED)
22
+
23
+ add_subdirectory(dialogs)
24
+ set(SRCS
25
+ sdl_types.hpp
26
+ sdl_utils.cpp
27
+ sdl_utils.hpp
28
+ sdl_kbd.cpp
29
+ sdl_kbd.hpp
30
+ sdl_touch.cpp
31
+ sdl_touch.hpp
32
+ sdl_pointer.cpp
33
+ sdl_pointer.hpp
34
+ sdl_disp.cpp
35
+ sdl_disp.hpp
36
+ sdl_monitor.cpp
37
+ sdl_monitor.hpp
38
+ sdl_freerdp.hpp
39
+ sdl_freerdp.cpp
40
+ sdl_channels.hpp
41
+ sdl_channels.cpp
42
+ sdl_window.hpp
43
+ sdl_window.cpp
44
+ )
45
+
46
+ list(
47
+ APPEND
48
+ LIBS
49
+ winpr
50
+ freerdp
51
+ freerdp-client
52
+ Threads::Threads
53
+ sdl2_client_res
54
+ sdl2-dialogs
55
+ sdl-common-aad-view
56
+ sdl-common-prefs
57
+ )
58
+
59
+ if(NOT WITH_SDL_LINK_SHARED)
60
+ list(APPEND LIBS SDL2::SDL2-static)
61
+ set_target_properties(SDL2::SDL2-static PROPERTIES SYSTEM TRUE)
62
+ else()
63
+ list(APPEND LIBS SDL2::SDL2)
64
+ set_target_properties(SDL2::SDL2 PROPERTIES SYSTEM TRUE)
65
+ endif()
66
+
67
+ addtargetwithresourcefile(${MODULE_NAME} TRUE "${PROJECT_VERSION}" SRCS)
68
+
69
+ target_link_libraries(${MODULE_NAME} PRIVATE ${LIBS})
70
+
71
+ set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "Client/SDL")
72
+
73
+ if(NOT WITH_CLIENT_SDL_VERSIONED)
74
+ get_target_property(OUT_NAME ${MODULE_NAME} OUTPUT_NAME)
75
+ string(REPLACE "${MODULE_NAME}" "${PROJECT_NAME}" OUT_NAME "${OUT_NAME}")
76
+ set_target_properties(${MODULE_NAME} PROPERTIES OUTPUT_NAME ${OUT_NAME})
77
+ endif()
78
+
79
+ install(TARGETS ${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT client)
80
+ install_freerdp_desktop("${MODULE_NAME}")
81
+
82
+ add_subdirectory(man)
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_channels.hpp ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client Channels
4
+ *
5
+ * Copyright 2022 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #pragma once
21
+
22
+ #include <freerdp/freerdp.h>
23
+ #include <freerdp/client/channels.h>
24
+
25
+ int sdl_on_channel_connected(freerdp* instance, const char* name, void* pInterface);
26
+ int sdl_on_channel_disconnected(freerdp* instance, const char* name, void* pInterface);
27
+
28
+ void sdl_OnChannelConnectedEventHandler(void* context, const ChannelConnectedEventArgs* e);
29
+ void sdl_OnChannelDisconnectedEventHandler(void* context, const ChannelDisconnectedEventArgs* e);
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_disp.hpp ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Display Control Channel
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ #pragma once
20
+
21
+ #include <freerdp/types.h>
22
+ #include <freerdp/event.h>
23
+ #include <freerdp/client/disp.h>
24
+
25
+ #include "sdl_types.hpp"
26
+
27
+ #include <SDL.h>
28
+
29
+ class sdlDispContext
30
+ {
31
+
32
+ public:
33
+ explicit sdlDispContext(SdlContext* sdl);
34
+ sdlDispContext(const sdlDispContext& other) = delete;
35
+ sdlDispContext(sdlDispContext&& other) = delete;
36
+ ~sdlDispContext();
37
+
38
+ sdlDispContext& operator=(const sdlDispContext& other) = delete;
39
+ sdlDispContext& operator=(sdlDispContext&& other) = delete;
40
+
41
+ BOOL init(DispClientContext* disp);
42
+ BOOL uninit(DispClientContext* disp);
43
+
44
+ #if SDL_VERSION_ATLEAST(2, 0, 10)
45
+ BOOL handle_display_event(const SDL_DisplayEvent* ev);
46
+ #endif
47
+
48
+ BOOL handle_window_event(const SDL_WindowEvent* ev);
49
+
50
+ private:
51
+ UINT DisplayControlCaps(UINT32 maxNumMonitors, UINT32 maxMonitorAreaFactorA,
52
+ UINT32 maxMonitorAreaFactorB);
53
+ BOOL set_window_resizable();
54
+
55
+ BOOL sendResize();
56
+ BOOL settings_changed();
57
+ BOOL update_last_sent();
58
+ UINT sendLayout(const rdpMonitor* monitors, size_t nmonitors);
59
+
60
+ BOOL addTimer();
61
+
62
+ static UINT DisplayControlCaps(DispClientContext* disp, UINT32 maxNumMonitors,
63
+ UINT32 maxMonitorAreaFactorA, UINT32 maxMonitorAreaFactorB);
64
+ static void OnActivated(void* context, const ActivatedEventArgs* e);
65
+ static void OnGraphicsReset(void* context, const GraphicsResetEventArgs* e);
66
+ static Uint32 SDLCALL OnTimer(Uint32 interval, void* param);
67
+
68
+ SdlContext* _sdl = nullptr;
69
+ DispClientContext* _disp = nullptr;
70
+ int _lastSentWidth = -1;
71
+ int _lastSentHeight = -1;
72
+ UINT64 _lastSentDate = 0;
73
+ int _targetWidth = -1;
74
+ int _targetHeight = -1;
75
+ BOOL _activated = FALSE;
76
+ BOOL _waitingResize = FALSE;
77
+ UINT16 _lastSentDesktopOrientation = 0;
78
+ UINT32 _lastSentDesktopScaleFactor = 0;
79
+ UINT32 _lastSentDeviceScaleFactor = 0;
80
+ SDL_TimerID _timer = 0;
81
+ unsigned _timer_retries = 0;
82
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_kbd.cpp ADDED
@@ -0,0 +1,579 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * FreeRDP SDL keyboard helper
4
+ *
5
+ * Copyright 2022 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <winpr/cast.h>
21
+
22
+ #include "sdl_kbd.hpp"
23
+ #include "sdl_disp.hpp"
24
+ #include "sdl_freerdp.hpp"
25
+ #include "sdl_utils.hpp"
26
+ #include "sdl_prefs.hpp"
27
+
28
+ #include <map>
29
+
30
+ #include <freerdp/utils/string.h>
31
+ #include <freerdp/scancode.h>
32
+
33
+ #include <freerdp/log.h>
34
+ #define TAG CLIENT_TAG("SDL.kbd")
35
+
36
+ using scancode_entry_t = struct
37
+ {
38
+ Uint32 sdl;
39
+ const char* sdl_name;
40
+ UINT32 rdp;
41
+ const char* rdp_name;
42
+ };
43
+
44
+ #define STR(x) #x
45
+ #define ENTRY(x, y) \
46
+ { \
47
+ x, STR(x), y, #y \
48
+ }
49
+ static const scancode_entry_t map[] = {
50
+ ENTRY(SDL_SCANCODE_UNKNOWN, RDP_SCANCODE_UNKNOWN),
51
+ ENTRY(SDL_SCANCODE_A, RDP_SCANCODE_KEY_A),
52
+ ENTRY(SDL_SCANCODE_B, RDP_SCANCODE_KEY_B),
53
+ ENTRY(SDL_SCANCODE_C, RDP_SCANCODE_KEY_C),
54
+ ENTRY(SDL_SCANCODE_D, RDP_SCANCODE_KEY_D),
55
+ ENTRY(SDL_SCANCODE_E, RDP_SCANCODE_KEY_E),
56
+ ENTRY(SDL_SCANCODE_F, RDP_SCANCODE_KEY_F),
57
+ ENTRY(SDL_SCANCODE_G, RDP_SCANCODE_KEY_G),
58
+ ENTRY(SDL_SCANCODE_H, RDP_SCANCODE_KEY_H),
59
+ ENTRY(SDL_SCANCODE_I, RDP_SCANCODE_KEY_I),
60
+ ENTRY(SDL_SCANCODE_J, RDP_SCANCODE_KEY_J),
61
+ ENTRY(SDL_SCANCODE_K, RDP_SCANCODE_KEY_K),
62
+ ENTRY(SDL_SCANCODE_L, RDP_SCANCODE_KEY_L),
63
+ ENTRY(SDL_SCANCODE_M, RDP_SCANCODE_KEY_M),
64
+ ENTRY(SDL_SCANCODE_N, RDP_SCANCODE_KEY_N),
65
+ ENTRY(SDL_SCANCODE_O, RDP_SCANCODE_KEY_O),
66
+ ENTRY(SDL_SCANCODE_P, RDP_SCANCODE_KEY_P),
67
+ ENTRY(SDL_SCANCODE_Q, RDP_SCANCODE_KEY_Q),
68
+ ENTRY(SDL_SCANCODE_R, RDP_SCANCODE_KEY_R),
69
+ ENTRY(SDL_SCANCODE_S, RDP_SCANCODE_KEY_S),
70
+ ENTRY(SDL_SCANCODE_T, RDP_SCANCODE_KEY_T),
71
+ ENTRY(SDL_SCANCODE_U, RDP_SCANCODE_KEY_U),
72
+ ENTRY(SDL_SCANCODE_V, RDP_SCANCODE_KEY_V),
73
+ ENTRY(SDL_SCANCODE_W, RDP_SCANCODE_KEY_W),
74
+ ENTRY(SDL_SCANCODE_X, RDP_SCANCODE_KEY_X),
75
+ ENTRY(SDL_SCANCODE_Y, RDP_SCANCODE_KEY_Y),
76
+ ENTRY(SDL_SCANCODE_Z, RDP_SCANCODE_KEY_Z),
77
+ ENTRY(SDL_SCANCODE_1, RDP_SCANCODE_KEY_1),
78
+ ENTRY(SDL_SCANCODE_2, RDP_SCANCODE_KEY_2),
79
+ ENTRY(SDL_SCANCODE_3, RDP_SCANCODE_KEY_3),
80
+ ENTRY(SDL_SCANCODE_4, RDP_SCANCODE_KEY_4),
81
+ ENTRY(SDL_SCANCODE_5, RDP_SCANCODE_KEY_5),
82
+ ENTRY(SDL_SCANCODE_6, RDP_SCANCODE_KEY_6),
83
+ ENTRY(SDL_SCANCODE_7, RDP_SCANCODE_KEY_7),
84
+ ENTRY(SDL_SCANCODE_8, RDP_SCANCODE_KEY_8),
85
+ ENTRY(SDL_SCANCODE_9, RDP_SCANCODE_KEY_9),
86
+ ENTRY(SDL_SCANCODE_0, RDP_SCANCODE_KEY_0),
87
+ ENTRY(SDL_SCANCODE_RETURN, RDP_SCANCODE_RETURN),
88
+ ENTRY(SDL_SCANCODE_ESCAPE, RDP_SCANCODE_ESCAPE),
89
+ ENTRY(SDL_SCANCODE_BACKSPACE, RDP_SCANCODE_BACKSPACE),
90
+ ENTRY(SDL_SCANCODE_TAB, RDP_SCANCODE_TAB),
91
+ ENTRY(SDL_SCANCODE_SPACE, RDP_SCANCODE_SPACE),
92
+ ENTRY(SDL_SCANCODE_MINUS, RDP_SCANCODE_OEM_MINUS),
93
+ ENTRY(SDL_SCANCODE_CAPSLOCK, RDP_SCANCODE_CAPSLOCK),
94
+ ENTRY(SDL_SCANCODE_F1, RDP_SCANCODE_F1),
95
+ ENTRY(SDL_SCANCODE_F2, RDP_SCANCODE_F2),
96
+ ENTRY(SDL_SCANCODE_F3, RDP_SCANCODE_F3),
97
+ ENTRY(SDL_SCANCODE_F4, RDP_SCANCODE_F4),
98
+ ENTRY(SDL_SCANCODE_F5, RDP_SCANCODE_F5),
99
+ ENTRY(SDL_SCANCODE_F6, RDP_SCANCODE_F6),
100
+ ENTRY(SDL_SCANCODE_F7, RDP_SCANCODE_F7),
101
+ ENTRY(SDL_SCANCODE_F8, RDP_SCANCODE_F8),
102
+ ENTRY(SDL_SCANCODE_F9, RDP_SCANCODE_F9),
103
+ ENTRY(SDL_SCANCODE_F10, RDP_SCANCODE_F10),
104
+ ENTRY(SDL_SCANCODE_F11, RDP_SCANCODE_F11),
105
+ ENTRY(SDL_SCANCODE_F12, RDP_SCANCODE_F12),
106
+ ENTRY(SDL_SCANCODE_F13, RDP_SCANCODE_F13),
107
+ ENTRY(SDL_SCANCODE_F14, RDP_SCANCODE_F14),
108
+ ENTRY(SDL_SCANCODE_F15, RDP_SCANCODE_F15),
109
+ ENTRY(SDL_SCANCODE_F16, RDP_SCANCODE_F16),
110
+ ENTRY(SDL_SCANCODE_F17, RDP_SCANCODE_F17),
111
+ ENTRY(SDL_SCANCODE_F18, RDP_SCANCODE_F18),
112
+ ENTRY(SDL_SCANCODE_F19, RDP_SCANCODE_F19),
113
+ ENTRY(SDL_SCANCODE_F20, RDP_SCANCODE_F20),
114
+ ENTRY(SDL_SCANCODE_F21, RDP_SCANCODE_F21),
115
+ ENTRY(SDL_SCANCODE_F22, RDP_SCANCODE_F22),
116
+ ENTRY(SDL_SCANCODE_F23, RDP_SCANCODE_F23),
117
+ ENTRY(SDL_SCANCODE_F24, RDP_SCANCODE_F24),
118
+ ENTRY(SDL_SCANCODE_NUMLOCKCLEAR, RDP_SCANCODE_NUMLOCK),
119
+ ENTRY(SDL_SCANCODE_KP_DIVIDE, RDP_SCANCODE_DIVIDE),
120
+ ENTRY(SDL_SCANCODE_KP_MULTIPLY, RDP_SCANCODE_MULTIPLY),
121
+ ENTRY(SDL_SCANCODE_KP_MINUS, RDP_SCANCODE_SUBTRACT),
122
+ ENTRY(SDL_SCANCODE_KP_PLUS, RDP_SCANCODE_ADD),
123
+ ENTRY(SDL_SCANCODE_KP_ENTER, RDP_SCANCODE_RETURN_KP),
124
+ ENTRY(SDL_SCANCODE_KP_1, RDP_SCANCODE_NUMPAD1),
125
+ ENTRY(SDL_SCANCODE_KP_2, RDP_SCANCODE_NUMPAD2),
126
+ ENTRY(SDL_SCANCODE_KP_3, RDP_SCANCODE_NUMPAD3),
127
+ ENTRY(SDL_SCANCODE_KP_4, RDP_SCANCODE_NUMPAD4),
128
+ ENTRY(SDL_SCANCODE_KP_5, RDP_SCANCODE_NUMPAD5),
129
+ ENTRY(SDL_SCANCODE_KP_6, RDP_SCANCODE_NUMPAD6),
130
+ ENTRY(SDL_SCANCODE_KP_7, RDP_SCANCODE_NUMPAD7),
131
+ ENTRY(SDL_SCANCODE_KP_8, RDP_SCANCODE_NUMPAD8),
132
+ ENTRY(SDL_SCANCODE_KP_9, RDP_SCANCODE_NUMPAD9),
133
+ ENTRY(SDL_SCANCODE_KP_0, RDP_SCANCODE_NUMPAD0),
134
+ ENTRY(SDL_SCANCODE_KP_PERIOD, RDP_SCANCODE_OEM_PERIOD),
135
+ ENTRY(SDL_SCANCODE_LCTRL, RDP_SCANCODE_LCONTROL),
136
+ ENTRY(SDL_SCANCODE_LSHIFT, RDP_SCANCODE_LSHIFT),
137
+ ENTRY(SDL_SCANCODE_LALT, RDP_SCANCODE_LMENU),
138
+ ENTRY(SDL_SCANCODE_LGUI, RDP_SCANCODE_LWIN),
139
+ ENTRY(SDL_SCANCODE_RCTRL, RDP_SCANCODE_RCONTROL),
140
+ ENTRY(SDL_SCANCODE_RSHIFT, RDP_SCANCODE_RSHIFT),
141
+ ENTRY(SDL_SCANCODE_RALT, RDP_SCANCODE_RMENU),
142
+ ENTRY(SDL_SCANCODE_RGUI, RDP_SCANCODE_RWIN),
143
+ ENTRY(SDL_SCANCODE_MODE, RDP_SCANCODE_APPS),
144
+ ENTRY(SDL_SCANCODE_MUTE, RDP_SCANCODE_VOLUME_MUTE),
145
+ ENTRY(SDL_SCANCODE_VOLUMEUP, RDP_SCANCODE_VOLUME_UP),
146
+ ENTRY(SDL_SCANCODE_VOLUMEDOWN, RDP_SCANCODE_VOLUME_DOWN),
147
+ ENTRY(SDL_SCANCODE_GRAVE, RDP_SCANCODE_OEM_3),
148
+ ENTRY(SDL_SCANCODE_COMMA, RDP_SCANCODE_OEM_COMMA),
149
+ ENTRY(SDL_SCANCODE_PERIOD, RDP_SCANCODE_OEM_PERIOD),
150
+ ENTRY(SDL_SCANCODE_SLASH, RDP_SCANCODE_OEM_2),
151
+ ENTRY(SDL_SCANCODE_BACKSLASH, RDP_SCANCODE_OEM_5),
152
+ ENTRY(SDL_SCANCODE_SCROLLLOCK, RDP_SCANCODE_SCROLLLOCK),
153
+ ENTRY(SDL_SCANCODE_INSERT, RDP_SCANCODE_INSERT),
154
+ ENTRY(SDL_SCANCODE_PRINTSCREEN, RDP_SCANCODE_PRINTSCREEN),
155
+ ENTRY(SDL_SCANCODE_HOME, RDP_SCANCODE_HOME),
156
+ ENTRY(SDL_SCANCODE_DELETE, RDP_SCANCODE_DELETE),
157
+ ENTRY(SDL_SCANCODE_RIGHT, RDP_SCANCODE_RIGHT),
158
+ ENTRY(SDL_SCANCODE_LEFT, RDP_SCANCODE_LEFT),
159
+ ENTRY(SDL_SCANCODE_DOWN, RDP_SCANCODE_DOWN),
160
+ ENTRY(SDL_SCANCODE_UP, RDP_SCANCODE_UP),
161
+ ENTRY(SDL_SCANCODE_SEMICOLON, RDP_SCANCODE_OEM_1),
162
+ ENTRY(SDL_SCANCODE_PAUSE, RDP_SCANCODE_PAUSE),
163
+ ENTRY(SDL_SCANCODE_PAGEUP, RDP_SCANCODE_PRIOR),
164
+ ENTRY(SDL_SCANCODE_END, RDP_SCANCODE_END),
165
+ ENTRY(SDL_SCANCODE_PAGEDOWN, RDP_SCANCODE_NEXT),
166
+ ENTRY(SDL_SCANCODE_AUDIONEXT, RDP_SCANCODE_MEDIA_NEXT_TRACK),
167
+ ENTRY(SDL_SCANCODE_AUDIOPREV, RDP_SCANCODE_MEDIA_PREV_TRACK),
168
+ ENTRY(SDL_SCANCODE_AUDIOSTOP, RDP_SCANCODE_MEDIA_STOP),
169
+ ENTRY(SDL_SCANCODE_AUDIOPLAY, RDP_SCANCODE_MEDIA_PLAY_PAUSE),
170
+ ENTRY(SDL_SCANCODE_AUDIOMUTE, RDP_SCANCODE_VOLUME_MUTE),
171
+ ENTRY(SDL_SCANCODE_MEDIASELECT, RDP_SCANCODE_LAUNCH_MEDIA_SELECT),
172
+ ENTRY(SDL_SCANCODE_MAIL, RDP_SCANCODE_LAUNCH_MAIL),
173
+ ENTRY(SDL_SCANCODE_APP1, RDP_SCANCODE_LAUNCH_APP1),
174
+ ENTRY(SDL_SCANCODE_APP2, RDP_SCANCODE_LAUNCH_APP2),
175
+ ENTRY(SDL_SCANCODE_SYSREQ, RDP_SCANCODE_SYSREQ),
176
+ ENTRY(SDL_SCANCODE_WWW, RDP_SCANCODE_BROWSER_HOME),
177
+ ENTRY(SDL_SCANCODE_LEFTBRACKET, RDP_SCANCODE_OEM_4),
178
+ ENTRY(SDL_SCANCODE_RIGHTBRACKET, RDP_SCANCODE_OEM_6),
179
+ ENTRY(SDL_SCANCODE_APOSTROPHE, RDP_SCANCODE_OEM_7),
180
+ ENTRY(SDL_SCANCODE_NONUSBACKSLASH, RDP_SCANCODE_OEM_102),
181
+ ENTRY(SDL_SCANCODE_SLEEP, RDP_SCANCODE_SLEEP),
182
+ ENTRY(SDL_SCANCODE_EQUALS, RDP_SCANCODE_OEM_PLUS),
183
+ ENTRY(SDL_SCANCODE_KP_COMMA, RDP_SCANCODE_DECIMAL),
184
+ ENTRY(SDL_SCANCODE_FIND, RDP_SCANCODE_BROWSER_SEARCH),
185
+ ENTRY(SDL_SCANCODE_RETURN2, RDP_SCANCODE_RETURN_KP),
186
+ ENTRY(SDL_SCANCODE_AC_SEARCH, RDP_SCANCODE_BROWSER_SEARCH),
187
+ ENTRY(SDL_SCANCODE_AC_HOME, RDP_SCANCODE_BROWSER_HOME),
188
+ ENTRY(SDL_SCANCODE_AC_BACK, RDP_SCANCODE_BROWSER_BACK),
189
+ ENTRY(SDL_SCANCODE_AC_FORWARD, RDP_SCANCODE_BROWSER_FORWARD),
190
+ ENTRY(SDL_SCANCODE_AC_STOP, RDP_SCANCODE_BROWSER_STOP),
191
+
192
+ #if 1 // TODO: unmapped
193
+ ENTRY(SDL_SCANCODE_NONUSHASH, RDP_SCANCODE_UNKNOWN),
194
+ ENTRY(SDL_SCANCODE_APPLICATION, RDP_SCANCODE_UNKNOWN),
195
+ ENTRY(SDL_SCANCODE_POWER, RDP_SCANCODE_UNKNOWN),
196
+ ENTRY(SDL_SCANCODE_KP_EQUALS, RDP_SCANCODE_UNKNOWN),
197
+ ENTRY(SDL_SCANCODE_EXECUTE, RDP_SCANCODE_UNKNOWN),
198
+ ENTRY(SDL_SCANCODE_HELP, RDP_SCANCODE_UNKNOWN),
199
+ ENTRY(SDL_SCANCODE_MENU, RDP_SCANCODE_UNKNOWN),
200
+ ENTRY(SDL_SCANCODE_SELECT, RDP_SCANCODE_UNKNOWN),
201
+ ENTRY(SDL_SCANCODE_STOP, RDP_SCANCODE_UNKNOWN),
202
+ ENTRY(SDL_SCANCODE_AGAIN, RDP_SCANCODE_UNKNOWN),
203
+ ENTRY(SDL_SCANCODE_UNDO, RDP_SCANCODE_UNKNOWN),
204
+ ENTRY(SDL_SCANCODE_CUT, RDP_SCANCODE_UNKNOWN),
205
+ ENTRY(SDL_SCANCODE_COPY, RDP_SCANCODE_UNKNOWN),
206
+ ENTRY(SDL_SCANCODE_PASTE, RDP_SCANCODE_UNKNOWN),
207
+ ENTRY(SDL_SCANCODE_KP_EQUALSAS400, RDP_SCANCODE_UNKNOWN),
208
+ ENTRY(SDL_SCANCODE_INTERNATIONAL1, RDP_SCANCODE_UNKNOWN),
209
+ ENTRY(SDL_SCANCODE_INTERNATIONAL2, RDP_SCANCODE_UNKNOWN),
210
+ ENTRY(SDL_SCANCODE_INTERNATIONAL3, RDP_SCANCODE_UNKNOWN),
211
+ ENTRY(SDL_SCANCODE_INTERNATIONAL4, RDP_SCANCODE_UNKNOWN),
212
+ ENTRY(SDL_SCANCODE_INTERNATIONAL5, RDP_SCANCODE_UNKNOWN),
213
+ ENTRY(SDL_SCANCODE_INTERNATIONAL6, RDP_SCANCODE_UNKNOWN),
214
+ ENTRY(SDL_SCANCODE_INTERNATIONAL7, RDP_SCANCODE_UNKNOWN),
215
+ ENTRY(SDL_SCANCODE_INTERNATIONAL8, RDP_SCANCODE_UNKNOWN),
216
+ ENTRY(SDL_SCANCODE_INTERNATIONAL9, RDP_SCANCODE_UNKNOWN),
217
+ ENTRY(SDL_SCANCODE_LANG1, RDP_SCANCODE_UNKNOWN),
218
+ ENTRY(SDL_SCANCODE_LANG2, RDP_SCANCODE_UNKNOWN),
219
+ ENTRY(SDL_SCANCODE_LANG3, RDP_SCANCODE_UNKNOWN),
220
+ ENTRY(SDL_SCANCODE_LANG4, RDP_SCANCODE_UNKNOWN),
221
+ ENTRY(SDL_SCANCODE_LANG5, RDP_SCANCODE_UNKNOWN),
222
+ ENTRY(SDL_SCANCODE_LANG6, RDP_SCANCODE_UNKNOWN),
223
+ ENTRY(SDL_SCANCODE_LANG7, RDP_SCANCODE_UNKNOWN),
224
+ ENTRY(SDL_SCANCODE_LANG8, RDP_SCANCODE_UNKNOWN),
225
+ ENTRY(SDL_SCANCODE_LANG9, RDP_SCANCODE_UNKNOWN),
226
+ ENTRY(SDL_SCANCODE_ALTERASE, RDP_SCANCODE_UNKNOWN),
227
+ ENTRY(SDL_SCANCODE_CANCEL, RDP_SCANCODE_UNKNOWN),
228
+ ENTRY(SDL_SCANCODE_CLEAR, RDP_SCANCODE_UNKNOWN),
229
+ ENTRY(SDL_SCANCODE_PRIOR, RDP_SCANCODE_UNKNOWN),
230
+ ENTRY(SDL_SCANCODE_SEPARATOR, RDP_SCANCODE_UNKNOWN),
231
+ ENTRY(SDL_SCANCODE_OUT, RDP_SCANCODE_UNKNOWN),
232
+ ENTRY(SDL_SCANCODE_OPER, RDP_SCANCODE_UNKNOWN),
233
+ ENTRY(SDL_SCANCODE_CLEARAGAIN, RDP_SCANCODE_UNKNOWN),
234
+ ENTRY(SDL_SCANCODE_CRSEL, RDP_SCANCODE_UNKNOWN),
235
+ ENTRY(SDL_SCANCODE_EXSEL, RDP_SCANCODE_UNKNOWN),
236
+ ENTRY(SDL_SCANCODE_KP_00, RDP_SCANCODE_UNKNOWN),
237
+ ENTRY(SDL_SCANCODE_KP_000, RDP_SCANCODE_UNKNOWN),
238
+ ENTRY(SDL_SCANCODE_THOUSANDSSEPARATOR, RDP_SCANCODE_UNKNOWN),
239
+ ENTRY(SDL_SCANCODE_DECIMALSEPARATOR, RDP_SCANCODE_UNKNOWN),
240
+ ENTRY(SDL_SCANCODE_CURRENCYUNIT, RDP_SCANCODE_UNKNOWN),
241
+ ENTRY(SDL_SCANCODE_CURRENCYSUBUNIT, RDP_SCANCODE_UNKNOWN),
242
+ ENTRY(SDL_SCANCODE_KP_LEFTPAREN, RDP_SCANCODE_UNKNOWN),
243
+ ENTRY(SDL_SCANCODE_KP_RIGHTPAREN, RDP_SCANCODE_UNKNOWN),
244
+ ENTRY(SDL_SCANCODE_KP_LEFTBRACE, RDP_SCANCODE_UNKNOWN),
245
+ ENTRY(SDL_SCANCODE_KP_RIGHTBRACE, RDP_SCANCODE_UNKNOWN),
246
+ ENTRY(SDL_SCANCODE_KP_TAB, RDP_SCANCODE_UNKNOWN),
247
+ ENTRY(SDL_SCANCODE_KP_BACKSPACE, RDP_SCANCODE_UNKNOWN),
248
+ ENTRY(SDL_SCANCODE_KP_A, RDP_SCANCODE_UNKNOWN),
249
+ ENTRY(SDL_SCANCODE_KP_B, RDP_SCANCODE_UNKNOWN),
250
+ ENTRY(SDL_SCANCODE_KP_C, RDP_SCANCODE_UNKNOWN),
251
+ ENTRY(SDL_SCANCODE_KP_D, RDP_SCANCODE_UNKNOWN),
252
+ ENTRY(SDL_SCANCODE_KP_E, RDP_SCANCODE_UNKNOWN),
253
+ ENTRY(SDL_SCANCODE_KP_F, RDP_SCANCODE_UNKNOWN),
254
+ ENTRY(SDL_SCANCODE_KP_XOR, RDP_SCANCODE_UNKNOWN),
255
+ ENTRY(SDL_SCANCODE_KP_POWER, RDP_SCANCODE_UNKNOWN),
256
+ ENTRY(SDL_SCANCODE_KP_PERCENT, RDP_SCANCODE_UNKNOWN),
257
+ ENTRY(SDL_SCANCODE_KP_LESS, RDP_SCANCODE_UNKNOWN),
258
+ ENTRY(SDL_SCANCODE_KP_GREATER, RDP_SCANCODE_UNKNOWN),
259
+ ENTRY(SDL_SCANCODE_KP_AMPERSAND, RDP_SCANCODE_UNKNOWN),
260
+ ENTRY(SDL_SCANCODE_KP_DBLAMPERSAND, RDP_SCANCODE_UNKNOWN),
261
+ ENTRY(SDL_SCANCODE_KP_VERTICALBAR, RDP_SCANCODE_UNKNOWN),
262
+ ENTRY(SDL_SCANCODE_KP_DBLVERTICALBAR, RDP_SCANCODE_UNKNOWN),
263
+ ENTRY(SDL_SCANCODE_KP_COLON, RDP_SCANCODE_UNKNOWN),
264
+ ENTRY(SDL_SCANCODE_KP_HASH, RDP_SCANCODE_UNKNOWN),
265
+ ENTRY(SDL_SCANCODE_KP_SPACE, RDP_SCANCODE_UNKNOWN),
266
+ ENTRY(SDL_SCANCODE_KP_AT, RDP_SCANCODE_UNKNOWN),
267
+ ENTRY(SDL_SCANCODE_KP_EXCLAM, RDP_SCANCODE_UNKNOWN),
268
+ ENTRY(SDL_SCANCODE_KP_MEMSTORE, RDP_SCANCODE_UNKNOWN),
269
+ ENTRY(SDL_SCANCODE_KP_MEMRECALL, RDP_SCANCODE_UNKNOWN),
270
+ ENTRY(SDL_SCANCODE_KP_MEMCLEAR, RDP_SCANCODE_UNKNOWN),
271
+ ENTRY(SDL_SCANCODE_KP_MEMADD, RDP_SCANCODE_UNKNOWN),
272
+ ENTRY(SDL_SCANCODE_KP_MEMSUBTRACT, RDP_SCANCODE_UNKNOWN),
273
+ ENTRY(SDL_SCANCODE_KP_MEMMULTIPLY, RDP_SCANCODE_UNKNOWN),
274
+ ENTRY(SDL_SCANCODE_KP_MEMDIVIDE, RDP_SCANCODE_UNKNOWN),
275
+ ENTRY(SDL_SCANCODE_KP_PLUSMINUS, RDP_SCANCODE_UNKNOWN),
276
+ ENTRY(SDL_SCANCODE_KP_CLEAR, RDP_SCANCODE_UNKNOWN),
277
+ ENTRY(SDL_SCANCODE_KP_CLEARENTRY, RDP_SCANCODE_UNKNOWN),
278
+ ENTRY(SDL_SCANCODE_KP_BINARY, RDP_SCANCODE_UNKNOWN),
279
+ ENTRY(SDL_SCANCODE_KP_OCTAL, RDP_SCANCODE_UNKNOWN),
280
+ ENTRY(SDL_SCANCODE_KP_DECIMAL, RDP_SCANCODE_UNKNOWN),
281
+ ENTRY(SDL_SCANCODE_KP_HEXADECIMAL, RDP_SCANCODE_UNKNOWN),
282
+ ENTRY(SDL_SCANCODE_CALCULATOR, RDP_SCANCODE_UNKNOWN),
283
+ ENTRY(SDL_SCANCODE_COMPUTER, RDP_SCANCODE_UNKNOWN),
284
+ ENTRY(SDL_SCANCODE_AC_REFRESH, RDP_SCANCODE_UNKNOWN),
285
+ ENTRY(SDL_SCANCODE_AC_BOOKMARKS, RDP_SCANCODE_UNKNOWN),
286
+ ENTRY(SDL_SCANCODE_BRIGHTNESSDOWN, RDP_SCANCODE_UNKNOWN),
287
+ ENTRY(SDL_SCANCODE_BRIGHTNESSUP, RDP_SCANCODE_UNKNOWN),
288
+ ENTRY(SDL_SCANCODE_DISPLAYSWITCH, RDP_SCANCODE_UNKNOWN),
289
+ ENTRY(SDL_SCANCODE_KBDILLUMTOGGLE, RDP_SCANCODE_UNKNOWN),
290
+ ENTRY(SDL_SCANCODE_KBDILLUMDOWN, RDP_SCANCODE_UNKNOWN),
291
+ ENTRY(SDL_SCANCODE_KBDILLUMUP, RDP_SCANCODE_UNKNOWN),
292
+ ENTRY(SDL_SCANCODE_EJECT, RDP_SCANCODE_UNKNOWN),
293
+ ENTRY(SDL_SCANCODE_AUDIOREWIND, RDP_SCANCODE_UNKNOWN),
294
+ ENTRY(SDL_SCANCODE_AUDIOFASTFORWARD, RDP_SCANCODE_UNKNOWN)
295
+ #endif
296
+ };
297
+
298
+ static UINT16 sdl_get_kbd_flags()
299
+ {
300
+ UINT16 flags = 0;
301
+
302
+ SDL_Keymod mod = SDL_GetModState();
303
+ if ((mod & KMOD_NUM) != 0)
304
+ flags |= KBD_SYNC_NUM_LOCK;
305
+ if ((mod & KMOD_CAPS) != 0)
306
+ flags |= KBD_SYNC_CAPS_LOCK;
307
+ #if SDL_VERSION_ATLEAST(2, 0, 18)
308
+ if ((mod & KMOD_SCROLL) != 0)
309
+ flags |= KBD_SYNC_SCROLL_LOCK;
310
+ #endif
311
+
312
+ // TODO: KBD_SYNC_KANA_LOCK
313
+
314
+ return flags;
315
+ }
316
+
317
+ BOOL sdlInput::keyboard_sync_state()
318
+ {
319
+ const auto syncFlags = sdl_get_kbd_flags();
320
+ return freerdp_input_send_synchronize_event(_sdl->context()->input, syncFlags);
321
+ }
322
+
323
+ BOOL sdlInput::keyboard_focus_in()
324
+ {
325
+ auto input = _sdl->context()->input;
326
+ WINPR_ASSERT(input);
327
+
328
+ auto syncFlags = sdl_get_kbd_flags();
329
+ freerdp_input_send_focus_in_event(input, WINPR_ASSERTING_INT_CAST(UINT16, syncFlags));
330
+
331
+ /* finish with a mouse pointer position like mstsc.exe if required */
332
+ #if 0
333
+ if (xfc->remote_app)
334
+ return;
335
+
336
+ if (XQueryPointer(xfc->display, xfc->window->handle, &w, &w, &d, &d, &x, &y, &state))
337
+ {
338
+ if ((x >= 0) && (x < xfc->window->width) && (y >= 0) && (y < xfc->window->height))
339
+ {
340
+ xf_event_adjust_coordinates(xfc, &x, &y);
341
+ freerdp_client_send_button_event(&xfc->common, FALSE, PTR_FLAGS_MOVE, x, y);
342
+ }
343
+ }
344
+ #endif
345
+ return TRUE;
346
+ }
347
+
348
+ /* This function is called to update the keyboard indicator LED */
349
+ BOOL sdlInput::keyboard_set_indicators(rdpContext* context, UINT16 led_flags)
350
+ {
351
+ WINPR_UNUSED(context);
352
+
353
+ int state = KMOD_NONE;
354
+
355
+ if ((led_flags & KBD_SYNC_NUM_LOCK) != 0)
356
+ state |= KMOD_NUM;
357
+ if ((led_flags & KBD_SYNC_CAPS_LOCK) != 0)
358
+ state |= KMOD_CAPS;
359
+ #if SDL_VERSION_ATLEAST(2, 0, 18)
360
+ if ((led_flags & KBD_SYNC_SCROLL_LOCK) != 0)
361
+ state |= KMOD_SCROLL;
362
+ #endif
363
+
364
+ // TODO: KBD_SYNC_KANA_LOCK
365
+
366
+ SDL_SetModState(static_cast<SDL_Keymod>(state));
367
+
368
+ return TRUE;
369
+ }
370
+
371
+ /* This function is called to set the IME state */
372
+ BOOL sdlInput::keyboard_set_ime_status(rdpContext* context, UINT16 imeId, UINT32 imeState,
373
+ UINT32 imeConvMode)
374
+ {
375
+ if (!context)
376
+ return FALSE;
377
+
378
+ WLog_WARN(TAG,
379
+ "KeyboardSetImeStatus(unitId=%04" PRIx16 ", imeState=%08" PRIx32
380
+ ", imeConvMode=%08" PRIx32 ") ignored",
381
+ imeId, imeState, imeConvMode);
382
+ return TRUE;
383
+ }
384
+
385
+ uint32_t sdlInput::prefToMask()
386
+ {
387
+ const std::map<std::string, uint32_t> mapping = {
388
+ { "KMOD_LSHIFT", KMOD_LSHIFT },
389
+ { "KMOD_RSHIFT", KMOD_RSHIFT },
390
+ { "KMOD_LCTRL", KMOD_LCTRL },
391
+ { "KMOD_RCTRL", KMOD_RCTRL },
392
+ { "KMOD_LALT", KMOD_LALT },
393
+ { "KMOD_RALT", KMOD_RALT },
394
+ { "KMOD_LGUI", KMOD_LGUI },
395
+ { "KMOD_RGUI", KMOD_RGUI },
396
+ { "KMOD_NUM", KMOD_NUM },
397
+ { "KMOD_CAPS", KMOD_CAPS },
398
+ { "KMOD_MODE", KMOD_MODE },
399
+ #if SDL_VERSION_ATLEAST(2, 0, 18)
400
+ { "KMOD_SCROLL", KMOD_SCROLL },
401
+ #endif
402
+ { "KMOD_CTRL", KMOD_CTRL },
403
+ { "KMOD_SHIFT", KMOD_SHIFT },
404
+ { "KMOD_ALT", KMOD_ALT },
405
+ { "KMOD_GUI", KMOD_GUI }
406
+ };
407
+ uint32_t mod = KMOD_NONE;
408
+ for (const auto& val : SdlPref::instance()->get_array("SDL_KeyModMask", { "KMOD_RSHIFT" }))
409
+ {
410
+ auto it = mapping.find(val);
411
+ if (it != mapping.end())
412
+ {
413
+ mod |= it->second;
414
+ }
415
+ }
416
+ return mod;
417
+ }
418
+
419
+ static Uint32 sdl_scancode_val(const char* scancodeName)
420
+ {
421
+ for (const auto& cur : map)
422
+ {
423
+ if (strcmp(cur.sdl_name, scancodeName) == 0)
424
+ return cur.sdl;
425
+ }
426
+
427
+ return SDL_SCANCODE_UNKNOWN;
428
+ }
429
+
430
+ static UINT32 sdl_scancode_to_rdp(Uint32 scancode)
431
+ {
432
+ UINT32 rdp = RDP_SCANCODE_UNKNOWN;
433
+
434
+ for (const auto& cur : map)
435
+ {
436
+ if (cur.sdl == scancode)
437
+ {
438
+ rdp = cur.rdp;
439
+ break;
440
+ }
441
+ }
442
+
443
+ #if defined(WITH_DEBUG_SDL_KBD_EVENTS)
444
+ auto code = static_cast<SDL_Scancode>(scancode);
445
+ WLog_DBG(TAG, "got %s [0x%08" PRIx32 "] -> [%s]", SDL_GetScancodeName(code), scancode,
446
+ freerdp_keyboard_scancode_name(rdp));
447
+ #endif
448
+ return rdp;
449
+ }
450
+
451
+ uint32_t sdlInput::prefKeyValue(const std::string& key, uint32_t fallback)
452
+ {
453
+ auto item = SdlPref::instance()->get_string(key);
454
+ if (item.empty())
455
+ return fallback;
456
+ auto val = sdl_scancode_val(item.c_str());
457
+ if (val == SDL_SCANCODE_UNKNOWN)
458
+ return fallback;
459
+ return val;
460
+ }
461
+
462
+ std::list<std::string> sdlInput::tokenize(const std::string& data, const std::string& delimiter)
463
+ {
464
+ size_t lastpos = 0;
465
+ size_t pos = 0;
466
+ std::list<std::string> list;
467
+ while ((pos = data.find(delimiter, lastpos)) != std::string::npos)
468
+ {
469
+ auto token = data.substr(lastpos, pos);
470
+ lastpos = pos + 1;
471
+ list.push_back(std::move(token));
472
+ }
473
+ auto token = data.substr(lastpos);
474
+ list.push_back(std::move(token));
475
+ return list;
476
+ }
477
+
478
+ bool sdlInput::extract(const std::string& token, uint32_t& key, uint32_t& value)
479
+ {
480
+ return freerdp_extract_key_value(token.c_str(), &key, &value);
481
+ }
482
+
483
+ BOOL sdlInput::keyboard_handle_event(const SDL_KeyboardEvent* ev)
484
+ {
485
+ WINPR_ASSERT(ev);
486
+ const UINT32 rdp_scancode = sdl_scancode_to_rdp(ev->keysym.scancode);
487
+ const SDL_Keymod mods = SDL_GetModState();
488
+
489
+ if ((mods & _hotkeyModmask) == _hotkeyModmask)
490
+ {
491
+ if (ev->type == SDL_KEYDOWN)
492
+ {
493
+ if (ev->keysym.scancode == _hotkeyFullscreen)
494
+ {
495
+ _sdl->update_fullscreen(!_sdl->fullscreen);
496
+ return TRUE;
497
+ }
498
+ if (ev->keysym.scancode == _hotkeyResizable)
499
+ {
500
+ _sdl->update_resizeable(!_sdl->resizeable);
501
+ return TRUE;
502
+ }
503
+
504
+ if (ev->keysym.scancode == _hotkeyGrab)
505
+ {
506
+ _sdl->grab_kbd_enabled = !_sdl->grab_kbd_enabled;
507
+ keyboard_grab(ev->windowID, _sdl->grab_kbd ? SDL_FALSE : SDL_TRUE);
508
+ return TRUE;
509
+ }
510
+ if (ev->keysym.scancode == _hotkeyDisconnect)
511
+ {
512
+ freerdp_abort_connect_context(_sdl->context());
513
+ return TRUE;
514
+ }
515
+ if (ev->keysym.scancode == _hotkeyMinimize)
516
+ {
517
+ _sdl->update_minimize();
518
+ return TRUE;
519
+ }
520
+ }
521
+ }
522
+
523
+ auto scancode = freerdp_keyboard_remap_key(_remapTable, rdp_scancode);
524
+ return freerdp_input_send_keyboard_event_ex(_sdl->context()->input, ev->type == SDL_KEYDOWN,
525
+ ev->repeat, scancode);
526
+ }
527
+
528
+ BOOL sdlInput::keyboard_grab(Uint32 windowID, SDL_bool enable)
529
+ {
530
+ auto it = _sdl->windows.find(windowID);
531
+ if (it == _sdl->windows.end())
532
+ return FALSE;
533
+
534
+ auto status = enable && _sdl->grab_kbd_enabled;
535
+ _sdl->grab_kbd = status;
536
+ return it->second.grabKeyboard(status);
537
+ }
538
+
539
+ BOOL sdlInput::mouse_focus(Uint32 windowID)
540
+ {
541
+ if (_lastWindowID != windowID)
542
+ {
543
+ _lastWindowID = windowID;
544
+ auto it = _sdl->windows.find(windowID);
545
+ if (it == _sdl->windows.end())
546
+ return FALSE;
547
+
548
+ it->second.raise();
549
+ }
550
+ return TRUE;
551
+ }
552
+
553
+ BOOL sdlInput::mouse_grab(Uint32 windowID, SDL_bool enable)
554
+ {
555
+ auto it = _sdl->windows.find(windowID);
556
+ if (it == _sdl->windows.end())
557
+ return FALSE;
558
+ _sdl->grab_mouse = enable;
559
+ return it->second.grabMouse(enable);
560
+ }
561
+
562
+ sdlInput::sdlInput(SdlContext* sdl)
563
+ : _sdl(sdl), _lastWindowID(UINT32_MAX), _hotkeyModmask(prefToMask())
564
+ {
565
+ auto list =
566
+ freerdp_settings_get_string(_sdl->context()->settings, FreeRDP_KeyboardRemappingList);
567
+ _remapTable = freerdp_keyboard_remap_string_to_list(list);
568
+ assert(_remapTable);
569
+ _hotkeyFullscreen = prefKeyValue("SDL_Fullscreen", SDL_SCANCODE_RETURN);
570
+ _hotkeyResizable = prefKeyValue("SDL_Resizeable", SDL_SCANCODE_R);
571
+ _hotkeyGrab = prefKeyValue("SDL_Grab", SDL_SCANCODE_G);
572
+ _hotkeyDisconnect = prefKeyValue("SDL_Disconnect", SDL_SCANCODE_D);
573
+ _hotkeyMinimize = prefKeyValue("SDL_Minimize", SDL_SCANCODE_M);
574
+ }
575
+
576
+ sdlInput::~sdlInput()
577
+ {
578
+ freerdp_keyboard_remap_free(_remapTable);
579
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_kbd.hpp ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client keyboard helper
4
+ *
5
+ * Copyright 2022 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #pragma once
21
+
22
+ #include <string>
23
+ #include <map>
24
+ #include <list>
25
+ #include <atomic>
26
+
27
+ #include <winpr/wtypes.h>
28
+ #include <freerdp/freerdp.h>
29
+ #include <freerdp/locale/keyboard.h>
30
+ #include <SDL.h>
31
+
32
+ #include "sdl_types.hpp"
33
+
34
+ class sdlInput
35
+ {
36
+ public:
37
+ explicit sdlInput(SdlContext* sdl);
38
+ sdlInput(const sdlInput& other) = delete;
39
+ sdlInput(sdlInput&& other) = delete;
40
+ ~sdlInput();
41
+
42
+ sdlInput& operator=(const sdlInput& other) = delete;
43
+ sdlInput& operator=(sdlInput&& other) = delete;
44
+
45
+ BOOL keyboard_sync_state();
46
+ BOOL keyboard_focus_in();
47
+
48
+ BOOL keyboard_handle_event(const SDL_KeyboardEvent* ev);
49
+
50
+ BOOL keyboard_grab(Uint32 windowID, SDL_bool enable);
51
+ BOOL mouse_focus(Uint32 windowID);
52
+ BOOL mouse_grab(Uint32 windowID, SDL_bool enable);
53
+
54
+ static BOOL keyboard_set_indicators(rdpContext* context, UINT16 led_flags);
55
+ static BOOL keyboard_set_ime_status(rdpContext* context, UINT16 imeId, UINT32 imeState,
56
+ UINT32 imeConvMode);
57
+
58
+ static uint32_t prefToMask();
59
+ static uint32_t prefKeyValue(const std::string& key, uint32_t fallback = SDL_SCANCODE_UNKNOWN);
60
+
61
+ private:
62
+ static std::list<std::string> tokenize(const std::string& data,
63
+ const std::string& delimiter = ",");
64
+ static bool extract(const std::string& token, uint32_t& key, uint32_t& value);
65
+
66
+ SdlContext* _sdl;
67
+ Uint32 _lastWindowID;
68
+
69
+ // hotkey handling
70
+ uint32_t _hotkeyModmask; // modifier keys mask
71
+ uint32_t _hotkeyFullscreen;
72
+ uint32_t _hotkeyResizable;
73
+ uint32_t _hotkeyGrab;
74
+ uint32_t _hotkeyDisconnect;
75
+ uint32_t _hotkeyMinimize;
76
+ FREERDP_REMAP_TABLE* _remapTable;
77
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL2/sdl_window.hpp ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ * Copyright 2023 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+ #pragma once
21
+
22
+ #include <string>
23
+ #include <SDL.h>
24
+
25
+ class SdlWindow
26
+ {
27
+ public:
28
+ SdlWindow(const std::string& title, Sint32 startupX, Sint32 startupY, Sint32 width,
29
+ Sint32 height, Uint32 flags);
30
+ SdlWindow(const SdlWindow& other) = delete;
31
+ SdlWindow(SdlWindow&& other) noexcept;
32
+ ~SdlWindow();
33
+
34
+ SdlWindow& operator=(const SdlWindow& other) = delete;
35
+ SdlWindow& operator=(SdlWindow&& other) = delete;
36
+
37
+ [[nodiscard]] Uint32 id() const;
38
+ [[nodiscard]] int displayIndex() const;
39
+ [[nodiscard]] SDL_Rect rect() const;
40
+ [[nodiscard]] SDL_Window* window() const;
41
+
42
+ [[nodiscard]] Sint32 offsetX() const;
43
+ void setOffsetX(Sint32 x);
44
+
45
+ void setOffsetY(Sint32 y);
46
+ [[nodiscard]] Sint32 offsetY() const;
47
+
48
+ bool grabKeyboard(bool enable);
49
+ bool grabMouse(bool enable);
50
+ void setBordered(bool bordered);
51
+ void raise();
52
+ void resizeable(bool use);
53
+ void fullscreen(bool enter);
54
+ void minimize();
55
+
56
+ bool fill(Uint8 r = 0x00, Uint8 g = 0x00, Uint8 b = 0x00, Uint8 a = 0xff);
57
+ bool blit(SDL_Surface* surface, SDL_Rect src, SDL_Rect& dst);
58
+ void updateSurface();
59
+
60
+ private:
61
+ SDL_Window* _window = nullptr;
62
+ Sint32 _offset_x = 0;
63
+ Sint32 _offset_y = 0;
64
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/CMakeLists.txt ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FreeRDP: A Remote Desktop Protocol Implementation
2
+ # FreeRDP SDL Client
3
+ #
4
+ # Copyright 2024 Armin Novak <[email protected]>
5
+ # Copyright 2024 Thincast Technologies GmbH
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ set(SRCS
19
+ sdl_button.hpp
20
+ sdl_button.cpp
21
+ sdl_buttons.hpp
22
+ sdl_buttons.cpp
23
+ sdl_dialogs.cpp
24
+ sdl_dialogs.hpp
25
+ sdl_widget.hpp
26
+ sdl_widget.cpp
27
+ sdl_input.hpp
28
+ sdl_input.cpp
29
+ sdl_input_widgets.cpp
30
+ sdl_input_widgets.hpp
31
+ sdl_select.hpp
32
+ sdl_select.cpp
33
+ sdl_selectlist.hpp
34
+ sdl_selectlist.cpp
35
+ sdl_connection_dialog.cpp
36
+ sdl_connection_dialog.hpp
37
+ )
38
+
39
+ list(APPEND LIBS sdl3_client_res winpr)
40
+
41
+ if(NOT WITH_SDL_LINK_SHARED)
42
+ list(APPEND LIBS ${SDL3_STATIC_LIBRARIES})
43
+ else()
44
+ list(APPEND LIBS ${SDL3_LIBRARIES})
45
+ endif()
46
+
47
+ macro(find_sdl_component name)
48
+ find_package(${name} REQUIRED)
49
+ if(WITH_SDL_LINK_SHARED)
50
+ list(APPEND LIBS ${name}::${name})
51
+ set_target_properties(${name}::${name}-shared PROPERTIES SYSTEM TRUE)
52
+ else()
53
+ list(APPEND LIBS ${name}::${name}-static)
54
+ set_target_properties(${name}::${name}-static PROPERTIES SYSTEM TRUE)
55
+ endif()
56
+
57
+ endmacro()
58
+
59
+ find_sdl_component(SDL3_ttf)
60
+
61
+ option(WITH_SDL_IMAGE_DIALOGS "Build with SDL_image support (recommended)" OFF)
62
+ if(WITH_SDL_IMAGE_DIALOGS)
63
+ find_sdl_component(SDL3_image)
64
+ add_compile_definitions(WITH_SDL_IMAGE_DIALOGS)
65
+ endif()
66
+
67
+ add_subdirectory(res)
68
+
69
+ add_library(sdl3-dialogs STATIC ${SRCS})
70
+
71
+ target_link_libraries(sdl3-dialogs PRIVATE ${LIBS})
72
+
73
+ if(BUILD_TESTING)
74
+ # add_subdirectory(test)
75
+ endif()
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/res/sdl3_resource_manager.cpp ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ *
4
+ * Copyright 2023 Armin Novak <[email protected]>
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #include "sdl3_resource_manager.hpp"
19
+ #include <iostream>
20
+ #if __has_include(<filesystem>)
21
+ #include <filesystem>
22
+ namespace fs = std::filesystem;
23
+ #elif __has_include(<experimental/filesystem>)
24
+ #include <experimental/filesystem>
25
+ namespace fs = std::experimental::filesystem;
26
+ #else
27
+ #error Could not find system header "<filesystem>" or "<experimental/filesystem>"
28
+ #endif
29
+
30
+ SDL_IOStream* SDL3ResourceManager::get(const std::string& type, const std::string& id)
31
+ {
32
+ if (useCompiledResources())
33
+ {
34
+ auto d = data(type, id);
35
+ if (!d)
36
+ return nullptr;
37
+
38
+ return SDL_IOFromConstMem(d->data(), d->size());
39
+ }
40
+
41
+ auto name = filename(type, id);
42
+ return SDL_IOFromFile(name.c_str(), "rb");
43
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/res/sdl3_resource_manager.hpp ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ *
4
+ * Copyright 2023 Armin Novak <[email protected]>
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #pragma once
19
+
20
+ #include <string>
21
+ #include <map>
22
+ #include <vector>
23
+ #include <SDL3/SDL.h>
24
+
25
+ #include <res/sdl_resource_manager.hpp>
26
+
27
+ class SDL3ResourceManager : public SDLResourceManager
28
+ {
29
+ public:
30
+ SDL3ResourceManager() = delete;
31
+ SDL3ResourceManager(const SDL3ResourceManager& other) = delete;
32
+ SDL3ResourceManager(const SDL3ResourceManager&& other) = delete;
33
+ ~SDL3ResourceManager() = delete;
34
+ SDL3ResourceManager& operator=(const SDL3ResourceManager& other) = delete;
35
+ SDL3ResourceManager& operator=(SDL3ResourceManager&& other) = delete;
36
+
37
+ static SDL_IOStream* get(const std::string& type, const std::string& id);
38
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_button.hpp ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <string>
4
+
5
+ #include "sdl_widget.hpp"
6
+
7
+ class SdlButton : public SdlWidget
8
+ {
9
+ public:
10
+ SdlButton(SDL_Renderer* renderer, std::string label, int id, const SDL_FRect& rect);
11
+ SdlButton(SdlButton&& other) noexcept;
12
+ SdlButton(const SdlButton& other) = delete;
13
+ ~SdlButton() override;
14
+
15
+ SdlButton& operator=(const SdlButton& other) = delete;
16
+ SdlButton& operator=(SdlButton&& other) = delete;
17
+
18
+ bool highlight(SDL_Renderer* renderer);
19
+ bool mouseover(SDL_Renderer* renderer);
20
+ bool update(SDL_Renderer* renderer);
21
+
22
+ [[nodiscard]] int id() const;
23
+
24
+ private:
25
+ std::string _name;
26
+ int _id;
27
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_buttons.cpp ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <cassert>
2
+ #include <algorithm>
3
+
4
+ #include "sdl_buttons.hpp"
5
+
6
+ static const Uint32 hpadding = 10;
7
+
8
+ SdlButtonList::~SdlButtonList() = default;
9
+
10
+ bool SdlButtonList::populate(SDL_Renderer* renderer, const std::vector<std::string>& labels,
11
+ const std::vector<int>& ids, Sint32 total_width, Sint32 offsetY,
12
+ Sint32 width, Sint32 height)
13
+ {
14
+ assert(renderer);
15
+ assert(width >= 0);
16
+ assert(height >= 0);
17
+ assert(labels.size() == ids.size());
18
+
19
+ _list.clear();
20
+ size_t button_width = ids.size() * (static_cast<size_t>(width) + hpadding) + hpadding;
21
+ size_t offsetX = static_cast<size_t>(total_width) -
22
+ std::min<size_t>(static_cast<size_t>(total_width), button_width);
23
+ for (size_t x = 0; x < ids.size(); x++)
24
+ {
25
+ const size_t curOffsetX = offsetX + x * (static_cast<size_t>(width) + hpadding);
26
+ const SDL_FRect rect = { static_cast<float>(curOffsetX), static_cast<float>(offsetY),
27
+ static_cast<float>(width), static_cast<float>(height) };
28
+ _list.emplace_back(renderer, labels[x], ids[x], rect);
29
+ }
30
+ return true;
31
+ }
32
+
33
+ SdlButton* SdlButtonList::get_selected(const SDL_MouseButtonEvent& button)
34
+ {
35
+ const auto x = button.x;
36
+ const auto y = button.y;
37
+
38
+ return get_selected(x, y);
39
+ }
40
+
41
+ SdlButton* SdlButtonList::get_selected(float x, float y)
42
+ {
43
+ for (auto& btn : _list)
44
+ {
45
+ auto r = btn.rect();
46
+ if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
47
+ return &btn;
48
+ }
49
+ return nullptr;
50
+ }
51
+
52
+ bool SdlButtonList::set_highlight_next(bool reset)
53
+ {
54
+ if (reset)
55
+ _highlighted = nullptr;
56
+ else
57
+ {
58
+ auto next = _highlight_index++;
59
+ _highlight_index %= _list.size();
60
+ auto& element = _list[next];
61
+ _highlighted = &element;
62
+ }
63
+ return true;
64
+ }
65
+
66
+ bool SdlButtonList::set_highlight(size_t index)
67
+ {
68
+ if (index >= _list.size())
69
+ {
70
+ _highlighted = nullptr;
71
+ return false;
72
+ }
73
+ auto& element = _list[index];
74
+ _highlighted = &element;
75
+ _highlight_index = ++index % _list.size();
76
+ return true;
77
+ }
78
+
79
+ bool SdlButtonList::set_mouseover(float x, float y)
80
+ {
81
+ _mouseover = get_selected(x, y);
82
+ return _mouseover != nullptr;
83
+ }
84
+
85
+ void SdlButtonList::clear()
86
+ {
87
+ _list.clear();
88
+ _mouseover = nullptr;
89
+ _highlighted = nullptr;
90
+ _highlight_index = 0;
91
+ }
92
+
93
+ bool SdlButtonList::update(SDL_Renderer* renderer)
94
+ {
95
+ assert(renderer);
96
+
97
+ for (auto& btn : _list)
98
+ {
99
+ if (!btn.update(renderer))
100
+ return false;
101
+ }
102
+
103
+ if (_highlighted)
104
+ _highlighted->highlight(renderer);
105
+
106
+ if (_mouseover)
107
+ _mouseover->mouseover(renderer);
108
+ return true;
109
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_buttons.hpp ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <vector>
4
+ #include <cstdint>
5
+
6
+ #include "sdl_button.hpp"
7
+
8
+ class SdlButtonList
9
+ {
10
+ public:
11
+ SdlButtonList() = default;
12
+ SdlButtonList(const SdlButtonList& other) = delete;
13
+ SdlButtonList(SdlButtonList&& other) = delete;
14
+ virtual ~SdlButtonList();
15
+
16
+ SdlButtonList& operator=(const SdlButtonList& other) = delete;
17
+ SdlButtonList& operator=(SdlButtonList&& other) = delete;
18
+
19
+ bool populate(SDL_Renderer* renderer, const std::vector<std::string>& labels,
20
+ const std::vector<int>& ids, Sint32 total_width, Sint32 offsetY, Sint32 width,
21
+ Sint32 height);
22
+
23
+ bool update(SDL_Renderer* renderer);
24
+ SdlButton* get_selected(const SDL_MouseButtonEvent& button);
25
+ SdlButton* get_selected(float x, float y);
26
+
27
+ bool set_highlight_next(bool reset = false);
28
+ bool set_highlight(size_t index);
29
+ bool set_mouseover(float x, float y);
30
+
31
+ void clear();
32
+
33
+ private:
34
+ std::vector<SdlButton> _list;
35
+ SdlButton* _highlighted = nullptr;
36
+ size_t _highlight_index = 0;
37
+ SdlButton* _mouseover = nullptr;
38
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_connection_dialog.cpp ADDED
@@ -0,0 +1,537 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client helper dialogs
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ #include <cassert>
20
+ #include <thread>
21
+
22
+ #include "sdl_connection_dialog.hpp"
23
+ #include "../sdl_utils.hpp"
24
+ #include "../sdl_freerdp.hpp"
25
+ #include "res/sdl3_resource_manager.hpp"
26
+
27
+ static const SDL_Color backgroundcolor = { 0x38, 0x36, 0x35, 0xff };
28
+ static const SDL_Color textcolor = { 0xd1, 0xcf, 0xcd, 0xff };
29
+ static const SDL_Color infocolor = { 0x43, 0xe0, 0x0f, 0x60 };
30
+ static const SDL_Color warncolor = { 0xcd, 0xca, 0x35, 0x60 };
31
+ static const SDL_Color errorcolor = { 0xf7, 0x22, 0x30, 0x60 };
32
+
33
+ static const Uint32 vpadding = 5;
34
+ static const Uint32 hpadding = 5;
35
+
36
+ SDLConnectionDialog::SDLConnectionDialog(rdpContext* context) : _context(context)
37
+ {
38
+ SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO);
39
+ hide();
40
+ }
41
+
42
+ SDLConnectionDialog::~SDLConnectionDialog()
43
+ {
44
+ resetTimer();
45
+ destroyWindow();
46
+ SDL_Quit();
47
+ }
48
+
49
+ bool SDLConnectionDialog::visible() const
50
+ {
51
+ if (!_window || !_renderer)
52
+ return false;
53
+
54
+ auto flags = SDL_GetWindowFlags(_window);
55
+ return (flags & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) == 0;
56
+ }
57
+
58
+ bool SDLConnectionDialog::setTitle(const char* fmt, ...)
59
+ {
60
+ std::lock_guard lock(_mux);
61
+ va_list ap = {};
62
+ va_start(ap, fmt);
63
+ _title = print(fmt, ap);
64
+ va_end(ap);
65
+
66
+ return show(MSG_NONE);
67
+ }
68
+
69
+ bool SDLConnectionDialog::showInfo(const char* fmt, ...)
70
+ {
71
+ va_list ap = {};
72
+ va_start(ap, fmt);
73
+ auto rc = show(MSG_INFO, fmt, ap);
74
+ va_end(ap);
75
+ return rc;
76
+ }
77
+
78
+ bool SDLConnectionDialog::showWarn(const char* fmt, ...)
79
+ {
80
+ va_list ap = {};
81
+ va_start(ap, fmt);
82
+ auto rc = show(MSG_WARN, fmt, ap);
83
+ va_end(ap);
84
+ return rc;
85
+ }
86
+
87
+ bool SDLConnectionDialog::showError(const char* fmt, ...)
88
+ {
89
+ va_list ap = {};
90
+ va_start(ap, fmt);
91
+ auto rc = show(MSG_ERROR, fmt, ap);
92
+ va_end(ap);
93
+ if (!rc)
94
+ return rc;
95
+ return setTimer();
96
+ }
97
+
98
+ bool SDLConnectionDialog::show()
99
+ {
100
+ std::lock_guard lock(_mux);
101
+ return show(_type_active);
102
+ }
103
+
104
+ bool SDLConnectionDialog::hide()
105
+ {
106
+ std::lock_guard lock(_mux);
107
+ return show(MSG_DISCARD);
108
+ }
109
+
110
+ bool SDLConnectionDialog::running() const
111
+ {
112
+ std::lock_guard lock(_mux);
113
+ return _running;
114
+ }
115
+
116
+ bool SDLConnectionDialog::update()
117
+ {
118
+ std::lock_guard lock(_mux);
119
+ switch (_type)
120
+ {
121
+ case MSG_INFO:
122
+ case MSG_WARN:
123
+ case MSG_ERROR:
124
+ _type_active = _type;
125
+ createWindow();
126
+ break;
127
+ case MSG_DISCARD:
128
+ resetTimer();
129
+ destroyWindow();
130
+ break;
131
+ default:
132
+ if (_window)
133
+ {
134
+ SDL_SetWindowTitle(_window, _title.c_str());
135
+ }
136
+ break;
137
+ }
138
+ _type = MSG_NONE;
139
+ return true;
140
+ }
141
+
142
+ bool SDLConnectionDialog::setModal()
143
+ {
144
+ if (_window)
145
+ {
146
+ auto sdl = get_context(_context);
147
+ if (sdl->windows.empty())
148
+ return true;
149
+
150
+ auto parent = sdl->windows.begin()->second.window();
151
+ SDL_SetWindowParent(_window, parent);
152
+ SDL_SetWindowModal(_window, true);
153
+ SDL_RaiseWindow(_window);
154
+ }
155
+ return true;
156
+ }
157
+
158
+ bool SDLConnectionDialog::clearWindow(SDL_Renderer* renderer)
159
+ {
160
+ assert(renderer);
161
+
162
+ const int drc = SDL_SetRenderDrawColor(renderer, backgroundcolor.r, backgroundcolor.g,
163
+ backgroundcolor.b, backgroundcolor.a);
164
+ if (widget_log_error(drc, "SDL_SetRenderDrawColor"))
165
+ return false;
166
+
167
+ const int rcls = SDL_RenderClear(renderer);
168
+ return !widget_log_error(rcls, "SDL_RenderClear");
169
+ }
170
+
171
+ bool SDLConnectionDialog::update(SDL_Renderer* renderer)
172
+ {
173
+ std::lock_guard lock(_mux);
174
+ if (!renderer)
175
+ return false;
176
+
177
+ if (!clearWindow(renderer))
178
+ return false;
179
+
180
+ for (auto& btn : _list)
181
+ {
182
+ if (!btn.widget.update_text(renderer, _msg, btn.fgcolor, btn.bgcolor))
183
+ return false;
184
+ }
185
+
186
+ if (!_buttons.update(renderer))
187
+ return false;
188
+
189
+ SDL_RenderPresent(renderer);
190
+ return true;
191
+ }
192
+
193
+ bool SDLConnectionDialog::wait(bool ignoreRdpContext)
194
+ {
195
+ while (running())
196
+ {
197
+ if (!ignoreRdpContext)
198
+ {
199
+ if (freerdp_shall_disconnect_context(_context))
200
+ return false;
201
+ }
202
+ std::this_thread::yield();
203
+ }
204
+ return true;
205
+ }
206
+
207
+ bool SDLConnectionDialog::handle(const SDL_Event& event)
208
+ {
209
+ Uint32 windowID = 0;
210
+ if (_window)
211
+ {
212
+ windowID = SDL_GetWindowID(_window);
213
+ }
214
+
215
+ switch (event.type)
216
+ {
217
+ case SDL_EVENT_USER_RETRY_DIALOG:
218
+ return update();
219
+ case SDL_EVENT_QUIT:
220
+ resetTimer();
221
+ destroyWindow();
222
+ return false;
223
+ case SDL_EVENT_KEY_DOWN:
224
+ case SDL_EVENT_KEY_UP:
225
+ if (visible())
226
+ {
227
+ auto& ev = reinterpret_cast<const SDL_KeyboardEvent&>(event);
228
+ update(_renderer);
229
+ switch (event.key.key)
230
+ {
231
+ case SDLK_RETURN:
232
+ case SDLK_RETURN2:
233
+ case SDLK_ESCAPE:
234
+ case SDLK_KP_ENTER:
235
+ if (event.type == SDL_EVENT_KEY_UP)
236
+ {
237
+ freerdp_abort_event(_context);
238
+ sdl_push_quit();
239
+ }
240
+ break;
241
+ case SDLK_TAB:
242
+ _buttons.set_highlight_next();
243
+ break;
244
+ default:
245
+ break;
246
+ }
247
+
248
+ return windowID == ev.windowID;
249
+ }
250
+ return false;
251
+ case SDL_EVENT_MOUSE_MOTION:
252
+ if (visible())
253
+ {
254
+ auto& ev = reinterpret_cast<const SDL_MouseMotionEvent&>(event);
255
+
256
+ _buttons.set_mouseover(event.button.x, event.button.y);
257
+ update(_renderer);
258
+ return windowID == ev.windowID;
259
+ }
260
+ return false;
261
+ case SDL_EVENT_MOUSE_BUTTON_DOWN:
262
+ case SDL_EVENT_MOUSE_BUTTON_UP:
263
+ if (visible())
264
+ {
265
+ auto& ev = reinterpret_cast<const SDL_MouseButtonEvent&>(event);
266
+ update(_renderer);
267
+
268
+ auto button = _buttons.get_selected(event.button);
269
+ if (button)
270
+ {
271
+ if (event.type == SDL_EVENT_MOUSE_BUTTON_UP)
272
+ {
273
+ freerdp_abort_event(_context);
274
+ sdl_push_quit();
275
+ }
276
+ }
277
+
278
+ return windowID == ev.windowID;
279
+ }
280
+ return false;
281
+ case SDL_EVENT_MOUSE_WHEEL:
282
+ if (visible())
283
+ {
284
+ auto& ev = reinterpret_cast<const SDL_MouseWheelEvent&>(event);
285
+ update(_renderer);
286
+ return windowID == ev.windowID;
287
+ }
288
+ return false;
289
+ case SDL_EVENT_FINGER_UP:
290
+ case SDL_EVENT_FINGER_DOWN:
291
+ if (visible())
292
+ {
293
+ auto& ev = reinterpret_cast<const SDL_TouchFingerEvent&>(event);
294
+ update(_renderer);
295
+ return windowID == ev.windowID;
296
+ }
297
+ return false;
298
+ default:
299
+ if ((event.type >= SDL_EVENT_WINDOW_FIRST) && (event.type <= SDL_EVENT_WINDOW_LAST))
300
+ {
301
+ auto& ev = reinterpret_cast<const SDL_WindowEvent&>(event);
302
+ switch (ev.type)
303
+ {
304
+ case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
305
+ if (windowID == ev.windowID)
306
+ {
307
+ freerdp_abort_event(_context);
308
+ sdl_push_quit();
309
+ }
310
+ break;
311
+ default:
312
+ update(_renderer);
313
+ setModal();
314
+ break;
315
+ }
316
+
317
+ return windowID == ev.windowID;
318
+ }
319
+ return false;
320
+ }
321
+ }
322
+
323
+ bool SDLConnectionDialog::createWindow()
324
+ {
325
+ destroyWindow();
326
+
327
+ const int widget_height = 50;
328
+ const int widget_width = 600;
329
+ const int total_height = 300;
330
+
331
+ auto rc = SDL_CreateWindowAndRenderer(_title.c_str(), widget_width, total_height,
332
+ SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_MOUSE_FOCUS |
333
+ SDL_WINDOW_INPUT_FOCUS,
334
+ &_window, &_renderer);
335
+ if (rc != 0)
336
+ {
337
+ widget_log_error(rc, "SDL_CreateWindowAndRenderer");
338
+ return false;
339
+ }
340
+ setModal();
341
+
342
+ SDL_Color res_bgcolor;
343
+ switch (_type_active)
344
+ {
345
+ case MSG_INFO:
346
+ res_bgcolor = infocolor;
347
+ break;
348
+ case MSG_WARN:
349
+ res_bgcolor = warncolor;
350
+ break;
351
+ case MSG_ERROR:
352
+ res_bgcolor = errorcolor;
353
+ break;
354
+ case MSG_DISCARD:
355
+ default:
356
+ res_bgcolor = backgroundcolor;
357
+ break;
358
+ }
359
+
360
+ #if defined(WITH_SDL_IMAGE_DIALOGS)
361
+ std::string res_name;
362
+ switch (_type_active)
363
+ {
364
+ case MSG_INFO:
365
+ res_name = "icon_info.svg";
366
+ break;
367
+ case MSG_WARN:
368
+ res_name = "icon_warning.svg";
369
+ break;
370
+ case MSG_ERROR:
371
+ res_name = "icon_error.svg";
372
+ break;
373
+ case MSG_DISCARD:
374
+ default:
375
+ res_name = "";
376
+ break;
377
+ }
378
+
379
+ int height = (total_height - 3ul * vpadding) / 2ul;
380
+ SDL_FRect iconRect{ hpadding, vpadding, widget_width / 4ul - 2ul * hpadding,
381
+ static_cast<float>(height) };
382
+ widget_cfg_t icon{ textcolor,
383
+ res_bgcolor,
384
+ { _renderer, iconRect,
385
+ SDL3ResourceManager::get(SDLResourceManager::typeImages(), res_name) } };
386
+ _list.emplace_back(std::move(icon));
387
+
388
+ iconRect.y += static_cast<float>(height);
389
+
390
+ widget_cfg_t logo{ textcolor,
391
+ backgroundcolor,
392
+ { _renderer, iconRect,
393
+ SDL3ResourceManager::get(SDLResourceManager::typeImages(),
394
+ "FreeRDP_Icon.svg") } };
395
+ _list.emplace_back(std::move(logo));
396
+
397
+ SDL_FRect rect = { widget_width / 4ul, vpadding, widget_width * 3ul / 4ul,
398
+ total_height - 3ul * vpadding - widget_height };
399
+ #else
400
+ SDL_FRect rect = { hpadding, vpadding, widget_width - 2ul * hpadding,
401
+ total_height - 2ul * vpadding };
402
+ #endif
403
+
404
+ widget_cfg_t w{ textcolor, backgroundcolor, { _renderer, rect, false } };
405
+ w.widget.set_wrap(true, widget_width);
406
+ _list.emplace_back(std::move(w));
407
+ rect.y += widget_height + vpadding;
408
+
409
+ const std::vector<int> buttonids = { 1 };
410
+ const std::vector<std::string> buttonlabels = { "cancel" };
411
+ _buttons.populate(_renderer, buttonlabels, buttonids, widget_width,
412
+ total_height - widget_height - vpadding,
413
+ static_cast<Sint32>(widget_width / 2), static_cast<Sint32>(widget_height));
414
+ _buttons.set_highlight(0);
415
+
416
+ SDL_ShowWindow(_window);
417
+ SDL_RaiseWindow(_window);
418
+
419
+ return true;
420
+ }
421
+
422
+ void SDLConnectionDialog::destroyWindow()
423
+ {
424
+ _buttons.clear();
425
+ _list.clear();
426
+ SDL_DestroyRenderer(_renderer);
427
+ SDL_DestroyWindow(_window);
428
+ _renderer = nullptr;
429
+ _window = nullptr;
430
+ }
431
+
432
+ bool SDLConnectionDialog::show(MsgType type, const char* fmt, va_list ap)
433
+ {
434
+ std::lock_guard lock(_mux);
435
+ _msg = print(fmt, ap);
436
+ return show(type);
437
+ }
438
+
439
+ bool SDLConnectionDialog::show(MsgType type)
440
+ {
441
+ _type = type;
442
+ return sdl_push_user_event(SDL_EVENT_USER_RETRY_DIALOG);
443
+ }
444
+
445
+ std::string SDLConnectionDialog::print(const char* fmt, va_list ap)
446
+ {
447
+ int size = -1;
448
+ std::string res;
449
+
450
+ do
451
+ {
452
+ res.resize(128);
453
+ if (size > 0)
454
+ res.resize(WINPR_ASSERTING_INT_CAST(uint32_t, size));
455
+
456
+ va_list copy;
457
+ va_copy(copy, ap);
458
+ WINPR_PRAGMA_DIAG_PUSH
459
+ WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
460
+ size = vsnprintf(res.data(), res.size(), fmt, copy);
461
+ WINPR_PRAGMA_DIAG_POP
462
+ va_end(copy);
463
+
464
+ } while ((size > 0) && (static_cast<size_t>(size) > res.size()));
465
+
466
+ return res;
467
+ }
468
+
469
+ bool SDLConnectionDialog::setTimer(Uint32 timeoutMS)
470
+ {
471
+ std::lock_guard lock(_mux);
472
+ resetTimer();
473
+
474
+ _timer = SDL_AddTimer(timeoutMS, &SDLConnectionDialog::timeout, this);
475
+ _running = true;
476
+ return true;
477
+ }
478
+
479
+ void SDLConnectionDialog::resetTimer()
480
+ {
481
+ if (_running)
482
+ SDL_RemoveTimer(_timer);
483
+ _running = false;
484
+ }
485
+
486
+ Uint32 SDLConnectionDialog::timeout(void* pvthis, SDL_TimerID timerID, Uint32 intervalMS)
487
+ {
488
+ auto self = static_cast<SDLConnectionDialog*>(pvthis);
489
+ self->hide();
490
+ self->_running = false;
491
+ return 0;
492
+ }
493
+
494
+ SDLConnectionDialogHider::SDLConnectionDialogHider(freerdp* instance)
495
+ : SDLConnectionDialogHider(get(instance))
496
+ {
497
+ }
498
+
499
+ SDLConnectionDialogHider::SDLConnectionDialogHider(rdpContext* context)
500
+ : SDLConnectionDialogHider(get(context))
501
+ {
502
+ }
503
+
504
+ SDLConnectionDialogHider::SDLConnectionDialogHider(SDLConnectionDialog* dialog) : _dialog(dialog)
505
+ {
506
+ if (_dialog)
507
+ {
508
+ _visible = _dialog->visible();
509
+ if (_visible)
510
+ {
511
+ _dialog->hide();
512
+ }
513
+ }
514
+ }
515
+
516
+ SDLConnectionDialogHider::~SDLConnectionDialogHider()
517
+ {
518
+ if (_dialog && _visible)
519
+ {
520
+ _dialog->show();
521
+ }
522
+ }
523
+
524
+ SDLConnectionDialog* SDLConnectionDialogHider::get(freerdp* instance)
525
+ {
526
+ if (!instance)
527
+ return nullptr;
528
+ return get(instance->context);
529
+ }
530
+
531
+ SDLConnectionDialog* SDLConnectionDialogHider::get(rdpContext* context)
532
+ {
533
+ auto sdl = get_context(context);
534
+ if (!sdl)
535
+ return nullptr;
536
+ return sdl->connection_dialog.get();
537
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_connection_dialog.hpp ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client helper dialogs
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #pragma once
21
+
22
+ #include <memory>
23
+ #include <mutex>
24
+ #include <string>
25
+ #include <vector>
26
+
27
+ #include <SDL3/SDL.h>
28
+
29
+ #include <freerdp/freerdp.h>
30
+
31
+ #include "sdl_widget.hpp"
32
+ #include "sdl_buttons.hpp"
33
+
34
+ class SDLConnectionDialog
35
+ {
36
+ public:
37
+ explicit SDLConnectionDialog(rdpContext* context);
38
+ SDLConnectionDialog(const SDLConnectionDialog& other) = delete;
39
+ SDLConnectionDialog(const SDLConnectionDialog&& other) = delete;
40
+ virtual ~SDLConnectionDialog();
41
+
42
+ SDLConnectionDialog& operator=(const SDLConnectionDialog& other) = delete;
43
+ SDLConnectionDialog& operator=(SDLConnectionDialog& other) = delete;
44
+
45
+ bool visible() const;
46
+
47
+ bool setTitle(const char* fmt, ...);
48
+ bool showInfo(const char* fmt, ...);
49
+ bool showWarn(const char* fmt, ...);
50
+ bool showError(const char* fmt, ...);
51
+
52
+ bool show();
53
+ bool hide();
54
+
55
+ bool running() const;
56
+ bool wait(bool ignoreRdpContextQuit = false);
57
+
58
+ bool handle(const SDL_Event& event);
59
+
60
+ private:
61
+ enum MsgType
62
+ {
63
+ MSG_NONE,
64
+ MSG_INFO,
65
+ MSG_WARN,
66
+ MSG_ERROR,
67
+ MSG_DISCARD
68
+ };
69
+
70
+ bool createWindow();
71
+ void destroyWindow();
72
+
73
+ bool update();
74
+
75
+ bool setModal();
76
+
77
+ static bool clearWindow(SDL_Renderer* renderer);
78
+
79
+ bool update(SDL_Renderer* renderer);
80
+
81
+ bool show(MsgType type, const char* fmt, va_list ap);
82
+ bool show(MsgType type);
83
+
84
+ static std::string print(const char* fmt, va_list ap);
85
+ bool setTimer(Uint32 timeoutMS = 15000);
86
+ void resetTimer();
87
+
88
+ static Uint32 timeout(void* pvthis, SDL_TimerID timerID, Uint32 intervalMS);
89
+
90
+ struct widget_cfg_t
91
+ {
92
+ SDL_Color fgcolor = {};
93
+ SDL_Color bgcolor = {};
94
+ SdlWidget widget;
95
+ };
96
+
97
+ rdpContext* _context = nullptr;
98
+ SDL_Window* _window = nullptr;
99
+ SDL_Renderer* _renderer = nullptr;
100
+ mutable std::mutex _mux;
101
+ std::string _title;
102
+ std::string _msg;
103
+ MsgType _type = MSG_NONE;
104
+ MsgType _type_active = MSG_NONE;
105
+ SDL_TimerID _timer = 0;
106
+ bool _running = false;
107
+ std::vector<widget_cfg_t> _list;
108
+ SdlButtonList _buttons;
109
+ };
110
+
111
+ class SDLConnectionDialogHider
112
+ {
113
+ public:
114
+ explicit SDLConnectionDialogHider(freerdp* instance);
115
+ explicit SDLConnectionDialogHider(rdpContext* context);
116
+
117
+ explicit SDLConnectionDialogHider(SDLConnectionDialog* dialog);
118
+
119
+ SDLConnectionDialogHider(const SDLConnectionDialogHider& other) = delete;
120
+ SDLConnectionDialogHider(SDLConnectionDialogHider&& other) = delete;
121
+ SDLConnectionDialogHider& operator=(const SDLConnectionDialogHider& other) = delete;
122
+ SDLConnectionDialogHider& operator=(SDLConnectionDialogHider& other) = delete;
123
+
124
+ ~SDLConnectionDialogHider();
125
+
126
+ private:
127
+ SDLConnectionDialog* get(freerdp* instance);
128
+ static SDLConnectionDialog* get(rdpContext* context);
129
+
130
+ SDLConnectionDialog* _dialog = nullptr;
131
+ bool _visible = false;
132
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_dialogs.cpp ADDED
@@ -0,0 +1,620 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client helper dialogs
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <vector>
21
+ #include <string>
22
+ #include <cassert>
23
+
24
+ #include <freerdp/log.h>
25
+ #include <freerdp/utils/smartcardlogon.h>
26
+
27
+ #include <SDL3/SDL.h>
28
+
29
+ #include "../sdl_freerdp.hpp"
30
+ #include "sdl_dialogs.hpp"
31
+ #include "sdl_input.hpp"
32
+ #include "sdl_input_widgets.hpp"
33
+ #include "sdl_select.hpp"
34
+ #include "sdl_selectlist.hpp"
35
+
36
+ enum
37
+ {
38
+ SHOW_DIALOG_ACCEPT_REJECT = 1,
39
+ SHOW_DIALOG_TIMED_ACCEPT = 2
40
+ };
41
+
42
+ static const char* type_str_for_flags(UINT32 flags)
43
+ {
44
+ const char* type = "RDP-Server";
45
+
46
+ if (flags & VERIFY_CERT_FLAG_GATEWAY)
47
+ type = "RDP-Gateway";
48
+
49
+ if (flags & VERIFY_CERT_FLAG_REDIRECT)
50
+ type = "RDP-Redirect";
51
+ return type;
52
+ }
53
+
54
+ static BOOL sdl_wait_for_result(rdpContext* context, Uint32 type, SDL_Event* result)
55
+ {
56
+ const SDL_Event empty = {};
57
+
58
+ WINPR_ASSERT(context);
59
+ WINPR_ASSERT(result);
60
+
61
+ while (!freerdp_shall_disconnect_context(context))
62
+ {
63
+ *result = empty;
64
+ const int rc = SDL_PeepEvents(result, 1, SDL_GETEVENT, type, type);
65
+ if (rc > 0)
66
+ return TRUE;
67
+ Sleep(1);
68
+ }
69
+ return FALSE;
70
+ }
71
+
72
+ static int sdl_show_dialog(rdpContext* context, const char* title, const char* message,
73
+ Sint32 flags)
74
+ {
75
+ SDL_Event event = {};
76
+
77
+ if (!sdl_push_user_event(SDL_EVENT_USER_SHOW_DIALOG, title, message, flags))
78
+ return 0;
79
+
80
+ if (!sdl_wait_for_result(context, SDL_EVENT_USER_SHOW_RESULT, &event))
81
+ return 0;
82
+
83
+ return event.user.code;
84
+ }
85
+
86
+ BOOL sdl_authenticate_ex(freerdp* instance, char** username, char** password, char** domain,
87
+ rdp_auth_reason reason)
88
+ {
89
+ SDL_Event event = {};
90
+ BOOL res = FALSE;
91
+
92
+ SDLConnectionDialogHider hider(instance);
93
+
94
+ const char* target = freerdp_settings_get_server_name(instance->context->settings);
95
+ switch (reason)
96
+ {
97
+ case AUTH_NLA:
98
+ break;
99
+
100
+ case AUTH_TLS:
101
+ case AUTH_RDP:
102
+ case AUTH_SMARTCARD_PIN: /* in this case password is pin code */
103
+ if ((*username) && (*password))
104
+ return TRUE;
105
+ break;
106
+ case GW_AUTH_HTTP:
107
+ case GW_AUTH_RDG:
108
+ case GW_AUTH_RPC:
109
+ target =
110
+ freerdp_settings_get_string(instance->context->settings, FreeRDP_GatewayHostname);
111
+ break;
112
+ default:
113
+ break;
114
+ }
115
+
116
+ char* title = nullptr;
117
+ size_t titlesize = 0;
118
+ winpr_asprintf(&title, &titlesize, "Credentials required for %s", target);
119
+
120
+ std::unique_ptr<char, decltype(&free)> guard(title, free);
121
+ char* u = nullptr;
122
+ char* d = nullptr;
123
+ char* p = nullptr;
124
+
125
+ assert(username);
126
+ assert(domain);
127
+ assert(password);
128
+
129
+ u = *username;
130
+ d = *domain;
131
+ p = *password;
132
+
133
+ if (!sdl_push_user_event(SDL_EVENT_USER_AUTH_DIALOG, title, u, d, p, reason))
134
+ return res;
135
+
136
+ if (!sdl_wait_for_result(instance->context, SDL_EVENT_USER_AUTH_RESULT, &event))
137
+ return res;
138
+
139
+ auto arg = reinterpret_cast<SDL_UserAuthArg*>(event.padding);
140
+
141
+ res = arg->result > 0;
142
+
143
+ free(*username);
144
+ free(*domain);
145
+ free(*password);
146
+ *username = arg->user;
147
+ *domain = arg->domain;
148
+ *password = arg->password;
149
+
150
+ return res;
151
+ }
152
+
153
+ BOOL sdl_choose_smartcard(freerdp* instance, SmartcardCertInfo** cert_list, DWORD count,
154
+ DWORD* choice, BOOL gateway)
155
+ {
156
+ BOOL res = FALSE;
157
+
158
+ WINPR_ASSERT(instance);
159
+ WINPR_ASSERT(cert_list);
160
+ WINPR_ASSERT(choice);
161
+
162
+ SDLConnectionDialogHider hider(instance);
163
+ std::vector<std::string> strlist;
164
+ std::vector<const char*> list;
165
+ for (DWORD i = 0; i < count; i++)
166
+ {
167
+ const SmartcardCertInfo* cert = cert_list[i];
168
+ char* reader = ConvertWCharToUtf8Alloc(cert->reader, nullptr);
169
+ char* container_name = ConvertWCharToUtf8Alloc(cert->containerName, nullptr);
170
+
171
+ char* msg = nullptr;
172
+ size_t len = 0;
173
+
174
+ winpr_asprintf(&msg, &len,
175
+ "%s\n\tReader: %s\n\tUser: %s@%s\n\tSubject: %s\n\tIssuer: %s\n\tUPN: %s",
176
+ container_name, reader, cert->userHint, cert->domainHint, cert->subject,
177
+ cert->issuer, cert->upn);
178
+
179
+ strlist.emplace_back(msg);
180
+ free(msg);
181
+ free(reader);
182
+ free(container_name);
183
+
184
+ auto& m = strlist.back();
185
+ list.push_back(m.c_str());
186
+ }
187
+
188
+ SDL_Event event = {};
189
+ const char* title = "Select a logon smartcard certificate";
190
+ if (gateway)
191
+ title = "Select a gateway logon smartcard certificate";
192
+ if (!sdl_push_user_event(SDL_EVENT_USER_SCARD_DIALOG, title, list.data(), count))
193
+ return res;
194
+
195
+ if (!sdl_wait_for_result(instance->context, SDL_EVENT_USER_SCARD_RESULT, &event))
196
+ return res;
197
+
198
+ res = (event.user.code >= 0);
199
+ *choice = static_cast<DWORD>(event.user.code);
200
+
201
+ return res;
202
+ }
203
+
204
+ SSIZE_T sdl_retry_dialog(freerdp* instance, const char* what, size_t current, void* userarg)
205
+ {
206
+ WINPR_ASSERT(instance);
207
+ WINPR_ASSERT(instance->context);
208
+ WINPR_ASSERT(what);
209
+
210
+ auto sdl = get_context(instance->context);
211
+ auto settings = instance->context->settings;
212
+ const BOOL enabled = freerdp_settings_get_bool(settings, FreeRDP_AutoReconnectionEnabled);
213
+ const size_t delay = freerdp_settings_get_uint32(settings, FreeRDP_TcpConnectTimeout);
214
+ std::lock_guard<CriticalSection> lock(sdl->critical);
215
+ if (!sdl->connection_dialog)
216
+ return WINPR_ASSERTING_INT_CAST(ssize_t, delay);
217
+
218
+ sdl->connection_dialog->setTitle("Retry connection to %s",
219
+ freerdp_settings_get_server_name(instance->context->settings));
220
+
221
+ if ((strcmp(what, "arm-transport") != 0) && (strcmp(what, "connection") != 0))
222
+ {
223
+ sdl->connection_dialog->showError("Unknown module %s, aborting", what);
224
+ return -1;
225
+ }
226
+
227
+ if (current == 0)
228
+ {
229
+ if (strcmp(what, "arm-transport") == 0)
230
+ sdl->connection_dialog->showWarn("[%s] Starting your VM. It may take up to 5 minutes",
231
+ what);
232
+ }
233
+
234
+ if (!enabled)
235
+ {
236
+ sdl->connection_dialog->showError(
237
+ "Automatic reconnection disabled, terminating. Try to connect again later");
238
+ return -1;
239
+ }
240
+
241
+ const size_t max = freerdp_settings_get_uint32(settings, FreeRDP_AutoReconnectMaxRetries);
242
+ if (current >= max)
243
+ {
244
+ sdl->connection_dialog->showError(
245
+ "[%s] retries exceeded. Your VM failed to start. Try again later or contact your "
246
+ "tech support for help if this keeps happening.",
247
+ what);
248
+ return -1;
249
+ }
250
+
251
+ sdl->connection_dialog->showInfo("[%s] retry %" PRIuz "/%" PRIuz ", delaying %" PRIuz
252
+ "ms before next attempt",
253
+ what, current, max, delay);
254
+ return WINPR_ASSERTING_INT_CAST(ssize_t, delay);
255
+ }
256
+
257
+ BOOL sdl_present_gateway_message(freerdp* instance, UINT32 type, BOOL isDisplayMandatory,
258
+ BOOL isConsentMandatory, size_t length, const WCHAR* wmessage)
259
+ {
260
+ if (!isDisplayMandatory)
261
+ return TRUE;
262
+
263
+ char* title = nullptr;
264
+ size_t len = 0;
265
+ winpr_asprintf(&title, &len, "[gateway]");
266
+
267
+ Sint32 flags = 0;
268
+ if (isConsentMandatory)
269
+ flags = SHOW_DIALOG_ACCEPT_REJECT;
270
+ else if (isDisplayMandatory)
271
+ flags = SHOW_DIALOG_TIMED_ACCEPT;
272
+ char* message = ConvertWCharNToUtf8Alloc(wmessage, length, nullptr);
273
+
274
+ SDLConnectionDialogHider hider(instance);
275
+ const int rc = sdl_show_dialog(instance->context, title, message, flags);
276
+ free(title);
277
+ free(message);
278
+ return rc > 0;
279
+ }
280
+
281
+ int sdl_logon_error_info(freerdp* instance, UINT32 data, UINT32 type)
282
+ {
283
+ int rc = -1;
284
+ const char* str_data = freerdp_get_logon_error_info_data(data);
285
+ const char* str_type = freerdp_get_logon_error_info_type(type);
286
+
287
+ if (!instance || !instance->context)
288
+ return -1;
289
+
290
+ /* ignore LOGON_MSG_SESSION_CONTINUE message */
291
+ if (type == LOGON_MSG_SESSION_CONTINUE)
292
+ return 0;
293
+
294
+ SDLConnectionDialogHider hider(instance);
295
+
296
+ char* title = nullptr;
297
+ size_t tlen = 0;
298
+ winpr_asprintf(&title, &tlen, "[%s] info",
299
+ freerdp_settings_get_server_name(instance->context->settings));
300
+
301
+ char* message = nullptr;
302
+ size_t mlen = 0;
303
+ winpr_asprintf(&message, &mlen, "Logon Error Info %s [%s]", str_data, str_type);
304
+
305
+ rc = sdl_show_dialog(instance->context, title, message, SHOW_DIALOG_ACCEPT_REJECT);
306
+ free(title);
307
+ free(message);
308
+ return rc;
309
+ }
310
+
311
+ static DWORD sdl_show_ceritifcate_dialog(rdpContext* context, const char* title,
312
+ const char* message)
313
+ {
314
+ SDLConnectionDialogHider hider(context);
315
+ if (!sdl_push_user_event(SDL_EVENT_USER_CERT_DIALOG, title, message))
316
+ return 0;
317
+
318
+ SDL_Event event = {};
319
+ if (!sdl_wait_for_result(context, SDL_EVENT_USER_CERT_RESULT, &event))
320
+ return 0;
321
+ return static_cast<DWORD>(event.user.code);
322
+ }
323
+
324
+ static char* sdl_pem_cert(const char* pem)
325
+ {
326
+ rdpCertificate* cert = freerdp_certificate_new_from_pem(pem);
327
+ if (!cert)
328
+ return nullptr;
329
+
330
+ char* fp = freerdp_certificate_get_fingerprint(cert);
331
+ char* start = freerdp_certificate_get_validity(cert, TRUE);
332
+ char* end = freerdp_certificate_get_validity(cert, FALSE);
333
+ freerdp_certificate_free(cert);
334
+
335
+ char* str = nullptr;
336
+ size_t slen = 0;
337
+ winpr_asprintf(&str, &slen,
338
+ "Valid from: %s\n"
339
+ "Valid to: %s\n"
340
+ "Thumbprint: %s\n",
341
+ start, end, fp);
342
+ free(fp);
343
+ free(start);
344
+ free(end);
345
+ return str;
346
+ }
347
+
348
+ DWORD sdl_verify_changed_certificate_ex(freerdp* instance, const char* host, UINT16 port,
349
+ const char* common_name, const char* subject,
350
+ const char* issuer, const char* new_fingerprint,
351
+ const char* old_subject, const char* old_issuer,
352
+ const char* old_fingerprint, DWORD flags)
353
+ {
354
+ const char* type = type_str_for_flags(flags);
355
+
356
+ WINPR_ASSERT(instance);
357
+ WINPR_ASSERT(instance->context);
358
+ WINPR_ASSERT(instance->context->settings);
359
+
360
+ SDLConnectionDialogHider hider(instance);
361
+ /* Newer versions of FreeRDP allow exposing the whole PEM by setting
362
+ * FreeRDP_CertificateCallbackPreferPEM to TRUE
363
+ */
364
+ char* new_fp_str = nullptr;
365
+ size_t len = 0;
366
+ if (flags & VERIFY_CERT_FLAG_FP_IS_PEM)
367
+ new_fp_str = sdl_pem_cert(new_fingerprint);
368
+ else
369
+ winpr_asprintf(&new_fp_str, &len, "Thumbprint: %s\n", new_fingerprint);
370
+
371
+ /* Newer versions of FreeRDP allow exposing the whole PEM by setting
372
+ * FreeRDP_CertificateCallbackPreferPEM to TRUE
373
+ */
374
+ char* old_fp_str = nullptr;
375
+ size_t olen = 0;
376
+ if (flags & VERIFY_CERT_FLAG_FP_IS_PEM)
377
+ old_fp_str = sdl_pem_cert(old_fingerprint);
378
+ else
379
+ winpr_asprintf(&old_fp_str, &olen, "Thumbprint: %s\n", old_fingerprint);
380
+
381
+ const char* collission_str = "";
382
+ if (flags & VERIFY_CERT_FLAG_MATCH_LEGACY_SHA1)
383
+ {
384
+ collission_str =
385
+ "A matching entry with legacy SHA1 was found in local known_hosts2 store.\n"
386
+ "If you just upgraded from a FreeRDP version before 2.0 this is expected.\n"
387
+ "The hashing algorithm has been upgraded from SHA1 to SHA256.\n"
388
+ "All manually accepted certificates must be reconfirmed!\n"
389
+ "\n";
390
+ }
391
+
392
+ char* title = nullptr;
393
+ size_t tlen = 0;
394
+ winpr_asprintf(&title, &tlen, "Certificate for %s:%" PRIu16 " (%s) has changed", host, port,
395
+ type);
396
+
397
+ char* message = nullptr;
398
+ size_t mlen = 0;
399
+ winpr_asprintf(&message, &mlen,
400
+ "New Certificate details:\n"
401
+ "Common Name: %s\n"
402
+ "Subject: %s\n"
403
+ "Issuer: %s\n"
404
+ "%s\n"
405
+ "Old Certificate details:\n"
406
+ "Subject: %s\n"
407
+ "Issuer: %s\n"
408
+ "%s\n"
409
+ "%s\n"
410
+ "The above X.509 certificate does not match the certificate used for previous "
411
+ "connections.\n"
412
+ "This may indicate that the certificate has been tampered with.\n"
413
+ "Please contact the administrator of the RDP server and clarify.\n",
414
+ common_name, subject, issuer, new_fp_str, old_subject, old_issuer, old_fp_str,
415
+ collission_str);
416
+
417
+ const DWORD rc = sdl_show_ceritifcate_dialog(instance->context, title, message);
418
+ free(title);
419
+ free(message);
420
+ free(new_fp_str);
421
+ free(old_fp_str);
422
+
423
+ return rc;
424
+ }
425
+
426
+ DWORD sdl_verify_certificate_ex(freerdp* instance, const char* host, UINT16 port,
427
+ const char* common_name, const char* subject, const char* issuer,
428
+ const char* fingerprint, DWORD flags)
429
+ {
430
+ const char* type = type_str_for_flags(flags);
431
+
432
+ /* Newer versions of FreeRDP allow exposing the whole PEM by setting
433
+ * FreeRDP_CertificateCallbackPreferPEM to TRUE
434
+ */
435
+ char* fp_str = nullptr;
436
+ size_t len = 0;
437
+ if (flags & VERIFY_CERT_FLAG_FP_IS_PEM)
438
+ fp_str = sdl_pem_cert(fingerprint);
439
+ else
440
+ winpr_asprintf(&fp_str, &len, "Thumbprint: %s\n", fingerprint);
441
+
442
+ char* title = nullptr;
443
+ size_t tlen = 0;
444
+ winpr_asprintf(&title, &tlen, "New certificate for %s:%" PRIu16 " (%s)", host, port, type);
445
+
446
+ char* message = nullptr;
447
+ size_t mlen = 0;
448
+ winpr_asprintf(
449
+ &message, &mlen,
450
+ "Common Name: %s\n"
451
+ "Subject: %s\n"
452
+ "Issuer: %s\n"
453
+ "%s\n"
454
+ "The above X.509 certificate could not be verified, possibly because you do not have\n"
455
+ "the CA certificate in your certificate store, or the certificate has expired.\n"
456
+ "Please look at the OpenSSL documentation on how to add a private CA to the store.\n",
457
+ common_name, subject, issuer, fp_str);
458
+
459
+ SDLConnectionDialogHider hider(instance);
460
+ const DWORD rc = sdl_show_ceritifcate_dialog(instance->context, title, message);
461
+ free(fp_str);
462
+ free(title);
463
+ free(message);
464
+ return rc;
465
+ }
466
+
467
+ BOOL sdl_cert_dialog_show(const char* title, const char* message)
468
+ {
469
+ int buttonid = -1;
470
+ enum
471
+ {
472
+ BUTTONID_CERT_ACCEPT_PERMANENT = 23,
473
+ BUTTONID_CERT_ACCEPT_TEMPORARY = 24,
474
+ BUTTONID_CERT_DENY = 25
475
+ };
476
+ const SDL_MessageBoxButtonData buttons[] = {
477
+ { 0, BUTTONID_CERT_ACCEPT_PERMANENT, "permanent" },
478
+ { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, BUTTONID_CERT_ACCEPT_TEMPORARY, "temporary" },
479
+ { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, BUTTONID_CERT_DENY, "cancel" }
480
+ };
481
+
482
+ const SDL_MessageBoxData data = { SDL_MESSAGEBOX_WARNING, nullptr, title, message,
483
+ ARRAYSIZE(buttons), buttons, nullptr };
484
+ const int rc = SDL_ShowMessageBox(&data, &buttonid);
485
+
486
+ Sint32 value = -1;
487
+ if (rc < 0)
488
+ value = 0;
489
+ else
490
+ {
491
+ switch (buttonid)
492
+ {
493
+ case BUTTONID_CERT_ACCEPT_PERMANENT:
494
+ value = 1;
495
+ break;
496
+ case BUTTONID_CERT_ACCEPT_TEMPORARY:
497
+ value = 2;
498
+ break;
499
+ default:
500
+ value = 0;
501
+ break;
502
+ }
503
+ }
504
+
505
+ return sdl_push_user_event(SDL_EVENT_USER_CERT_RESULT, value);
506
+ }
507
+
508
+ BOOL sdl_message_dialog_show(const char* title, const char* message, Sint32 flags)
509
+ {
510
+ int buttonid = -1;
511
+ enum
512
+ {
513
+ BUTTONID_SHOW_ACCEPT = 24,
514
+ BUTTONID_SHOW_DENY = 25
515
+ };
516
+ const SDL_MessageBoxButtonData buttons[] = {
517
+ { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, BUTTONID_SHOW_ACCEPT, "accept" },
518
+ { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, BUTTONID_SHOW_DENY, "cancel" }
519
+ };
520
+
521
+ const int button_cnt = (flags & SHOW_DIALOG_ACCEPT_REJECT) ? 2 : 1;
522
+ const SDL_MessageBoxData data = {
523
+ SDL_MESSAGEBOX_WARNING, nullptr, title, message, button_cnt, buttons, nullptr
524
+ };
525
+ const int rc = SDL_ShowMessageBox(&data, &buttonid);
526
+
527
+ Sint32 value = -1;
528
+ if (rc < 0)
529
+ value = 0;
530
+ else
531
+ {
532
+ switch (buttonid)
533
+ {
534
+ case BUTTONID_SHOW_ACCEPT:
535
+ value = 1;
536
+ break;
537
+ default:
538
+ value = 0;
539
+ break;
540
+ }
541
+ }
542
+
543
+ return sdl_push_user_event(SDL_EVENT_USER_SHOW_RESULT, value);
544
+ }
545
+
546
+ BOOL sdl_auth_dialog_show(const SDL_UserAuthArg* args)
547
+ {
548
+ std::vector<std::string> auth = { "Username: ", "Domain: ",
549
+ "Password: " };
550
+ std::vector<std::string> authPin = { "Device: ", "PIN: " };
551
+ std::vector<std::string> gw = { "GatewayUsername: ", "GatewayDomain: ", "GatewayPassword: " };
552
+ std::vector<std::string> prompt;
553
+ Sint32 rc = -1;
554
+
555
+ switch (args->result)
556
+ {
557
+ case AUTH_SMARTCARD_PIN:
558
+ prompt = std::move(authPin);
559
+ break;
560
+ case AUTH_TLS:
561
+ case AUTH_RDP:
562
+ case AUTH_NLA:
563
+ prompt = std::move(auth);
564
+ break;
565
+ case GW_AUTH_HTTP:
566
+ case GW_AUTH_RDG:
567
+ case GW_AUTH_RPC:
568
+ prompt = std::move(gw);
569
+ break;
570
+ default:
571
+ break;
572
+ }
573
+
574
+ std::vector<std::string> result;
575
+
576
+ if (!prompt.empty())
577
+ {
578
+ std::vector<std::string> initial{ args->user ? args->user : "Smartcard", "" };
579
+ std::vector<Uint32> flags = { SdlInputWidget::SDL_INPUT_READONLY,
580
+ SdlInputWidget::SDL_INPUT_MASK };
581
+ if (args->result != AUTH_SMARTCARD_PIN)
582
+ {
583
+ initial = { args->user ? args->user : "", args->domain ? args->domain : "",
584
+ args->password ? args->password : "" };
585
+ flags = { 0, 0, SdlInputWidget::SDL_INPUT_MASK };
586
+ }
587
+ SdlInputWidgetList ilist(args->title, prompt, initial, flags);
588
+ rc = ilist.run(result);
589
+ }
590
+
591
+ if ((result.size() < prompt.size()))
592
+ rc = -1;
593
+
594
+ char* user = nullptr;
595
+ char* domain = nullptr;
596
+ char* pwd = nullptr;
597
+ if (rc > 0)
598
+ {
599
+ user = _strdup(result[0].c_str());
600
+ if (args->result == AUTH_SMARTCARD_PIN)
601
+ pwd = _strdup(result[1].c_str());
602
+ else
603
+ {
604
+ domain = _strdup(result[1].c_str());
605
+ pwd = _strdup(result[2].c_str());
606
+ }
607
+ }
608
+ return sdl_push_user_event(SDL_EVENT_USER_AUTH_RESULT, user, domain, pwd, rc);
609
+ }
610
+
611
+ BOOL sdl_scard_dialog_show(const char* title, Sint32 count, const char** list)
612
+ {
613
+ std::vector<std::string> vlist;
614
+ vlist.reserve(WINPR_ASSERTING_INT_CAST(size_t, count));
615
+ for (Sint32 x = 0; x < count; x++)
616
+ vlist.emplace_back(list[x]);
617
+ SdlSelectList slist(title, vlist);
618
+ Sint32 value = slist.run();
619
+ return sdl_push_user_event(SDL_EVENT_USER_SCARD_RESULT, value);
620
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_dialogs.hpp ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client helper dialogs
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #pragma once
21
+
22
+ #include <winpr/wtypes.h>
23
+ #include <freerdp/freerdp.h>
24
+
25
+ #include "../sdl_types.hpp"
26
+ #include "../sdl_utils.hpp"
27
+
28
+ BOOL sdl_authenticate_ex(freerdp* instance, char** username, char** password, char** domain,
29
+ rdp_auth_reason reason);
30
+ BOOL sdl_choose_smartcard(freerdp* instance, SmartcardCertInfo** cert_list, DWORD count,
31
+ DWORD* choice, BOOL gateway);
32
+
33
+ SSIZE_T sdl_retry_dialog(freerdp* instance, const char* what, size_t current, void* userarg);
34
+
35
+ DWORD sdl_verify_certificate_ex(freerdp* instance, const char* host, UINT16 port,
36
+ const char* common_name, const char* subject, const char* issuer,
37
+ const char* fingerprint, DWORD flags);
38
+
39
+ DWORD sdl_verify_changed_certificate_ex(freerdp* instance, const char* host, UINT16 port,
40
+ const char* common_name, const char* subject,
41
+ const char* issuer, const char* new_fingerprint,
42
+ const char* old_subject, const char* old_issuer,
43
+ const char* old_fingerprint, DWORD flags);
44
+
45
+ int sdl_logon_error_info(freerdp* instance, UINT32 data, UINT32 type);
46
+
47
+ BOOL sdl_present_gateway_message(freerdp* instance, UINT32 type, BOOL isDisplayMandatory,
48
+ BOOL isConsentMandatory, size_t length, const WCHAR* message);
49
+
50
+ BOOL sdl_message_dialog_show(const char* title, const char* message, Sint32 flags);
51
+ BOOL sdl_cert_dialog_show(const char* title, const char* message);
52
+ BOOL sdl_scard_dialog_show(const char* title, Sint32 count, const char** list);
53
+ BOOL sdl_auth_dialog_show(const SDL_UserAuthArg* args);
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_input.cpp ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client helper dialogs
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include "sdl_input.hpp"
21
+
22
+ #include <cassert>
23
+
24
+ #include <string>
25
+ #include <utility>
26
+
27
+ #include <SDL3/SDL.h>
28
+ #include <SDL3_ttf/SDL_ttf.h>
29
+
30
+ #include "sdl_widget.hpp"
31
+ #include "sdl_button.hpp"
32
+ #include "sdl_buttons.hpp"
33
+
34
+ static const SDL_Color inputbackgroundcolor = { 0x56, 0x56, 0x56, 0xff };
35
+ static const SDL_Color inputhighlightcolor = { 0x80, 0, 0, 0x60 };
36
+ static const SDL_Color inputmouseovercolor = { 0, 0x80, 0, 0x60 };
37
+ static const SDL_Color inputfontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
38
+ static const SDL_Color labelbackgroundcolor = { 0x56, 0x56, 0x56, 0xff };
39
+ static const SDL_Color labelfontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
40
+ static const Uint32 vpadding = 5;
41
+ static const Uint32 hpadding = 10;
42
+
43
+ SdlInputWidget::SdlInputWidget(SDL_Renderer* renderer, std::string label, std::string initial,
44
+ Uint32 flags, size_t offset, size_t width, size_t height)
45
+ : _flags(flags), _text(std::move(initial)), _text_label(std::move(label)),
46
+ _label(renderer,
47
+ { 0, static_cast<float>(offset * (height + vpadding)), static_cast<float>(width),
48
+ static_cast<float>(height) },
49
+ false),
50
+ _input(renderer,
51
+ { static_cast<float>(width + hpadding),
52
+ static_cast<float>(offset * (height + vpadding)), static_cast<float>(width),
53
+ static_cast<float>(height) },
54
+ true),
55
+ _highlight(false), _mouseover(false)
56
+ {
57
+ }
58
+
59
+ SdlInputWidget::SdlInputWidget(SdlInputWidget&& other) noexcept
60
+ : _flags(other._flags), _text(std::move(other._text)),
61
+ _text_label(std::move(other._text_label)), _label(std::move(other._label)),
62
+ _input(std::move(other._input)), _highlight(other._highlight), _mouseover(other._mouseover)
63
+ {
64
+ }
65
+
66
+ bool SdlInputWidget::fill_label(SDL_Renderer* renderer, SDL_Color color)
67
+ {
68
+ if (!_label.fill(renderer, color))
69
+ return false;
70
+ return _label.update_text(renderer, _text_label, labelfontcolor);
71
+ }
72
+
73
+ bool SdlInputWidget::update_label(SDL_Renderer* renderer)
74
+ {
75
+ return _label.update_text(renderer, _text_label, labelfontcolor, labelbackgroundcolor);
76
+ }
77
+
78
+ bool SdlInputWidget::set_mouseover(SDL_Renderer* renderer, bool mouseOver)
79
+ {
80
+ if (readonly())
81
+ return true;
82
+ _mouseover = mouseOver;
83
+ return update_input(renderer);
84
+ }
85
+
86
+ bool SdlInputWidget::set_highlight(SDL_Renderer* renderer, bool highlight)
87
+ {
88
+ if (readonly())
89
+ return true;
90
+ _highlight = highlight;
91
+ return update_input(renderer);
92
+ }
93
+
94
+ bool SdlInputWidget::update_input(SDL_Renderer* renderer)
95
+ {
96
+ std::vector<SDL_Color> colors = { inputbackgroundcolor };
97
+ if (_highlight)
98
+ colors.push_back(inputhighlightcolor);
99
+ if (_mouseover)
100
+ colors.push_back(inputmouseovercolor);
101
+
102
+ if (!_input.fill(renderer, colors))
103
+ return false;
104
+ return update_input(renderer, inputfontcolor);
105
+ }
106
+
107
+ bool SdlInputWidget::resize_input(size_t size)
108
+ {
109
+ _text.resize(size);
110
+
111
+ return true;
112
+ }
113
+
114
+ bool SdlInputWidget::set_str(SDL_Renderer* renderer, const std::string& text)
115
+ {
116
+ if (readonly())
117
+ return true;
118
+ _text = text;
119
+ if (!resize_input(_text.size()))
120
+ return false;
121
+ return update_input(renderer);
122
+ }
123
+
124
+ bool SdlInputWidget::remove_str(SDL_Renderer* renderer, size_t count)
125
+ {
126
+ if (readonly())
127
+ return true;
128
+
129
+ assert(renderer);
130
+ if (_text.empty())
131
+ return true;
132
+
133
+ if (!resize_input(_text.size() - count))
134
+ return false;
135
+ return update_input(renderer);
136
+ }
137
+
138
+ bool SdlInputWidget::append_str(SDL_Renderer* renderer, const std::string& text)
139
+ {
140
+ assert(renderer);
141
+ if (readonly())
142
+ return true;
143
+
144
+ _text.append(text);
145
+ if (!resize_input(_text.size()))
146
+ return false;
147
+ return update_input(renderer);
148
+ }
149
+
150
+ const SDL_FRect& SdlInputWidget::input_rect() const
151
+ {
152
+ return _input.rect();
153
+ }
154
+
155
+ std::string SdlInputWidget::value() const
156
+ {
157
+ return _text;
158
+ }
159
+
160
+ bool SdlInputWidget::readonly() const
161
+ {
162
+ return (_flags & SDL_INPUT_READONLY) != 0;
163
+ }
164
+
165
+ bool SdlInputWidget::update_input(SDL_Renderer* renderer, SDL_Color fgcolor)
166
+ {
167
+ std::string text = _text;
168
+ if (!text.empty())
169
+ {
170
+ if (_flags & SDL_INPUT_MASK)
171
+ {
172
+ for (char& x : text)
173
+ x = '*';
174
+ }
175
+ }
176
+
177
+ return _input.update_text(renderer, text, fgcolor);
178
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_input_widgets.cpp ADDED
@@ -0,0 +1,295 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <cassert>
2
+ #include <algorithm>
3
+
4
+ #include <winpr/cast.h>
5
+
6
+ #include "sdl_input_widgets.hpp"
7
+
8
+ static const Uint32 vpadding = 5;
9
+
10
+ SdlInputWidgetList::SdlInputWidgetList(const std::string& title,
11
+ const std::vector<std::string>& labels,
12
+ const std::vector<std::string>& initial,
13
+ const std::vector<Uint32>& flags)
14
+ : _window(nullptr), _renderer(nullptr)
15
+ {
16
+ assert(labels.size() == initial.size());
17
+ assert(labels.size() == flags.size());
18
+ const std::vector<int> buttonids = { INPUT_BUTTON_ACCEPT, INPUT_BUTTON_CANCEL };
19
+ const std::vector<std::string> buttonlabels = { "accept", "cancel" };
20
+
21
+ const size_t widget_width = 300;
22
+ const size_t widget_heigth = 50;
23
+
24
+ const size_t total_width = widget_width + widget_width;
25
+ const size_t input_height = labels.size() * (widget_heigth + vpadding) + vpadding;
26
+ const size_t total_height = input_height + widget_heigth;
27
+ assert(total_width <= INT32_MAX);
28
+ assert(total_height <= INT32_MAX);
29
+ auto rc = SDL_CreateWindowAndRenderer(
30
+ title.c_str(), total_width, static_cast<int>(total_height),
31
+ SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_INPUT_FOCUS, &_window,
32
+ &_renderer);
33
+ if (!rc)
34
+ widget_log_error(rc, "SDL_CreateWindowAndRenderer");
35
+ else
36
+ {
37
+ for (size_t x = 0; x < labels.size(); x++)
38
+ _list.emplace_back(_renderer, labels[x], initial[x], flags[x], x, widget_width,
39
+ widget_heigth);
40
+
41
+ _buttons.populate(_renderer, buttonlabels, buttonids, total_width,
42
+ static_cast<Sint32>(input_height), static_cast<Sint32>(widget_width),
43
+ static_cast<Sint32>(widget_heigth));
44
+ _buttons.set_highlight(0);
45
+ }
46
+ }
47
+
48
+ ssize_t SdlInputWidgetList::next(ssize_t current)
49
+ {
50
+ size_t iteration = 0;
51
+ auto val = static_cast<size_t>(current);
52
+
53
+ do
54
+ {
55
+ if (iteration >= _list.size())
56
+ return -1;
57
+
58
+ if (iteration == 0)
59
+ {
60
+ if (current < 0)
61
+ val = 0;
62
+ else
63
+ val++;
64
+ }
65
+ else
66
+ val++;
67
+ iteration++;
68
+ val %= _list.size();
69
+ } while (!valid(static_cast<ssize_t>(val)));
70
+ return static_cast<ssize_t>(val);
71
+ }
72
+
73
+ bool SdlInputWidgetList::valid(ssize_t current) const
74
+ {
75
+ if (current < 0)
76
+ return false;
77
+ auto s = static_cast<size_t>(current);
78
+ if (s >= _list.size())
79
+ return false;
80
+ return !_list[s].readonly();
81
+ }
82
+
83
+ SdlInputWidget* SdlInputWidgetList::get(ssize_t index)
84
+ {
85
+ if (index < 0)
86
+ return nullptr;
87
+ auto s = static_cast<size_t>(index);
88
+ if (s >= _list.size())
89
+ return nullptr;
90
+ return &_list[s];
91
+ }
92
+
93
+ SdlInputWidgetList::~SdlInputWidgetList()
94
+ {
95
+ _list.clear();
96
+ _buttons.clear();
97
+ SDL_DestroyRenderer(_renderer);
98
+ SDL_DestroyWindow(_window);
99
+ }
100
+
101
+ bool SdlInputWidgetList::update(SDL_Renderer* renderer)
102
+ {
103
+ for (auto& btn : _list)
104
+ {
105
+ if (!btn.update_label(renderer))
106
+ return false;
107
+ if (!btn.update_input(renderer))
108
+ return false;
109
+ }
110
+
111
+ return _buttons.update(renderer);
112
+ }
113
+
114
+ ssize_t SdlInputWidgetList::get_index(const SDL_MouseButtonEvent& button)
115
+ {
116
+ const auto x = button.x;
117
+ const auto y = button.y;
118
+ for (size_t i = 0; i < _list.size(); i++)
119
+ {
120
+ auto& cur = _list[i];
121
+ auto r = cur.input_rect();
122
+
123
+ if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
124
+ return WINPR_ASSERTING_INT_CAST(ssize_t, i);
125
+ }
126
+ return -1;
127
+ }
128
+
129
+ int SdlInputWidgetList::run(std::vector<std::string>& result)
130
+ {
131
+ int res = -1;
132
+ ssize_t LastActiveTextInput = -1;
133
+ ssize_t CurrentActiveTextInput = next(-1);
134
+
135
+ if (!_window || !_renderer)
136
+ return -2;
137
+
138
+ try
139
+ {
140
+ bool running = true;
141
+ std::vector<SDL_Keycode> pressed;
142
+ while (running)
143
+ {
144
+ if (!clear_window(_renderer))
145
+ throw;
146
+
147
+ if (!update(_renderer))
148
+ throw;
149
+
150
+ if (!_buttons.update(_renderer))
151
+ throw;
152
+
153
+ SDL_Event event = {};
154
+ SDL_WaitEvent(&event);
155
+ switch (event.type)
156
+ {
157
+ case SDL_EVENT_KEY_UP:
158
+ {
159
+ auto it = std::remove(pressed.begin(), pressed.end(), event.key.key);
160
+ pressed.erase(it, pressed.end());
161
+
162
+ switch (event.key.key)
163
+ {
164
+ case SDLK_BACKSPACE:
165
+ {
166
+ auto cur = get(CurrentActiveTextInput);
167
+ if (cur)
168
+ {
169
+ if (!cur->remove_str(_renderer, 1))
170
+ throw;
171
+ }
172
+ }
173
+ break;
174
+ case SDLK_TAB:
175
+ CurrentActiveTextInput = next(CurrentActiveTextInput);
176
+ break;
177
+ case SDLK_RETURN:
178
+ case SDLK_RETURN2:
179
+ case SDLK_KP_ENTER:
180
+ running = false;
181
+ res = INPUT_BUTTON_ACCEPT;
182
+ break;
183
+ case SDLK_ESCAPE:
184
+ running = false;
185
+ res = INPUT_BUTTON_CANCEL;
186
+ break;
187
+ case SDLK_V:
188
+ if (pressed.size() == 2)
189
+ {
190
+ if ((pressed[0] == SDLK_LCTRL) || (pressed[0] == SDLK_RCTRL))
191
+ {
192
+ auto cur = get(CurrentActiveTextInput);
193
+ if (cur)
194
+ {
195
+ auto text = SDL_GetClipboardText();
196
+ cur->set_str(_renderer, text);
197
+ }
198
+ }
199
+ }
200
+ break;
201
+ default:
202
+ break;
203
+ }
204
+ }
205
+ break;
206
+ case SDL_EVENT_KEY_DOWN:
207
+ pressed.push_back(event.key.key);
208
+ break;
209
+ case SDL_EVENT_TEXT_INPUT:
210
+ {
211
+ auto cur = get(CurrentActiveTextInput);
212
+ if (cur)
213
+ {
214
+ if (!cur->append_str(_renderer, event.text.text))
215
+ throw;
216
+ }
217
+ }
218
+ break;
219
+ case SDL_EVENT_MOUSE_MOTION:
220
+ {
221
+ auto TextInputIndex = get_index(event.button);
222
+ for (auto& cur : _list)
223
+ {
224
+ if (!cur.set_mouseover(_renderer, false))
225
+ throw;
226
+ }
227
+ if (TextInputIndex >= 0)
228
+ {
229
+ auto& cur = _list[static_cast<size_t>(TextInputIndex)];
230
+ if (!cur.set_mouseover(_renderer, true))
231
+ throw;
232
+ }
233
+
234
+ _buttons.set_mouseover(event.button.x, event.button.y);
235
+ }
236
+ break;
237
+ case SDL_EVENT_MOUSE_BUTTON_DOWN:
238
+ {
239
+ auto val = get_index(event.button);
240
+ if (valid(val))
241
+ CurrentActiveTextInput = val;
242
+
243
+ auto button = _buttons.get_selected(event.button);
244
+ if (button)
245
+ {
246
+ running = false;
247
+ if (button->id() == INPUT_BUTTON_CANCEL)
248
+ res = INPUT_BUTTON_CANCEL;
249
+ else
250
+ res = INPUT_BUTTON_ACCEPT;
251
+ }
252
+ }
253
+ break;
254
+ case SDL_EVENT_QUIT:
255
+ res = INPUT_BUTTON_CANCEL;
256
+ running = false;
257
+ break;
258
+ default:
259
+ break;
260
+ }
261
+
262
+ if (LastActiveTextInput != CurrentActiveTextInput)
263
+ {
264
+ if (CurrentActiveTextInput < 0)
265
+ SDL_StopTextInput(_window);
266
+ else
267
+ SDL_StartTextInput(_window);
268
+ LastActiveTextInput = CurrentActiveTextInput;
269
+ }
270
+
271
+ for (auto& cur : _list)
272
+ {
273
+ if (!cur.set_highlight(_renderer, false))
274
+ throw;
275
+ }
276
+ auto cur = get(CurrentActiveTextInput);
277
+ if (cur)
278
+ {
279
+ if (!cur->set_highlight(_renderer, true))
280
+ throw;
281
+ }
282
+
283
+ SDL_RenderPresent(_renderer);
284
+ }
285
+
286
+ for (auto& cur : _list)
287
+ result.push_back(cur.value());
288
+ }
289
+ catch (...)
290
+ {
291
+ res = -2;
292
+ }
293
+
294
+ return res;
295
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_input_widgets.hpp ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <string>
4
+ #include <vector>
5
+ #include <SDL3/SDL.h>
6
+
7
+ #include "sdl_input.hpp"
8
+ #include "sdl_buttons.hpp"
9
+
10
+ class SdlInputWidgetList
11
+ {
12
+ public:
13
+ SdlInputWidgetList(const std::string& title, const std::vector<std::string>& labels,
14
+ const std::vector<std::string>& initial, const std::vector<Uint32>& flags);
15
+ SdlInputWidgetList(const SdlInputWidgetList& other) = delete;
16
+ SdlInputWidgetList(SdlInputWidgetList&& other) = delete;
17
+
18
+ virtual ~SdlInputWidgetList();
19
+
20
+ SdlInputWidgetList& operator=(const SdlInputWidgetList& other) = delete;
21
+ SdlInputWidgetList& operator=(SdlInputWidgetList&& other) = delete;
22
+
23
+ int run(std::vector<std::string>& result);
24
+
25
+ protected:
26
+ bool update(SDL_Renderer* renderer);
27
+ ssize_t get_index(const SDL_MouseButtonEvent& button);
28
+
29
+ private:
30
+ enum
31
+ {
32
+ INPUT_BUTTON_ACCEPT = 1,
33
+ INPUT_BUTTON_CANCEL = -2
34
+ };
35
+
36
+ ssize_t next(ssize_t current);
37
+ [[nodiscard]] bool valid(ssize_t current) const;
38
+ SdlInputWidget* get(ssize_t index);
39
+
40
+ SDL_Window* _window;
41
+ SDL_Renderer* _renderer;
42
+ std::vector<SdlInputWidget> _list;
43
+ SdlButtonList _buttons;
44
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_select.cpp ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client helper dialogs
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <cassert>
21
+
22
+ #include <string>
23
+ #include <utility>
24
+
25
+ #include <SDL3/SDL.h>
26
+ #include <SDL3_ttf/SDL_ttf.h>
27
+
28
+ #include "sdl_select.hpp"
29
+ #include "sdl_widget.hpp"
30
+ #include "sdl_button.hpp"
31
+ #include "sdl_buttons.hpp"
32
+ #include "sdl_input_widgets.hpp"
33
+
34
+ static const SDL_Color labelmouseovercolor = { 0, 0x80, 0, 0x60 };
35
+ static const SDL_Color labelbackgroundcolor = { 0x69, 0x66, 0x63, 0xff };
36
+ static const SDL_Color labelhighlightcolor = { 0xcd, 0xca, 0x35, 0x60 };
37
+ static const SDL_Color labelfontcolor = { 0xd1, 0xcf, 0xcd, 0xff };
38
+
39
+ SdlSelectWidget::SdlSelectWidget(SDL_Renderer* renderer, std::string label, const SDL_FRect& rect)
40
+ : SdlWidget(renderer, rect, true), _text(std::move(label)), _mouseover(false), _highlight(false)
41
+ {
42
+ }
43
+
44
+ SdlSelectWidget::SdlSelectWidget(SdlSelectWidget&& other) noexcept = default;
45
+
46
+ bool SdlSelectWidget::set_mouseover(SDL_Renderer* renderer, bool mouseOver)
47
+ {
48
+ _mouseover = mouseOver;
49
+ return update_text(renderer);
50
+ }
51
+
52
+ bool SdlSelectWidget::set_highlight(SDL_Renderer* renderer, bool highlight)
53
+ {
54
+ _highlight = highlight;
55
+ return update_text(renderer);
56
+ }
57
+
58
+ bool SdlSelectWidget::update_text(SDL_Renderer* renderer)
59
+ {
60
+ assert(renderer);
61
+ std::vector<SDL_Color> colors = { labelbackgroundcolor };
62
+ if (_highlight)
63
+ colors.push_back(labelhighlightcolor);
64
+ if (_mouseover)
65
+ colors.push_back(labelmouseovercolor);
66
+ if (!fill(renderer, colors))
67
+ return false;
68
+ return SdlWidget::update_text(renderer, _text, labelfontcolor);
69
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_select.hpp ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client helper dialogs
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #pragma once
21
+
22
+ #include <string>
23
+ #include <vector>
24
+
25
+ #include <SDL3/SDL.h>
26
+ #include "sdl_widget.hpp"
27
+
28
+ class SdlSelectWidget : public SdlWidget
29
+ {
30
+ public:
31
+ SdlSelectWidget(SDL_Renderer* renderer, std::string label, const SDL_FRect& rect);
32
+ SdlSelectWidget(SdlSelectWidget&& other) noexcept;
33
+ SdlSelectWidget(const SdlSelectWidget& other) = delete;
34
+
35
+ SdlSelectWidget& operator=(const SdlSelectWidget& other) = delete;
36
+ SdlSelectWidget& operator=(SdlSelectWidget&& other) = delete;
37
+
38
+ virtual bool set_mouseover(SDL_Renderer* renderer, bool mouseOver);
39
+ virtual bool set_highlight(SDL_Renderer* renderer, bool highlight);
40
+ virtual bool update_text(SDL_Renderer* renderer);
41
+
42
+ private:
43
+ std::string _text;
44
+ bool _mouseover;
45
+ bool _highlight;
46
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_selectlist.hpp ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ #include <string>
4
+ #include <vector>
5
+
6
+ #include <SDL3/SDL.h>
7
+
8
+ #include "sdl_select.hpp"
9
+ #include "sdl_button.hpp"
10
+ #include "sdl_buttons.hpp"
11
+
12
+ class SdlSelectList
13
+ {
14
+ public:
15
+ SdlSelectList(const std::string& title, const std::vector<std::string>& labels);
16
+ SdlSelectList(const SdlSelectList& other) = delete;
17
+ SdlSelectList(SdlSelectList&& other) = delete;
18
+ virtual ~SdlSelectList();
19
+
20
+ SdlSelectList& operator=(const SdlSelectList& other) = delete;
21
+ SdlSelectList& operator=(SdlSelectList&& other) = delete;
22
+
23
+ int run();
24
+
25
+ private:
26
+ enum
27
+ {
28
+ INPUT_BUTTON_ACCEPT = 0,
29
+ INPUT_BUTTON_CANCEL = -2
30
+ };
31
+
32
+ ssize_t get_index(const SDL_MouseButtonEvent& button);
33
+ bool update_text();
34
+ void reset_mouseover();
35
+ void reset_highlight();
36
+
37
+ SDL_Window* _window;
38
+ SDL_Renderer* _renderer;
39
+ std::vector<SdlSelectWidget> _list;
40
+ SdlButtonList _buttons;
41
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_widget.cpp ADDED
@@ -0,0 +1,320 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client helper dialogs
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <cassert>
21
+ #include <cstdio>
22
+ #include <cstdlib>
23
+ #include <algorithm>
24
+
25
+ #include <SDL3/SDL.h>
26
+ #include <SDL3_ttf/SDL_ttf.h>
27
+
28
+ #include "sdl_widget.hpp"
29
+ #include "../sdl_utils.hpp"
30
+
31
+ #include "res/sdl3_resource_manager.hpp"
32
+
33
+ #include <freerdp/log.h>
34
+
35
+ #if defined(WITH_SDL_IMAGE_DIALOGS)
36
+ #include <SDL3_image/SDL_image.h>
37
+ #endif
38
+
39
+ #define TAG CLIENT_TAG("SDL.widget")
40
+
41
+ static const SDL_Color backgroundcolor = { 0x38, 0x36, 0x35, 0xff };
42
+
43
+ static const Uint32 hpadding = 10;
44
+
45
+ SdlWidget::SdlWidget(SDL_Renderer* renderer, const SDL_FRect& rect, bool input)
46
+ : _rect(rect), _input(input)
47
+ {
48
+ assert(renderer);
49
+
50
+ auto ops = SDL3ResourceManager::get(SDLResourceManager::typeFonts(),
51
+ "OpenSans-VariableFont_wdth,wght.ttf");
52
+ if (!ops)
53
+ widget_log_error(-1, "SDLResourceManager::get");
54
+ else
55
+ {
56
+ _font = TTF_OpenFontIO(ops, true, 64);
57
+ if (!_font)
58
+ widget_log_error(-1, "TTF_OpenFontRW");
59
+ }
60
+ }
61
+
62
+ #if defined(WITH_SDL_IMAGE_DIALOGS)
63
+ SdlWidget::SdlWidget(SDL_Renderer* renderer, const SDL_FRect& rect, SDL_IOStream* ops) : _rect(rect)
64
+ {
65
+ if (ops)
66
+ {
67
+ _image = IMG_LoadTexture_IO(renderer, ops, 1);
68
+ if (!_image)
69
+ widget_log_error(-1, "IMG_LoadTexture_IO");
70
+ }
71
+ }
72
+ #endif
73
+
74
+ SdlWidget::SdlWidget(SdlWidget&& other) noexcept
75
+ : _font(other._font), _image(other._image), _rect(other._rect), _input(other._input),
76
+ _wrap(other._wrap), _text_width(other._text_width)
77
+ {
78
+ other._font = nullptr;
79
+ other._image = nullptr;
80
+ }
81
+
82
+ SDL_Texture* SdlWidget::render_text(SDL_Renderer* renderer, const std::string& text,
83
+ SDL_Color fgcolor, SDL_FRect& src, SDL_FRect& dst)
84
+ {
85
+ auto surface = TTF_RenderText_Blended(_font, text.c_str(), 0, fgcolor);
86
+ if (!surface)
87
+ {
88
+ widget_log_error(-1, "TTF_RenderText_Blended");
89
+ return nullptr;
90
+ }
91
+
92
+ auto texture = SDL_CreateTextureFromSurface(renderer, surface);
93
+ SDL_DestroySurface(surface);
94
+ if (!texture)
95
+ {
96
+ widget_log_error(-1, "SDL_CreateTextureFromSurface");
97
+ return nullptr;
98
+ }
99
+
100
+ std::unique_ptr<TTF_TextEngine, decltype(&TTF_DestroySurfaceTextEngine)> engine(
101
+ TTF_CreateRendererTextEngine(renderer), TTF_DestroySurfaceTextEngine);
102
+ if (!engine)
103
+ {
104
+ widget_log_error(-1, "TTF_CreateRendererTextEngine");
105
+ return nullptr;
106
+ }
107
+
108
+ std::unique_ptr<TTF_Text, decltype(&TTF_DestroyText)> txt(
109
+ TTF_CreateText(engine.get(), _font, text.c_str(), text.size()), TTF_DestroyText);
110
+
111
+ if (!txt)
112
+ {
113
+ widget_log_error(-1, "TTF_CreateText");
114
+ return nullptr;
115
+ }
116
+ int w = 0;
117
+ int h = 0;
118
+ if (!TTF_GetTextSize(txt.get(), &w, &h))
119
+ {
120
+ widget_log_error(-1, "TTF_GetTextSize");
121
+ return nullptr;
122
+ }
123
+
124
+ src.w = static_cast<float>(w);
125
+ src.h = static_cast<float>(h);
126
+ /* Do some magic:
127
+ * - Add padding before and after text
128
+ * - if text is too long only show the last elements
129
+ * - if text is too short only update used space
130
+ */
131
+ dst = _rect;
132
+ dst.x += hpadding;
133
+ dst.w -= 2 * hpadding;
134
+ const float scale = dst.h / src.h;
135
+ const float sws = (src.w) * scale;
136
+ const float dws = (dst.w) / scale;
137
+ dst.w = std::min(dst.w, sws);
138
+ if (src.w > dws)
139
+ {
140
+ src.x = src.w - dws;
141
+ src.w = dws;
142
+ }
143
+ return texture;
144
+ }
145
+
146
+ static float scale(float dw, float dh)
147
+ {
148
+ const auto scale = dh / dw;
149
+ const auto dr = dh * scale;
150
+ return dr;
151
+ }
152
+
153
+ SDL_Texture* SdlWidget::render_text_wrapped(SDL_Renderer* renderer, const std::string& text,
154
+ SDL_Color fgcolor, SDL_FRect& src, SDL_FRect& dst)
155
+ {
156
+ assert(_text_width < INT32_MAX);
157
+
158
+ auto surface = TTF_RenderText_Blended_Wrapped(_font, text.c_str(), 0, fgcolor,
159
+ static_cast<int>(_text_width));
160
+ if (!surface)
161
+ {
162
+ widget_log_error(-1, "TTF_RenderText_Blended");
163
+ return nullptr;
164
+ }
165
+
166
+ src.w = static_cast<float>(surface->w);
167
+ src.h = static_cast<float>(surface->h);
168
+
169
+ auto texture = SDL_CreateTextureFromSurface(renderer, surface);
170
+ SDL_DestroySurface(surface);
171
+ if (!texture)
172
+ {
173
+ widget_log_error(-1, "SDL_CreateTextureFromSurface");
174
+ return nullptr;
175
+ }
176
+
177
+ /* Do some magic:
178
+ * - Add padding before and after text
179
+ * - if text is too long only show the last elements
180
+ * - if text is too short only update used space
181
+ */
182
+ dst = _rect;
183
+ dst.x += hpadding;
184
+ dst.w -= 2 * hpadding;
185
+ auto dh = scale(src.w, src.h);
186
+ dst.h = std::min<float>(dh, dst.h);
187
+
188
+ return texture;
189
+ }
190
+
191
+ SdlWidget::~SdlWidget()
192
+ {
193
+ TTF_CloseFont(_font);
194
+ if (_image)
195
+ SDL_DestroyTexture(_image);
196
+ }
197
+
198
+ bool SdlWidget::error_ex(bool success, const char* what, const char* file, size_t line,
199
+ const char* fkt)
200
+ {
201
+ if (success)
202
+ {
203
+ // Flip SDL3 convention to existing code convention to minimize code changes
204
+ return false;
205
+ }
206
+ static wLog* log = nullptr;
207
+ if (!log)
208
+ log = WLog_Get(TAG);
209
+ // Use -1 as it indicates error similar to SDL2 conventions
210
+ // sdl_log_error_ex treats any value other than 0 as SDL error
211
+ return sdl_log_error_ex(-1, log, what, file, line, fkt);
212
+ }
213
+
214
+ static bool draw_rect(SDL_Renderer* renderer, const SDL_FRect* rect, SDL_Color color)
215
+ {
216
+ const int drc = SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
217
+ if (widget_log_error(drc, "SDL_SetRenderDrawColor"))
218
+ return false;
219
+
220
+ const int rc = SDL_RenderFillRect(renderer, rect);
221
+ return !widget_log_error(rc, "SDL_RenderFillRect");
222
+ }
223
+
224
+ bool SdlWidget::fill(SDL_Renderer* renderer, SDL_Color color)
225
+ {
226
+ std::vector<SDL_Color> colors = { color };
227
+ return fill(renderer, colors);
228
+ }
229
+
230
+ bool SdlWidget::fill(SDL_Renderer* renderer, const std::vector<SDL_Color>& colors)
231
+ {
232
+ assert(renderer);
233
+ SDL_BlendMode mode = SDL_BLENDMODE_INVALID;
234
+ SDL_GetRenderDrawBlendMode(renderer, &mode);
235
+ SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
236
+ for (auto color : colors)
237
+ {
238
+ draw_rect(renderer, &_rect, color);
239
+ SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
240
+ }
241
+ SDL_SetRenderDrawBlendMode(renderer, mode);
242
+ return true;
243
+ }
244
+
245
+ bool SdlWidget::update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
246
+ SDL_Color bgcolor)
247
+ {
248
+ assert(renderer);
249
+
250
+ if (!fill(renderer, bgcolor))
251
+ return false;
252
+ return update_text(renderer, text, fgcolor);
253
+ }
254
+
255
+ bool SdlWidget::wrap() const
256
+ {
257
+ return _wrap;
258
+ }
259
+
260
+ bool SdlWidget::set_wrap(bool wrap, size_t width)
261
+ {
262
+ _wrap = wrap;
263
+ _text_width = width;
264
+ return _wrap;
265
+ }
266
+
267
+ const SDL_FRect& SdlWidget::rect() const
268
+ {
269
+ return _rect;
270
+ }
271
+
272
+ bool SdlWidget::update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor)
273
+ {
274
+
275
+ if (text.empty())
276
+ return true;
277
+
278
+ SDL_FRect src{};
279
+ SDL_FRect dst{};
280
+
281
+ SDL_Texture* texture = nullptr;
282
+ if (_image)
283
+ {
284
+ texture = _image;
285
+ dst = _rect;
286
+ auto propId = SDL_GetTextureProperties(_image);
287
+ auto w = SDL_GetNumberProperty(propId, SDL_PROP_TEXTURE_WIDTH_NUMBER, -1);
288
+ auto h = SDL_GetNumberProperty(propId, SDL_PROP_TEXTURE_HEIGHT_NUMBER, -1);
289
+ if (w < 0 || h < 0)
290
+ widget_log_error(-1, "SDL_GetTextureProperties");
291
+ src.w = static_cast<float>(w);
292
+ src.h = static_cast<float>(h);
293
+ }
294
+ else if (_wrap)
295
+ texture = render_text_wrapped(renderer, text, fgcolor, src, dst);
296
+ else
297
+ texture = render_text(renderer, text, fgcolor, src, dst);
298
+ if (!texture)
299
+ return false;
300
+
301
+ const int rc = SDL_RenderTexture(renderer, texture, &src, &dst);
302
+ if (!_image)
303
+ SDL_DestroyTexture(texture);
304
+ if (rc < 0)
305
+ return !widget_log_error(rc, "SDL_RenderCopy");
306
+ return true;
307
+ }
308
+
309
+ bool clear_window(SDL_Renderer* renderer)
310
+ {
311
+ assert(renderer);
312
+
313
+ const int drc = SDL_SetRenderDrawColor(renderer, backgroundcolor.r, backgroundcolor.g,
314
+ backgroundcolor.b, backgroundcolor.a);
315
+ if (widget_log_error(drc, "SDL_SetRenderDrawColor"))
316
+ return false;
317
+
318
+ const int rcls = SDL_RenderClear(renderer);
319
+ return !widget_log_error(rcls, "SDL_RenderClear");
320
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/sdl_widget.hpp ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Client helper dialogs
4
+ *
5
+ * Copyright 2023 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #pragma once
21
+
22
+ #include <string>
23
+
24
+ #include <vector>
25
+ #include <SDL3/SDL.h>
26
+ #include <SDL3_ttf/SDL_ttf.h>
27
+
28
+ #if defined(_MSC_VER)
29
+ #include <BaseTsd.h>
30
+ typedef SSIZE_T ssize_t;
31
+ #endif
32
+
33
+ #if !defined(HAS_NOEXCEPT)
34
+ #if defined(__clang__)
35
+ #if __has_feature(cxx_noexcept)
36
+ #define HAS_NOEXCEPT
37
+ #endif
38
+ #elif defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46 || \
39
+ defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026
40
+ #define HAS_NOEXCEPT
41
+ #endif
42
+ #endif
43
+
44
+ #ifndef HAS_NOEXCEPT
45
+ #define noexcept
46
+ #endif
47
+
48
+ class SdlWidget
49
+ {
50
+ public:
51
+ SdlWidget(SDL_Renderer* renderer, const SDL_FRect& rect, bool input);
52
+ SdlWidget(SDL_Renderer* renderer, const SDL_FRect& rect, SDL_IOStream* ops);
53
+ SdlWidget(const SdlWidget& other) = delete;
54
+ SdlWidget(SdlWidget&& other) noexcept;
55
+ virtual ~SdlWidget();
56
+
57
+ SdlWidget& operator=(const SdlWidget& other) = delete;
58
+ SdlWidget& operator=(SdlWidget&& other) = delete;
59
+
60
+ bool fill(SDL_Renderer* renderer, SDL_Color color);
61
+ bool fill(SDL_Renderer* renderer, const std::vector<SDL_Color>& colors);
62
+ bool update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor);
63
+ bool update_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
64
+ SDL_Color bgcolor);
65
+
66
+ [[nodiscard]] bool wrap() const;
67
+ bool set_wrap(bool wrap = true, size_t width = 0);
68
+ [[nodiscard]] const SDL_FRect& rect() const;
69
+
70
+ #define widget_log_error(res, what) SdlWidget::error_ex(res, what, __FILE__, __LINE__, __func__)
71
+ static bool error_ex(bool success, const char* what, const char* file, size_t line,
72
+ const char* fkt);
73
+
74
+ private:
75
+ SDL_Texture* render_text(SDL_Renderer* renderer, const std::string& text, SDL_Color fgcolor,
76
+ SDL_FRect& src, SDL_FRect& dst);
77
+ SDL_Texture* render_text_wrapped(SDL_Renderer* renderer, const std::string& text,
78
+ SDL_Color fgcolor, SDL_FRect& src, SDL_FRect& dst);
79
+
80
+ TTF_Font* _font = nullptr;
81
+ SDL_Texture* _image = nullptr;
82
+ SDL_FRect _rect;
83
+ bool _input = false;
84
+ bool _wrap = false;
85
+ size_t _text_width = 0;
86
+ };
87
+
88
+ bool clear_window(SDL_Renderer* renderer);
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/dialogs/test/TestSDLDialogs.cpp ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "../sdl_selectlist.hpp"
2
+ #include "../sdl_input_widgets.hpp"
3
+
4
+ #include <freerdp/api.h>
5
+ #include <winpr/wlog.h>
6
+
7
+ BOOL sdl_log_error_ex(Uint32 res, wLog* log, const char* what, const char* file, size_t line,
8
+ const char* fkt)
9
+ {
10
+ return FALSE;
11
+ }
12
+
13
+ static bool test_input_dialog()
14
+ {
15
+ const auto title = "sometitle";
16
+ std::vector<std::string> labels;
17
+ std::vector<std::string> initial;
18
+ std::vector<Uint32> flags;
19
+ for (size_t x = 0; x < 12; x++)
20
+ {
21
+ labels.push_back("label" + std::to_string(x));
22
+ initial.push_back(std::to_string(x));
23
+
24
+ Uint32 flag = 0;
25
+ if ((x % 2) != 0)
26
+ flag |= SdlInputWidget::SDL_INPUT_MASK;
27
+ if ((x % 3) == 0)
28
+ flag |= SdlInputWidget::SDL_INPUT_READONLY;
29
+
30
+ flags.push_back(flag);
31
+ }
32
+ SdlInputWidgetList list{ title, labels, initial, flags };
33
+ std::vector<std::string> result;
34
+ auto rc = list.run(result);
35
+ if (rc < 0)
36
+ {
37
+ return false;
38
+ }
39
+ if (result.size() != labels.size())
40
+ {
41
+ return false;
42
+ }
43
+ return true;
44
+ }
45
+
46
+ static bool test_select_dialog()
47
+ {
48
+ const auto title = "sometitle";
49
+ std::vector<std::string> labels;
50
+ for (size_t x = 0; x < 12; x++)
51
+ {
52
+ labels.push_back("label" + std::to_string(x));
53
+ }
54
+ SdlSelectList list{ title, labels };
55
+ auto rc = list.run();
56
+ if (rc < 0)
57
+ {
58
+ return false;
59
+ }
60
+ if (static_cast<size_t>(rc) >= labels.size())
61
+ return false;
62
+
63
+ return true;
64
+ }
65
+
66
+ extern "C"
67
+ {
68
+ FREERDP_API int TestSDLDialogs(int argc, char* argv[]);
69
+ }
70
+
71
+ int TestSDLDialogs(int argc, char* argv[])
72
+ {
73
+ int rc = 0;
74
+
75
+ (void)argc;
76
+ (void)argv;
77
+
78
+ #if 0
79
+ SDL_Init(SDL_INIT_VIDEO);
80
+ try
81
+ {
82
+ #if 1
83
+ if (!test_input_dialog())
84
+ throw -1;
85
+ #endif
86
+ #if 1
87
+ if (!test_select_dialog())
88
+ throw -2;
89
+ #endif
90
+ }
91
+ catch (int e)
92
+ {
93
+ rc = e;
94
+ }
95
+ SDL_Quit();
96
+
97
+ #endif
98
+ return rc;
99
+ }
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/man/CMakeLists.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ set(DEPS
2
+ ../../../common/man/freerdp-global-options.1 ../../common/man/sdl-freerdp-config.1
3
+ ../../../common/man/freerdp-global-envvar.1 ../../../common/man/freerdp-global-config.1
4
+ ../../common/man/sdl-freerdp-examples.1 ../../../common/man/freerdp-global-links.1
5
+ )
6
+
7
+ set(VAR_NAMES "VENDOR" "PRODUCT" "VENDOR_PRODUCT" "CMAKE_INSTALL_FULL_SYSCONFDIR")
8
+ generate_and_install_freerdp_man_from_xml(${MODULE_NAME} "1" "${DEPS}" "${VAR_NAMES}")
local-test-freerdp-delta-01/afc-freerdp/client/SDL/SDL3/man/sdl3-freerdp.1.in ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .TH "@MANPAGE_NAME@" "1" "@MAN_TODAY@" "freerdp" "@MANPAGE_NAME@"
2
+ .ie \n(.g .ds Aq \(aq
3
+ .el .ds Aq '
4
+ .nh
5
+ .ad l
6
+ .SH "NAME"
7
+ @MANPAGE_NAME@ \- FreeRDP SDL client
8
+ .SH "SYNOPSIS"
9
+ .PP
10
+ \fB@MANPAGE_NAME@\fR
11
+ [file] [options] [/v:server[:port]]
12
+ .SH "DESCRIPTION"
13
+ .PP
14
+ \fB@MANPAGE_NAME@\fR
15
+ is an SDL Remote Desktop Protocol (RDP) client which is part of the FreeRDP project\&. An RDP server is built\-in to many editions of Windows\&. Alternative servers included ogon, gnome\-remote\-desktop, xrdp and VRDP (VirtualBox)\&.
local-test-freerdp-delta-01/afc-freerdp/client/SDL/common/CMakeLists.txt ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FreeRDP: A Remote Desktop Protocol Implementation
2
+ # FreeRDP SDL Client
3
+ #
4
+ # Copyright 2024 Armin Novak <[email protected]>
5
+ # Copyright 2024 Thincast Technologies GmbH
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ add_subdirectory(aad)
20
+ add_subdirectory(res)
21
+
22
+ add_library(sdl-common-prefs STATIC sdl_prefs.hpp sdl_prefs.cpp scoped_guard.hpp)
23
+ target_link_libraries(sdl-common-prefs winpr freerdp)
24
+
25
+ if(BUILD_TESTING_INTERNAL OR BUILD_TESTING)
26
+ add_subdirectory(test)
27
+ endif()
local-test-freerdp-delta-01/afc-freerdp/client/SDL/common/scoped_guard.hpp ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL ScopeGuard
4
+ *
5
+ * Copyright 2024 Armin Novak <[email protected]>
6
+ * Copyright 2024 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #pragma once
22
+
23
+ #include <functional>
24
+
25
+ class ScopeGuard
26
+ {
27
+ public:
28
+ template <class Callable> explicit ScopeGuard(Callable&& cleanupFunction)
29
+ try : f(std::forward<Callable>(cleanupFunction))
30
+ {
31
+ }
32
+ catch (...)
33
+ {
34
+ cleanupFunction();
35
+ throw;
36
+ }
37
+
38
+ ~ScopeGuard()
39
+ {
40
+ if (f)
41
+ f();
42
+ }
43
+
44
+ void dismiss() noexcept
45
+ {
46
+ f = nullptr;
47
+ }
48
+
49
+ ScopeGuard(const ScopeGuard&) = delete;
50
+ ScopeGuard(ScopeGuard&& other) noexcept = delete;
51
+ ScopeGuard& operator=(const ScopeGuard&) = delete;
52
+ ScopeGuard& operator=(const ScopeGuard&&) = delete;
53
+
54
+ private:
55
+ std::function<void()> f;
56
+ };
local-test-freerdp-delta-01/afc-freerdp/client/SDL/common/sdl_prefs.cpp ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * SDL Prefs
4
+ *
5
+ * Copyright 2022 Armin Novak <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <iostream>
21
+ #include <fstream>
22
+ #if __has_include(<filesystem>)
23
+ #include <filesystem>
24
+ #include <utility>
25
+ namespace fs = std::filesystem;
26
+ #elif __has_include(<experimental/filesystem>)
27
+ #include <experimental/filesystem>
28
+ namespace fs = std::experimental::filesystem;
29
+ #else
30
+ #error Could not find system header "<filesystem>" or "<experimental/filesystem>"
31
+ #endif
32
+
33
+ #include "sdl_prefs.hpp"
34
+
35
+ #include <winpr/path.h>
36
+ #include <winpr/config.h>
37
+ #include <freerdp/version.h>
38
+ #include <winpr/json.h>
39
+ #include <freerdp/settings.h>
40
+
41
+ SdlPref::WINPR_JSONPtr SdlPref::get()
42
+ {
43
+ auto config = get_pref_file();
44
+
45
+ std::ifstream ifs(config);
46
+ std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
47
+ return { WINPR_JSON_ParseWithLength(content.c_str(), content.size()), WINPR_JSON_Delete };
48
+ }
49
+
50
+ WINPR_JSON* SdlPref::get_item(const std::string& key)
51
+ {
52
+ if (!_config)
53
+ return nullptr;
54
+ return WINPR_JSON_GetObjectItem(_config.get(), key.c_str());
55
+ }
56
+
57
+ std::string SdlPref::item_to_str(WINPR_JSON* item, const std::string& fallback)
58
+ {
59
+ if (!item || !WINPR_JSON_IsString(item))
60
+ return fallback;
61
+ auto str = WINPR_JSON_GetStringValue(item);
62
+ if (!str)
63
+ return {};
64
+ return str;
65
+ }
66
+
67
+ std::string SdlPref::get_string(const std::string& key, const std::string& fallback)
68
+ {
69
+ auto item = get_item(key);
70
+ return item_to_str(item, fallback);
71
+ }
72
+
73
+ bool SdlPref::get_bool(const std::string& key, bool fallback)
74
+ {
75
+ auto item = get_item(key);
76
+ if (!item || !WINPR_JSON_IsBool(item))
77
+ return fallback;
78
+ return WINPR_JSON_IsTrue(item);
79
+ }
80
+
81
+ int64_t SdlPref::get_int(const std::string& key, int64_t fallback)
82
+ {
83
+ auto item = get_item(key);
84
+ if (!item || !WINPR_JSON_IsNumber(item))
85
+ return fallback;
86
+ auto val = WINPR_JSON_GetNumberValue(item);
87
+ return static_cast<int64_t>(val);
88
+ }
89
+
90
+ std::vector<std::string> SdlPref::get_array(const std::string& key,
91
+ const std::vector<std::string>& fallback)
92
+ {
93
+ auto item = get_item(key);
94
+ if (!item || !WINPR_JSON_IsArray(item))
95
+ return fallback;
96
+
97
+ std::vector<std::string> values;
98
+ for (size_t x = 0; x < WINPR_JSON_GetArraySize(item); x++)
99
+ {
100
+ auto cur = WINPR_JSON_GetArrayItem(item, x);
101
+ values.push_back(item_to_str(cur));
102
+ }
103
+
104
+ return values;
105
+ }
106
+
107
+ void SdlPref::print_config_file_help(int version)
108
+ {
109
+ #if defined(WITH_WINPR_JSON)
110
+ const std::string url = "https://wiki.libsdl.org/SDL" + std::to_string(version);
111
+ std::cout << "CONFIGURATION FILE" << std::endl;
112
+ std::cout << std::endl;
113
+ std::cout << " The SDL client supports some user defined configuration options." << std::endl;
114
+ std::cout << " Settings are stored in JSON format" << std::endl;
115
+ std::cout << " The location is a per user file. Location for current user is "
116
+ << SdlPref::instance()->get_pref_file() << std::endl;
117
+ std::cout
118
+ << " The XDG_CONFIG_HOME environment variable can be used to override the base directory."
119
+ << std::endl;
120
+ std::cout << std::endl;
121
+ std::cout << " The following configuration options are supported:" << std::endl;
122
+ std::cout << std::endl;
123
+ std::cout << " SDL_KeyModMask" << std::endl;
124
+ std::cout << " Defines the key combination required for SDL client shortcuts."
125
+ << std::endl;
126
+ std::cout << " Default KMOD_RSHIFT" << std::endl;
127
+ std::cout << " An array of SDL_Keymod strings as defined at "
128
+ ""
129
+ << url << "/SDL_Keymod" << std::endl;
130
+ std::cout << std::endl;
131
+ std::cout << " SDL_Fullscreen" << std::endl;
132
+ std::cout << " Toggles client fullscreen state." << std::endl;
133
+ std::cout << " Default SDL_SCANCODE_RETURN." << std::endl;
134
+ std::cout << " A string as "
135
+ "defined at "
136
+ << url << "/SDLScancodeLookup" << std::endl;
137
+ std::cout << std::endl;
138
+ std::cout << " SDL_Minimize" << std::endl;
139
+ std::cout << " Minimizes client windows." << std::endl;
140
+ std::cout << " Default SDL_SCANCODE_M." << std::endl;
141
+ std::cout << " A string as "
142
+ "defined at "
143
+ << url << "/SDLScancodeLookup" << std::endl;
144
+ std::cout << std::endl;
145
+ std::cout << " SDL_Resizeable" << std::endl;
146
+ std::cout << " Toggles local window resizeable state." << std::endl;
147
+ std::cout << " Default SDL_SCANCODE_R." << std::endl;
148
+ std::cout << " A string as "
149
+ "defined at "
150
+ << url << "/SDLScancodeLookup" << std::endl;
151
+ std::cout << std::endl;
152
+ std::cout << " SDL_Grab" << std::endl;
153
+ std::cout << " Toggles keyboard and mouse grab state." << std::endl;
154
+ std::cout << " Default SDL_SCANCODE_G." << std::endl;
155
+ std::cout << " A string as "
156
+ "defined at "
157
+ << url << "/SDLScancodeLookup" << std::endl;
158
+ std::cout << std::endl;
159
+ std::cout << " SDL_Disconnect" << std::endl;
160
+ std::cout << " Disconnects from the RDP session." << std::endl;
161
+ std::cout << " Default SDL_SCANCODE_D." << std::endl;
162
+ std::cout << " A string as defined at " << url << "/SDLScancodeLookup" << std::endl;
163
+ #endif
164
+ }
165
+
166
+ SdlPref::SdlPref(std::string file) : _name(std::move(file)), _config(get())
167
+ {
168
+ }
169
+
170
+ std::string SdlPref::get_pref_dir()
171
+ {
172
+ using CStringPtr = std::unique_ptr<char, decltype(&free)>;
173
+ CStringPtr path(freerdp_settings_get_config_path(), free);
174
+ if (!path)
175
+ return {};
176
+
177
+ fs::path config{ path.get() };
178
+ return config.string();
179
+ }
180
+
181
+ std::string SdlPref::get_default_file()
182
+ {
183
+ fs::path config{ SdlPref::get_pref_dir() };
184
+ config /= "sdl-freerdp.json";
185
+ return config.string();
186
+ }
187
+
188
+ std::shared_ptr<SdlPref> SdlPref::instance(const std::string& name)
189
+ {
190
+ static std::shared_ptr<SdlPref> _instance;
191
+ if (!_instance || (_instance->get_pref_file() != name))
192
+ _instance.reset(new SdlPref(name));
193
+ return _instance;
194
+ }
195
+
196
+ std::string SdlPref::get_pref_file()
197
+ {
198
+ return _name;
199
+ }
local-test-freerdp-full-01/afc-freerdp/client/X11/cli/xfreerdp.c ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * X11 Client
4
+ *
5
+ * Copyright 2011 Marc-Andre Moreau <[email protected]>
6
+ * Copyright 2012 HP Development Company, LLC
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #include <freerdp/config.h>
22
+
23
+ #include <winpr/crt.h>
24
+ #include <winpr/synch.h>
25
+ #include <winpr/thread.h>
26
+
27
+ #include <freerdp/streamdump.h>
28
+ #include <freerdp/freerdp.h>
29
+ #include <freerdp/client/cmdline.h>
30
+
31
+ #include "../xf_client.h"
32
+ #include "../xfreerdp.h"
33
+
34
+ static void xfreerdp_print_help(void)
35
+ {
36
+ printf("Keyboard Shortcuts:\n");
37
+ printf("\t<Right CTRL>\n");
38
+ printf("\t\treleases keyboard and mouse grab\n");
39
+ printf("\t<CTRL>+<ALT>+<Return>\n");
40
+ printf("\t\ttoggles fullscreen state of the application\n");
41
+ printf("\t<CTRL>+<ALT>+c\n");
42
+ printf("\t\ttoggles remote control in a remote assistance session\n");
43
+ printf("\t<CTRL>+<ALT>+m\n");
44
+ printf("\t\tminimizes the application\n");
45
+ printf("\tAction Script\n");
46
+ printf("\t\tExecutes a predefined script on key press.\n");
47
+ printf("\t\tShould the script not exist it is ignored.\n");
48
+ printf("\t\tScripts can be provided at the default location ~/.config/freerdp/action.sh or as "
49
+ "command line argument /action:script:<path>\n");
50
+ printf("\t\tThe script will receive the current key combination as argument.\n");
51
+ printf("\t\tThe output of the script is parsed for 'key-local' which tells that the script "
52
+ "used the key combination, otherwise the combination is forwarded to the remote.\n");
53
+ }
54
+
55
+ int main(int argc, char* argv[])
56
+ {
57
+ int rc = 1;
58
+ int status = 0;
59
+ HANDLE thread = NULL;
60
+ xfContext* xfc = NULL;
61
+ DWORD dwExitCode = 0;
62
+ rdpContext* context = NULL;
63
+ rdpSettings* settings = NULL;
64
+ RDP_CLIENT_ENTRY_POINTS clientEntryPoints = { 0 };
65
+
66
+ clientEntryPoints.Size = sizeof(RDP_CLIENT_ENTRY_POINTS);
67
+ clientEntryPoints.Version = RDP_CLIENT_INTERFACE_VERSION;
68
+
69
+ RdpClientEntry(&clientEntryPoints);
70
+
71
+ context = freerdp_client_context_new(&clientEntryPoints);
72
+ if (!context)
73
+ return 1;
74
+
75
+ settings = context->settings;
76
+ xfc = (xfContext*)context;
77
+
78
+ status = freerdp_client_settings_parse_command_line(context->settings, argc, argv, FALSE);
79
+ if (status)
80
+ {
81
+ rc = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
82
+
83
+ if (freerdp_settings_get_bool(settings, FreeRDP_ListMonitors))
84
+ xf_list_monitors(xfc);
85
+ else
86
+ {
87
+ switch (status)
88
+ {
89
+ case COMMAND_LINE_STATUS_PRINT:
90
+ case COMMAND_LINE_STATUS_PRINT_VERSION:
91
+ case COMMAND_LINE_STATUS_PRINT_BUILDCONFIG:
92
+ break;
93
+ case COMMAND_LINE_STATUS_PRINT_HELP:
94
+ default:
95
+ xfreerdp_print_help();
96
+ break;
97
+ }
98
+ }
99
+ goto out;
100
+ }
101
+
102
+ if (!stream_dump_register_handlers(context, CONNECTION_STATE_MCS_CREATE_REQUEST, FALSE))
103
+ goto out;
104
+
105
+ if (freerdp_client_start(context) != 0)
106
+ goto out;
107
+
108
+ thread = freerdp_client_get_thread(context);
109
+
110
+ (void)WaitForSingleObject(thread, INFINITE);
111
+ GetExitCodeThread(thread, &dwExitCode);
112
+ rc = xf_exit_code_from_disconnect_reason(dwExitCode);
113
+
114
+ freerdp_client_stop(context);
115
+
116
+ out:
117
+ freerdp_client_context_free(context);
118
+
119
+ return rc;
120
+ }
local-test-freerdp-full-01/afc-freerdp/client/X11/resource/lock.xbm ADDED
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/audio.c ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Audio Formats
4
+ *
5
+ * Copyright 2013 Marc-Andre Moreau <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include <winpr/crt.h>
23
+
24
+ #include <freerdp/log.h>
25
+ #include <freerdp/codec/audio.h>
26
+
27
+ #define TAG FREERDP_TAG("codec")
28
+
29
+ UINT32 audio_format_compute_time_length(const AUDIO_FORMAT* format, size_t size)
30
+ {
31
+ UINT32 mstime = 0;
32
+ UINT32 wSamples = 0;
33
+
34
+ /**
35
+ * [MSDN-AUDIOFORMAT]:
36
+ * http://msdn.microsoft.com/en-us/library/ms713497.aspx
37
+ */
38
+
39
+ if (format->wBitsPerSample)
40
+ {
41
+ const size_t samples = (size * 8) / format->wBitsPerSample;
42
+ WINPR_ASSERT(samples <= UINT32_MAX);
43
+ wSamples = (UINT32)samples;
44
+ mstime = (((wSamples * 1000) / format->nSamplesPerSec) / format->nChannels);
45
+ }
46
+ else
47
+ {
48
+ mstime = 0;
49
+
50
+ if (format->wFormatTag == WAVE_FORMAT_GSM610)
51
+ {
52
+ UINT16 nSamplesPerBlock = 0;
53
+
54
+ if ((format->cbSize == 2) && (format->data))
55
+ {
56
+ nSamplesPerBlock = *((UINT16*)format->data);
57
+ const size_t samples = (size / format->nBlockAlign) * nSamplesPerBlock;
58
+ WINPR_ASSERT(samples <= UINT32_MAX);
59
+ wSamples = (UINT32)samples;
60
+ mstime = (((wSamples * 1000) / format->nSamplesPerSec) / format->nChannels);
61
+ }
62
+ else
63
+ {
64
+ WLog_ERR(TAG,
65
+ "audio_format_compute_time_length: invalid WAVE_FORMAT_GSM610 format");
66
+ }
67
+ }
68
+ else
69
+ {
70
+ WLog_ERR(TAG, "audio_format_compute_time_length: unknown format %" PRIu16 "",
71
+ format->wFormatTag);
72
+ }
73
+ }
74
+
75
+ return mstime;
76
+ }
77
+
78
+ const char* audio_format_get_tag_string(UINT16 wFormatTag)
79
+ {
80
+ switch (wFormatTag)
81
+ {
82
+ case WAVE_FORMAT_PCM:
83
+ return "WAVE_FORMAT_PCM";
84
+
85
+ case WAVE_FORMAT_ADPCM:
86
+ return "WAVE_FORMAT_ADPCM";
87
+
88
+ case WAVE_FORMAT_ALAW:
89
+ return "WAVE_FORMAT_ALAW";
90
+
91
+ case WAVE_FORMAT_MULAW:
92
+ return "WAVE_FORMAT_MULAW";
93
+
94
+ case WAVE_FORMAT_DVI_ADPCM:
95
+ return "WAVE_FORMAT_DVI_ADPCM";
96
+
97
+ case WAVE_FORMAT_GSM610:
98
+ return "WAVE_FORMAT_GSM610";
99
+
100
+ case WAVE_FORMAT_MSG723:
101
+ return "WAVE_FORMAT_MSG723";
102
+
103
+ case WAVE_FORMAT_DSPGROUP_TRUESPEECH:
104
+ return "WAVE_FORMAT_DSPGROUP_TRUESPEECH ";
105
+
106
+ case WAVE_FORMAT_MPEGLAYER3:
107
+ return "WAVE_FORMAT_MPEGLAYER3";
108
+
109
+ case WAVE_FORMAT_WMAUDIO2:
110
+ return "WAVE_FORMAT_WMAUDIO2";
111
+
112
+ case WAVE_FORMAT_AAC_MS:
113
+ return "WAVE_FORMAT_AAC_MS";
114
+ default:
115
+ return "WAVE_FORMAT_UNKNOWN";
116
+ }
117
+ }
118
+
119
+ void audio_format_print(wLog* log, DWORD level, const AUDIO_FORMAT* format)
120
+ {
121
+ WLog_Print(log, level,
122
+ "%s:\t wFormatTag: 0x%04" PRIX16 " nChannels: %" PRIu16 " nSamplesPerSec: %" PRIu32
123
+ " "
124
+ "nAvgBytesPerSec: %" PRIu32 " nBlockAlign: %" PRIu16 " wBitsPerSample: %" PRIu16
125
+ " cbSize: %" PRIu16 "",
126
+ audio_format_get_tag_string(format->wFormatTag), format->wFormatTag,
127
+ format->nChannels, format->nSamplesPerSec, format->nAvgBytesPerSec,
128
+ format->nBlockAlign, format->wBitsPerSample, format->cbSize);
129
+ }
130
+
131
+ void audio_formats_print(wLog* log, DWORD level, const AUDIO_FORMAT* formats, UINT16 count)
132
+ {
133
+ if (formats)
134
+ {
135
+ WLog_Print(log, level, "AUDIO_FORMATS (%" PRIu16 ") ={", count);
136
+
137
+ for (UINT32 index = 0; index < count; index++)
138
+ {
139
+ const AUDIO_FORMAT* format = &formats[index];
140
+ WLog_Print(log, level, "\t");
141
+ audio_format_print(log, level, format);
142
+ }
143
+
144
+ WLog_Print(log, level, "}");
145
+ }
146
+ }
147
+
148
+ BOOL audio_format_read(wStream* s, AUDIO_FORMAT* format)
149
+ {
150
+ if (!s || !format)
151
+ return FALSE;
152
+
153
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 18))
154
+ return FALSE;
155
+
156
+ Stream_Read_UINT16(s, format->wFormatTag);
157
+ Stream_Read_UINT16(s, format->nChannels);
158
+ Stream_Read_UINT32(s, format->nSamplesPerSec);
159
+ Stream_Read_UINT32(s, format->nAvgBytesPerSec);
160
+ Stream_Read_UINT16(s, format->nBlockAlign);
161
+ Stream_Read_UINT16(s, format->wBitsPerSample);
162
+ Stream_Read_UINT16(s, format->cbSize);
163
+
164
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, format->cbSize))
165
+ return FALSE;
166
+
167
+ format->data = NULL;
168
+
169
+ if (format->cbSize > 0)
170
+ {
171
+ format->data = malloc(format->cbSize);
172
+
173
+ if (!format->data)
174
+ return FALSE;
175
+
176
+ Stream_Read(s, format->data, format->cbSize);
177
+ }
178
+
179
+ return TRUE;
180
+ }
181
+
182
+ BOOL audio_format_write(wStream* s, const AUDIO_FORMAT* format)
183
+ {
184
+ if (!s || !format)
185
+ return FALSE;
186
+
187
+ if (!Stream_EnsureRemainingCapacity(s, 18 + format->cbSize))
188
+ return FALSE;
189
+
190
+ Stream_Write_UINT16(s, format->wFormatTag); /* wFormatTag (WAVE_FORMAT_PCM) */
191
+ Stream_Write_UINT16(s, format->nChannels); /* nChannels */
192
+ Stream_Write_UINT32(s, format->nSamplesPerSec); /* nSamplesPerSec */
193
+ Stream_Write_UINT32(s, format->nAvgBytesPerSec); /* nAvgBytesPerSec */
194
+ Stream_Write_UINT16(s, format->nBlockAlign); /* nBlockAlign */
195
+ Stream_Write_UINT16(s, format->wBitsPerSample); /* wBitsPerSample */
196
+ Stream_Write_UINT16(s, format->cbSize); /* cbSize */
197
+
198
+ if (format->cbSize > 0)
199
+ Stream_Write(s, format->data, format->cbSize);
200
+
201
+ return TRUE;
202
+ }
203
+
204
+ BOOL audio_format_copy(const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
205
+ AUDIO_FORMAT* WINPR_RESTRICT dstFormat)
206
+ {
207
+ if (!srcFormat || !dstFormat)
208
+ return FALSE;
209
+
210
+ *dstFormat = *srcFormat;
211
+
212
+ if (srcFormat->cbSize > 0)
213
+ {
214
+ dstFormat->data = malloc(srcFormat->cbSize);
215
+
216
+ if (!dstFormat->data)
217
+ return FALSE;
218
+
219
+ memcpy(dstFormat->data, srcFormat->data, dstFormat->cbSize);
220
+ }
221
+
222
+ return TRUE;
223
+ }
224
+
225
+ BOOL audio_format_compatible(const AUDIO_FORMAT* with, const AUDIO_FORMAT* what)
226
+ {
227
+ if (!with || !what)
228
+ return FALSE;
229
+
230
+ if (with->wFormatTag != WAVE_FORMAT_UNKNOWN)
231
+ {
232
+ if (with->wFormatTag != what->wFormatTag)
233
+ return FALSE;
234
+ }
235
+
236
+ if (with->nChannels != 0)
237
+ {
238
+ if (with->nChannels != what->nChannels)
239
+ return FALSE;
240
+ }
241
+
242
+ if (with->nSamplesPerSec != 0)
243
+ {
244
+ if (with->nSamplesPerSec != what->nSamplesPerSec)
245
+ return FALSE;
246
+ }
247
+
248
+ if (with->wBitsPerSample != 0)
249
+ {
250
+ if (with->wBitsPerSample != what->wBitsPerSample)
251
+ return FALSE;
252
+ }
253
+
254
+ return TRUE;
255
+ }
256
+
257
+ AUDIO_FORMAT* audio_format_new(void)
258
+ {
259
+ return audio_formats_new(1);
260
+ }
261
+
262
+ AUDIO_FORMAT* audio_formats_new(size_t count)
263
+ {
264
+ return calloc(count, sizeof(AUDIO_FORMAT));
265
+ }
266
+
267
+ void audio_format_free(AUDIO_FORMAT* format)
268
+ {
269
+ if (format)
270
+ free(format->data);
271
+ }
272
+
273
+ void audio_formats_free(AUDIO_FORMAT* formats, size_t count)
274
+ {
275
+ if (formats)
276
+ {
277
+ for (size_t index = 0; index < count; index++)
278
+ {
279
+ AUDIO_FORMAT* format = &formats[index];
280
+ audio_format_free(format);
281
+ }
282
+
283
+ free(formats);
284
+ }
285
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/dsp.h ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Digital Sound Processing - backend
4
+ *
5
+ * Copyright 2018 Armin Novak <[email protected]>
6
+ * Copyright 2018 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_LIB_CODEC_DSP_H
22
+ #define FREERDP_LIB_CODEC_DSP_H
23
+
24
+ #include <freerdp/api.h>
25
+ #include <freerdp/codec/audio.h>
26
+ #include <freerdp/codec/dsp.h>
27
+
28
+ typedef struct
29
+ {
30
+ ALIGN64 AUDIO_FORMAT format;
31
+ ALIGN64 BOOL encoder;
32
+ ALIGN64 wStream* buffer;
33
+ ALIGN64 wStream* resample;
34
+ ALIGN64 wStream* channelmix;
35
+ #if defined(WITH_FDK_AAC)
36
+ ALIGN64 BOOL fdkSetup;
37
+ ALIGN64 void* fdkAacInstance;
38
+ ALIGN64 size_t buffersize;
39
+ ALIGN64 unsigned frames_per_packet;
40
+ #endif
41
+ } FREERDP_DSP_COMMON_CONTEXT;
42
+
43
+ BOOL freerdp_dsp_common_context_init(FREERDP_DSP_COMMON_CONTEXT* context, BOOL encode);
44
+ void freerdp_dsp_common_context_uninit(FREERDP_DSP_COMMON_CONTEXT* context);
45
+
46
+ #endif /* FREERDP_LIB_CODEC_DSP_H */
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/dsp_fdk_aac.c ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Digital Sound Processing
4
+ *
5
+ * Copyright 2022 Armin Novak <[email protected]>
6
+ * Copyright 2022 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #include "dsp_fdk_aac.h"
22
+ #include "dsp_fdk_impl.h"
23
+
24
+ #include <freerdp/log.h>
25
+ #define TAG FREERDP_TAG("dsp.fdk")
26
+
27
+ static void write_log(unsigned log_level, const char* fmt, ...)
28
+ {
29
+ wLog* log = WLog_Get(TAG);
30
+
31
+ if (WLog_IsLevelActive(log, log_level))
32
+ {
33
+ char buffer[1024] = { 0 };
34
+
35
+ va_list ap = { 0 };
36
+ va_start(ap, fmt);
37
+ vsnprintf(buffer, sizeof(buffer), fmt, ap);
38
+ va_end(ap);
39
+
40
+ WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, log_level, __LINE__, __FILE__, __func__, "%s",
41
+ buffer);
42
+ }
43
+ }
44
+
45
+ BOOL fdk_aac_dsp_encode(FREERDP_DSP_COMMON_CONTEXT* context, const AUDIO_FORMAT* srcFormat,
46
+ const BYTE* data, size_t length, wStream* out)
47
+ {
48
+ WINPR_ASSERT(context);
49
+ WINPR_ASSERT(srcFormat);
50
+
51
+ if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
52
+ {
53
+ WLog_WARN(TAG, "Feeding %s format data to encoder function, but require %s",
54
+ audio_format_get_tag_string(srcFormat->wFormatTag),
55
+ audio_format_get_tag_string(WAVE_FORMAT_PCM));
56
+ return FALSE;
57
+ }
58
+
59
+ if (!context->fdkSetup)
60
+ {
61
+ ssize_t rc = fdk_aac_dsp_impl_config(
62
+ context->fdkAacInstance, &context->buffersize, context->encoder,
63
+ context->format.nSamplesPerSec, context->format.nChannels,
64
+ context->format.nAvgBytesPerSec, context->frames_per_packet, write_log);
65
+ if (rc < 0)
66
+ return FALSE;
67
+
68
+ context->fdkSetup = TRUE;
69
+ }
70
+
71
+ if (!Stream_EnsureRemainingCapacity(out, context->buffersize))
72
+ return FALSE;
73
+
74
+ {
75
+ const ssize_t encoded =
76
+ fdk_aac_dsp_impl_encode(context->fdkAacInstance, data, length, Stream_Pointer(out),
77
+ Stream_GetRemainingCapacity(out), write_log);
78
+ if (encoded < 0)
79
+ return FALSE;
80
+ Stream_Seek(out, (size_t)encoded);
81
+ return TRUE;
82
+ }
83
+ }
84
+
85
+ BOOL fdk_aac_dsp_decode(FREERDP_DSP_COMMON_CONTEXT* context, const AUDIO_FORMAT* srcFormat,
86
+ const BYTE* data, size_t length, wStream* out)
87
+ {
88
+ WINPR_ASSERT(context);
89
+ WINPR_ASSERT(srcFormat);
90
+
91
+ if (srcFormat->wFormatTag != WAVE_FORMAT_AAC_MS)
92
+ {
93
+ WLog_WARN(TAG, "Feeding %s format data to encoder function, but require %s",
94
+ audio_format_get_tag_string(srcFormat->wFormatTag),
95
+ audio_format_get_tag_string(WAVE_FORMAT_AAC_MS));
96
+ return FALSE;
97
+ }
98
+
99
+ if (!context->fdkSetup)
100
+ {
101
+ ssize_t rc = fdk_aac_dsp_impl_config(
102
+ context->fdkAacInstance, &context->buffersize, context->encoder,
103
+ context->format.nSamplesPerSec, context->format.nChannels,
104
+ context->format.nAvgBytesPerSec, context->frames_per_packet, write_log);
105
+ if (rc < 0)
106
+ return FALSE;
107
+
108
+ context->fdkSetup = TRUE;
109
+ }
110
+
111
+ ssize_t rest = 0;
112
+ do
113
+ {
114
+ rest = fdk_aac_dsp_impl_decode_fill(context->fdkAacInstance, data, length, write_log);
115
+ if (rest < 0)
116
+ {
117
+ WLog_WARN(TAG, "DecodeFill() failed");
118
+ return FALSE;
119
+ }
120
+
121
+ ssize_t ret = -1;
122
+ do
123
+ {
124
+ const size_t expect = context->buffersize;
125
+ if (!Stream_EnsureRemainingCapacity(out, expect))
126
+ return FALSE;
127
+
128
+ ret = fdk_aac_dsp_impl_decode_read(context->fdkAacInstance, Stream_Pointer(out), expect,
129
+ write_log);
130
+ if (ret < 0)
131
+ return FALSE;
132
+
133
+ Stream_Seek(out, (size_t)ret);
134
+ } while (ret > 0);
135
+ } while (rest > 0);
136
+
137
+ return TRUE;
138
+ }
139
+
140
+ void fdk_aac_dsp_uninit(FREERDP_DSP_COMMON_CONTEXT* context)
141
+ {
142
+ WINPR_ASSERT(context);
143
+
144
+ fdk_aac_dsp_impl_uninit(&context->fdkAacInstance, context->encoder, write_log);
145
+ }
146
+
147
+ BOOL fdk_aac_dsp_init(FREERDP_DSP_COMMON_CONTEXT* context, size_t frames_per_packet)
148
+ {
149
+ WINPR_ASSERT(context);
150
+ context->fdkSetup = FALSE;
151
+ WINPR_ASSERT(frames_per_packet <= UINT_MAX);
152
+ context->frames_per_packet = (unsigned)frames_per_packet;
153
+ return fdk_aac_dsp_impl_init(&context->fdkAacInstance, context->encoder, write_log);
154
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/dsp_fdk_impl.h ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Digital Sound Processing
4
+ *
5
+ * Copyright 2022 Armin Novak <[email protected]>
6
+ * Copyright 2022 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_DSP_FDK_IMPL_H_
22
+ #define FREERDP_DSP_FDK_IMPL_H_
23
+
24
+ #include <stdlib.h>
25
+
26
+ typedef void (*fdk_log_fkt_t)(unsigned log_level, const char* fmt, ...);
27
+
28
+ int fdk_aac_dsp_impl_init(void** handle, int encoder, fdk_log_fkt_t log);
29
+ void fdk_aac_dsp_impl_uninit(void** handle, int encoder, fdk_log_fkt_t log);
30
+
31
+ ssize_t fdk_aac_dsp_impl_stream_info(void* handle, int encoder, fdk_log_fkt_t log);
32
+
33
+ int fdk_aac_dsp_impl_config(void* handle, size_t* pbuffersize, int encoder, unsigned samplerate,
34
+ unsigned channels, unsigned bytes_per_second,
35
+ unsigned frames_per_packet, fdk_log_fkt_t log);
36
+
37
+ ssize_t fdk_aac_dsp_impl_decode_fill(void* handle, const void* data, size_t size,
38
+ fdk_log_fkt_t log);
39
+
40
+ ssize_t fdk_aac_dsp_impl_encode(void* handle, const void* data, size_t size, void* dst,
41
+ size_t dstSize, fdk_log_fkt_t log);
42
+
43
+ ssize_t fdk_aac_dsp_impl_decode_read(void* handle, void* dst, size_t dstSize, fdk_log_fkt_t log);
44
+
45
+ #endif
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/planar.c ADDED
@@ -0,0 +1,1797 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RDP6 Planar Codec
4
+ *
5
+ * Copyright 2013 Marc-Andre Moreau <[email protected]>
6
+ * Copyright 2016 Armin Novak <[email protected]>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <winpr/crt.h>
25
+ #include <winpr/wtypes.h>
26
+ #include <winpr/assert.h>
27
+ #include <winpr/cast.h>
28
+ #include <winpr/print.h>
29
+
30
+ #include <freerdp/primitives.h>
31
+ #include <freerdp/log.h>
32
+ #include <freerdp/codec/bitmap.h>
33
+ #include <freerdp/codec/planar.h>
34
+
35
+ #define TAG FREERDP_TAG("codec")
36
+
37
+ #define PLANAR_ALIGN(val, align) \
38
+ ((val) % (align) == 0) ? (val) : ((val) + (align) - (val) % (align))
39
+
40
+ typedef struct
41
+ {
42
+ /**
43
+ * controlByte:
44
+ * [0-3]: nRunLength
45
+ * [4-7]: cRawBytes
46
+ */
47
+ BYTE controlByte;
48
+ BYTE* rawValues;
49
+ } RDP6_RLE_SEGMENT;
50
+
51
+ typedef struct
52
+ {
53
+ UINT32 cSegments;
54
+ RDP6_RLE_SEGMENT* segments;
55
+ } RDP6_RLE_SEGMENTS;
56
+
57
+ typedef struct
58
+ {
59
+ /**
60
+ * formatHeader:
61
+ * [0-2]: Color Loss Level (CLL)
62
+ * [3] : Chroma Subsampling (CS)
63
+ * [4] : Run Length Encoding (RLE)
64
+ * [5] : No Alpha (NA)
65
+ * [6-7]: Reserved
66
+ */
67
+ BYTE formatHeader;
68
+ } RDP6_BITMAP_STREAM;
69
+
70
+ struct S_BITMAP_PLANAR_CONTEXT
71
+ {
72
+ UINT32 maxWidth;
73
+ UINT32 maxHeight;
74
+ UINT32 maxPlaneSize;
75
+
76
+ BOOL AllowSkipAlpha;
77
+ BOOL AllowRunLengthEncoding;
78
+ BOOL AllowColorSubsampling;
79
+ BOOL AllowDynamicColorFidelity;
80
+
81
+ UINT32 ColorLossLevel;
82
+
83
+ BYTE* planes[4];
84
+ BYTE* planesBuffer;
85
+
86
+ BYTE* deltaPlanes[4];
87
+ BYTE* deltaPlanesBuffer;
88
+
89
+ BYTE* rlePlanes[4];
90
+ BYTE* rlePlanesBuffer;
91
+
92
+ BYTE* pTempData;
93
+ UINT32 nTempStep;
94
+
95
+ BOOL bgr;
96
+ BOOL topdown;
97
+ };
98
+
99
+ static INLINE UINT32 planar_invert_format(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL alpha,
100
+ UINT32 DstFormat)
101
+ {
102
+
103
+ if (planar->bgr && alpha)
104
+ {
105
+ switch (DstFormat)
106
+ {
107
+ case PIXEL_FORMAT_ARGB32:
108
+ DstFormat = PIXEL_FORMAT_ABGR32;
109
+ break;
110
+ case PIXEL_FORMAT_XRGB32:
111
+ DstFormat = PIXEL_FORMAT_XBGR32;
112
+ break;
113
+ case PIXEL_FORMAT_ABGR32:
114
+ DstFormat = PIXEL_FORMAT_ARGB32;
115
+ break;
116
+ case PIXEL_FORMAT_XBGR32:
117
+ DstFormat = PIXEL_FORMAT_XRGB32;
118
+ break;
119
+ case PIXEL_FORMAT_BGRA32:
120
+ DstFormat = PIXEL_FORMAT_RGBA32;
121
+ break;
122
+ case PIXEL_FORMAT_BGRX32:
123
+ DstFormat = PIXEL_FORMAT_RGBX32;
124
+ break;
125
+ case PIXEL_FORMAT_RGBA32:
126
+ DstFormat = PIXEL_FORMAT_BGRA32;
127
+ break;
128
+ case PIXEL_FORMAT_RGBX32:
129
+ DstFormat = PIXEL_FORMAT_BGRX32;
130
+ break;
131
+ case PIXEL_FORMAT_RGB24:
132
+ DstFormat = PIXEL_FORMAT_BGR24;
133
+ break;
134
+ case PIXEL_FORMAT_BGR24:
135
+ DstFormat = PIXEL_FORMAT_RGB24;
136
+ break;
137
+ case PIXEL_FORMAT_RGB16:
138
+ DstFormat = PIXEL_FORMAT_BGR16;
139
+ break;
140
+ case PIXEL_FORMAT_BGR16:
141
+ DstFormat = PIXEL_FORMAT_RGB16;
142
+ break;
143
+ case PIXEL_FORMAT_ARGB15:
144
+ DstFormat = PIXEL_FORMAT_ABGR15;
145
+ break;
146
+ case PIXEL_FORMAT_RGB15:
147
+ DstFormat = PIXEL_FORMAT_BGR15;
148
+ break;
149
+ case PIXEL_FORMAT_ABGR15:
150
+ DstFormat = PIXEL_FORMAT_ARGB15;
151
+ break;
152
+ case PIXEL_FORMAT_BGR15:
153
+ DstFormat = PIXEL_FORMAT_RGB15;
154
+ break;
155
+ default:
156
+ break;
157
+ }
158
+ }
159
+ return DstFormat;
160
+ }
161
+
162
+ static INLINE BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane,
163
+ UINT32 width, UINT32 height,
164
+ BYTE* WINPR_RESTRICT outPlane,
165
+ UINT32* WINPR_RESTRICT dstSize);
166
+ static INLINE BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane,
167
+ UINT32 width, UINT32 height,
168
+ BYTE* WINPR_RESTRICT outPlane);
169
+
170
+ static INLINE INT32 planar_skip_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
171
+ UINT32 nWidth, UINT32 nHeight)
172
+ {
173
+ UINT32 used = 0;
174
+
175
+ WINPR_ASSERT(pSrcData);
176
+
177
+ for (UINT32 y = 0; y < nHeight; y++)
178
+ {
179
+ for (UINT32 x = 0; x < nWidth;)
180
+ {
181
+ if (used >= SrcSize)
182
+ {
183
+ WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used,
184
+ SrcSize);
185
+ return -1;
186
+ }
187
+
188
+ const uint8_t controlByte = pSrcData[used++];
189
+ uint32_t nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
190
+ uint32_t cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
191
+
192
+ if (nRunLength == 1)
193
+ {
194
+ nRunLength = cRawBytes + 16;
195
+ cRawBytes = 0;
196
+ }
197
+ else if (nRunLength == 2)
198
+ {
199
+ nRunLength = cRawBytes + 32;
200
+ cRawBytes = 0;
201
+ }
202
+
203
+ used += cRawBytes;
204
+ x += cRawBytes;
205
+ x += nRunLength;
206
+
207
+ if (x > nWidth)
208
+ {
209
+ WLog_ERR(TAG, "planar plane x %" PRIu32 " exceeds width %" PRIu32, x, nWidth);
210
+ return -1;
211
+ }
212
+
213
+ if (used > SrcSize)
214
+ {
215
+ WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used,
216
+ INT32_MAX);
217
+ return -1;
218
+ }
219
+ }
220
+ }
221
+
222
+ if (used > INT32_MAX)
223
+ {
224
+ WLog_ERR(TAG, "planar plane used %" PRIu32 " exceeds SrcSize %" PRIu32, used, SrcSize);
225
+ return -1;
226
+ }
227
+ return (INT32)used;
228
+ }
229
+
230
+ static inline UINT8 clamp(INT16 val)
231
+ {
232
+ return (UINT8)val;
233
+ }
234
+
235
+ static INLINE INT32 planar_decompress_plane_rle_only(const BYTE* WINPR_RESTRICT pSrcData,
236
+ UINT32 SrcSize, BYTE* WINPR_RESTRICT pDstData,
237
+ UINT32 nWidth, UINT32 nHeight)
238
+ {
239
+ BYTE* previousScanline = NULL;
240
+ const BYTE* srcp = pSrcData;
241
+
242
+ WINPR_ASSERT(nHeight <= INT32_MAX);
243
+ WINPR_ASSERT(nWidth <= INT32_MAX);
244
+
245
+ for (UINT32 y = 0; y < nHeight; y++)
246
+ {
247
+ BYTE* dstp = &pDstData[1ULL * (y)*nWidth];
248
+ INT16 pixel = 0;
249
+ BYTE* currentScanline = dstp;
250
+
251
+ for (UINT32 x = 0; x < nWidth;)
252
+ {
253
+ BYTE controlByte = *srcp;
254
+ srcp++;
255
+
256
+ if ((srcp - pSrcData) > SrcSize * 1ll)
257
+ {
258
+ WLog_ERR(TAG, "error reading input buffer");
259
+ return -1;
260
+ }
261
+
262
+ UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
263
+ UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
264
+
265
+ if (nRunLength == 1)
266
+ {
267
+ nRunLength = cRawBytes + 16;
268
+ cRawBytes = 0;
269
+ }
270
+ else if (nRunLength == 2)
271
+ {
272
+ nRunLength = cRawBytes + 32;
273
+ cRawBytes = 0;
274
+ }
275
+
276
+ if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 1ll)
277
+ {
278
+ WLog_ERR(TAG, "too many pixels in scanline");
279
+ return -1;
280
+ }
281
+
282
+ if (!previousScanline)
283
+ {
284
+ /* first scanline, absolute values */
285
+ while (cRawBytes > 0)
286
+ {
287
+ pixel = *srcp;
288
+ srcp++;
289
+ *dstp = clamp(pixel);
290
+ dstp++;
291
+ x++;
292
+ cRawBytes--;
293
+ }
294
+
295
+ while (nRunLength > 0)
296
+ {
297
+ *dstp = clamp(pixel);
298
+ dstp++;
299
+ x++;
300
+ nRunLength--;
301
+ }
302
+ }
303
+ else
304
+ {
305
+ /* delta values relative to previous scanline */
306
+ while (cRawBytes > 0)
307
+ {
308
+ UINT8 deltaValue = *srcp;
309
+ srcp++;
310
+
311
+ if (deltaValue & 1)
312
+ {
313
+ deltaValue = deltaValue >> 1;
314
+ deltaValue = deltaValue + 1;
315
+ pixel = WINPR_ASSERTING_INT_CAST(int16_t, -1 * (int16_t)deltaValue);
316
+ }
317
+ else
318
+ {
319
+ deltaValue = deltaValue >> 1;
320
+ pixel = WINPR_ASSERTING_INT_CAST(INT16, deltaValue);
321
+ }
322
+
323
+ const INT16 delta =
324
+ WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel);
325
+ *dstp = clamp(delta);
326
+ dstp++;
327
+ x++;
328
+ cRawBytes--;
329
+ }
330
+
331
+ while (nRunLength > 0)
332
+ {
333
+ const INT16 deltaValue =
334
+ WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[x] + pixel);
335
+ *dstp = clamp(deltaValue);
336
+ dstp++;
337
+ x++;
338
+ nRunLength--;
339
+ }
340
+ }
341
+ }
342
+
343
+ previousScanline = currentScanline;
344
+ }
345
+
346
+ return (INT32)(srcp - pSrcData);
347
+ }
348
+
349
+ static INLINE INT32 planar_decompress_plane_rle(const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
350
+ BYTE* WINPR_RESTRICT pDstData, UINT32 nDstStep,
351
+ UINT32 nXDst, UINT32 nYDst, UINT32 nWidth,
352
+ UINT32 nHeight, UINT32 nChannel, BOOL vFlip)
353
+ {
354
+ INT32 beg = 0;
355
+ INT32 end = 0;
356
+ INT32 inc = 0;
357
+ BYTE* previousScanline = NULL;
358
+ const BYTE* srcp = pSrcData;
359
+
360
+ WINPR_ASSERT(nHeight <= INT32_MAX);
361
+ WINPR_ASSERT(nWidth <= INT32_MAX);
362
+ WINPR_ASSERT(nDstStep <= INT32_MAX);
363
+
364
+ previousScanline = NULL;
365
+
366
+ if (vFlip)
367
+ {
368
+ beg = (INT32)nHeight - 1;
369
+ end = -1;
370
+ inc = -1;
371
+ }
372
+ else
373
+ {
374
+ beg = 0;
375
+ end = (INT32)nHeight;
376
+ inc = 1;
377
+ }
378
+
379
+ for (INT32 y = beg; y != end; y += inc)
380
+ {
381
+ const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL;
382
+ BYTE* dstp = &pDstData[off];
383
+ BYTE* currentScanline = dstp;
384
+ INT16 pixel = 0;
385
+
386
+ for (INT32 x = 0; x < (INT32)nWidth;)
387
+ {
388
+ const BYTE controlByte = *srcp;
389
+ srcp++;
390
+
391
+ if ((srcp - pSrcData) > SrcSize * 1ll)
392
+ {
393
+ WLog_ERR(TAG, "error reading input buffer");
394
+ return -1;
395
+ }
396
+
397
+ UINT32 nRunLength = PLANAR_CONTROL_BYTE_RUN_LENGTH(controlByte);
398
+ UINT32 cRawBytes = PLANAR_CONTROL_BYTE_RAW_BYTES(controlByte);
399
+
400
+ if (nRunLength == 1)
401
+ {
402
+ nRunLength = cRawBytes + 16;
403
+ cRawBytes = 0;
404
+ }
405
+ else if (nRunLength == 2)
406
+ {
407
+ nRunLength = cRawBytes + 32;
408
+ cRawBytes = 0;
409
+ }
410
+
411
+ if (((dstp + (cRawBytes + nRunLength)) - currentScanline) > nWidth * 4ll)
412
+ {
413
+ WLog_ERR(TAG, "too many pixels in scanline");
414
+ return -1;
415
+ }
416
+
417
+ if (!previousScanline)
418
+ {
419
+ /* first scanline, absolute values */
420
+ while (cRawBytes > 0)
421
+ {
422
+ pixel = *srcp;
423
+ srcp++;
424
+ *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel);
425
+ dstp += 4;
426
+ x++;
427
+ cRawBytes--;
428
+ }
429
+
430
+ while (nRunLength > 0)
431
+ {
432
+ *dstp = WINPR_ASSERTING_INT_CAST(BYTE, pixel);
433
+ dstp += 4;
434
+ x++;
435
+ nRunLength--;
436
+ }
437
+ }
438
+ else
439
+ {
440
+ /* delta values relative to previous scanline */
441
+ while (cRawBytes > 0)
442
+ {
443
+ BYTE deltaValue = *srcp;
444
+ srcp++;
445
+
446
+ if (deltaValue & 1)
447
+ {
448
+ deltaValue = deltaValue >> 1;
449
+ deltaValue = deltaValue + 1;
450
+ pixel = WINPR_ASSERTING_INT_CAST(int16_t, -deltaValue);
451
+ }
452
+ else
453
+ {
454
+ deltaValue = deltaValue >> 1;
455
+ pixel = deltaValue;
456
+ }
457
+
458
+ const INT16 delta =
459
+ WINPR_ASSERTING_INT_CAST(int16_t, previousScanline[4LL * x] + pixel);
460
+ *dstp = clamp(delta);
461
+ dstp += 4;
462
+ x++;
463
+ cRawBytes--;
464
+ }
465
+
466
+ while (nRunLength > 0)
467
+ {
468
+ const INT16 deltaValue =
469
+ WINPR_ASSERTING_INT_CAST(int16_t, pixel + previousScanline[4LL * x]);
470
+ *dstp = clamp(deltaValue);
471
+ dstp += 4;
472
+ x++;
473
+ nRunLength--;
474
+ }
475
+ }
476
+ }
477
+
478
+ previousScanline = currentScanline;
479
+ }
480
+
481
+ return (INT32)(srcp - pSrcData);
482
+ }
483
+
484
+ static INLINE INT32 planar_set_plane(BYTE bValue, BYTE* pDstData, UINT32 nDstStep, UINT32 nXDst,
485
+ UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, UINT32 nChannel,
486
+ BOOL vFlip)
487
+ {
488
+ INT32 beg = 0;
489
+ INT32 end = 0;
490
+ INT32 inc = 0;
491
+
492
+ WINPR_ASSERT(nHeight <= INT32_MAX);
493
+ WINPR_ASSERT(nWidth <= INT32_MAX);
494
+ WINPR_ASSERT(nDstStep <= INT32_MAX);
495
+
496
+ if (vFlip)
497
+ {
498
+ beg = (INT32)nHeight - 1;
499
+ end = -1;
500
+ inc = -1;
501
+ }
502
+ else
503
+ {
504
+ beg = 0;
505
+ end = (INT32)nHeight;
506
+ inc = 1;
507
+ }
508
+
509
+ for (INT32 y = beg; y != end; y += inc)
510
+ {
511
+ const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (4LL * nXDst) + nChannel * 1LL;
512
+ BYTE* dstp = &pDstData[off];
513
+
514
+ for (INT32 x = 0; x < (INT32)nWidth; ++x)
515
+ {
516
+ *dstp = bValue;
517
+ dstp += 4;
518
+ }
519
+ }
520
+
521
+ return 0;
522
+ }
523
+
524
+ static INLINE BOOL writeLine(BYTE** WINPR_RESTRICT ppRgba, UINT32 DstFormat, UINT32 width,
525
+ const BYTE** WINPR_RESTRICT ppR, const BYTE** WINPR_RESTRICT ppG,
526
+ const BYTE** WINPR_RESTRICT ppB, const BYTE** WINPR_RESTRICT ppA)
527
+ {
528
+ WINPR_ASSERT(ppRgba);
529
+ WINPR_ASSERT(ppR);
530
+ WINPR_ASSERT(ppG);
531
+ WINPR_ASSERT(ppB);
532
+
533
+ switch (DstFormat)
534
+ {
535
+ case PIXEL_FORMAT_BGRA32:
536
+ for (UINT32 x = 0; x < width; x++)
537
+ {
538
+ *(*ppRgba)++ = *(*ppB)++;
539
+ *(*ppRgba)++ = *(*ppG)++;
540
+ *(*ppRgba)++ = *(*ppR)++;
541
+ *(*ppRgba)++ = *(*ppA)++;
542
+ }
543
+
544
+ return TRUE;
545
+
546
+ case PIXEL_FORMAT_BGRX32:
547
+ for (UINT32 x = 0; x < width; x++)
548
+ {
549
+ *(*ppRgba)++ = *(*ppB)++;
550
+ *(*ppRgba)++ = *(*ppG)++;
551
+ *(*ppRgba)++ = *(*ppR)++;
552
+ *(*ppRgba)++ = 0xFF;
553
+ }
554
+
555
+ return TRUE;
556
+
557
+ default:
558
+ if (ppA)
559
+ {
560
+ for (UINT32 x = 0; x < width; x++)
561
+ {
562
+ BYTE alpha = *(*ppA)++;
563
+ UINT32 color =
564
+ FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha);
565
+ FreeRDPWriteColor(*ppRgba, DstFormat, color);
566
+ *ppRgba += FreeRDPGetBytesPerPixel(DstFormat);
567
+ }
568
+ }
569
+ else
570
+ {
571
+ const BYTE alpha = 0xFF;
572
+
573
+ for (UINT32 x = 0; x < width; x++)
574
+ {
575
+ UINT32 color =
576
+ FreeRDPGetColor(DstFormat, *(*ppR)++, *(*ppG)++, *(*ppB)++, alpha);
577
+ FreeRDPWriteColor(*ppRgba, DstFormat, color);
578
+ *ppRgba += FreeRDPGetBytesPerPixel(DstFormat);
579
+ }
580
+ }
581
+
582
+ return TRUE;
583
+ }
584
+ }
585
+
586
+ static INLINE BOOL planar_decompress_planes_raw(const BYTE* WINPR_RESTRICT pSrcData[4],
587
+ BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
588
+ UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst,
589
+ UINT32 nWidth, UINT32 nHeight, BOOL vFlip,
590
+ UINT32 totalHeight)
591
+ {
592
+ INT32 beg = 0;
593
+ INT32 end = 0;
594
+ INT32 inc = 0;
595
+ const BYTE* pR = pSrcData[0];
596
+ const BYTE* pG = pSrcData[1];
597
+ const BYTE* pB = pSrcData[2];
598
+ const BYTE* pA = pSrcData[3];
599
+ const UINT32 bpp = FreeRDPGetBytesPerPixel(DstFormat);
600
+
601
+ if (vFlip)
602
+ {
603
+ beg = WINPR_ASSERTING_INT_CAST(int32_t, nHeight - 1);
604
+ end = -1;
605
+ inc = -1;
606
+ }
607
+ else
608
+ {
609
+ beg = 0;
610
+ end = WINPR_ASSERTING_INT_CAST(int32_t, nHeight);
611
+ inc = 1;
612
+ }
613
+
614
+ if (nYDst + nHeight > totalHeight)
615
+ {
616
+ WLog_ERR(TAG,
617
+ "planar plane destination Y %" PRIu32 " + height %" PRIu32
618
+ " exceeds totalHeight %" PRIu32,
619
+ nYDst, nHeight, totalHeight);
620
+ return FALSE;
621
+ }
622
+
623
+ if ((nXDst + nWidth) * bpp > nDstStep)
624
+ {
625
+ WLog_ERR(TAG,
626
+ "planar plane destination (X %" PRIu32 " + width %" PRIu32 ") * bpp %" PRIu32
627
+ " exceeds stride %" PRIu32,
628
+ nXDst, nWidth, bpp, nDstStep);
629
+ return FALSE;
630
+ }
631
+
632
+ for (INT32 y = beg; y != end; y += inc)
633
+ {
634
+ BYTE* pRGB = NULL;
635
+
636
+ if (y > WINPR_ASSERTING_INT_CAST(INT64, nHeight))
637
+ {
638
+ WLog_ERR(TAG, "planar plane destination Y %" PRId32 " exceeds height %" PRIu32, y,
639
+ nHeight);
640
+ return FALSE;
641
+ }
642
+
643
+ const intptr_t off = ((1LL * nYDst + y) * nDstStep) + (1LL * nXDst * bpp);
644
+ pRGB = &pDstData[off];
645
+
646
+ if (!writeLine(&pRGB, DstFormat, nWidth, &pR, &pG, &pB, &pA))
647
+ return FALSE;
648
+ }
649
+
650
+ return TRUE;
651
+ }
652
+
653
+ static BOOL planar_subsample_expand(const BYTE* WINPR_RESTRICT plane, size_t planeLength,
654
+ UINT32 nWidth, UINT32 nHeight, UINT32 nPlaneWidth,
655
+ UINT32 nPlaneHeight, BYTE* WINPR_RESTRICT deltaPlane)
656
+ {
657
+ size_t pos = 0;
658
+ WINPR_UNUSED(planeLength);
659
+
660
+ WINPR_ASSERT(plane);
661
+ WINPR_ASSERT(deltaPlane);
662
+
663
+ if (nWidth > nPlaneWidth * 2)
664
+ {
665
+ WLog_ERR(TAG, "planar subsample width %" PRIu32 " > PlaneWidth %" PRIu32 " * 2", nWidth,
666
+ nPlaneWidth);
667
+ return FALSE;
668
+ }
669
+
670
+ if (nHeight > nPlaneHeight * 2)
671
+ {
672
+ WLog_ERR(TAG, "planar subsample height %" PRIu32 " > PlaneHeight %" PRIu32 " * 2", nHeight,
673
+ nPlaneHeight);
674
+ return FALSE;
675
+ }
676
+
677
+ for (size_t y = 0; y < nHeight; y++)
678
+ {
679
+ const BYTE* src = plane + y / 2 * nPlaneWidth;
680
+
681
+ for (UINT32 x = 0; x < nWidth; x++)
682
+ {
683
+ deltaPlane[pos++] = src[x / 2];
684
+ }
685
+ }
686
+
687
+ return TRUE;
688
+ }
689
+
690
+ BOOL planar_decompress(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar,
691
+ const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 nSrcWidth,
692
+ UINT32 nSrcHeight, BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat,
693
+ UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, UINT32 nDstWidth,
694
+ UINT32 nDstHeight, BOOL vFlip)
695
+ {
696
+ BOOL useAlpha = FALSE;
697
+ INT32 status = 0;
698
+ INT32 rleSizes[4] = { 0, 0, 0, 0 };
699
+ UINT32 rawSizes[4] = { 0 };
700
+ UINT32 rawWidths[4] = { 0 };
701
+ UINT32 rawHeights[4] = { 0 };
702
+ const BYTE* planes[4] = { 0 };
703
+ const UINT32 w = MIN(nSrcWidth, nDstWidth);
704
+ const UINT32 h = MIN(nSrcHeight, nDstHeight);
705
+ const primitives_t* prims = primitives_get();
706
+
707
+ WINPR_ASSERT(planar);
708
+ WINPR_ASSERT(prims);
709
+
710
+ if (nDstStep <= 0)
711
+ nDstStep = nDstWidth * FreeRDPGetBytesPerPixel(DstFormat);
712
+
713
+ const BYTE* srcp = pSrcData;
714
+
715
+ if (!pSrcData)
716
+ {
717
+ WLog_ERR(TAG, "Invalid argument pSrcData=NULL");
718
+ return FALSE;
719
+ }
720
+
721
+ if (!pDstData)
722
+ {
723
+ WLog_ERR(TAG, "Invalid argument pDstData=NULL");
724
+ return FALSE;
725
+ }
726
+
727
+ const BYTE FormatHeader = *srcp++;
728
+ const BYTE cll = (FormatHeader & PLANAR_FORMAT_HEADER_CLL_MASK);
729
+ const BYTE cs = (FormatHeader & PLANAR_FORMAT_HEADER_CS) ? TRUE : FALSE;
730
+ const BYTE rle = (FormatHeader & PLANAR_FORMAT_HEADER_RLE) ? TRUE : FALSE;
731
+ const BYTE alpha = (FormatHeader & PLANAR_FORMAT_HEADER_NA) ? FALSE : TRUE;
732
+
733
+ DstFormat = planar_invert_format(planar, alpha, DstFormat);
734
+
735
+ if (alpha)
736
+ useAlpha = FreeRDPColorHasAlpha(DstFormat);
737
+
738
+ // WLog_INFO(TAG, "CLL: %"PRIu32" CS: %"PRIu8" RLE: %"PRIu8" ALPHA: %"PRIu8"", cll, cs, rle,
739
+ // alpha);
740
+
741
+ if (!cll && cs)
742
+ {
743
+ WLog_ERR(TAG, "Chroma subsampling requires YCoCg and does not work with RGB data");
744
+ return FALSE; /* Chroma subsampling requires YCoCg */
745
+ }
746
+
747
+ const UINT32 subWidth = (nSrcWidth / 2) + (nSrcWidth % 2);
748
+ const UINT32 subHeight = (nSrcHeight / 2) + (nSrcHeight % 2);
749
+ const UINT32 planeSize = nSrcWidth * nSrcHeight;
750
+ const UINT32 subSize = subWidth * subHeight;
751
+
752
+ if (!cs)
753
+ {
754
+ rawSizes[0] = planeSize; /* LumaOrRedPlane */
755
+ rawWidths[0] = nSrcWidth;
756
+ rawHeights[0] = nSrcHeight;
757
+ rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
758
+ rawWidths[1] = nSrcWidth;
759
+ rawHeights[1] = nSrcHeight;
760
+ rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
761
+ rawWidths[2] = nSrcWidth;
762
+ rawHeights[2] = nSrcHeight;
763
+ rawSizes[3] = planeSize; /* AlphaPlane */
764
+ rawWidths[3] = nSrcWidth;
765
+ rawHeights[3] = nSrcHeight;
766
+ }
767
+ else /* Chroma Subsampling */
768
+ {
769
+ rawSizes[0] = planeSize; /* LumaOrRedPlane */
770
+ rawWidths[0] = nSrcWidth;
771
+ rawHeights[0] = nSrcHeight;
772
+ rawSizes[1] = subSize; /* OrangeChromaOrGreenPlane */
773
+ rawWidths[1] = subWidth;
774
+ rawHeights[1] = subHeight;
775
+ rawSizes[2] = subSize; /* GreenChromaOrBluePlane */
776
+ rawWidths[2] = subWidth;
777
+ rawHeights[2] = subHeight;
778
+ rawSizes[3] = planeSize; /* AlphaPlane */
779
+ rawWidths[3] = nSrcWidth;
780
+ rawHeights[3] = nSrcHeight;
781
+ }
782
+
783
+ const size_t diff = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(srcp - pSrcData));
784
+ if (SrcSize < diff)
785
+ {
786
+ WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
787
+ return FALSE;
788
+ }
789
+
790
+ if (!rle) /* RAW */
791
+ {
792
+
793
+ UINT32 base = planeSize * 3;
794
+ if (cs)
795
+ base = planeSize + planeSize / 2;
796
+
797
+ if (alpha)
798
+ {
799
+ if ((SrcSize - diff) < (planeSize + base))
800
+ {
801
+ WLog_ERR(TAG, "Alpha plane size mismatch %" PRIuz " < %" PRIu32, SrcSize - diff,
802
+ (planeSize + base));
803
+ return FALSE;
804
+ }
805
+
806
+ planes[3] = srcp; /* AlphaPlane */
807
+ planes[0] = planes[3] + rawSizes[3]; /* LumaOrRedPlane */
808
+ planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
809
+ planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
810
+
811
+ if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
812
+ {
813
+ WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2],
814
+ &pSrcData[SrcSize]);
815
+ return FALSE;
816
+ }
817
+ }
818
+ else
819
+ {
820
+ if ((SrcSize - diff) < base)
821
+ {
822
+ WLog_ERR(TAG, "plane size mismatch %" PRIu32 " < %" PRIu32, SrcSize - diff, base);
823
+ return FALSE;
824
+ }
825
+
826
+ planes[0] = srcp; /* LumaOrRedPlane */
827
+ planes[1] = planes[0] + rawSizes[0]; /* OrangeChromaOrGreenPlane */
828
+ planes[2] = planes[1] + rawSizes[1]; /* GreenChromaOrBluePlane */
829
+
830
+ if ((planes[2] + rawSizes[2]) > &pSrcData[SrcSize])
831
+ {
832
+ WLog_ERR(TAG, "plane size mismatch %p + %" PRIu32 " > %p", planes[2], rawSizes[2],
833
+ &pSrcData[SrcSize]);
834
+ return FALSE;
835
+ }
836
+ }
837
+ }
838
+ else /* RLE */
839
+ {
840
+ if (alpha)
841
+ {
842
+ planes[3] = srcp;
843
+ rleSizes[3] = planar_skip_plane_rle(planes[3], (UINT32)(SrcSize - diff), rawWidths[3],
844
+ rawHeights[3]); /* AlphaPlane */
845
+
846
+ if (rleSizes[3] < 0)
847
+ return FALSE;
848
+
849
+ planes[0] = planes[3] + rleSizes[3];
850
+ }
851
+ else
852
+ planes[0] = srcp;
853
+
854
+ const size_t diff0 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[0] - pSrcData));
855
+ if (SrcSize < diff0)
856
+ {
857
+ WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff0);
858
+ return FALSE;
859
+ }
860
+ rleSizes[0] = planar_skip_plane_rle(planes[0], (UINT32)(SrcSize - diff0), rawWidths[0],
861
+ rawHeights[0]); /* RedPlane */
862
+
863
+ if (rleSizes[0] < 0)
864
+ return FALSE;
865
+
866
+ planes[1] = planes[0] + rleSizes[0];
867
+
868
+ const size_t diff1 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[1] - pSrcData));
869
+ if (SrcSize < diff1)
870
+ {
871
+ WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff1);
872
+ return FALSE;
873
+ }
874
+ rleSizes[1] = planar_skip_plane_rle(planes[1], (UINT32)(SrcSize - diff1), rawWidths[1],
875
+ rawHeights[1]); /* GreenPlane */
876
+
877
+ if (rleSizes[1] < 1)
878
+ return FALSE;
879
+
880
+ planes[2] = planes[1] + rleSizes[1];
881
+ const size_t diff2 = WINPR_ASSERTING_INT_CAST(size_t, (intptr_t)(planes[2] - pSrcData));
882
+ if (SrcSize < diff2)
883
+ {
884
+ WLog_ERR(TAG, "Size mismatch %" PRIu32 " < %" PRIuz, SrcSize, diff);
885
+ return FALSE;
886
+ }
887
+ rleSizes[2] = planar_skip_plane_rle(planes[2], (UINT32)(SrcSize - diff2), rawWidths[2],
888
+ rawHeights[2]); /* BluePlane */
889
+
890
+ if (rleSizes[2] < 1)
891
+ return FALSE;
892
+ }
893
+
894
+ if (!cll) /* RGB */
895
+ {
896
+ UINT32 TempFormat = 0;
897
+ BYTE* pTempData = pDstData;
898
+ UINT32 nTempStep = nDstStep;
899
+ UINT32 nTotalHeight = nYDst + nDstHeight;
900
+
901
+ if (useAlpha)
902
+ TempFormat = PIXEL_FORMAT_BGRA32;
903
+ else
904
+ TempFormat = PIXEL_FORMAT_BGRX32;
905
+
906
+ TempFormat = planar_invert_format(planar, alpha, TempFormat);
907
+
908
+ if ((TempFormat != DstFormat) || (nSrcWidth != nDstWidth) || (nSrcHeight != nDstHeight))
909
+ {
910
+ pTempData = planar->pTempData;
911
+ nTempStep = planar->nTempStep;
912
+ nTotalHeight = planar->maxHeight;
913
+ }
914
+
915
+ if (!rle) /* RAW */
916
+ {
917
+ if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
918
+ nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
919
+ return FALSE;
920
+
921
+ if (alpha)
922
+ srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
923
+ else /* NoAlpha */
924
+ srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
925
+
926
+ if ((SrcSize - (srcp - pSrcData)) == 1)
927
+ srcp++; /* pad */
928
+ }
929
+ else /* RLE */
930
+ {
931
+ status = planar_decompress_plane_rle(
932
+ planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), pTempData, nTempStep,
933
+ nXDst, nYDst, nSrcWidth, nSrcHeight, 2, vFlip); /* RedPlane */
934
+
935
+ if (status < 0)
936
+ return FALSE;
937
+
938
+ status = planar_decompress_plane_rle(
939
+ planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), pTempData, nTempStep,
940
+ nXDst, nYDst, nSrcWidth, nSrcHeight, 1, vFlip); /* GreenPlane */
941
+
942
+ if (status < 0)
943
+ return FALSE;
944
+
945
+ status = planar_decompress_plane_rle(
946
+ planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), pTempData, nTempStep,
947
+ nXDst, nYDst, nSrcWidth, nSrcHeight, 0, vFlip); /* BluePlane */
948
+
949
+ if (status < 0)
950
+ return FALSE;
951
+
952
+ srcp += rleSizes[0] + rleSizes[1] + rleSizes[2];
953
+
954
+ if (useAlpha)
955
+ {
956
+ status = planar_decompress_plane_rle(
957
+ planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), pTempData,
958
+ nTempStep, nXDst, nYDst, nSrcWidth, nSrcHeight, 3, vFlip); /* AlphaPlane */
959
+ }
960
+ else
961
+ status = planar_set_plane(0xFF, pTempData, nTempStep, nXDst, nYDst, nSrcWidth,
962
+ nSrcHeight, 3, vFlip);
963
+
964
+ if (status < 0)
965
+ return FALSE;
966
+
967
+ if (alpha)
968
+ srcp += rleSizes[3];
969
+ }
970
+
971
+ if (pTempData != pDstData)
972
+ {
973
+ if (!freerdp_image_copy_no_overlap(pDstData, DstFormat, nDstStep, nXDst, nYDst, w, h,
974
+ pTempData, TempFormat, nTempStep, nXDst, nYDst, NULL,
975
+ FREERDP_FLIP_NONE))
976
+ {
977
+ WLog_ERR(TAG, "planar image copy failed");
978
+ return FALSE;
979
+ }
980
+ }
981
+ }
982
+ else /* YCoCg */
983
+ {
984
+ UINT32 TempFormat = 0;
985
+ BYTE* pTempData = planar->pTempData;
986
+ UINT32 nTempStep = planar->nTempStep;
987
+ UINT32 nTotalHeight = planar->maxHeight;
988
+ BYTE* dst = &pDstData[nXDst * FreeRDPGetBytesPerPixel(DstFormat) + nYDst * nDstStep];
989
+
990
+ if (useAlpha)
991
+ TempFormat = PIXEL_FORMAT_BGRA32;
992
+ else
993
+ TempFormat = PIXEL_FORMAT_BGRX32;
994
+
995
+ if (!pTempData)
996
+ return FALSE;
997
+
998
+ if (rle) /* RLE encoded data. Decode and handle it like raw data. */
999
+ {
1000
+ BYTE* rleBuffer[4] = { 0 };
1001
+
1002
+ if (!planar->rlePlanesBuffer)
1003
+ return FALSE;
1004
+
1005
+ rleBuffer[3] = planar->rlePlanesBuffer; /* AlphaPlane */
1006
+ rleBuffer[0] = rleBuffer[3] + planeSize; /* LumaOrRedPlane */
1007
+ rleBuffer[1] = rleBuffer[0] + planeSize; /* OrangeChromaOrGreenPlane */
1008
+ rleBuffer[2] = rleBuffer[1] + planeSize; /* GreenChromaOrBluePlane */
1009
+ if (useAlpha)
1010
+ {
1011
+ status = planar_decompress_plane_rle_only(
1012
+ planes[3], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[3]), rleBuffer[3],
1013
+ rawWidths[3], rawHeights[3]); /* AlphaPlane */
1014
+
1015
+ if (status < 0)
1016
+ return FALSE;
1017
+ }
1018
+
1019
+ if (alpha)
1020
+ srcp += rleSizes[3];
1021
+
1022
+ status = planar_decompress_plane_rle_only(
1023
+ planes[0], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[0]), rleBuffer[0],
1024
+ rawWidths[0], rawHeights[0]); /* LumaPlane */
1025
+
1026
+ if (status < 0)
1027
+ return FALSE;
1028
+
1029
+ status = planar_decompress_plane_rle_only(
1030
+ planes[1], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[1]), rleBuffer[1],
1031
+ rawWidths[1], rawHeights[1]); /* OrangeChromaPlane */
1032
+
1033
+ if (status < 0)
1034
+ return FALSE;
1035
+
1036
+ status = planar_decompress_plane_rle_only(
1037
+ planes[2], WINPR_ASSERTING_INT_CAST(uint32_t, rleSizes[2]), rleBuffer[2],
1038
+ rawWidths[2], rawHeights[2]); /* GreenChromaPlane */
1039
+
1040
+ if (status < 0)
1041
+ return FALSE;
1042
+
1043
+ planes[0] = rleBuffer[0];
1044
+ planes[1] = rleBuffer[1];
1045
+ planes[2] = rleBuffer[2];
1046
+ planes[3] = rleBuffer[3];
1047
+ }
1048
+
1049
+ /* RAW */
1050
+ {
1051
+ if (cs)
1052
+ { /* Chroma subsampling for Co and Cg:
1053
+ * Each pixel contains the value that should be expanded to
1054
+ * [2x,2y;2x+1,2y;2x+1,2y+1;2x;2y+1] */
1055
+ if (!planar_subsample_expand(planes[1], rawSizes[1], nSrcWidth, nSrcHeight,
1056
+ rawWidths[1], rawHeights[1], planar->deltaPlanes[0]))
1057
+ return FALSE;
1058
+
1059
+ planes[1] = planar->deltaPlanes[0];
1060
+ rawSizes[1] = planeSize; /* OrangeChromaOrGreenPlane */
1061
+ rawWidths[1] = nSrcWidth;
1062
+ rawHeights[1] = nSrcHeight;
1063
+
1064
+ if (!planar_subsample_expand(planes[2], rawSizes[2], nSrcWidth, nSrcHeight,
1065
+ rawWidths[2], rawHeights[2], planar->deltaPlanes[1]))
1066
+ return FALSE;
1067
+
1068
+ planes[2] = planar->deltaPlanes[1];
1069
+ rawSizes[2] = planeSize; /* GreenChromaOrBluePlane */
1070
+ rawWidths[2] = nSrcWidth;
1071
+ rawHeights[2] = nSrcHeight;
1072
+ }
1073
+
1074
+ if (!planar_decompress_planes_raw(planes, pTempData, TempFormat, nTempStep, nXDst,
1075
+ nYDst, nSrcWidth, nSrcHeight, vFlip, nTotalHeight))
1076
+ return FALSE;
1077
+
1078
+ if (alpha)
1079
+ srcp += rawSizes[0] + rawSizes[1] + rawSizes[2] + rawSizes[3];
1080
+ else /* NoAlpha */
1081
+ srcp += rawSizes[0] + rawSizes[1] + rawSizes[2];
1082
+
1083
+ if ((SrcSize - (srcp - pSrcData)) == 1)
1084
+ srcp++; /* pad */
1085
+ }
1086
+
1087
+ WINPR_ASSERT(prims->YCoCgToRGB_8u_AC4R);
1088
+ int rc = prims->YCoCgToRGB_8u_AC4R(
1089
+ pTempData, WINPR_ASSERTING_INT_CAST(int32_t, nTempStep), dst, DstFormat,
1090
+ WINPR_ASSERTING_INT_CAST(int32_t, nDstStep), w, h, cll, useAlpha);
1091
+ if (rc != PRIMITIVES_SUCCESS)
1092
+ {
1093
+ WLog_ERR(TAG, "YCoCgToRGB_8u_AC4R failed with %d", rc);
1094
+ return FALSE;
1095
+ }
1096
+ }
1097
+
1098
+ WINPR_UNUSED(srcp);
1099
+ return TRUE;
1100
+ }
1101
+
1102
+ static INLINE BOOL freerdp_split_color_planes(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar,
1103
+ const BYTE* WINPR_RESTRICT data, UINT32 format,
1104
+ UINT32 width, UINT32 height, UINT32 scanline,
1105
+ BYTE* WINPR_RESTRICT planes[4])
1106
+ {
1107
+ WINPR_ASSERT(planar);
1108
+
1109
+ if ((width > INT32_MAX) || (height > INT32_MAX) || (scanline > INT32_MAX))
1110
+ return FALSE;
1111
+
1112
+ if (scanline == 0)
1113
+ scanline = width * FreeRDPGetBytesPerPixel(format);
1114
+
1115
+ if (planar->topdown)
1116
+ {
1117
+ UINT32 k = 0;
1118
+ for (UINT32 i = 0; i < height; i++)
1119
+ {
1120
+ const BYTE* pixel = &data[1ULL * scanline * i];
1121
+
1122
+ for (UINT32 j = 0; j < width; j++)
1123
+ {
1124
+ const UINT32 color = FreeRDPReadColor(pixel, format);
1125
+ pixel += FreeRDPGetBytesPerPixel(format);
1126
+ FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1127
+ &planes[0][k], NULL);
1128
+ k++;
1129
+ }
1130
+ }
1131
+ }
1132
+ else
1133
+ {
1134
+ UINT32 k = 0;
1135
+
1136
+ for (INT64 i = (INT64)height - 1; i >= 0; i--)
1137
+ {
1138
+ const BYTE* pixel = &data[1ULL * scanline * (UINT32)i];
1139
+
1140
+ for (UINT32 j = 0; j < width; j++)
1141
+ {
1142
+ const UINT32 color = FreeRDPReadColor(pixel, format);
1143
+ pixel += FreeRDPGetBytesPerPixel(format);
1144
+ FreeRDPSplitColor(color, format, &planes[1][k], &planes[2][k], &planes[3][k],
1145
+ &planes[0][k], NULL);
1146
+ k++;
1147
+ }
1148
+ }
1149
+ }
1150
+ return TRUE;
1151
+ }
1152
+
1153
+ static INLINE UINT32 freerdp_bitmap_planar_write_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1154
+ UINT32 cRawBytes, UINT32 nRunLength,
1155
+ BYTE* WINPR_RESTRICT pOutBuffer,
1156
+ UINT32 outBufferSize)
1157
+ {
1158
+ const BYTE* pInput = NULL;
1159
+ BYTE* pOutput = NULL;
1160
+ BYTE controlByte = 0;
1161
+ UINT32 nBytesToWrite = 0;
1162
+ pInput = pInBuffer;
1163
+ pOutput = pOutBuffer;
1164
+
1165
+ if (!cRawBytes && !nRunLength)
1166
+ return 0;
1167
+
1168
+ if (nRunLength < 3)
1169
+ {
1170
+ cRawBytes += nRunLength;
1171
+ nRunLength = 0;
1172
+ }
1173
+
1174
+ while (cRawBytes)
1175
+ {
1176
+ if (cRawBytes < 16)
1177
+ {
1178
+ if (nRunLength > 15)
1179
+ {
1180
+ if (nRunLength < 18)
1181
+ {
1182
+ controlByte = PLANAR_CONTROL_BYTE(13, cRawBytes);
1183
+ nRunLength -= 13;
1184
+ cRawBytes = 0;
1185
+ }
1186
+ else
1187
+ {
1188
+ controlByte = PLANAR_CONTROL_BYTE(15, cRawBytes);
1189
+ nRunLength -= 15;
1190
+ cRawBytes = 0;
1191
+ }
1192
+ }
1193
+ else
1194
+ {
1195
+ controlByte = PLANAR_CONTROL_BYTE(nRunLength, cRawBytes);
1196
+ nRunLength = 0;
1197
+ cRawBytes = 0;
1198
+ }
1199
+ }
1200
+ else
1201
+ {
1202
+ controlByte = PLANAR_CONTROL_BYTE(0, 15);
1203
+ cRawBytes -= 15;
1204
+ }
1205
+
1206
+ if (outBufferSize < 1)
1207
+ return 0;
1208
+
1209
+ outBufferSize--;
1210
+ *pOutput = controlByte;
1211
+ pOutput++;
1212
+ nBytesToWrite = (controlByte >> 4);
1213
+
1214
+ if (nBytesToWrite)
1215
+ {
1216
+ if (outBufferSize < nBytesToWrite)
1217
+ return 0;
1218
+
1219
+ outBufferSize -= nBytesToWrite;
1220
+ CopyMemory(pOutput, pInput, nBytesToWrite);
1221
+ pOutput += nBytesToWrite;
1222
+ pInput += nBytesToWrite;
1223
+ }
1224
+ }
1225
+
1226
+ while (nRunLength)
1227
+ {
1228
+ if (nRunLength > 47)
1229
+ {
1230
+ if (nRunLength < 50)
1231
+ {
1232
+ controlByte = PLANAR_CONTROL_BYTE(2, 13);
1233
+ nRunLength -= 45;
1234
+ }
1235
+ else
1236
+ {
1237
+ controlByte = PLANAR_CONTROL_BYTE(2, 15);
1238
+ nRunLength -= 47;
1239
+ }
1240
+ }
1241
+ else if (nRunLength > 31)
1242
+ {
1243
+ controlByte = PLANAR_CONTROL_BYTE(2, (nRunLength - 32));
1244
+ nRunLength = 0;
1245
+ }
1246
+ else if (nRunLength > 15)
1247
+ {
1248
+ controlByte = PLANAR_CONTROL_BYTE(1, (nRunLength - 16));
1249
+ nRunLength = 0;
1250
+ }
1251
+ else
1252
+ {
1253
+ controlByte = PLANAR_CONTROL_BYTE(nRunLength, 0);
1254
+ nRunLength = 0;
1255
+ }
1256
+
1257
+ if (outBufferSize < 1)
1258
+ return 0;
1259
+
1260
+ --outBufferSize;
1261
+ *pOutput = controlByte;
1262
+ pOutput++;
1263
+ }
1264
+
1265
+ const intptr_t diff = (pOutput - pOutBuffer);
1266
+ if ((diff < 0) || (diff > UINT32_MAX))
1267
+ return 0;
1268
+ return (UINT32)diff;
1269
+ }
1270
+
1271
+ static INLINE UINT32 freerdp_bitmap_planar_encode_rle_bytes(const BYTE* WINPR_RESTRICT pInBuffer,
1272
+ UINT32 inBufferSize,
1273
+ BYTE* WINPR_RESTRICT pOutBuffer,
1274
+ UINT32 outBufferSize)
1275
+ {
1276
+ BYTE symbol = 0;
1277
+ const BYTE* pInput = NULL;
1278
+ BYTE* pOutput = NULL;
1279
+ const BYTE* pBytes = NULL;
1280
+ UINT32 cRawBytes = 0;
1281
+ UINT32 nRunLength = 0;
1282
+ UINT32 bSymbolMatch = 0;
1283
+ UINT32 nBytesWritten = 0;
1284
+ UINT32 nTotalBytesWritten = 0;
1285
+ symbol = 0;
1286
+ cRawBytes = 0;
1287
+ nRunLength = 0;
1288
+ pInput = pInBuffer;
1289
+ pOutput = pOutBuffer;
1290
+ nTotalBytesWritten = 0;
1291
+
1292
+ if (!outBufferSize)
1293
+ return 0;
1294
+
1295
+ do
1296
+ {
1297
+ if (!inBufferSize)
1298
+ break;
1299
+
1300
+ bSymbolMatch = (symbol == *pInput) ? TRUE : FALSE;
1301
+ symbol = *pInput;
1302
+ pInput++;
1303
+ inBufferSize--;
1304
+
1305
+ if (nRunLength && !bSymbolMatch)
1306
+ {
1307
+ if (nRunLength < 3)
1308
+ {
1309
+ cRawBytes += nRunLength;
1310
+ nRunLength = 0;
1311
+ }
1312
+ else
1313
+ {
1314
+ pBytes = pInput - (cRawBytes + nRunLength + 1);
1315
+ nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1316
+ pOutput, outBufferSize);
1317
+ nRunLength = 0;
1318
+
1319
+ if (!nBytesWritten || (nBytesWritten > outBufferSize))
1320
+ return nRunLength;
1321
+
1322
+ nTotalBytesWritten += nBytesWritten;
1323
+ outBufferSize -= nBytesWritten;
1324
+ pOutput += nBytesWritten;
1325
+ cRawBytes = 0;
1326
+ }
1327
+ }
1328
+
1329
+ nRunLength += bSymbolMatch;
1330
+ cRawBytes += (!bSymbolMatch) ? TRUE : FALSE;
1331
+ } while (outBufferSize);
1332
+
1333
+ if (cRawBytes || nRunLength)
1334
+ {
1335
+ pBytes = pInput - (cRawBytes + nRunLength);
1336
+ nBytesWritten = freerdp_bitmap_planar_write_rle_bytes(pBytes, cRawBytes, nRunLength,
1337
+ pOutput, outBufferSize);
1338
+
1339
+ if (!nBytesWritten)
1340
+ return 0;
1341
+
1342
+ nTotalBytesWritten += nBytesWritten;
1343
+ }
1344
+
1345
+ if (inBufferSize)
1346
+ return 0;
1347
+
1348
+ return nTotalBytesWritten;
1349
+ }
1350
+
1351
+ BOOL freerdp_bitmap_planar_compress_plane_rle(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1352
+ UINT32 height, BYTE* WINPR_RESTRICT outPlane,
1353
+ UINT32* WINPR_RESTRICT dstSize)
1354
+ {
1355
+ UINT32 index = 0;
1356
+ const BYTE* pInput = NULL;
1357
+ BYTE* pOutput = NULL;
1358
+ UINT32 outBufferSize = 0;
1359
+ UINT32 nBytesWritten = 0;
1360
+ UINT32 nTotalBytesWritten = 0;
1361
+
1362
+ if (!outPlane)
1363
+ return FALSE;
1364
+
1365
+ index = 0;
1366
+ pInput = inPlane;
1367
+ pOutput = outPlane;
1368
+ outBufferSize = *dstSize;
1369
+ nTotalBytesWritten = 0;
1370
+
1371
+ while (outBufferSize)
1372
+ {
1373
+ nBytesWritten =
1374
+ freerdp_bitmap_planar_encode_rle_bytes(pInput, width, pOutput, outBufferSize);
1375
+
1376
+ if ((!nBytesWritten) || (nBytesWritten > outBufferSize))
1377
+ return FALSE;
1378
+
1379
+ outBufferSize -= nBytesWritten;
1380
+ nTotalBytesWritten += nBytesWritten;
1381
+ pOutput += nBytesWritten;
1382
+ pInput += width;
1383
+ index++;
1384
+
1385
+ if (index >= height)
1386
+ break;
1387
+ }
1388
+
1389
+ *dstSize = nTotalBytesWritten;
1390
+ return TRUE;
1391
+ }
1392
+
1393
+ static INLINE BOOL freerdp_bitmap_planar_compress_planes_rle(BYTE* WINPR_RESTRICT inPlanes[4],
1394
+ UINT32 width, UINT32 height,
1395
+ BYTE* WINPR_RESTRICT outPlanes,
1396
+ UINT32* WINPR_RESTRICT dstSizes,
1397
+ BOOL skipAlpha)
1398
+ {
1399
+ UINT32 outPlanesSize = width * height * 4;
1400
+
1401
+ /* AlphaPlane */
1402
+ if (skipAlpha)
1403
+ {
1404
+ dstSizes[0] = 0;
1405
+ }
1406
+ else
1407
+ {
1408
+ dstSizes[0] = outPlanesSize;
1409
+
1410
+ if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[0], width, height, outPlanes,
1411
+ &dstSizes[0]))
1412
+ return FALSE;
1413
+
1414
+ outPlanes += dstSizes[0];
1415
+ outPlanesSize -= dstSizes[0];
1416
+ }
1417
+
1418
+ /* LumaOrRedPlane */
1419
+ dstSizes[1] = outPlanesSize;
1420
+
1421
+ if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[1], width, height, outPlanes,
1422
+ &dstSizes[1]))
1423
+ return FALSE;
1424
+
1425
+ outPlanes += dstSizes[1];
1426
+ outPlanesSize -= dstSizes[1];
1427
+ /* OrangeChromaOrGreenPlane */
1428
+ dstSizes[2] = outPlanesSize;
1429
+
1430
+ if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[2], width, height, outPlanes,
1431
+ &dstSizes[2]))
1432
+ return FALSE;
1433
+
1434
+ outPlanes += dstSizes[2];
1435
+ outPlanesSize -= dstSizes[2];
1436
+ /* GreenChromeOrBluePlane */
1437
+ dstSizes[3] = outPlanesSize;
1438
+
1439
+ if (!freerdp_bitmap_planar_compress_plane_rle(inPlanes[3], width, height, outPlanes,
1440
+ &dstSizes[3]))
1441
+ return FALSE;
1442
+
1443
+ return TRUE;
1444
+ }
1445
+
1446
+ BYTE* freerdp_bitmap_planar_delta_encode_plane(const BYTE* WINPR_RESTRICT inPlane, UINT32 width,
1447
+ UINT32 height, BYTE* WINPR_RESTRICT outPlane)
1448
+ {
1449
+ BYTE* outPtr = NULL;
1450
+ const BYTE* srcPtr = NULL;
1451
+ const BYTE* prevLinePtr = NULL;
1452
+
1453
+ if (!outPlane)
1454
+ {
1455
+ if (width * height == 0)
1456
+ return NULL;
1457
+
1458
+ if (!(outPlane = (BYTE*)calloc(height, width)))
1459
+ return NULL;
1460
+ }
1461
+
1462
+ // first line is copied as is
1463
+ CopyMemory(outPlane, inPlane, width);
1464
+ outPtr = outPlane + width;
1465
+ srcPtr = inPlane + width;
1466
+ prevLinePtr = inPlane;
1467
+
1468
+ for (UINT32 y = 1; y < height; y++)
1469
+ {
1470
+ for (UINT32 x = 0; x < width; x++, outPtr++, srcPtr++, prevLinePtr++)
1471
+ {
1472
+ INT32 delta = *srcPtr - *prevLinePtr;
1473
+ BYTE s2c = WINPR_ASSERTING_INT_CAST(
1474
+ BYTE, (delta >= 0) ? delta : (~((BYTE)(-delta)) + 1) & 0xFF);
1475
+ s2c = WINPR_ASSERTING_INT_CAST(
1476
+ BYTE,
1477
+ (s2c >= 0) ? (s2c << 1) & 0xFF : (((UINT32)(~((BYTE)s2c) + 1) << 1) - 1) & 0xFF);
1478
+ *outPtr = s2c;
1479
+ }
1480
+ }
1481
+
1482
+ return outPlane;
1483
+ }
1484
+
1485
+ static INLINE BOOL freerdp_bitmap_planar_delta_encode_planes(BYTE* WINPR_RESTRICT inPlanes[4],
1486
+ UINT32 width, UINT32 height,
1487
+ BYTE* WINPR_RESTRICT outPlanes[4])
1488
+ {
1489
+ for (UINT32 i = 0; i < 4; i++)
1490
+ {
1491
+ outPlanes[i] =
1492
+ freerdp_bitmap_planar_delta_encode_plane(inPlanes[i], width, height, outPlanes[i]);
1493
+
1494
+ if (!outPlanes[i])
1495
+ return FALSE;
1496
+ }
1497
+
1498
+ return TRUE;
1499
+ }
1500
+
1501
+ BYTE* freerdp_bitmap_compress_planar(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1502
+ const BYTE* WINPR_RESTRICT data, UINT32 format, UINT32 width,
1503
+ UINT32 height, UINT32 scanline, BYTE* WINPR_RESTRICT dstData,
1504
+ UINT32* WINPR_RESTRICT pDstSize)
1505
+ {
1506
+ UINT32 size = 0;
1507
+ BYTE* dstp = NULL;
1508
+ UINT32 planeSize = 0;
1509
+ UINT32 dstSizes[4] = { 0 };
1510
+ BYTE FormatHeader = 0;
1511
+
1512
+ if (!context || !context->rlePlanesBuffer)
1513
+ return NULL;
1514
+
1515
+ if (context->AllowSkipAlpha)
1516
+ FormatHeader |= PLANAR_FORMAT_HEADER_NA;
1517
+
1518
+ planeSize = width * height;
1519
+
1520
+ if (!context->AllowSkipAlpha)
1521
+ format = planar_invert_format(context, TRUE, format);
1522
+
1523
+ if (!freerdp_split_color_planes(context, data, format, width, height, scanline,
1524
+ context->planes))
1525
+ return NULL;
1526
+
1527
+ if (context->AllowRunLengthEncoding)
1528
+ {
1529
+ if (!freerdp_bitmap_planar_delta_encode_planes(context->planes, width, height,
1530
+ context->deltaPlanes))
1531
+ return NULL;
1532
+
1533
+ if (!freerdp_bitmap_planar_compress_planes_rle(context->deltaPlanes, width, height,
1534
+ context->rlePlanesBuffer, dstSizes,
1535
+ context->AllowSkipAlpha))
1536
+ return NULL;
1537
+
1538
+ {
1539
+ uint32_t offset = 0;
1540
+ FormatHeader |= PLANAR_FORMAT_HEADER_RLE;
1541
+ context->rlePlanes[0] = &context->rlePlanesBuffer[offset];
1542
+ offset += dstSizes[0];
1543
+ context->rlePlanes[1] = &context->rlePlanesBuffer[offset];
1544
+ offset += dstSizes[1];
1545
+ context->rlePlanes[2] = &context->rlePlanesBuffer[offset];
1546
+ offset += dstSizes[2];
1547
+ context->rlePlanes[3] = &context->rlePlanesBuffer[offset];
1548
+
1549
+ #if defined(WITH_DEBUG_CODECS)
1550
+ WLog_DBG(TAG,
1551
+ "R: [%" PRIu32 "/%" PRIu32 "] G: [%" PRIu32 "/%" PRIu32 "] B: [%" PRIu32
1552
+ " / %" PRIu32 "] ",
1553
+ dstSizes[1], planeSize, dstSizes[2], planeSize, dstSizes[3], planeSize);
1554
+ #endif
1555
+ }
1556
+ }
1557
+
1558
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1559
+ {
1560
+ if (!context->AllowRunLengthEncoding)
1561
+ return NULL;
1562
+
1563
+ if (context->rlePlanes[0] == NULL)
1564
+ return NULL;
1565
+
1566
+ if (context->rlePlanes[1] == NULL)
1567
+ return NULL;
1568
+
1569
+ if (context->rlePlanes[2] == NULL)
1570
+ return NULL;
1571
+
1572
+ if (context->rlePlanes[3] == NULL)
1573
+ return NULL;
1574
+ }
1575
+
1576
+ if (!dstData)
1577
+ {
1578
+ size = 1;
1579
+
1580
+ if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1581
+ {
1582
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1583
+ size += dstSizes[0];
1584
+ else
1585
+ size += planeSize;
1586
+ }
1587
+
1588
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1589
+ size += (dstSizes[1] + dstSizes[2] + dstSizes[3]);
1590
+ else
1591
+ size += (planeSize * 3);
1592
+
1593
+ if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1594
+ size++;
1595
+
1596
+ dstData = malloc(size);
1597
+
1598
+ if (!dstData)
1599
+ return NULL;
1600
+
1601
+ *pDstSize = size;
1602
+ }
1603
+
1604
+ dstp = dstData;
1605
+ *dstp = FormatHeader; /* FormatHeader */
1606
+ dstp++;
1607
+
1608
+ /* AlphaPlane */
1609
+
1610
+ if (!(FormatHeader & PLANAR_FORMAT_HEADER_NA))
1611
+ {
1612
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1613
+ {
1614
+ CopyMemory(dstp, context->rlePlanes[0], dstSizes[0]); /* Alpha */
1615
+ dstp += dstSizes[0];
1616
+ }
1617
+ else
1618
+ {
1619
+ CopyMemory(dstp, context->planes[0], planeSize); /* Alpha */
1620
+ dstp += planeSize;
1621
+ }
1622
+ }
1623
+
1624
+ /* LumaOrRedPlane */
1625
+
1626
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1627
+ {
1628
+ CopyMemory(dstp, context->rlePlanes[1], dstSizes[1]); /* Red */
1629
+ dstp += dstSizes[1];
1630
+ }
1631
+ else
1632
+ {
1633
+ CopyMemory(dstp, context->planes[1], planeSize); /* Red */
1634
+ dstp += planeSize;
1635
+ }
1636
+
1637
+ /* OrangeChromaOrGreenPlane */
1638
+
1639
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1640
+ {
1641
+ CopyMemory(dstp, context->rlePlanes[2], dstSizes[2]); /* Green */
1642
+ dstp += dstSizes[2];
1643
+ }
1644
+ else
1645
+ {
1646
+ CopyMemory(dstp, context->planes[2], planeSize); /* Green */
1647
+ dstp += planeSize;
1648
+ }
1649
+
1650
+ /* GreenChromeOrBluePlane */
1651
+
1652
+ if (FormatHeader & PLANAR_FORMAT_HEADER_RLE)
1653
+ {
1654
+ CopyMemory(dstp, context->rlePlanes[3], dstSizes[3]); /* Blue */
1655
+ dstp += dstSizes[3];
1656
+ }
1657
+ else
1658
+ {
1659
+ CopyMemory(dstp, context->planes[3], planeSize); /* Blue */
1660
+ dstp += planeSize;
1661
+ }
1662
+
1663
+ /* Pad1 (1 byte) */
1664
+
1665
+ if (!(FormatHeader & PLANAR_FORMAT_HEADER_RLE))
1666
+ {
1667
+ *dstp = 0;
1668
+ dstp++;
1669
+ }
1670
+
1671
+ const intptr_t diff = (dstp - dstData);
1672
+ if ((diff < 0) || (diff > UINT32_MAX))
1673
+ {
1674
+ free(dstData);
1675
+ return NULL;
1676
+ }
1677
+ size = (UINT32)diff;
1678
+ *pDstSize = size;
1679
+ return dstData;
1680
+ }
1681
+
1682
+ BOOL freerdp_bitmap_planar_context_reset(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT context,
1683
+ UINT32 width, UINT32 height)
1684
+ {
1685
+ if (!context)
1686
+ return FALSE;
1687
+
1688
+ context->bgr = FALSE;
1689
+ context->maxWidth = PLANAR_ALIGN(width, 4);
1690
+ context->maxHeight = PLANAR_ALIGN(height, 4);
1691
+ {
1692
+ const UINT64 tmp = (UINT64)context->maxWidth * context->maxHeight;
1693
+ if (tmp > UINT32_MAX)
1694
+ return FALSE;
1695
+ context->maxPlaneSize = (UINT32)tmp;
1696
+ }
1697
+
1698
+ if (context->maxWidth > UINT32_MAX / 4)
1699
+ return FALSE;
1700
+ context->nTempStep = context->maxWidth * 4;
1701
+
1702
+ memset((void*)context->planes, 0, sizeof(context->planes));
1703
+ memset((void*)context->rlePlanes, 0, sizeof(context->rlePlanes));
1704
+ memset((void*)context->deltaPlanes, 0, sizeof(context->deltaPlanes));
1705
+
1706
+ if (context->maxPlaneSize > 0)
1707
+ {
1708
+ void* tmp = winpr_aligned_recalloc(context->planesBuffer, context->maxPlaneSize, 4, 32);
1709
+ if (!tmp)
1710
+ return FALSE;
1711
+ context->planesBuffer = tmp;
1712
+
1713
+ tmp = winpr_aligned_recalloc(context->pTempData, context->maxPlaneSize, 6, 32);
1714
+ if (!tmp)
1715
+ return FALSE;
1716
+ context->pTempData = tmp;
1717
+
1718
+ tmp = winpr_aligned_recalloc(context->deltaPlanesBuffer, context->maxPlaneSize, 4, 32);
1719
+ if (!tmp)
1720
+ return FALSE;
1721
+ context->deltaPlanesBuffer = tmp;
1722
+
1723
+ tmp = winpr_aligned_recalloc(context->rlePlanesBuffer, context->maxPlaneSize, 4, 32);
1724
+ if (!tmp)
1725
+ return FALSE;
1726
+ context->rlePlanesBuffer = tmp;
1727
+
1728
+ context->planes[0] = &context->planesBuffer[0ULL * context->maxPlaneSize];
1729
+ context->planes[1] = &context->planesBuffer[1ULL * context->maxPlaneSize];
1730
+ context->planes[2] = &context->planesBuffer[2ULL * context->maxPlaneSize];
1731
+ context->planes[3] = &context->planesBuffer[3ULL * context->maxPlaneSize];
1732
+ context->deltaPlanes[0] = &context->deltaPlanesBuffer[0ULL * context->maxPlaneSize];
1733
+ context->deltaPlanes[1] = &context->deltaPlanesBuffer[1ULL * context->maxPlaneSize];
1734
+ context->deltaPlanes[2] = &context->deltaPlanesBuffer[2ULL * context->maxPlaneSize];
1735
+ context->deltaPlanes[3] = &context->deltaPlanesBuffer[3ULL * context->maxPlaneSize];
1736
+ }
1737
+ return TRUE;
1738
+ }
1739
+
1740
+ BITMAP_PLANAR_CONTEXT* freerdp_bitmap_planar_context_new(DWORD flags, UINT32 maxWidth,
1741
+ UINT32 maxHeight)
1742
+ {
1743
+ BITMAP_PLANAR_CONTEXT* context =
1744
+ (BITMAP_PLANAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(BITMAP_PLANAR_CONTEXT), 32);
1745
+
1746
+ if (!context)
1747
+ return NULL;
1748
+
1749
+ if (flags & PLANAR_FORMAT_HEADER_NA)
1750
+ context->AllowSkipAlpha = TRUE;
1751
+
1752
+ if (flags & PLANAR_FORMAT_HEADER_RLE)
1753
+ context->AllowRunLengthEncoding = TRUE;
1754
+
1755
+ if (flags & PLANAR_FORMAT_HEADER_CS)
1756
+ context->AllowColorSubsampling = TRUE;
1757
+
1758
+ context->ColorLossLevel = flags & PLANAR_FORMAT_HEADER_CLL_MASK;
1759
+
1760
+ if (context->ColorLossLevel)
1761
+ context->AllowDynamicColorFidelity = TRUE;
1762
+
1763
+ if (!freerdp_bitmap_planar_context_reset(context, maxWidth, maxHeight))
1764
+ {
1765
+ WINPR_PRAGMA_DIAG_PUSH
1766
+ WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1767
+ freerdp_bitmap_planar_context_free(context);
1768
+ WINPR_PRAGMA_DIAG_POP
1769
+ return NULL;
1770
+ }
1771
+
1772
+ return context;
1773
+ }
1774
+
1775
+ void freerdp_bitmap_planar_context_free(BITMAP_PLANAR_CONTEXT* context)
1776
+ {
1777
+ if (!context)
1778
+ return;
1779
+
1780
+ winpr_aligned_free(context->pTempData);
1781
+ winpr_aligned_free(context->planesBuffer);
1782
+ winpr_aligned_free(context->deltaPlanesBuffer);
1783
+ winpr_aligned_free(context->rlePlanesBuffer);
1784
+ winpr_aligned_free(context);
1785
+ }
1786
+
1787
+ void freerdp_planar_switch_bgr(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL bgr)
1788
+ {
1789
+ WINPR_ASSERT(planar);
1790
+ planar->bgr = bgr;
1791
+ }
1792
+
1793
+ void freerdp_planar_topdown_image(BITMAP_PLANAR_CONTEXT* WINPR_RESTRICT planar, BOOL topdown)
1794
+ {
1795
+ WINPR_ASSERT(planar);
1796
+ planar->topdown = topdown;
1797
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/progressive.c ADDED
@@ -0,0 +1,2681 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Progressive Codec Bitmap Compression
4
+ *
5
+ * Copyright 2014 Marc-Andre Moreau <[email protected]>
6
+ * Copyright 2019 Armin Novak <[email protected]>
7
+ * Copyright 2019 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <winpr/assert.h>
25
+ #include <winpr/cast.h>
26
+ #include <winpr/crt.h>
27
+ #include <winpr/print.h>
28
+ #include <winpr/bitstream.h>
29
+
30
+ #include <freerdp/primitives.h>
31
+ #include <freerdp/codec/color.h>
32
+ #include <freerdp/codec/progressive.h>
33
+ #include <freerdp/codec/region.h>
34
+ #include <freerdp/log.h>
35
+
36
+ #include "rfx_differential.h"
37
+ #include "rfx_quantization.h"
38
+ #include "rfx_dwt.h"
39
+ #include "rfx_rlgr.h"
40
+ #include "rfx_constants.h"
41
+ #include "rfx_types.h"
42
+ #include "progressive.h"
43
+
44
+ #define TAG FREERDP_TAG("codec.progressive")
45
+
46
+ typedef struct
47
+ {
48
+ BOOL nonLL;
49
+ wBitStream* srl;
50
+ wBitStream* raw;
51
+
52
+ /* SRL state */
53
+
54
+ UINT32 kp;
55
+ int nz;
56
+ BOOL mode;
57
+ } RFX_PROGRESSIVE_UPGRADE_STATE;
58
+
59
+ static INLINE void
60
+ progressive_component_codec_quant_read(wStream* WINPR_RESTRICT s,
61
+ RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT quantVal)
62
+ {
63
+ BYTE b = 0;
64
+ Stream_Read_UINT8(s, b);
65
+ quantVal->LL3 = b & 0x0F;
66
+ quantVal->HL3 = b >> 4;
67
+ Stream_Read_UINT8(s, b);
68
+ quantVal->LH3 = b & 0x0F;
69
+ quantVal->HH3 = b >> 4;
70
+ Stream_Read_UINT8(s, b);
71
+ quantVal->HL2 = b & 0x0F;
72
+ quantVal->LH2 = b >> 4;
73
+ Stream_Read_UINT8(s, b);
74
+ quantVal->HH2 = b & 0x0F;
75
+ quantVal->HL1 = b >> 4;
76
+ Stream_Read_UINT8(s, b);
77
+ quantVal->LH1 = b & 0x0F;
78
+ quantVal->HH1 = b >> 4;
79
+ }
80
+
81
+ static INLINE void progressive_rfx_quant_ladd(RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q, int val)
82
+ {
83
+ q->HL1 += val; /* HL1 */
84
+ q->LH1 += val; /* LH1 */
85
+ q->HH1 += val; /* HH1 */
86
+ q->HL2 += val; /* HL2 */
87
+ q->LH2 += val; /* LH2 */
88
+ q->HH2 += val; /* HH2 */
89
+ q->HL3 += val; /* HL3 */
90
+ q->LH3 += val; /* LH3 */
91
+ q->HH3 += val; /* HH3 */
92
+ q->LL3 += val; /* LL3 */
93
+ }
94
+
95
+ static INLINE void progressive_rfx_quant_add(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q1,
96
+ const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q2,
97
+ RFX_COMPONENT_CODEC_QUANT* dst)
98
+ {
99
+ dst->HL1 = q1->HL1 + q2->HL1; /* HL1 */
100
+ dst->LH1 = q1->LH1 + q2->LH1; /* LH1 */
101
+ dst->HH1 = q1->HH1 + q2->HH1; /* HH1 */
102
+ dst->HL2 = q1->HL2 + q2->HL2; /* HL2 */
103
+ dst->LH2 = q1->LH2 + q2->LH2; /* LH2 */
104
+ dst->HH2 = q1->HH2 + q2->HH2; /* HH2 */
105
+ dst->HL3 = q1->HL3 + q2->HL3; /* HL3 */
106
+ dst->LH3 = q1->LH3 + q2->LH3; /* LH3 */
107
+ dst->HH3 = q1->HH3 + q2->HH3; /* HH3 */
108
+ dst->LL3 = q1->LL3 + q2->LL3; /* LL3 */
109
+ }
110
+
111
+ static INLINE void progressive_rfx_quant_lsub(RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q, int val)
112
+ {
113
+ q->HL1 -= val; /* HL1 */
114
+ q->LH1 -= val; /* LH1 */
115
+ q->HH1 -= val; /* HH1 */
116
+ q->HL2 -= val; /* HL2 */
117
+ q->LH2 -= val; /* LH2 */
118
+ q->HH2 -= val; /* HH2 */
119
+ q->HL3 -= val; /* HL3 */
120
+ q->LH3 -= val; /* LH3 */
121
+ q->HH3 -= val; /* HH3 */
122
+ q->LL3 -= val; /* LL3 */
123
+ }
124
+
125
+ static INLINE void progressive_rfx_quant_sub(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q1,
126
+ const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q2,
127
+ RFX_COMPONENT_CODEC_QUANT* dst)
128
+ {
129
+ dst->HL1 = q1->HL1 - q2->HL1; /* HL1 */
130
+ dst->LH1 = q1->LH1 - q2->LH1; /* LH1 */
131
+ dst->HH1 = q1->HH1 - q2->HH1; /* HH1 */
132
+ dst->HL2 = q1->HL2 - q2->HL2; /* HL2 */
133
+ dst->LH2 = q1->LH2 - q2->LH2; /* LH2 */
134
+ dst->HH2 = q1->HH2 - q2->HH2; /* HH2 */
135
+ dst->HL3 = q1->HL3 - q2->HL3; /* HL3 */
136
+ dst->LH3 = q1->LH3 - q2->LH3; /* LH3 */
137
+ dst->HH3 = q1->HH3 - q2->HH3; /* HH3 */
138
+ dst->LL3 = q1->LL3 - q2->LL3; /* LL3 */
139
+ }
140
+
141
+ static INLINE BOOL
142
+ progressive_rfx_quant_lcmp_less_equal(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q, int val)
143
+ {
144
+ if (q->HL1 > val)
145
+ return FALSE; /* HL1 */
146
+
147
+ if (q->LH1 > val)
148
+ return FALSE; /* LH1 */
149
+
150
+ if (q->HH1 > val)
151
+ return FALSE; /* HH1 */
152
+
153
+ if (q->HL2 > val)
154
+ return FALSE; /* HL2 */
155
+
156
+ if (q->LH2 > val)
157
+ return FALSE; /* LH2 */
158
+
159
+ if (q->HH2 > val)
160
+ return FALSE; /* HH2 */
161
+
162
+ if (q->HL3 > val)
163
+ return FALSE; /* HL3 */
164
+
165
+ if (q->LH3 > val)
166
+ return FALSE; /* LH3 */
167
+
168
+ if (q->HH3 > val)
169
+ return FALSE; /* HH3 */
170
+
171
+ if (q->LL3 > val)
172
+ return FALSE; /* LL3 */
173
+
174
+ return TRUE;
175
+ }
176
+
177
+ static INLINE BOOL
178
+ progressive_rfx_quant_cmp_less_equal(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q1,
179
+ const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q2)
180
+ {
181
+ if (q1->HL1 > q2->HL1)
182
+ return FALSE; /* HL1 */
183
+
184
+ if (q1->LH1 > q2->LH1)
185
+ return FALSE; /* LH1 */
186
+
187
+ if (q1->HH1 > q2->HH1)
188
+ return FALSE; /* HH1 */
189
+
190
+ if (q1->HL2 > q2->HL2)
191
+ return FALSE; /* HL2 */
192
+
193
+ if (q1->LH2 > q2->LH2)
194
+ return FALSE; /* LH2 */
195
+
196
+ if (q1->HH2 > q2->HH2)
197
+ return FALSE; /* HH2 */
198
+
199
+ if (q1->HL3 > q2->HL3)
200
+ return FALSE; /* HL3 */
201
+
202
+ if (q1->LH3 > q2->LH3)
203
+ return FALSE; /* LH3 */
204
+
205
+ if (q1->HH3 > q2->HH3)
206
+ return FALSE; /* HH3 */
207
+
208
+ if (q1->LL3 > q2->LL3)
209
+ return FALSE; /* LL3 */
210
+
211
+ return TRUE;
212
+ }
213
+
214
+ static INLINE BOOL
215
+ progressive_rfx_quant_lcmp_greater_equal(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q, int val)
216
+ {
217
+ if (q->HL1 < val)
218
+ return FALSE; /* HL1 */
219
+
220
+ if (q->LH1 < val)
221
+ return FALSE; /* LH1 */
222
+
223
+ if (q->HH1 < val)
224
+ return FALSE; /* HH1 */
225
+
226
+ if (q->HL2 < val)
227
+ return FALSE; /* HL2 */
228
+
229
+ if (q->LH2 < val)
230
+ return FALSE; /* LH2 */
231
+
232
+ if (q->HH2 < val)
233
+ return FALSE; /* HH2 */
234
+
235
+ if (q->HL3 < val)
236
+ return FALSE; /* HL3 */
237
+
238
+ if (q->LH3 < val)
239
+ return FALSE; /* LH3 */
240
+
241
+ if (q->HH3 < val)
242
+ return FALSE; /* HH3 */
243
+
244
+ if (q->LL3 < val)
245
+ return FALSE; /* LL3 */
246
+
247
+ return TRUE;
248
+ }
249
+
250
+ static INLINE BOOL
251
+ progressive_rfx_quant_cmp_greater_equal(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q1,
252
+ const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q2)
253
+ {
254
+ if (q1->HL1 < q2->HL1)
255
+ return FALSE; /* HL1 */
256
+
257
+ if (q1->LH1 < q2->LH1)
258
+ return FALSE; /* LH1 */
259
+
260
+ if (q1->HH1 < q2->HH1)
261
+ return FALSE; /* HH1 */
262
+
263
+ if (q1->HL2 < q2->HL2)
264
+ return FALSE; /* HL2 */
265
+
266
+ if (q1->LH2 < q2->LH2)
267
+ return FALSE; /* LH2 */
268
+
269
+ if (q1->HH2 < q2->HH2)
270
+ return FALSE; /* HH2 */
271
+
272
+ if (q1->HL3 < q2->HL3)
273
+ return FALSE; /* HL3 */
274
+
275
+ if (q1->LH3 < q2->LH3)
276
+ return FALSE; /* LH3 */
277
+
278
+ if (q1->HH3 < q2->HH3)
279
+ return FALSE; /* HH3 */
280
+
281
+ if (q1->LL3 < q2->LL3)
282
+ return FALSE; /* LL3 */
283
+
284
+ return TRUE;
285
+ }
286
+
287
+ static INLINE BOOL
288
+ progressive_rfx_quant_cmp_equal(const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q1,
289
+ const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT q2)
290
+ {
291
+ if (q1->HL1 != q2->HL1)
292
+ return FALSE; /* HL1 */
293
+
294
+ if (q1->LH1 != q2->LH1)
295
+ return FALSE; /* LH1 */
296
+
297
+ if (q1->HH1 != q2->HH1)
298
+ return FALSE; /* HH1 */
299
+
300
+ if (q1->HL2 != q2->HL2)
301
+ return FALSE; /* HL2 */
302
+
303
+ if (q1->LH2 != q2->LH2)
304
+ return FALSE; /* LH2 */
305
+
306
+ if (q1->HH2 != q2->HH2)
307
+ return FALSE; /* HH2 */
308
+
309
+ if (q1->HL3 != q2->HL3)
310
+ return FALSE; /* HL3 */
311
+
312
+ if (q1->LH3 != q2->LH3)
313
+ return FALSE; /* LH3 */
314
+
315
+ if (q1->HH3 != q2->HH3)
316
+ return FALSE; /* HH3 */
317
+
318
+ if (q1->LL3 != q2->LL3)
319
+ return FALSE; /* LL3 */
320
+
321
+ return TRUE;
322
+ }
323
+
324
+ static INLINE BOOL progressive_set_surface_data(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
325
+ UINT16 surfaceId,
326
+ PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT pData)
327
+ {
328
+ ULONG_PTR key = 0;
329
+ key = ((ULONG_PTR)surfaceId) + 1;
330
+
331
+ if (pData)
332
+ return HashTable_Insert(progressive->SurfaceContexts, (void*)key, pData);
333
+
334
+ HashTable_Remove(progressive->SurfaceContexts, (void*)key);
335
+ return TRUE;
336
+ }
337
+
338
+ static INLINE PROGRESSIVE_SURFACE_CONTEXT*
339
+ progressive_get_surface_data(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, UINT16 surfaceId)
340
+ {
341
+ void* key = (void*)(((ULONG_PTR)surfaceId) + 1);
342
+
343
+ if (!progressive)
344
+ return NULL;
345
+
346
+ return HashTable_GetItemValue(progressive->SurfaceContexts, key);
347
+ }
348
+
349
+ static void progressive_tile_free(RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile)
350
+ {
351
+ if (tile)
352
+ {
353
+ winpr_aligned_free(tile->sign);
354
+ winpr_aligned_free(tile->current);
355
+ winpr_aligned_free(tile->data);
356
+ winpr_aligned_free(tile);
357
+ }
358
+ }
359
+
360
+ static void progressive_surface_context_free(void* ptr)
361
+ {
362
+ PROGRESSIVE_SURFACE_CONTEXT* surface = ptr;
363
+
364
+ if (!surface)
365
+ return;
366
+
367
+ if (surface->tiles)
368
+ {
369
+ for (size_t index = 0; index < surface->tilesSize; index++)
370
+ {
371
+ RFX_PROGRESSIVE_TILE* tile = surface->tiles[index];
372
+ progressive_tile_free(tile);
373
+ }
374
+ }
375
+
376
+ winpr_aligned_free((void*)surface->tiles);
377
+ winpr_aligned_free(surface->updatedTileIndices);
378
+ winpr_aligned_free(surface);
379
+ }
380
+
381
+ static INLINE RFX_PROGRESSIVE_TILE* progressive_tile_new(void)
382
+ {
383
+ RFX_PROGRESSIVE_TILE* tile = winpr_aligned_calloc(1, sizeof(RFX_PROGRESSIVE_TILE), 32);
384
+ if (!tile)
385
+ goto fail;
386
+
387
+ tile->width = 64;
388
+ tile->height = 64;
389
+ tile->stride = 4 * tile->width;
390
+
391
+ size_t dataLen = 1ull * tile->stride * tile->height;
392
+ tile->data = (BYTE*)winpr_aligned_malloc(dataLen, 16);
393
+ if (!tile->data)
394
+ goto fail;
395
+ memset(tile->data, 0xFF, dataLen);
396
+
397
+ size_t signLen = (8192ULL + 32ULL) * 3ULL;
398
+ tile->sign = (BYTE*)winpr_aligned_malloc(signLen, 16);
399
+ if (!tile->sign)
400
+ goto fail;
401
+
402
+ size_t currentLen = (8192ULL + 32ULL) * 3ULL;
403
+ tile->current = (BYTE*)winpr_aligned_malloc(currentLen, 16);
404
+ if (!tile->current)
405
+ goto fail;
406
+
407
+ return tile;
408
+
409
+ fail:
410
+ progressive_tile_free(tile);
411
+ return NULL;
412
+ }
413
+
414
+ static INLINE BOOL
415
+ progressive_allocate_tile_cache(PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface, size_t min)
416
+ {
417
+ size_t oldIndex = 0;
418
+
419
+ WINPR_ASSERT(surface);
420
+ WINPR_ASSERT(surface->gridSize > 0);
421
+
422
+ if (surface->tiles)
423
+ {
424
+ oldIndex = surface->gridSize;
425
+ while (surface->gridSize < min)
426
+ surface->gridSize += 1024;
427
+ }
428
+
429
+ void* tmp = winpr_aligned_recalloc((void*)surface->tiles, surface->gridSize,
430
+ sizeof(RFX_PROGRESSIVE_TILE*), 32);
431
+ if (!tmp)
432
+ return FALSE;
433
+ surface->tilesSize = surface->gridSize;
434
+ surface->tiles = (RFX_PROGRESSIVE_TILE**)tmp;
435
+
436
+ for (size_t x = oldIndex; x < surface->tilesSize; x++)
437
+ {
438
+ surface->tiles[x] = progressive_tile_new();
439
+ if (!surface->tiles[x])
440
+ return FALSE;
441
+ }
442
+
443
+ tmp =
444
+ winpr_aligned_recalloc(surface->updatedTileIndices, surface->gridSize, sizeof(UINT32), 32);
445
+ if (!tmp)
446
+ return FALSE;
447
+
448
+ surface->updatedTileIndices = tmp;
449
+
450
+ return TRUE;
451
+ }
452
+
453
+ static PROGRESSIVE_SURFACE_CONTEXT* progressive_surface_context_new(UINT16 surfaceId, UINT32 width,
454
+ UINT32 height)
455
+ {
456
+ PROGRESSIVE_SURFACE_CONTEXT* surface = (PROGRESSIVE_SURFACE_CONTEXT*)winpr_aligned_calloc(
457
+ 1, sizeof(PROGRESSIVE_SURFACE_CONTEXT), 32);
458
+
459
+ if (!surface)
460
+ return NULL;
461
+
462
+ surface->id = surfaceId;
463
+ surface->width = width;
464
+ surface->height = height;
465
+ surface->gridWidth = (width + (64 - width % 64)) / 64;
466
+ surface->gridHeight = (height + (64 - height % 64)) / 64;
467
+ surface->gridSize = surface->gridWidth * surface->gridHeight;
468
+
469
+ if (!progressive_allocate_tile_cache(surface, surface->gridSize))
470
+ {
471
+ progressive_surface_context_free(surface);
472
+ return NULL;
473
+ }
474
+
475
+ return surface;
476
+ }
477
+
478
+ static INLINE BOOL
479
+ progressive_surface_tile_replace(PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
480
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
481
+ const RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile, BOOL upgrade)
482
+ {
483
+ RFX_PROGRESSIVE_TILE* t = NULL;
484
+
485
+ size_t zIdx = 0;
486
+ if (!surface || !tile)
487
+ return FALSE;
488
+
489
+ zIdx = (tile->yIdx * surface->gridWidth) + tile->xIdx;
490
+
491
+ if (zIdx >= surface->tilesSize)
492
+ {
493
+ WLog_ERR(TAG, "Invalid zIndex %" PRIuz, zIdx);
494
+ return FALSE;
495
+ }
496
+
497
+ t = surface->tiles[zIdx];
498
+
499
+ t->blockType = tile->blockType;
500
+ t->blockLen = tile->blockLen;
501
+ t->quantIdxY = tile->quantIdxY;
502
+ t->quantIdxCb = tile->quantIdxCb;
503
+ t->quantIdxCr = tile->quantIdxCr;
504
+ t->xIdx = tile->xIdx;
505
+ t->yIdx = tile->yIdx;
506
+ t->flags = tile->flags;
507
+ t->quality = tile->quality;
508
+ t->x = tile->xIdx * t->width;
509
+ t->y = tile->yIdx * t->height;
510
+
511
+ if (upgrade)
512
+ {
513
+ t->ySrlLen = tile->ySrlLen;
514
+ t->yRawLen = tile->yRawLen;
515
+ t->cbSrlLen = tile->cbSrlLen;
516
+ t->cbRawLen = tile->cbRawLen;
517
+ t->crSrlLen = tile->crSrlLen;
518
+ t->crRawLen = tile->crRawLen;
519
+ t->ySrlData = tile->ySrlData;
520
+ t->yRawData = tile->yRawData;
521
+ t->cbSrlData = tile->cbSrlData;
522
+ t->cbRawData = tile->cbRawData;
523
+ t->crSrlData = tile->crSrlData;
524
+ t->crRawData = tile->crRawData;
525
+ }
526
+ else
527
+ {
528
+ t->yLen = tile->yLen;
529
+ t->cbLen = tile->cbLen;
530
+ t->crLen = tile->crLen;
531
+ t->tailLen = tile->tailLen;
532
+ t->yData = tile->yData;
533
+ t->cbData = tile->cbData;
534
+ t->crData = tile->crData;
535
+ t->tailData = tile->tailData;
536
+ }
537
+
538
+ if (region->usedTiles >= region->numTiles)
539
+ {
540
+ WLog_ERR(TAG, "Invalid tile count, only expected %" PRIu16 ", got %" PRIu16,
541
+ region->numTiles, region->usedTiles);
542
+ return FALSE;
543
+ }
544
+
545
+ region->tiles[region->usedTiles++] = t;
546
+ if (!t->dirty)
547
+ {
548
+ if (surface->numUpdatedTiles >= surface->gridSize)
549
+ {
550
+ if (!progressive_allocate_tile_cache(surface, surface->numUpdatedTiles + 1))
551
+ return FALSE;
552
+ }
553
+
554
+ surface->updatedTileIndices[surface->numUpdatedTiles++] = (UINT32)zIdx;
555
+ }
556
+
557
+ t->dirty = TRUE;
558
+ return TRUE;
559
+ }
560
+
561
+ INT32 progressive_create_surface_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
562
+ UINT16 surfaceId, UINT32 width, UINT32 height)
563
+ {
564
+ PROGRESSIVE_SURFACE_CONTEXT* surface = progressive_get_surface_data(progressive, surfaceId);
565
+
566
+ if (!surface)
567
+ {
568
+ surface = progressive_surface_context_new(surfaceId, width, height);
569
+
570
+ if (!surface)
571
+ return -1;
572
+
573
+ if (!progressive_set_surface_data(progressive, surfaceId, (void*)surface))
574
+ {
575
+ progressive_surface_context_free(surface);
576
+ return -1;
577
+ }
578
+ }
579
+
580
+ return 1;
581
+ }
582
+
583
+ int progressive_delete_surface_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
584
+ UINT16 surfaceId)
585
+ {
586
+ progressive_set_surface_data(progressive, surfaceId, NULL);
587
+
588
+ return 1;
589
+ }
590
+
591
+ /*
592
+ * Band Offset Dimensions Size
593
+ *
594
+ * HL1 0 31x33 1023
595
+ * LH1 1023 33x31 1023
596
+ * HH1 2046 31x31 961
597
+ *
598
+ * HL2 3007 16x17 272
599
+ * LH2 3279 17x16 272
600
+ * HH2 3551 16x16 256
601
+ *
602
+ * HL3 3807 8x9 72
603
+ * LH3 3879 9x8 72
604
+ * HH3 3951 8x8 64
605
+ *
606
+ * LL3 4015 9x9 81
607
+ */
608
+
609
+ static INLINE void progressive_rfx_idwt_x(const INT16* WINPR_RESTRICT pLowBand, size_t nLowStep,
610
+ const INT16* WINPR_RESTRICT pHighBand, size_t nHighStep,
611
+ INT16* WINPR_RESTRICT pDstBand, size_t nDstStep,
612
+ size_t nLowCount, size_t nHighCount, size_t nDstCount)
613
+ {
614
+ INT16 L0 = 0;
615
+ INT16 H0 = 0;
616
+ INT16 H1 = 0;
617
+ INT16 X0 = 0;
618
+ INT16 X1 = 0;
619
+ INT16 X2 = 0;
620
+
621
+ for (size_t i = 0; i < nDstCount; i++)
622
+ {
623
+ const INT16* pL = pLowBand;
624
+ const INT16* pH = pHighBand;
625
+ INT16* pX = pDstBand;
626
+ H0 = *pH++;
627
+ L0 = *pL++;
628
+ X0 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - H0);
629
+ X2 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - H0);
630
+
631
+ for (size_t j = 0; j < (nHighCount - 1); j++)
632
+ {
633
+ H1 = *pH;
634
+ pH++;
635
+ L0 = *pL;
636
+ pL++;
637
+ X2 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - ((H0 + H1) / 2));
638
+ X1 = WINPR_ASSERTING_INT_CAST(int16_t, ((X0 + X2) / 2) + (2 * H0));
639
+ pX[0] = X0;
640
+ pX[1] = X1;
641
+ pX += 2;
642
+ X0 = X2;
643
+ H0 = H1;
644
+ }
645
+
646
+ if (nLowCount <= (nHighCount + 1))
647
+ {
648
+ if (nLowCount <= nHighCount)
649
+ {
650
+ pX[0] = X2;
651
+ pX[1] = WINPR_ASSERTING_INT_CAST(int16_t, X2 + (2 * H0));
652
+ }
653
+ else
654
+ {
655
+ L0 = *pL;
656
+ pL++;
657
+ X0 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - H0);
658
+ pX[0] = X2;
659
+ pX[1] = WINPR_ASSERTING_INT_CAST(int16_t, ((X0 + X2) / 2) + (2 * H0));
660
+ pX[2] = X0;
661
+ }
662
+ }
663
+ else
664
+ {
665
+ L0 = *pL;
666
+ pL++;
667
+ X0 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - (H0 / 2));
668
+ pX[0] = X2;
669
+ pX[1] = WINPR_ASSERTING_INT_CAST(int16_t, ((X0 + X2) / 2) + (2 * H0));
670
+ pX[2] = X0;
671
+ L0 = *pL;
672
+ pL++;
673
+ pX[3] = WINPR_ASSERTING_INT_CAST(int16_t, (X0 + L0) / 2);
674
+ }
675
+
676
+ pLowBand += nLowStep;
677
+ pHighBand += nHighStep;
678
+ pDstBand += nDstStep;
679
+ }
680
+ }
681
+
682
+ static INLINE void progressive_rfx_idwt_y(const INT16* WINPR_RESTRICT pLowBand, size_t nLowStep,
683
+ const INT16* WINPR_RESTRICT pHighBand, size_t nHighStep,
684
+ INT16* WINPR_RESTRICT pDstBand, size_t nDstStep,
685
+ size_t nLowCount, size_t nHighCount, size_t nDstCount)
686
+ {
687
+ INT16 L0 = 0;
688
+ INT16 H0 = 0;
689
+ INT16 H1 = 0;
690
+ INT16 X0 = 0;
691
+ INT16 X1 = 0;
692
+ INT16 X2 = 0;
693
+
694
+ for (size_t i = 0; i < nDstCount; i++)
695
+ {
696
+ const INT16* pL = pLowBand;
697
+ const INT16* pH = pHighBand;
698
+ INT16* pX = pDstBand;
699
+ H0 = *pH;
700
+ pH += nHighStep;
701
+ L0 = *pL;
702
+ pL += nLowStep;
703
+ X0 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - H0);
704
+ X2 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - H0);
705
+
706
+ for (size_t j = 0; j < (nHighCount - 1); j++)
707
+ {
708
+ H1 = *pH;
709
+ pH += nHighStep;
710
+ L0 = *pL;
711
+ pL += nLowStep;
712
+ X2 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - ((H0 + H1) / 2));
713
+ X1 = WINPR_ASSERTING_INT_CAST(int16_t, ((X0 + X2) / 2) + (2 * H0));
714
+ *pX = X0;
715
+ pX += nDstStep;
716
+ *pX = X1;
717
+ pX += nDstStep;
718
+ X0 = X2;
719
+ H0 = H1;
720
+ }
721
+
722
+ if (nLowCount <= (nHighCount + 1))
723
+ {
724
+ if (nLowCount <= nHighCount)
725
+ {
726
+ *pX = X2;
727
+ pX += nDstStep;
728
+ *pX = WINPR_ASSERTING_INT_CAST(int16_t, X2 + (2 * H0));
729
+ }
730
+ else
731
+ {
732
+ L0 = *pL;
733
+ X0 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - H0);
734
+ *pX = X2;
735
+ pX += nDstStep;
736
+ *pX = WINPR_ASSERTING_INT_CAST(int16_t, ((X0 + X2) / 2) + (2 * H0));
737
+ pX += nDstStep;
738
+ *pX = X0;
739
+ }
740
+ }
741
+ else
742
+ {
743
+ L0 = *pL;
744
+ pL += nLowStep;
745
+ X0 = WINPR_ASSERTING_INT_CAST(int16_t, L0 - (H0 / 2));
746
+ *pX = X2;
747
+ pX += nDstStep;
748
+ *pX = WINPR_ASSERTING_INT_CAST(int16_t, ((X0 + X2) / 2) + (2 * H0));
749
+ pX += nDstStep;
750
+ *pX = X0;
751
+ pX += nDstStep;
752
+ L0 = *pL;
753
+ *pX = WINPR_ASSERTING_INT_CAST(int16_t, (X0 + L0) / 2);
754
+ }
755
+
756
+ pLowBand++;
757
+ pHighBand++;
758
+ pDstBand++;
759
+ }
760
+ }
761
+
762
+ static INLINE size_t progressive_rfx_get_band_l_count(size_t level)
763
+ {
764
+ return (64 >> level) + 1;
765
+ }
766
+
767
+ static INLINE size_t progressive_rfx_get_band_h_count(size_t level)
768
+ {
769
+ if (level == 1)
770
+ return (64 >> 1) - 1;
771
+ else
772
+ return (64 + (1 << (level - 1))) >> level;
773
+ }
774
+
775
+ static INLINE void progressive_rfx_dwt_2d_decode_block(INT16* WINPR_RESTRICT buffer,
776
+ INT16* WINPR_RESTRICT temp, size_t level)
777
+ {
778
+ size_t nDstStepX = 0;
779
+ size_t nDstStepY = 0;
780
+ const INT16* WINPR_RESTRICT HL = NULL;
781
+ const INT16* WINPR_RESTRICT LH = NULL;
782
+ const INT16* WINPR_RESTRICT HH = NULL;
783
+ INT16* WINPR_RESTRICT LL = NULL;
784
+ INT16* WINPR_RESTRICT L = NULL;
785
+ INT16* WINPR_RESTRICT H = NULL;
786
+ INT16* WINPR_RESTRICT LLx = NULL;
787
+
788
+ const size_t nBandL = progressive_rfx_get_band_l_count(level);
789
+ const size_t nBandH = progressive_rfx_get_band_h_count(level);
790
+ size_t offset = 0;
791
+
792
+ HL = &buffer[offset];
793
+ offset += (nBandH * nBandL);
794
+ LH = &buffer[offset];
795
+ offset += (nBandL * nBandH);
796
+ HH = &buffer[offset];
797
+ offset += (nBandH * nBandH);
798
+ LL = &buffer[offset];
799
+ nDstStepX = (nBandL + nBandH);
800
+ nDstStepY = (nBandL + nBandH);
801
+ offset = 0;
802
+ L = &temp[offset];
803
+ offset += (nBandL * nDstStepX);
804
+ H = &temp[offset];
805
+ LLx = &buffer[0];
806
+
807
+ /* horizontal (LL + HL -> L) */
808
+ progressive_rfx_idwt_x(LL, nBandL, HL, nBandH, L, nDstStepX, nBandL, nBandH, nBandL);
809
+
810
+ /* horizontal (LH + HH -> H) */
811
+ progressive_rfx_idwt_x(LH, nBandL, HH, nBandH, H, nDstStepX, nBandL, nBandH, nBandH);
812
+
813
+ /* vertical (L + H -> LL) */
814
+ progressive_rfx_idwt_y(L, nDstStepX, H, nDstStepX, LLx, nDstStepY, nBandL, nBandH,
815
+ nBandL + nBandH);
816
+ }
817
+
818
+ void rfx_dwt_2d_extrapolate_decode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT temp)
819
+ {
820
+ WINPR_ASSERT(buffer);
821
+ WINPR_ASSERT(temp);
822
+ progressive_rfx_dwt_2d_decode_block(&buffer[3807], temp, 3);
823
+ progressive_rfx_dwt_2d_decode_block(&buffer[3007], temp, 2);
824
+ progressive_rfx_dwt_2d_decode_block(&buffer[0], temp, 1);
825
+ }
826
+
827
+ static INLINE int progressive_rfx_dwt_2d_decode(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
828
+ INT16* WINPR_RESTRICT buffer,
829
+ INT16* WINPR_RESTRICT current, BOOL coeffDiff,
830
+ BOOL extrapolate, BOOL reverse)
831
+ {
832
+ const primitives_t* prims = primitives_get();
833
+
834
+ if (!progressive || !buffer || !current)
835
+ return -1;
836
+
837
+ const size_t belements = 4096;
838
+ const size_t bsize = belements * sizeof(INT16);
839
+ if (reverse)
840
+ memcpy(buffer, current, bsize);
841
+ else if (!coeffDiff)
842
+ memcpy(current, buffer, bsize);
843
+ else
844
+ prims->add_16s_inplace(buffer, current, belements);
845
+
846
+ INT16* temp = (INT16*)BufferPool_Take(progressive->bufferPool, -1); /* DWT buffer */
847
+
848
+ if (!temp)
849
+ return -2;
850
+
851
+ if (!extrapolate)
852
+ {
853
+ progressive->rfx_context->dwt_2d_decode(buffer, temp);
854
+ }
855
+ else
856
+ {
857
+ WINPR_ASSERT(progressive->rfx_context->dwt_2d_extrapolate_decode);
858
+ progressive->rfx_context->dwt_2d_extrapolate_decode(buffer, temp);
859
+ }
860
+ BufferPool_Return(progressive->bufferPool, temp);
861
+ return 1;
862
+ }
863
+
864
+ static INLINE void progressive_rfx_decode_block(const primitives_t* prims,
865
+ INT16* WINPR_RESTRICT buffer, UINT32 length,
866
+ UINT32 shift)
867
+ {
868
+ if (!shift)
869
+ return;
870
+
871
+ prims->lShiftC_16s_inplace(buffer, shift, length);
872
+ }
873
+
874
+ static INLINE int progressive_rfx_decode_component(
875
+ PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
876
+ const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT shift, const BYTE* WINPR_RESTRICT data,
877
+ UINT32 length, INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT current,
878
+ INT16* WINPR_RESTRICT sign, BOOL coeffDiff, BOOL subbandDiff, BOOL extrapolate)
879
+ {
880
+ int status = 0;
881
+ const primitives_t* prims = primitives_get();
882
+
883
+ status = progressive->rfx_context->rlgr_decode(RLGR1, data, length, buffer, 4096);
884
+
885
+ if (status < 0)
886
+ return status;
887
+
888
+ CopyMemory(sign, buffer, 4096ULL * 2ULL);
889
+ if (!extrapolate)
890
+ {
891
+ rfx_differential_decode(buffer + 4032, 64);
892
+ progressive_rfx_decode_block(prims, &buffer[0], 1024, shift->HL1); /* HL1 */
893
+ progressive_rfx_decode_block(prims, &buffer[1024], 1024, shift->LH1); /* LH1 */
894
+ progressive_rfx_decode_block(prims, &buffer[2048], 1024, shift->HH1); /* HH1 */
895
+ progressive_rfx_decode_block(prims, &buffer[3072], 256, shift->HL2); /* HL2 */
896
+ progressive_rfx_decode_block(prims, &buffer[3328], 256, shift->LH2); /* LH2 */
897
+ progressive_rfx_decode_block(prims, &buffer[3584], 256, shift->HH2); /* HH2 */
898
+ progressive_rfx_decode_block(prims, &buffer[3840], 64, shift->HL3); /* HL3 */
899
+ progressive_rfx_decode_block(prims, &buffer[3904], 64, shift->LH3); /* LH3 */
900
+ progressive_rfx_decode_block(prims, &buffer[3968], 64, shift->HH3); /* HH3 */
901
+ progressive_rfx_decode_block(prims, &buffer[4032], 64, shift->LL3); /* LL3 */
902
+ }
903
+ else
904
+ {
905
+ progressive_rfx_decode_block(prims, &buffer[0], 1023, shift->HL1); /* HL1 */
906
+ progressive_rfx_decode_block(prims, &buffer[1023], 1023, shift->LH1); /* LH1 */
907
+ progressive_rfx_decode_block(prims, &buffer[2046], 961, shift->HH1); /* HH1 */
908
+ progressive_rfx_decode_block(prims, &buffer[3007], 272, shift->HL2); /* HL2 */
909
+ progressive_rfx_decode_block(prims, &buffer[3279], 272, shift->LH2); /* LH2 */
910
+ progressive_rfx_decode_block(prims, &buffer[3551], 256, shift->HH2); /* HH2 */
911
+ progressive_rfx_decode_block(prims, &buffer[3807], 72, shift->HL3); /* HL3 */
912
+ progressive_rfx_decode_block(prims, &buffer[3879], 72, shift->LH3); /* LH3 */
913
+ progressive_rfx_decode_block(prims, &buffer[3951], 64, shift->HH3); /* HH3 */
914
+ rfx_differential_decode(&buffer[4015], 81); /* LL3 */
915
+ progressive_rfx_decode_block(prims, &buffer[4015], 81, shift->LL3); /* LL3 */
916
+ }
917
+ return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
918
+ FALSE);
919
+ }
920
+
921
+ static INLINE int
922
+ progressive_decompress_tile_first(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
923
+ RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile,
924
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
925
+ const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
926
+ {
927
+ int rc = 0;
928
+ BOOL diff = 0;
929
+ BOOL sub = 0;
930
+ BOOL extrapolate = 0;
931
+ BYTE* pBuffer = NULL;
932
+ INT16* pSign[3];
933
+ INT16* pSrcDst[3];
934
+ INT16* pCurrent[3];
935
+ RFX_COMPONENT_CODEC_QUANT shiftY = { 0 };
936
+ RFX_COMPONENT_CODEC_QUANT shiftCb = { 0 };
937
+ RFX_COMPONENT_CODEC_QUANT shiftCr = { 0 };
938
+ RFX_COMPONENT_CODEC_QUANT* quantY = NULL;
939
+ RFX_COMPONENT_CODEC_QUANT* quantCb = NULL;
940
+ RFX_COMPONENT_CODEC_QUANT* quantCr = NULL;
941
+ RFX_COMPONENT_CODEC_QUANT* quantProgY = NULL;
942
+ RFX_COMPONENT_CODEC_QUANT* quantProgCb = NULL;
943
+ RFX_COMPONENT_CODEC_QUANT* quantProgCr = NULL;
944
+ RFX_PROGRESSIVE_CODEC_QUANT* quantProgVal = NULL;
945
+ static const prim_size_t roi_64x64 = { 64, 64 };
946
+ const primitives_t* prims = primitives_get();
947
+
948
+ tile->pass = 1;
949
+ diff = tile->flags & RFX_TILE_DIFFERENCE;
950
+ sub = context->flags & RFX_SUBBAND_DIFFING;
951
+ extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
952
+
953
+ #if defined(WITH_DEBUG_CODECS)
954
+ WLog_Print(progressive->log, WLOG_DEBUG,
955
+ "ProgressiveTile%s: quantIdx Y: %" PRIu8 " Cb: %" PRIu8 " Cr: %" PRIu8
956
+ " xIdx: %" PRIu16 " yIdx: %" PRIu16 " flags: 0x%02" PRIX8 " quality: %" PRIu8
957
+ " yLen: %" PRIu16 " cbLen: %" PRIu16 " crLen: %" PRIu16 " tailLen: %" PRIu16 "",
958
+ (tile->blockType == PROGRESSIVE_WBT_TILE_FIRST) ? "First" : "Simple",
959
+ tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx, tile->yIdx,
960
+ tile->flags, tile->quality, tile->yLen, tile->cbLen, tile->crLen, tile->tailLen);
961
+ #endif
962
+
963
+ if (tile->quantIdxY >= region->numQuant)
964
+ {
965
+ WLog_ERR(TAG, "quantIdxY %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
966
+ return -1;
967
+ }
968
+
969
+ quantY = &(region->quantVals[tile->quantIdxY]);
970
+
971
+ if (tile->quantIdxCb >= region->numQuant)
972
+ {
973
+ WLog_ERR(TAG, "quantIdxCb %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCb,
974
+ region->numQuant);
975
+ return -1;
976
+ }
977
+
978
+ quantCb = &(region->quantVals[tile->quantIdxCb]);
979
+
980
+ if (tile->quantIdxCr >= region->numQuant)
981
+ {
982
+ WLog_ERR(TAG, "quantIdxCr %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCr,
983
+ region->numQuant);
984
+ return -1;
985
+ }
986
+
987
+ quantCr = &(region->quantVals[tile->quantIdxCr]);
988
+
989
+ if (tile->quality == 0xFF)
990
+ {
991
+ quantProgVal = &(progressive->quantProgValFull);
992
+ }
993
+ else
994
+ {
995
+ if (tile->quality >= region->numProgQuant)
996
+ {
997
+ WLog_ERR(TAG, "quality %" PRIu8 " > numProgQuant %" PRIu8, tile->quality,
998
+ region->numProgQuant);
999
+ return -1;
1000
+ }
1001
+
1002
+ quantProgVal = &(region->quantProgVals[tile->quality]);
1003
+ }
1004
+
1005
+ quantProgY = &(quantProgVal->yQuantValues);
1006
+ quantProgCb = &(quantProgVal->cbQuantValues);
1007
+ quantProgCr = &(quantProgVal->crQuantValues);
1008
+
1009
+ tile->yQuant = *quantY;
1010
+ tile->cbQuant = *quantCb;
1011
+ tile->crQuant = *quantCr;
1012
+ tile->yProgQuant = *quantProgY;
1013
+ tile->cbProgQuant = *quantProgCb;
1014
+ tile->crProgQuant = *quantProgCr;
1015
+
1016
+ progressive_rfx_quant_add(quantY, quantProgY, &(tile->yBitPos));
1017
+ progressive_rfx_quant_add(quantCb, quantProgCb, &(tile->cbBitPos));
1018
+ progressive_rfx_quant_add(quantCr, quantProgCr, &(tile->crBitPos));
1019
+ progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
1020
+ progressive_rfx_quant_lsub(&shiftY, 1); /* -6 + 5 = -1 */
1021
+ progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
1022
+ progressive_rfx_quant_lsub(&shiftCb, 1); /* -6 + 5 = -1 */
1023
+ progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
1024
+ progressive_rfx_quant_lsub(&shiftCr, 1); /* -6 + 5 = -1 */
1025
+
1026
+ pSign[0] = (INT16*)((&tile->sign[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1027
+ pSign[1] = (INT16*)((&tile->sign[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1028
+ pSign[2] = (INT16*)((&tile->sign[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1029
+
1030
+ pCurrent[0] = (INT16*)((&tile->current[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1031
+ pCurrent[1] = (INT16*)((&tile->current[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1032
+ pCurrent[2] = (INT16*)((&tile->current[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1033
+
1034
+ pBuffer = (BYTE*)BufferPool_Take(progressive->bufferPool, -1);
1035
+ pSrcDst[0] = (INT16*)((&pBuffer[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1036
+ pSrcDst[1] = (INT16*)((&pBuffer[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1037
+ pSrcDst[2] = (INT16*)((&pBuffer[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1038
+
1039
+ rc = progressive_rfx_decode_component(progressive, &shiftY, tile->yData, tile->yLen, pSrcDst[0],
1040
+ pCurrent[0], pSign[0], diff, sub, extrapolate); /* Y */
1041
+ if (rc < 0)
1042
+ goto fail;
1043
+ rc = progressive_rfx_decode_component(progressive, &shiftCb, tile->cbData, tile->cbLen,
1044
+ pSrcDst[1], pCurrent[1], pSign[1], diff, sub,
1045
+ extrapolate); /* Cb */
1046
+ if (rc < 0)
1047
+ goto fail;
1048
+ rc = progressive_rfx_decode_component(progressive, &shiftCr, tile->crData, tile->crLen,
1049
+ pSrcDst[2], pCurrent[2], pSign[2], diff, sub,
1050
+ extrapolate); /* Cr */
1051
+ if (rc < 0)
1052
+ goto fail;
1053
+
1054
+ const INT16** ptr = WINPR_REINTERPRET_CAST(pSrcDst, INT16**, const INT16**);
1055
+ rc = prims->yCbCrToRGB_16s8u_P3AC4R(ptr, 64 * 2, tile->data, tile->stride, progressive->format,
1056
+ &roi_64x64);
1057
+ fail:
1058
+ BufferPool_Return(progressive->bufferPool, pBuffer);
1059
+ return rc;
1060
+ }
1061
+
1062
+ static INLINE INT16 progressive_rfx_srl_read(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state,
1063
+ UINT32 numBits)
1064
+ {
1065
+ WINPR_ASSERT(state);
1066
+
1067
+ wBitStream* bs = state->srl;
1068
+ WINPR_ASSERT(bs);
1069
+
1070
+ if (state->nz)
1071
+ {
1072
+ state->nz--;
1073
+ return 0;
1074
+ }
1075
+
1076
+ const UINT32 k = state->kp / 8;
1077
+
1078
+ if (!state->mode)
1079
+ {
1080
+ /* zero encoding */
1081
+ const UINT32 bit = (bs->accumulator & 0x80000000) ? 1 : 0;
1082
+ BitStream_Shift(bs, 1);
1083
+
1084
+ if (!bit)
1085
+ {
1086
+ /* '0' bit, nz >= (1 << k), nz = (1 << k) */
1087
+ state->nz = (1 << k);
1088
+ state->kp += 4;
1089
+
1090
+ if (state->kp > 80)
1091
+ state->kp = 80;
1092
+
1093
+ state->nz--;
1094
+ return 0;
1095
+ }
1096
+ else
1097
+ {
1098
+ /* '1' bit, nz < (1 << k), nz = next k bits */
1099
+ state->nz = 0;
1100
+ state->mode = 1; /* unary encoding is next */
1101
+
1102
+ if (k)
1103
+ {
1104
+ bs->mask = ((1 << k) - 1);
1105
+ state->nz =
1106
+ WINPR_ASSERTING_INT_CAST(int16_t, ((bs->accumulator >> (32u - k)) & bs->mask));
1107
+ BitStream_Shift(bs, k);
1108
+ }
1109
+
1110
+ if (state->nz)
1111
+ {
1112
+ state->nz--;
1113
+ return 0;
1114
+ }
1115
+ }
1116
+ }
1117
+
1118
+ state->mode = 0; /* zero encoding is next */
1119
+ /* unary encoding */
1120
+ /* read sign bit */
1121
+ const UINT32 sign = (bs->accumulator & 0x80000000) ? 1 : 0;
1122
+ BitStream_Shift(bs, 1);
1123
+
1124
+ if (state->kp < 6)
1125
+ state->kp = 0;
1126
+ else
1127
+ state->kp -= 6;
1128
+
1129
+ if (numBits == 1)
1130
+ return sign ? -1 : 1;
1131
+
1132
+ UINT32 mag = 1;
1133
+ const UINT32 max = (1 << numBits) - 1;
1134
+
1135
+ while (mag < max)
1136
+ {
1137
+ const UINT32 bit = (bs->accumulator & 0x80000000) ? 1 : 0;
1138
+ BitStream_Shift(bs, 1);
1139
+
1140
+ if (bit)
1141
+ break;
1142
+
1143
+ mag++;
1144
+ }
1145
+
1146
+ if (mag > INT16_MAX)
1147
+ mag = INT16_MAX;
1148
+ return (INT16)(sign ? -1 * (int)mag : (INT16)mag);
1149
+ }
1150
+
1151
+ static INLINE int
1152
+ progressive_rfx_upgrade_state_finish(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state)
1153
+ {
1154
+ UINT32 pad = 0;
1155
+ wBitStream* srl = NULL;
1156
+ wBitStream* raw = NULL;
1157
+ if (!state)
1158
+ return -1;
1159
+
1160
+ srl = state->srl;
1161
+ raw = state->raw;
1162
+ /* Read trailing bits from RAW/SRL bit streams */
1163
+ pad = (raw->position % 8) ? (8 - (raw->position % 8)) : 0;
1164
+
1165
+ if (pad)
1166
+ BitStream_Shift(raw, pad);
1167
+
1168
+ pad = (srl->position % 8) ? (8 - (srl->position % 8)) : 0;
1169
+
1170
+ if (pad)
1171
+ BitStream_Shift(srl, pad);
1172
+
1173
+ if (BitStream_GetRemainingLength(srl) == 8)
1174
+ BitStream_Shift(srl, 8);
1175
+
1176
+ return 1;
1177
+ }
1178
+
1179
+ static INLINE int progressive_rfx_upgrade_block(RFX_PROGRESSIVE_UPGRADE_STATE* WINPR_RESTRICT state,
1180
+ INT16* WINPR_RESTRICT buffer,
1181
+ INT16* WINPR_RESTRICT sign, UINT32 length,
1182
+ UINT32 shift, UINT32 bitPos, UINT32 numBits)
1183
+ {
1184
+ if (!numBits)
1185
+ return 1;
1186
+
1187
+ wBitStream* raw = state->raw;
1188
+ int32_t input = 0;
1189
+
1190
+ if (!state->nonLL)
1191
+ {
1192
+ for (UINT32 index = 0; index < length; index++)
1193
+ {
1194
+ raw->mask = ((1 << numBits) - 1);
1195
+ input = (INT16)((raw->accumulator >> (32 - numBits)) & raw->mask);
1196
+ BitStream_Shift(raw, numBits);
1197
+
1198
+ const int32_t shifted = input << shift;
1199
+ const int32_t val = buffer[index] + shifted;
1200
+ const int16_t ival = WINPR_ASSERTING_INT_CAST(int16_t, val);
1201
+ buffer[index] = ival;
1202
+ }
1203
+
1204
+ return 1;
1205
+ }
1206
+
1207
+ for (UINT32 index = 0; index < length; index++)
1208
+ {
1209
+ if (sign[index] > 0)
1210
+ {
1211
+ /* sign > 0, read from raw */
1212
+ raw->mask = ((1 << numBits) - 1);
1213
+ input = (INT16)((raw->accumulator >> (32 - numBits)) & raw->mask);
1214
+ BitStream_Shift(raw, numBits);
1215
+ }
1216
+ else if (sign[index] < 0)
1217
+ {
1218
+ /* sign < 0, read from raw */
1219
+ raw->mask = ((1 << numBits) - 1);
1220
+ input = (INT16)((raw->accumulator >> (32 - numBits)) & raw->mask);
1221
+ BitStream_Shift(raw, numBits);
1222
+ input *= -1;
1223
+ }
1224
+ else
1225
+ {
1226
+ /* sign == 0, read from srl */
1227
+ input = progressive_rfx_srl_read(state, numBits);
1228
+ sign[index] = WINPR_ASSERTING_INT_CAST(int16_t, input);
1229
+ }
1230
+
1231
+ const int32_t val = input << shift;
1232
+ const int32_t ival = buffer[index] + val;
1233
+ buffer[index] = WINPR_ASSERTING_INT_CAST(INT16, ival);
1234
+ }
1235
+
1236
+ return 1;
1237
+ }
1238
+
1239
+ static INLINE int
1240
+ progressive_rfx_upgrade_component(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1241
+ const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT shift,
1242
+ const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT bitPos,
1243
+ const RFX_COMPONENT_CODEC_QUANT* WINPR_RESTRICT numBits,
1244
+ INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT current,
1245
+ INT16* WINPR_RESTRICT sign, const BYTE* WINPR_RESTRICT srlData,
1246
+ UINT32 srlLen, const BYTE* WINPR_RESTRICT rawData, UINT32 rawLen,
1247
+ BOOL coeffDiff, BOOL subbandDiff, BOOL extrapolate)
1248
+ {
1249
+ int rc = 0;
1250
+ UINT32 aRawLen = 0;
1251
+ UINT32 aSrlLen = 0;
1252
+ wBitStream s_srl = { 0 };
1253
+ wBitStream s_raw = { 0 };
1254
+ RFX_PROGRESSIVE_UPGRADE_STATE state = { 0 };
1255
+
1256
+ state.kp = 8;
1257
+ state.mode = 0;
1258
+ state.srl = &s_srl;
1259
+ state.raw = &s_raw;
1260
+ BitStream_Attach(state.srl, srlData, srlLen);
1261
+ BitStream_Fetch(state.srl);
1262
+ BitStream_Attach(state.raw, rawData, rawLen);
1263
+ BitStream_Fetch(state.raw);
1264
+
1265
+ state.nonLL = TRUE;
1266
+ rc = progressive_rfx_upgrade_block(&state, &current[0], &sign[0], 1023, shift->HL1, bitPos->HL1,
1267
+ numBits->HL1); /* HL1 */
1268
+ if (rc < 0)
1269
+ return rc;
1270
+ rc = progressive_rfx_upgrade_block(&state, &current[1023], &sign[1023], 1023, shift->LH1,
1271
+ bitPos->LH1, numBits->LH1); /* LH1 */
1272
+ if (rc < 0)
1273
+ return rc;
1274
+ rc = progressive_rfx_upgrade_block(&state, &current[2046], &sign[2046], 961, shift->HH1,
1275
+ bitPos->HH1, numBits->HH1); /* HH1 */
1276
+ if (rc < 0)
1277
+ return rc;
1278
+ rc = progressive_rfx_upgrade_block(&state, &current[3007], &sign[3007], 272, shift->HL2,
1279
+ bitPos->HL2, numBits->HL2); /* HL2 */
1280
+ if (rc < 0)
1281
+ return rc;
1282
+ rc = progressive_rfx_upgrade_block(&state, &current[3279], &sign[3279], 272, shift->LH2,
1283
+ bitPos->LH2, numBits->LH2); /* LH2 */
1284
+ if (rc < 0)
1285
+ return rc;
1286
+ rc = progressive_rfx_upgrade_block(&state, &current[3551], &sign[3551], 256, shift->HH2,
1287
+ bitPos->HH2, numBits->HH2); /* HH2 */
1288
+ if (rc < 0)
1289
+ return rc;
1290
+ rc = progressive_rfx_upgrade_block(&state, &current[3807], &sign[3807], 72, shift->HL3,
1291
+ bitPos->HL3, numBits->HL3); /* HL3 */
1292
+ if (rc < 0)
1293
+ return rc;
1294
+ rc = progressive_rfx_upgrade_block(&state, &current[3879], &sign[3879], 72, shift->LH3,
1295
+ bitPos->LH3, numBits->LH3); /* LH3 */
1296
+ if (rc < 0)
1297
+ return rc;
1298
+ rc = progressive_rfx_upgrade_block(&state, &current[3951], &sign[3951], 64, shift->HH3,
1299
+ bitPos->HH3, numBits->HH3); /* HH3 */
1300
+ if (rc < 0)
1301
+ return rc;
1302
+
1303
+ state.nonLL = FALSE;
1304
+ rc = progressive_rfx_upgrade_block(&state, &current[4015], &sign[4015], 81, shift->LL3,
1305
+ bitPos->LL3, numBits->LL3); /* LL3 */
1306
+ if (rc < 0)
1307
+ return rc;
1308
+ rc = progressive_rfx_upgrade_state_finish(&state);
1309
+ if (rc < 0)
1310
+ return rc;
1311
+ aRawLen = (state.raw->position + 7) / 8;
1312
+ aSrlLen = (state.srl->position + 7) / 8;
1313
+
1314
+ if ((aRawLen != rawLen) || (aSrlLen != srlLen))
1315
+ {
1316
+ int pRawLen = 0;
1317
+ int pSrlLen = 0;
1318
+
1319
+ if (rawLen)
1320
+ pRawLen = (int)((((float)aRawLen) / ((float)rawLen)) * 100.0f);
1321
+
1322
+ if (srlLen)
1323
+ pSrlLen = (int)((((float)aSrlLen) / ((float)srlLen)) * 100.0f);
1324
+
1325
+ WLog_Print(progressive->log, WLOG_WARN,
1326
+ "RAW: %" PRIu32 "/%" PRIu32 " %d%% (%" PRIu32 "/%" PRIu32 ":%" PRIu32
1327
+ ")\tSRL: %" PRIu32 "/%" PRIu32 " %d%% (%" PRIu32 "/%" PRIu32 ":%" PRIu32 ")",
1328
+ aRawLen, rawLen, pRawLen, state.raw->position, rawLen * 8,
1329
+ (rawLen * 8) - state.raw->position, aSrlLen, srlLen, pSrlLen,
1330
+ state.srl->position, srlLen * 8, (srlLen * 8) - state.srl->position);
1331
+ return -1;
1332
+ }
1333
+
1334
+ return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
1335
+ TRUE);
1336
+ }
1337
+
1338
+ static INLINE int
1339
+ progressive_decompress_tile_upgrade(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1340
+ RFX_PROGRESSIVE_TILE* WINPR_RESTRICT tile,
1341
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1342
+ const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1343
+ {
1344
+ int status = 0;
1345
+ BOOL coeffDiff = 0;
1346
+ BOOL sub = 0;
1347
+ BOOL extrapolate = 0;
1348
+ BYTE* pBuffer = NULL;
1349
+ INT16* pSign[3] = { 0 };
1350
+ INT16* pSrcDst[3] = { 0 };
1351
+ INT16* pCurrent[3] = { 0 };
1352
+ RFX_COMPONENT_CODEC_QUANT shiftY = { 0 };
1353
+ RFX_COMPONENT_CODEC_QUANT shiftCb = { 0 };
1354
+ RFX_COMPONENT_CODEC_QUANT shiftCr = { 0 };
1355
+ RFX_COMPONENT_CODEC_QUANT yBitPos = { 0 };
1356
+ RFX_COMPONENT_CODEC_QUANT cbBitPos = { 0 };
1357
+ RFX_COMPONENT_CODEC_QUANT crBitPos = { 0 };
1358
+ RFX_COMPONENT_CODEC_QUANT yNumBits = { 0 };
1359
+ RFX_COMPONENT_CODEC_QUANT cbNumBits = { 0 };
1360
+ RFX_COMPONENT_CODEC_QUANT crNumBits = { 0 };
1361
+ RFX_COMPONENT_CODEC_QUANT* quantY = NULL;
1362
+ RFX_COMPONENT_CODEC_QUANT* quantCb = NULL;
1363
+ RFX_COMPONENT_CODEC_QUANT* quantCr = NULL;
1364
+ RFX_COMPONENT_CODEC_QUANT* quantProgY = NULL;
1365
+ RFX_COMPONENT_CODEC_QUANT* quantProgCb = NULL;
1366
+ RFX_COMPONENT_CODEC_QUANT* quantProgCr = NULL;
1367
+ RFX_PROGRESSIVE_CODEC_QUANT* quantProg = NULL;
1368
+ static const prim_size_t roi_64x64 = { 64, 64 };
1369
+ const primitives_t* prims = primitives_get();
1370
+
1371
+ coeffDiff = tile->flags & RFX_TILE_DIFFERENCE;
1372
+ sub = context->flags & RFX_SUBBAND_DIFFING;
1373
+ extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
1374
+
1375
+ tile->pass++;
1376
+
1377
+ #if defined(WITH_DEBUG_CODECS)
1378
+ WLog_Print(progressive->log, WLOG_DEBUG,
1379
+ "ProgressiveTileUpgrade: pass: %" PRIu16 " quantIdx Y: %" PRIu8 " Cb: %" PRIu8
1380
+ " Cr: %" PRIu8 " xIdx: %" PRIu16 " yIdx: %" PRIu16 " quality: %" PRIu8
1381
+ " ySrlLen: %" PRIu16 " yRawLen: %" PRIu16 " cbSrlLen: %" PRIu16 " cbRawLen: %" PRIu16
1382
+ " crSrlLen: %" PRIu16 " crRawLen: %" PRIu16 "",
1383
+ tile->pass, tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx,
1384
+ tile->yIdx, tile->quality, tile->ySrlLen, tile->yRawLen, tile->cbSrlLen,
1385
+ tile->cbRawLen, tile->crSrlLen, tile->crRawLen);
1386
+ #endif
1387
+
1388
+ if (tile->quantIdxY >= region->numQuant)
1389
+ {
1390
+ WLog_ERR(TAG, "quantIdxY %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
1391
+ return -1;
1392
+ }
1393
+
1394
+ quantY = &(region->quantVals[tile->quantIdxY]);
1395
+
1396
+ if (tile->quantIdxCb >= region->numQuant)
1397
+ {
1398
+ WLog_ERR(TAG, "quantIdxCb %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCb,
1399
+ region->numQuant);
1400
+ return -1;
1401
+ }
1402
+
1403
+ quantCb = &(region->quantVals[tile->quantIdxCb]);
1404
+
1405
+ if (tile->quantIdxCr >= region->numQuant)
1406
+ {
1407
+ WLog_ERR(TAG, "quantIdxCr %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCr,
1408
+ region->numQuant);
1409
+ return -1;
1410
+ }
1411
+
1412
+ quantCr = &(region->quantVals[tile->quantIdxCr]);
1413
+
1414
+ if (tile->quality == 0xFF)
1415
+ {
1416
+ quantProg = &(progressive->quantProgValFull);
1417
+ }
1418
+ else
1419
+ {
1420
+ if (tile->quality >= region->numProgQuant)
1421
+ {
1422
+ WLog_ERR(TAG, "quality %" PRIu8 " > numProgQuant %" PRIu8, tile->quality,
1423
+ region->numProgQuant);
1424
+ return -1;
1425
+ }
1426
+
1427
+ quantProg = &(region->quantProgVals[tile->quality]);
1428
+ }
1429
+
1430
+ quantProgY = &(quantProg->yQuantValues);
1431
+ quantProgCb = &(quantProg->cbQuantValues);
1432
+ quantProgCr = &(quantProg->crQuantValues);
1433
+
1434
+ if (!progressive_rfx_quant_cmp_equal(quantY, &(tile->yQuant)))
1435
+ WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantY has changed!");
1436
+
1437
+ if (!progressive_rfx_quant_cmp_equal(quantCb, &(tile->cbQuant)))
1438
+ WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantCb has changed!");
1439
+
1440
+ if (!progressive_rfx_quant_cmp_equal(quantCr, &(tile->crQuant)))
1441
+ WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantCr has changed!");
1442
+
1443
+ if (!(context->flags & RFX_SUBBAND_DIFFING))
1444
+ WLog_WARN(TAG, "PROGRESSIVE_BLOCK_CONTEXT::flags & RFX_SUBBAND_DIFFING not set");
1445
+
1446
+ progressive_rfx_quant_add(quantY, quantProgY, &yBitPos);
1447
+ progressive_rfx_quant_add(quantCb, quantProgCb, &cbBitPos);
1448
+ progressive_rfx_quant_add(quantCr, quantProgCr, &crBitPos);
1449
+ progressive_rfx_quant_sub(&(tile->yBitPos), &yBitPos, &yNumBits);
1450
+ progressive_rfx_quant_sub(&(tile->cbBitPos), &cbBitPos, &cbNumBits);
1451
+ progressive_rfx_quant_sub(&(tile->crBitPos), &crBitPos, &crNumBits);
1452
+ progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
1453
+ progressive_rfx_quant_lsub(&shiftY, 1); /* -6 + 5 = -1 */
1454
+ progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
1455
+ progressive_rfx_quant_lsub(&shiftCb, 1); /* -6 + 5 = -1 */
1456
+ progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
1457
+ progressive_rfx_quant_lsub(&shiftCr, 1); /* -6 + 5 = -1 */
1458
+
1459
+ tile->yBitPos = yBitPos;
1460
+ tile->cbBitPos = cbBitPos;
1461
+ tile->crBitPos = crBitPos;
1462
+ tile->yQuant = *quantY;
1463
+ tile->cbQuant = *quantCb;
1464
+ tile->crQuant = *quantCr;
1465
+ tile->yProgQuant = *quantProgY;
1466
+ tile->cbProgQuant = *quantProgCb;
1467
+ tile->crProgQuant = *quantProgCr;
1468
+
1469
+ pSign[0] = (INT16*)((&tile->sign[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1470
+ pSign[1] = (INT16*)((&tile->sign[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1471
+ pSign[2] = (INT16*)((&tile->sign[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1472
+
1473
+ pCurrent[0] = (INT16*)((&tile->current[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1474
+ pCurrent[1] = (INT16*)((&tile->current[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1475
+ pCurrent[2] = (INT16*)((&tile->current[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1476
+
1477
+ pBuffer = (BYTE*)BufferPool_Take(progressive->bufferPool, -1);
1478
+ pSrcDst[0] = (INT16*)((&pBuffer[((8192 + 32) * 0) + 16])); /* Y/R buffer */
1479
+ pSrcDst[1] = (INT16*)((&pBuffer[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
1480
+ pSrcDst[2] = (INT16*)((&pBuffer[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
1481
+
1482
+ status = progressive_rfx_upgrade_component(progressive, &shiftY, quantProgY, &yNumBits,
1483
+ pSrcDst[0], pCurrent[0], pSign[0], tile->ySrlData,
1484
+ tile->ySrlLen, tile->yRawData, tile->yRawLen,
1485
+ coeffDiff, sub, extrapolate); /* Y */
1486
+
1487
+ if (status < 0)
1488
+ goto fail;
1489
+
1490
+ status = progressive_rfx_upgrade_component(progressive, &shiftCb, quantProgCb, &cbNumBits,
1491
+ pSrcDst[1], pCurrent[1], pSign[1], tile->cbSrlData,
1492
+ tile->cbSrlLen, tile->cbRawData, tile->cbRawLen,
1493
+ coeffDiff, sub, extrapolate); /* Cb */
1494
+
1495
+ if (status < 0)
1496
+ goto fail;
1497
+
1498
+ status = progressive_rfx_upgrade_component(progressive, &shiftCr, quantProgCr, &crNumBits,
1499
+ pSrcDst[2], pCurrent[2], pSign[2], tile->crSrlData,
1500
+ tile->crSrlLen, tile->crRawData, tile->crRawLen,
1501
+ coeffDiff, sub, extrapolate); /* Cr */
1502
+
1503
+ if (status < 0)
1504
+ goto fail;
1505
+
1506
+ const INT16** ptr = WINPR_REINTERPRET_CAST(pSrcDst, INT16**, const INT16**);
1507
+ status = prims->yCbCrToRGB_16s8u_P3AC4R(ptr, 64 * 2, tile->data, tile->stride,
1508
+ progressive->format, &roi_64x64);
1509
+ fail:
1510
+ BufferPool_Return(progressive->bufferPool, pBuffer);
1511
+ return status;
1512
+ }
1513
+
1514
+ static INLINE BOOL progressive_tile_read_upgrade(
1515
+ PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, wStream* WINPR_RESTRICT s, UINT16 blockType,
1516
+ UINT32 blockLen, PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
1517
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1518
+ const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1519
+ {
1520
+ RFX_PROGRESSIVE_TILE tile = { 0 };
1521
+ const size_t expect = 20;
1522
+
1523
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
1524
+ return FALSE;
1525
+
1526
+ tile.blockType = blockType;
1527
+ tile.blockLen = blockLen;
1528
+ tile.flags = 0;
1529
+
1530
+ Stream_Read_UINT8(s, tile.quantIdxY);
1531
+ Stream_Read_UINT8(s, tile.quantIdxCb);
1532
+ Stream_Read_UINT8(s, tile.quantIdxCr);
1533
+ Stream_Read_UINT16(s, tile.xIdx);
1534
+ Stream_Read_UINT16(s, tile.yIdx);
1535
+ Stream_Read_UINT8(s, tile.quality);
1536
+ Stream_Read_UINT16(s, tile.ySrlLen);
1537
+ Stream_Read_UINT16(s, tile.yRawLen);
1538
+ Stream_Read_UINT16(s, tile.cbSrlLen);
1539
+ Stream_Read_UINT16(s, tile.cbRawLen);
1540
+ Stream_Read_UINT16(s, tile.crSrlLen);
1541
+ Stream_Read_UINT16(s, tile.crRawLen);
1542
+
1543
+ tile.ySrlData = Stream_Pointer(s);
1544
+ if (!Stream_SafeSeek(s, tile.ySrlLen))
1545
+ {
1546
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.ySrlLen);
1547
+ return FALSE;
1548
+ }
1549
+
1550
+ tile.yRawData = Stream_Pointer(s);
1551
+ if (!Stream_SafeSeek(s, tile.yRawLen))
1552
+ {
1553
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.yRawLen);
1554
+ return FALSE;
1555
+ }
1556
+
1557
+ tile.cbSrlData = Stream_Pointer(s);
1558
+ if (!Stream_SafeSeek(s, tile.cbSrlLen))
1559
+ {
1560
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1561
+ tile.cbSrlLen);
1562
+ return FALSE;
1563
+ }
1564
+
1565
+ tile.cbRawData = Stream_Pointer(s);
1566
+ if (!Stream_SafeSeek(s, tile.cbRawLen))
1567
+ {
1568
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1569
+ tile.cbRawLen);
1570
+ return FALSE;
1571
+ }
1572
+
1573
+ tile.crSrlData = Stream_Pointer(s);
1574
+ if (!Stream_SafeSeek(s, tile.crSrlLen))
1575
+ {
1576
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1577
+ tile.crSrlLen);
1578
+ return FALSE;
1579
+ }
1580
+
1581
+ tile.crRawData = Stream_Pointer(s);
1582
+ if (!Stream_SafeSeek(s, tile.crRawLen))
1583
+ {
1584
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
1585
+ tile.crRawLen);
1586
+ return FALSE;
1587
+ }
1588
+
1589
+ return progressive_surface_tile_replace(surface, region, &tile, TRUE);
1590
+ }
1591
+
1592
+ static INLINE BOOL progressive_tile_read(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1593
+ BOOL simple, wStream* WINPR_RESTRICT s, UINT16 blockType,
1594
+ UINT32 blockLen,
1595
+ PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
1596
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1597
+ const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1598
+ {
1599
+ RFX_PROGRESSIVE_TILE tile = { 0 };
1600
+ size_t expect = simple ? 16 : 17;
1601
+
1602
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
1603
+ return FALSE;
1604
+
1605
+ tile.blockType = blockType;
1606
+ tile.blockLen = blockLen;
1607
+
1608
+ Stream_Read_UINT8(s, tile.quantIdxY);
1609
+ Stream_Read_UINT8(s, tile.quantIdxCb);
1610
+ Stream_Read_UINT8(s, tile.quantIdxCr);
1611
+ Stream_Read_UINT16(s, tile.xIdx);
1612
+ Stream_Read_UINT16(s, tile.yIdx);
1613
+ Stream_Read_UINT8(s, tile.flags);
1614
+
1615
+ if (!simple)
1616
+ Stream_Read_UINT8(s, tile.quality);
1617
+ else
1618
+ tile.quality = 0xFF;
1619
+ Stream_Read_UINT16(s, tile.yLen);
1620
+ Stream_Read_UINT16(s, tile.cbLen);
1621
+ Stream_Read_UINT16(s, tile.crLen);
1622
+ Stream_Read_UINT16(s, tile.tailLen);
1623
+
1624
+ tile.yData = Stream_Pointer(s);
1625
+ if (!Stream_SafeSeek(s, tile.yLen))
1626
+ {
1627
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.yLen);
1628
+ return FALSE;
1629
+ }
1630
+
1631
+ tile.cbData = Stream_Pointer(s);
1632
+ if (!Stream_SafeSeek(s, tile.cbLen))
1633
+ {
1634
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.cbLen);
1635
+ return FALSE;
1636
+ }
1637
+
1638
+ tile.crData = Stream_Pointer(s);
1639
+ if (!Stream_SafeSeek(s, tile.crLen))
1640
+ {
1641
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.crLen);
1642
+ return FALSE;
1643
+ }
1644
+
1645
+ tile.tailData = Stream_Pointer(s);
1646
+ if (!Stream_SafeSeek(s, tile.tailLen))
1647
+ {
1648
+ WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.tailLen);
1649
+ return FALSE;
1650
+ }
1651
+
1652
+ return progressive_surface_tile_replace(surface, region, &tile, FALSE);
1653
+ }
1654
+
1655
+ static void CALLBACK progressive_process_tiles_tile_work_callback(PTP_CALLBACK_INSTANCE instance,
1656
+ void* context, PTP_WORK work)
1657
+ {
1658
+ PROGRESSIVE_TILE_PROCESS_WORK_PARAM* param = (PROGRESSIVE_TILE_PROCESS_WORK_PARAM*)context;
1659
+
1660
+ WINPR_UNUSED(instance);
1661
+ WINPR_UNUSED(work);
1662
+
1663
+ switch (param->tile->blockType)
1664
+ {
1665
+ case PROGRESSIVE_WBT_TILE_SIMPLE:
1666
+ case PROGRESSIVE_WBT_TILE_FIRST:
1667
+ progressive_decompress_tile_first(param->progressive, param->tile, param->region,
1668
+ param->context);
1669
+ break;
1670
+
1671
+ case PROGRESSIVE_WBT_TILE_UPGRADE:
1672
+ progressive_decompress_tile_upgrade(param->progressive, param->tile, param->region,
1673
+ param->context);
1674
+ break;
1675
+ default:
1676
+ WLog_Print(param->progressive->log, WLOG_ERROR, "Invalid block type %04" PRIx16 " (%s)",
1677
+ param->tile->blockType,
1678
+ rfx_get_progressive_block_type_string(param->tile->blockType));
1679
+ break;
1680
+ }
1681
+ }
1682
+
1683
+ static INLINE SSIZE_T progressive_process_tiles(
1684
+ PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, wStream* WINPR_RESTRICT s,
1685
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
1686
+ PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
1687
+ const PROGRESSIVE_BLOCK_CONTEXT* WINPR_RESTRICT context)
1688
+ {
1689
+ int status = 0;
1690
+ size_t end = 0;
1691
+ const size_t start = Stream_GetPosition(s);
1692
+ UINT16 blockType = 0;
1693
+ UINT32 blockLen = 0;
1694
+ UINT32 count = 0;
1695
+ UINT16 close_cnt = 0;
1696
+
1697
+ WINPR_ASSERT(progressive);
1698
+ WINPR_ASSERT(region);
1699
+
1700
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, region->tileDataSize))
1701
+ return -1;
1702
+
1703
+ while ((Stream_GetRemainingLength(s) >= 6) &&
1704
+ (region->tileDataSize > (Stream_GetPosition(s) - start)))
1705
+ {
1706
+ const size_t pos = Stream_GetPosition(s);
1707
+
1708
+ Stream_Read_UINT16(s, blockType);
1709
+ Stream_Read_UINT32(s, blockLen);
1710
+
1711
+ #if defined(WITH_DEBUG_CODECS)
1712
+ WLog_Print(progressive->log, WLOG_DEBUG, "%s",
1713
+ rfx_get_progressive_block_type_string(blockType));
1714
+ #endif
1715
+
1716
+ if (blockLen < 6)
1717
+ {
1718
+ WLog_Print(progressive->log, WLOG_ERROR, "Expected >= %" PRIu32 " remaining %" PRIuz, 6,
1719
+ blockLen);
1720
+ return -1003;
1721
+ }
1722
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
1723
+ return -1003;
1724
+
1725
+ switch (blockType)
1726
+ {
1727
+ case PROGRESSIVE_WBT_TILE_SIMPLE:
1728
+ if (!progressive_tile_read(progressive, TRUE, s, blockType, blockLen, surface,
1729
+ region, context))
1730
+ return -1022;
1731
+ break;
1732
+
1733
+ case PROGRESSIVE_WBT_TILE_FIRST:
1734
+ if (!progressive_tile_read(progressive, FALSE, s, blockType, blockLen, surface,
1735
+ region, context))
1736
+ return -1027;
1737
+ break;
1738
+
1739
+ case PROGRESSIVE_WBT_TILE_UPGRADE:
1740
+ if (!progressive_tile_read_upgrade(progressive, s, blockType, blockLen, surface,
1741
+ region, context))
1742
+ return -1032;
1743
+ break;
1744
+ default:
1745
+ WLog_ERR(TAG, "Invalid block type %04" PRIx16 " (%s)", blockType,
1746
+ rfx_get_progressive_block_type_string(blockType));
1747
+ return -1039;
1748
+ }
1749
+
1750
+ size_t rem = Stream_GetPosition(s);
1751
+ if ((rem - pos) != blockLen)
1752
+ {
1753
+ WLog_Print(progressive->log, WLOG_ERROR,
1754
+ "Actual block read %" PRIuz " but expected %" PRIu32, rem - pos, blockLen);
1755
+ return -1040;
1756
+ }
1757
+ count++;
1758
+ }
1759
+
1760
+ end = Stream_GetPosition(s);
1761
+ if ((end - start) != region->tileDataSize)
1762
+ {
1763
+ WLog_Print(progressive->log, WLOG_ERROR,
1764
+ "Actual total blocks read %" PRIuz " but expected %" PRIu32, end - start,
1765
+ region->tileDataSize);
1766
+ return -1041;
1767
+ }
1768
+
1769
+ if (count != region->numTiles)
1770
+ {
1771
+ WLog_Print(progressive->log, WLOG_WARN,
1772
+ "numTiles inconsistency: actual: %" PRIu32 ", expected: %" PRIu16 "\n", count,
1773
+ region->numTiles);
1774
+ return -1044;
1775
+ }
1776
+
1777
+ for (UINT32 idx = 0; idx < region->numTiles; idx++)
1778
+ {
1779
+ RFX_PROGRESSIVE_TILE* tile = region->tiles[idx];
1780
+ PROGRESSIVE_TILE_PROCESS_WORK_PARAM* param = &progressive->params[idx];
1781
+ param->progressive = progressive;
1782
+ param->region = region;
1783
+ param->context = context;
1784
+ param->tile = tile;
1785
+
1786
+ if (progressive->rfx_context->priv->UseThreads)
1787
+ {
1788
+ progressive->work_objects[idx] =
1789
+ CreateThreadpoolWork(progressive_process_tiles_tile_work_callback, (void*)param,
1790
+ &progressive->rfx_context->priv->ThreadPoolEnv);
1791
+ if (!progressive->work_objects[idx])
1792
+ {
1793
+ WLog_Print(progressive->log, WLOG_ERROR,
1794
+ "Failed to create ThreadpoolWork for tile %" PRIu32, idx);
1795
+ status = -1;
1796
+ break;
1797
+ }
1798
+
1799
+ SubmitThreadpoolWork(progressive->work_objects[idx]);
1800
+
1801
+ close_cnt = WINPR_ASSERTING_INT_CAST(UINT16, idx + 1);
1802
+ }
1803
+ else
1804
+ {
1805
+ progressive_process_tiles_tile_work_callback(0, param, 0);
1806
+ }
1807
+
1808
+ if (status < 0)
1809
+ {
1810
+ WLog_Print(progressive->log, WLOG_ERROR, "Failed to decompress %s at %" PRIu16,
1811
+ rfx_get_progressive_block_type_string(tile->blockType), idx);
1812
+ goto fail;
1813
+ }
1814
+ }
1815
+
1816
+ if (progressive->rfx_context->priv->UseThreads)
1817
+ {
1818
+ for (UINT32 idx = 0; idx < close_cnt; idx++)
1819
+ {
1820
+ WaitForThreadpoolWorkCallbacks(progressive->work_objects[idx], FALSE);
1821
+ CloseThreadpoolWork(progressive->work_objects[idx]);
1822
+ }
1823
+ }
1824
+
1825
+ fail:
1826
+
1827
+ if (status < 0)
1828
+ return -1;
1829
+
1830
+ return (SSIZE_T)(end - start);
1831
+ }
1832
+
1833
+ static INLINE SSIZE_T progressive_wb_sync(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1834
+ wStream* WINPR_RESTRICT s, UINT16 blockType,
1835
+ UINT32 blockLen)
1836
+ {
1837
+ const UINT32 magic = 0xCACCACCA;
1838
+ const UINT16 version = 0x0100;
1839
+ PROGRESSIVE_BLOCK_SYNC sync = { 0 };
1840
+
1841
+ sync.blockType = blockType;
1842
+ sync.blockLen = blockLen;
1843
+
1844
+ if (sync.blockLen != 12)
1845
+ {
1846
+ WLog_Print(progressive->log, WLOG_ERROR,
1847
+ "PROGRESSIVE_BLOCK_SYNC::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1848
+ sync.blockLen, 12);
1849
+ return -1005;
1850
+ }
1851
+
1852
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1853
+ return -1004;
1854
+
1855
+ #if defined(WITH_DEBUG_CODECS)
1856
+ WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveSync");
1857
+ #endif
1858
+
1859
+ Stream_Read_UINT32(s, sync.magic);
1860
+ Stream_Read_UINT16(s, sync.version);
1861
+
1862
+ if (sync.magic != magic)
1863
+ {
1864
+ WLog_Print(progressive->log, WLOG_ERROR,
1865
+ "PROGRESSIVE_BLOCK_SYNC::magic = 0x%08" PRIx32 " != 0x%08" PRIx32, sync.magic,
1866
+ magic);
1867
+ return -1005;
1868
+ }
1869
+
1870
+ if (sync.version != 0x0100)
1871
+ {
1872
+ WLog_Print(progressive->log, WLOG_ERROR,
1873
+ "PROGRESSIVE_BLOCK_SYNC::version = 0x%04" PRIx16 " != 0x%04" PRIu16,
1874
+ sync.version, version);
1875
+ return -1006;
1876
+ }
1877
+
1878
+ if ((progressive->state & FLAG_WBT_SYNC) != 0)
1879
+ WLog_WARN(TAG, "Duplicate PROGRESSIVE_BLOCK_SYNC, ignoring");
1880
+
1881
+ progressive->state |= FLAG_WBT_SYNC;
1882
+ return 0;
1883
+ }
1884
+
1885
+ static INLINE SSIZE_T progressive_wb_frame_begin(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1886
+ wStream* WINPR_RESTRICT s, UINT16 blockType,
1887
+ UINT32 blockLen)
1888
+ {
1889
+ PROGRESSIVE_BLOCK_FRAME_BEGIN frameBegin = { 0 };
1890
+
1891
+ frameBegin.blockType = blockType;
1892
+ frameBegin.blockLen = blockLen;
1893
+
1894
+ if (frameBegin.blockLen != 12)
1895
+ {
1896
+ WLog_Print(progressive->log, WLOG_ERROR,
1897
+ " RFX_PROGRESSIVE_FRAME_BEGIN::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1898
+ frameBegin.blockLen, 12);
1899
+ return -1005;
1900
+ }
1901
+
1902
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1903
+ return -1007;
1904
+
1905
+ Stream_Read_UINT32(s, frameBegin.frameIndex);
1906
+ Stream_Read_UINT16(s, frameBegin.regionCount);
1907
+
1908
+ #if defined(WITH_DEBUG_CODECS)
1909
+ WLog_Print(progressive->log, WLOG_DEBUG,
1910
+ "ProgressiveFrameBegin: frameIndex: %" PRIu32 " regionCount: %" PRIu16 "",
1911
+ frameBegin.frameIndex, frameBegin.regionCount);
1912
+ #endif
1913
+
1914
+ /**
1915
+ * If the number of elements specified by the regionCount field is
1916
+ * larger than the actual number of elements in the regions field,
1917
+ * the decoder SHOULD ignore this inconsistency.
1918
+ */
1919
+
1920
+ if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
1921
+ {
1922
+ WLog_ERR(TAG, "Duplicate RFX_PROGRESSIVE_FRAME_BEGIN in stream, this is not allowed!");
1923
+ return -1008;
1924
+ }
1925
+
1926
+ if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
1927
+ {
1928
+ WLog_ERR(TAG, "RFX_PROGRESSIVE_FRAME_BEGIN after RFX_PROGRESSIVE_FRAME_END in stream, this "
1929
+ "is not allowed!");
1930
+ return -1008;
1931
+ }
1932
+
1933
+ progressive->state |= FLAG_WBT_FRAME_BEGIN;
1934
+ return 0;
1935
+ }
1936
+
1937
+ static INLINE SSIZE_T progressive_wb_frame_end(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1938
+ wStream* WINPR_RESTRICT s, UINT16 blockType,
1939
+ UINT32 blockLen)
1940
+ {
1941
+ PROGRESSIVE_BLOCK_FRAME_END frameEnd = { 0 };
1942
+
1943
+ frameEnd.blockType = blockType;
1944
+ frameEnd.blockLen = blockLen;
1945
+
1946
+ if (frameEnd.blockLen != 6)
1947
+ {
1948
+ WLog_Print(progressive->log, WLOG_ERROR,
1949
+ " RFX_PROGRESSIVE_FRAME_END::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1950
+ frameEnd.blockLen, 6);
1951
+ return -1005;
1952
+ }
1953
+
1954
+ if (Stream_GetRemainingLength(s) != 0)
1955
+ {
1956
+ WLog_Print(progressive->log, WLOG_ERROR,
1957
+ "ProgressiveFrameEnd short %" PRIuz ", expected %" PRIuz,
1958
+ Stream_GetRemainingLength(s), 0);
1959
+ return -1008;
1960
+ }
1961
+
1962
+ #if defined(WITH_DEBUG_CODECS)
1963
+ WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveFrameEnd");
1964
+ #endif
1965
+
1966
+ if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
1967
+ WLog_WARN(TAG, "RFX_PROGRESSIVE_FRAME_END before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
1968
+ if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
1969
+ WLog_WARN(TAG, "Duplicate RFX_PROGRESSIVE_FRAME_END, ignoring");
1970
+
1971
+ progressive->state |= FLAG_WBT_FRAME_END;
1972
+ return 0;
1973
+ }
1974
+
1975
+ static INLINE SSIZE_T progressive_wb_context(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
1976
+ wStream* WINPR_RESTRICT s, UINT16 blockType,
1977
+ UINT32 blockLen)
1978
+ {
1979
+ PROGRESSIVE_BLOCK_CONTEXT* context = &progressive->context;
1980
+ context->blockType = blockType;
1981
+ context->blockLen = blockLen;
1982
+
1983
+ if (context->blockLen != 10)
1984
+ {
1985
+ WLog_Print(progressive->log, WLOG_ERROR,
1986
+ "RFX_PROGRESSIVE_CONTEXT::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
1987
+ context->blockLen, 10);
1988
+ return -1005;
1989
+ }
1990
+
1991
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1992
+ return -1009;
1993
+
1994
+ Stream_Read_UINT8(s, context->ctxId);
1995
+ Stream_Read_UINT16(s, context->tileSize);
1996
+ Stream_Read_UINT8(s, context->flags);
1997
+
1998
+ if (context->ctxId != 0x00)
1999
+ WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT::ctxId != 0x00: %" PRIu8, context->ctxId);
2000
+
2001
+ if (context->tileSize != 64)
2002
+ {
2003
+ WLog_ERR(TAG, "RFX_PROGRESSIVE_CONTEXT::tileSize != 0x40: %" PRIu16, context->tileSize);
2004
+ return -1010;
2005
+ }
2006
+
2007
+ if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
2008
+ WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_BEGIN");
2009
+ if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
2010
+ WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_END");
2011
+ if ((progressive->state & FLAG_WBT_CONTEXT) != 0)
2012
+ WLog_WARN(TAG, "Duplicate RFX_PROGRESSIVE_CONTEXT received, ignoring.");
2013
+
2014
+ #if defined(WITH_DEBUG_CODECS)
2015
+ WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveContext: flags: 0x%02" PRIX8 "",
2016
+ context->flags);
2017
+ #endif
2018
+
2019
+ progressive->state |= FLAG_WBT_CONTEXT;
2020
+ return 0;
2021
+ }
2022
+
2023
+ static INLINE SSIZE_T progressive_wb_read_region_header(
2024
+ PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive, wStream* WINPR_RESTRICT s, UINT16 blockType,
2025
+ UINT32 blockLen, PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2026
+ {
2027
+ region->usedTiles = 0;
2028
+
2029
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2030
+ return -1011;
2031
+
2032
+ region->blockType = blockType;
2033
+ region->blockLen = blockLen;
2034
+ Stream_Read_UINT8(s, region->tileSize);
2035
+ Stream_Read_UINT16(s, region->numRects);
2036
+ Stream_Read_UINT8(s, region->numQuant);
2037
+ Stream_Read_UINT8(s, region->numProgQuant);
2038
+ Stream_Read_UINT8(s, region->flags);
2039
+ Stream_Read_UINT16(s, region->numTiles);
2040
+ Stream_Read_UINT32(s, region->tileDataSize);
2041
+
2042
+ if (region->tileSize != 64)
2043
+ {
2044
+ WLog_Print(progressive->log, WLOG_ERROR,
2045
+ "ProgressiveRegion tile size %" PRIu8 ", expected %" PRIuz, region->tileSize,
2046
+ 64);
2047
+ return -1012;
2048
+ }
2049
+
2050
+ if (region->numRects < 1)
2051
+ {
2052
+ WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion missing rect count %" PRIu16,
2053
+ region->numRects);
2054
+ return -1013;
2055
+ }
2056
+
2057
+ if (region->numQuant > 7)
2058
+ {
2059
+ WLog_Print(progressive->log, WLOG_ERROR,
2060
+ "ProgressiveRegion quant count too high %" PRIu8 ", expected < %" PRIuz,
2061
+ region->numQuant, 7);
2062
+ return -1014;
2063
+ }
2064
+
2065
+ const SSIZE_T rc = WINPR_ASSERTING_INT_CAST(SSIZE_T, Stream_GetRemainingLength(s));
2066
+ const SSIZE_T expect = region->numRects * 8ll + region->numQuant * 5ll +
2067
+ region->numProgQuant * 16ll + region->tileDataSize;
2068
+ SSIZE_T len = rc;
2069
+ if (expect != rc)
2070
+ {
2071
+ if (len / 8LL < region->numRects)
2072
+ {
2073
+ WLog_Print(progressive->log, WLOG_ERROR,
2074
+ "ProgressiveRegion data short for region->rects");
2075
+ return -1015;
2076
+ }
2077
+ len -= region->numRects * 8LL;
2078
+
2079
+ if (len / 5LL < region->numQuant)
2080
+ {
2081
+ WLog_Print(progressive->log, WLOG_ERROR,
2082
+ "ProgressiveRegion data short for region->cQuant");
2083
+ return -1018;
2084
+ }
2085
+ len -= region->numQuant * 5LL;
2086
+
2087
+ if (len / 16LL < region->numProgQuant)
2088
+ {
2089
+ WLog_Print(progressive->log, WLOG_ERROR,
2090
+ "ProgressiveRegion data short for region->cProgQuant");
2091
+ return -1021;
2092
+ }
2093
+ len -= region->numProgQuant * 16LL;
2094
+
2095
+ if (len < region->tileDataSize * 1ll)
2096
+ {
2097
+ WLog_Print(progressive->log, WLOG_ERROR,
2098
+ "ProgressiveRegion data short for region->tiles");
2099
+ return -1024;
2100
+ }
2101
+ len -= region->tileDataSize;
2102
+
2103
+ if (len > 0)
2104
+ WLog_Print(progressive->log, WLOG_WARN,
2105
+ "Unused bytes detected, %" PRIdz " bytes not processed", len);
2106
+ }
2107
+
2108
+ return rc;
2109
+ }
2110
+
2111
+ static INLINE SSIZE_T progressive_wb_skip_region(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2112
+ wStream* WINPR_RESTRICT s, UINT16 blockType,
2113
+ UINT32 blockLen)
2114
+ {
2115
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region = &progressive->region;
2116
+
2117
+ const SSIZE_T rc =
2118
+ progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
2119
+ if (rc < 0)
2120
+ return rc;
2121
+
2122
+ if (!Stream_SafeSeek(s, WINPR_ASSERTING_INT_CAST(size_t, rc)))
2123
+ return -1111;
2124
+
2125
+ return rc;
2126
+ }
2127
+
2128
+ static INLINE SSIZE_T progressive_wb_region(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2129
+ wStream* WINPR_RESTRICT s, UINT16 blockType,
2130
+ UINT32 blockLen,
2131
+ PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
2132
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2133
+ {
2134
+ SSIZE_T rc = -1;
2135
+ UINT16 boxLeft = 0;
2136
+ UINT16 boxTop = 0;
2137
+ UINT16 boxRight = 0;
2138
+ UINT16 boxBottom = 0;
2139
+ UINT16 idxLeft = 0;
2140
+ UINT16 idxTop = 0;
2141
+ UINT16 idxRight = 0;
2142
+ UINT16 idxBottom = 0;
2143
+ const PROGRESSIVE_BLOCK_CONTEXT* context = &progressive->context;
2144
+
2145
+ if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
2146
+ {
2147
+ WLog_WARN(TAG, "RFX_PROGRESSIVE_REGION before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
2148
+ return progressive_wb_skip_region(progressive, s, blockType, blockLen);
2149
+ }
2150
+ if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
2151
+ {
2152
+ WLog_WARN(TAG, "RFX_PROGRESSIVE_REGION after RFX_PROGRESSIVE_FRAME_END, ignoring");
2153
+ return progressive_wb_skip_region(progressive, s, blockType, blockLen);
2154
+ }
2155
+
2156
+ progressive->state |= FLAG_WBT_REGION;
2157
+
2158
+ rc = progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
2159
+ if (rc < 0)
2160
+ return rc;
2161
+
2162
+ for (UINT16 index = 0; index < region->numRects; index++)
2163
+ {
2164
+ RFX_RECT* rect = &(region->rects[index]);
2165
+ Stream_Read_UINT16(s, rect->x);
2166
+ Stream_Read_UINT16(s, rect->y);
2167
+ Stream_Read_UINT16(s, rect->width);
2168
+ Stream_Read_UINT16(s, rect->height);
2169
+ }
2170
+
2171
+ for (BYTE index = 0; index < region->numQuant; index++)
2172
+ {
2173
+ RFX_COMPONENT_CODEC_QUANT* quantVal = &(region->quantVals[index]);
2174
+ progressive_component_codec_quant_read(s, quantVal);
2175
+
2176
+ if (!progressive_rfx_quant_lcmp_greater_equal(quantVal, 6))
2177
+ {
2178
+ WLog_Print(progressive->log, WLOG_ERROR,
2179
+ "ProgressiveRegion region->cQuant[%" PRIu32 "] < 6", index);
2180
+ return -1;
2181
+ }
2182
+
2183
+ if (!progressive_rfx_quant_lcmp_less_equal(quantVal, 15))
2184
+ {
2185
+ WLog_Print(progressive->log, WLOG_ERROR,
2186
+ "ProgressiveRegion region->cQuant[%" PRIu32 "] > 15", index);
2187
+ return -1;
2188
+ }
2189
+ }
2190
+
2191
+ for (BYTE index = 0; index < region->numProgQuant; index++)
2192
+ {
2193
+ RFX_PROGRESSIVE_CODEC_QUANT* quantProgVal = &(region->quantProgVals[index]);
2194
+
2195
+ Stream_Read_UINT8(s, quantProgVal->quality);
2196
+
2197
+ progressive_component_codec_quant_read(s, &(quantProgVal->yQuantValues));
2198
+ progressive_component_codec_quant_read(s, &(quantProgVal->cbQuantValues));
2199
+ progressive_component_codec_quant_read(s, &(quantProgVal->crQuantValues));
2200
+ }
2201
+
2202
+ #if defined(WITH_DEBUG_CODECS)
2203
+ WLog_Print(progressive->log, WLOG_DEBUG,
2204
+ "ProgressiveRegion: numRects: %" PRIu16 " numTiles: %" PRIu16
2205
+ " tileDataSize: %" PRIu32 " flags: 0x%02" PRIX8 " numQuant: %" PRIu8
2206
+ " numProgQuant: %" PRIu8 "",
2207
+ region->numRects, region->numTiles, region->tileDataSize, region->flags,
2208
+ region->numQuant, region->numProgQuant);
2209
+ #endif
2210
+
2211
+ boxLeft = WINPR_ASSERTING_INT_CAST(UINT16, surface->gridWidth);
2212
+ boxTop = WINPR_ASSERTING_INT_CAST(UINT16, surface->gridHeight);
2213
+ boxRight = 0;
2214
+ boxBottom = 0;
2215
+
2216
+ for (UINT16 index = 0; index < region->numRects; index++)
2217
+ {
2218
+ RFX_RECT* rect = &(region->rects[index]);
2219
+ idxLeft = rect->x / 64;
2220
+ idxTop = rect->y / 64;
2221
+ idxRight = (rect->x + rect->width + 63) / 64;
2222
+ idxBottom = (rect->y + rect->height + 63) / 64;
2223
+
2224
+ if (idxLeft < boxLeft)
2225
+ boxLeft = idxLeft;
2226
+
2227
+ if (idxTop < boxTop)
2228
+ boxTop = idxTop;
2229
+
2230
+ if (idxRight > boxRight)
2231
+ boxRight = idxRight;
2232
+
2233
+ if (idxBottom > boxBottom)
2234
+ boxBottom = idxBottom;
2235
+
2236
+ #if defined(WITH_DEBUG_CODECS)
2237
+ WLog_Print(progressive->log, WLOG_DEBUG,
2238
+ "rect[%" PRIu16 "]: x: %" PRIu16 " y: %" PRIu16 " w: %" PRIu16 " h: %" PRIu16 "",
2239
+ index, rect->x, rect->y, rect->width, rect->height);
2240
+ #endif
2241
+ }
2242
+
2243
+ const SSIZE_T res = progressive_process_tiles(progressive, s, region, surface, context);
2244
+ if (res < 0)
2245
+ return -1;
2246
+ return rc;
2247
+ }
2248
+
2249
+ static INLINE SSIZE_T progressive_parse_block(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2250
+ wStream* WINPR_RESTRICT s,
2251
+ PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
2252
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region)
2253
+ {
2254
+ UINT16 blockType = 0;
2255
+ UINT32 blockLen = 0;
2256
+ SSIZE_T rc = -1;
2257
+ wStream sub = { 0 };
2258
+
2259
+ WINPR_ASSERT(progressive);
2260
+
2261
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
2262
+ return -1;
2263
+
2264
+ Stream_Read_UINT16(s, blockType);
2265
+ Stream_Read_UINT32(s, blockLen);
2266
+
2267
+ if (blockLen < 6)
2268
+ {
2269
+ WLog_WARN(TAG, "Invalid blockLen %" PRIu32 ", expected >= 6", blockLen);
2270
+ return -1;
2271
+ }
2272
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
2273
+ return -1;
2274
+ Stream_StaticConstInit(&sub, Stream_Pointer(s), blockLen - 6);
2275
+ Stream_Seek(s, blockLen - 6);
2276
+
2277
+ switch (blockType)
2278
+ {
2279
+ case PROGRESSIVE_WBT_SYNC:
2280
+ rc = progressive_wb_sync(progressive, &sub, blockType, blockLen);
2281
+ break;
2282
+
2283
+ case PROGRESSIVE_WBT_FRAME_BEGIN:
2284
+ rc = progressive_wb_frame_begin(progressive, &sub, blockType, blockLen);
2285
+ break;
2286
+
2287
+ case PROGRESSIVE_WBT_FRAME_END:
2288
+ rc = progressive_wb_frame_end(progressive, &sub, blockType, blockLen);
2289
+ break;
2290
+
2291
+ case PROGRESSIVE_WBT_CONTEXT:
2292
+ rc = progressive_wb_context(progressive, &sub, blockType, blockLen);
2293
+ break;
2294
+
2295
+ case PROGRESSIVE_WBT_REGION:
2296
+ rc = progressive_wb_region(progressive, &sub, blockType, blockLen, surface, region);
2297
+ break;
2298
+
2299
+ default:
2300
+ WLog_Print(progressive->log, WLOG_ERROR, "Invalid block type %04" PRIx16, blockType);
2301
+ return -1;
2302
+ }
2303
+
2304
+ if (rc < 0)
2305
+ return -1;
2306
+
2307
+ if (Stream_GetRemainingLength(&sub) > 0)
2308
+ {
2309
+ WLog_Print(progressive->log, WLOG_ERROR,
2310
+ "block len %" PRIu32 " does not match read data %" PRIuz, blockLen,
2311
+ blockLen - Stream_GetRemainingLength(&sub));
2312
+ return -1;
2313
+ }
2314
+
2315
+ return rc;
2316
+ }
2317
+
2318
+ static INLINE BOOL update_tiles(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2319
+ PROGRESSIVE_SURFACE_CONTEXT* WINPR_RESTRICT surface,
2320
+ BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep,
2321
+ UINT32 nXDst, UINT32 nYDst,
2322
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region,
2323
+ REGION16* WINPR_RESTRICT invalidRegion)
2324
+ {
2325
+ BOOL rc = TRUE;
2326
+ REGION16 clippingRects = { 0 };
2327
+ region16_init(&clippingRects);
2328
+
2329
+ for (UINT32 i = 0; i < region->numRects; i++)
2330
+ {
2331
+ RECTANGLE_16 clippingRect = { 0 };
2332
+ const RFX_RECT* rect = &(region->rects[i]);
2333
+
2334
+ clippingRect.left = (UINT16)nXDst + rect->x;
2335
+ clippingRect.top = (UINT16)nYDst + rect->y;
2336
+ clippingRect.right = clippingRect.left + rect->width;
2337
+ clippingRect.bottom = clippingRect.top + rect->height;
2338
+ region16_union_rect(&clippingRects, &clippingRects, &clippingRect);
2339
+ }
2340
+
2341
+ for (UINT32 i = 0; i < surface->numUpdatedTiles; i++)
2342
+ {
2343
+ UINT32 nbUpdateRects = 0;
2344
+ const RECTANGLE_16* updateRects = NULL;
2345
+ RECTANGLE_16 updateRect = { 0 };
2346
+
2347
+ WINPR_ASSERT(surface->updatedTileIndices);
2348
+ const UINT32 index = surface->updatedTileIndices[i];
2349
+
2350
+ WINPR_ASSERT(index < surface->tilesSize);
2351
+ RFX_PROGRESSIVE_TILE* tile = surface->tiles[index];
2352
+ WINPR_ASSERT(tile);
2353
+
2354
+ const UINT32 dl = nXDst + tile->x;
2355
+ updateRect.left = WINPR_ASSERTING_INT_CAST(UINT16, dl);
2356
+
2357
+ const UINT32 dt = nYDst + tile->y;
2358
+ updateRect.top = WINPR_ASSERTING_INT_CAST(UINT16, dt);
2359
+ updateRect.right = updateRect.left + 64;
2360
+ updateRect.bottom = updateRect.top + 64;
2361
+
2362
+ REGION16 updateRegion = { 0 };
2363
+ region16_init(&updateRegion);
2364
+ region16_intersect_rect(&updateRegion, &clippingRects, &updateRect);
2365
+ updateRects = region16_rects(&updateRegion, &nbUpdateRects);
2366
+
2367
+ for (UINT32 j = 0; j < nbUpdateRects; j++)
2368
+ {
2369
+ const RECTANGLE_16* rect = &updateRects[j];
2370
+ if (rect->left < updateRect.left)
2371
+ goto fail;
2372
+ const UINT32 nXSrc = rect->left - updateRect.left;
2373
+ const UINT32 nYSrc = rect->top - updateRect.top;
2374
+ const UINT32 width = rect->right - rect->left;
2375
+ const UINT32 height = rect->bottom - rect->top;
2376
+
2377
+ if (rect->left + width > surface->width)
2378
+ goto fail;
2379
+ if (rect->top + height > surface->height)
2380
+ goto fail;
2381
+ rc = freerdp_image_copy_no_overlap(
2382
+ pDstData, DstFormat, nDstStep, rect->left, rect->top, width, height, tile->data,
2383
+ progressive->format, tile->stride, nXSrc, nYSrc, NULL, FREERDP_KEEP_DST_ALPHA);
2384
+ if (!rc)
2385
+ break;
2386
+
2387
+ if (invalidRegion)
2388
+ region16_union_rect(invalidRegion, invalidRegion, rect);
2389
+ }
2390
+
2391
+ region16_uninit(&updateRegion);
2392
+ tile->dirty = FALSE;
2393
+ }
2394
+
2395
+ fail:
2396
+ region16_uninit(&clippingRects);
2397
+ return rc;
2398
+ }
2399
+
2400
+ INT32 progressive_decompress(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2401
+ const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
2402
+ BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, UINT32 nDstStep,
2403
+ UINT32 nXDst, UINT32 nYDst, REGION16* WINPR_RESTRICT invalidRegion,
2404
+ UINT16 surfaceId, UINT32 frameId)
2405
+ {
2406
+ INT32 rc = 1;
2407
+
2408
+ WINPR_ASSERT(progressive);
2409
+ PROGRESSIVE_SURFACE_CONTEXT* surface = progressive_get_surface_data(progressive, surfaceId);
2410
+
2411
+ if (!surface)
2412
+ {
2413
+ WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion no surface for %" PRIu16,
2414
+ surfaceId);
2415
+ return -1001;
2416
+ }
2417
+
2418
+ PROGRESSIVE_BLOCK_REGION* WINPR_RESTRICT region = &progressive->region;
2419
+ WINPR_ASSERT(region);
2420
+
2421
+ if (surface->frameId != frameId)
2422
+ {
2423
+ surface->frameId = frameId;
2424
+ surface->numUpdatedTiles = 0;
2425
+ }
2426
+
2427
+ wStream ss = { 0 };
2428
+ wStream* s = Stream_StaticConstInit(&ss, pSrcData, SrcSize);
2429
+ WINPR_ASSERT(s);
2430
+
2431
+ switch (DstFormat)
2432
+ {
2433
+ case PIXEL_FORMAT_RGBA32:
2434
+ case PIXEL_FORMAT_RGBX32:
2435
+ case PIXEL_FORMAT_BGRA32:
2436
+ case PIXEL_FORMAT_BGRX32:
2437
+ progressive->format = DstFormat;
2438
+ break;
2439
+ default:
2440
+ progressive->format = PIXEL_FORMAT_XRGB32;
2441
+ break;
2442
+ }
2443
+
2444
+ const size_t start = Stream_GetPosition(s);
2445
+ progressive->state = 0; /* Set state to not initialized */
2446
+ while (Stream_GetRemainingLength(s) > 0)
2447
+ {
2448
+ if (progressive_parse_block(progressive, s, surface, region) < 0)
2449
+ goto fail;
2450
+ }
2451
+
2452
+ const size_t end = Stream_GetPosition(s);
2453
+ if ((end - start) != SrcSize)
2454
+ {
2455
+ WLog_Print(progressive->log, WLOG_ERROR,
2456
+ "total block len %" PRIuz " does not match read data %" PRIu32, end - start,
2457
+ SrcSize);
2458
+ rc = -1041;
2459
+ goto fail;
2460
+ }
2461
+
2462
+ if (!update_tiles(progressive, surface, pDstData, DstFormat, nDstStep, nXDst, nYDst, region,
2463
+ invalidRegion))
2464
+ return -2002;
2465
+ fail:
2466
+ return rc;
2467
+ }
2468
+
2469
+ BOOL progressive_rfx_write_message_progressive_simple(PROGRESSIVE_CONTEXT* progressive, wStream* s,
2470
+ const RFX_MESSAGE* msg)
2471
+ {
2472
+ RFX_CONTEXT* context = NULL;
2473
+
2474
+ WINPR_ASSERT(progressive);
2475
+ WINPR_ASSERT(s);
2476
+ WINPR_ASSERT(msg);
2477
+ context = progressive->rfx_context;
2478
+ return rfx_write_message_progressive_simple(context, s, msg);
2479
+ }
2480
+
2481
+ int progressive_compress(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
2482
+ const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize, UINT32 SrcFormat,
2483
+ UINT32 Width, UINT32 Height, UINT32 ScanLine,
2484
+ const REGION16* WINPR_RESTRICT invalidRegion,
2485
+ BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize)
2486
+ {
2487
+ BOOL rc = FALSE;
2488
+ int res = -6;
2489
+ wStream* s = NULL;
2490
+ UINT32 numRects = 0;
2491
+ RFX_RECT* rects = NULL;
2492
+ RFX_MESSAGE* message = NULL;
2493
+
2494
+ if (!progressive || !pSrcData || !ppDstData || !pDstSize)
2495
+ {
2496
+ return -1;
2497
+ }
2498
+
2499
+ if (ScanLine == 0)
2500
+ {
2501
+ switch (SrcFormat)
2502
+ {
2503
+ case PIXEL_FORMAT_ABGR32:
2504
+ case PIXEL_FORMAT_ARGB32:
2505
+ case PIXEL_FORMAT_XBGR32:
2506
+ case PIXEL_FORMAT_XRGB32:
2507
+ case PIXEL_FORMAT_BGRA32:
2508
+ case PIXEL_FORMAT_BGRX32:
2509
+ case PIXEL_FORMAT_RGBA32:
2510
+ case PIXEL_FORMAT_RGBX32:
2511
+ ScanLine = Width * 4;
2512
+ break;
2513
+ default:
2514
+ return -2;
2515
+ }
2516
+ }
2517
+
2518
+ if (SrcSize < Height * ScanLine)
2519
+ return -4;
2520
+
2521
+ if (!invalidRegion)
2522
+ {
2523
+ numRects = (Width + 63) / 64;
2524
+ numRects *= (Height + 63) / 64;
2525
+ }
2526
+ else
2527
+ {
2528
+ const int nr = region16_n_rects(invalidRegion);
2529
+ numRects = WINPR_ASSERTING_INT_CAST(uint32_t, nr);
2530
+ }
2531
+
2532
+ if (numRects == 0)
2533
+ return 0;
2534
+
2535
+ if (!Stream_EnsureRemainingCapacity(progressive->rects, numRects * sizeof(RFX_RECT)))
2536
+ return -5;
2537
+ rects = Stream_BufferAs(progressive->rects, RFX_RECT);
2538
+ if (invalidRegion)
2539
+ {
2540
+ const RECTANGLE_16* region_rects = region16_rects(invalidRegion, NULL);
2541
+ for (UINT32 idx = 0; idx < numRects; idx++)
2542
+ {
2543
+ const RECTANGLE_16* r = &region_rects[idx];
2544
+ RFX_RECT* rect = &rects[idx];
2545
+
2546
+ rect->x = r->left;
2547
+ rect->y = r->top;
2548
+ rect->width = r->right - r->left;
2549
+ rect->height = r->bottom - r->top;
2550
+ }
2551
+ }
2552
+ else
2553
+ {
2554
+ UINT16 x = 0;
2555
+ UINT16 y = 0;
2556
+
2557
+ for (UINT32 i = 0; i < numRects; i++)
2558
+ {
2559
+ RFX_RECT* r = &rects[i];
2560
+ r->x = x;
2561
+ r->y = y;
2562
+
2563
+ WINPR_ASSERT(Width >= x);
2564
+ WINPR_ASSERT(Height >= y);
2565
+ r->width = MIN(64, WINPR_ASSERTING_INT_CAST(UINT16, Width - x));
2566
+ r->height = MIN(64, WINPR_ASSERTING_INT_CAST(UINT16, Height - y));
2567
+
2568
+ if (x + 64 >= Width)
2569
+ {
2570
+ y += 64;
2571
+ x = 0;
2572
+ }
2573
+ else
2574
+ x += 64;
2575
+
2576
+ WINPR_ASSERT(r->x % 64 == 0);
2577
+ WINPR_ASSERT(r->y % 64 == 0);
2578
+ WINPR_ASSERT(r->width <= 64);
2579
+ WINPR_ASSERT(r->height <= 64);
2580
+ }
2581
+ }
2582
+ s = progressive->buffer;
2583
+ Stream_SetPosition(s, 0);
2584
+
2585
+ progressive->rfx_context->mode = RLGR1;
2586
+
2587
+ progressive->rfx_context->width = WINPR_ASSERTING_INT_CAST(UINT16, Width);
2588
+ progressive->rfx_context->height = WINPR_ASSERTING_INT_CAST(UINT16, Height);
2589
+ rfx_context_set_pixel_format(progressive->rfx_context, SrcFormat);
2590
+ message = rfx_encode_message(progressive->rfx_context, rects, numRects, pSrcData, Width, Height,
2591
+ ScanLine);
2592
+ if (!message)
2593
+ {
2594
+ WLog_ERR(TAG, "failed to encode rfx message");
2595
+ goto fail;
2596
+ }
2597
+
2598
+ rc = progressive_rfx_write_message_progressive_simple(progressive, s, message);
2599
+ rfx_message_free(progressive->rfx_context, message);
2600
+ if (!rc)
2601
+ goto fail;
2602
+
2603
+ const size_t pos = Stream_GetPosition(s);
2604
+ WINPR_ASSERT(pos <= UINT32_MAX);
2605
+ *pDstSize = (UINT32)pos;
2606
+ *ppDstData = Stream_Buffer(s);
2607
+ res = 1;
2608
+ fail:
2609
+ return res;
2610
+ }
2611
+
2612
+ BOOL progressive_context_reset(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive)
2613
+ {
2614
+ if (!progressive)
2615
+ return FALSE;
2616
+
2617
+ return TRUE;
2618
+ }
2619
+
2620
+ PROGRESSIVE_CONTEXT* progressive_context_new(BOOL Compressor)
2621
+ {
2622
+ return progressive_context_new_ex(Compressor, 0);
2623
+ }
2624
+
2625
+ PROGRESSIVE_CONTEXT* progressive_context_new_ex(BOOL Compressor, UINT32 ThreadingFlags)
2626
+ {
2627
+ PROGRESSIVE_CONTEXT* progressive =
2628
+ (PROGRESSIVE_CONTEXT*)winpr_aligned_calloc(1, sizeof(PROGRESSIVE_CONTEXT), 32);
2629
+
2630
+ if (!progressive)
2631
+ return NULL;
2632
+
2633
+ progressive->Compressor = Compressor;
2634
+ progressive->quantProgValFull.quality = 100;
2635
+ progressive->log = WLog_Get(TAG);
2636
+ if (!progressive->log)
2637
+ goto fail;
2638
+ progressive->rfx_context = rfx_context_new_ex(Compressor, ThreadingFlags);
2639
+ if (!progressive->rfx_context)
2640
+ goto fail;
2641
+ progressive->buffer = Stream_New(NULL, 1024);
2642
+ if (!progressive->buffer)
2643
+ goto fail;
2644
+ progressive->rects = Stream_New(NULL, 1024);
2645
+ if (!progressive->rects)
2646
+ goto fail;
2647
+ progressive->bufferPool = BufferPool_New(TRUE, (8192LL + 32LL) * 3LL, 16);
2648
+ if (!progressive->bufferPool)
2649
+ goto fail;
2650
+ progressive->SurfaceContexts = HashTable_New(TRUE);
2651
+ if (!progressive->SurfaceContexts)
2652
+ goto fail;
2653
+
2654
+ {
2655
+ wObject* obj = HashTable_ValueObject(progressive->SurfaceContexts);
2656
+ WINPR_ASSERT(obj);
2657
+ obj->fnObjectFree = progressive_surface_context_free;
2658
+ }
2659
+ return progressive;
2660
+ fail:
2661
+ WINPR_PRAGMA_DIAG_PUSH
2662
+ WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2663
+ progressive_context_free(progressive);
2664
+ WINPR_PRAGMA_DIAG_POP
2665
+ return NULL;
2666
+ }
2667
+
2668
+ void progressive_context_free(PROGRESSIVE_CONTEXT* progressive)
2669
+ {
2670
+ if (!progressive)
2671
+ return;
2672
+
2673
+ Stream_Free(progressive->buffer, TRUE);
2674
+ Stream_Free(progressive->rects, TRUE);
2675
+ rfx_context_free(progressive->rfx_context);
2676
+
2677
+ BufferPool_Free(progressive->bufferPool);
2678
+ HashTable_Free(progressive->SurfaceContexts);
2679
+
2680
+ winpr_aligned_free(progressive);
2681
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/rfx_decode.h ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RemoteFX Codec Library - Decode
4
+ *
5
+ * Copyright 2011 Vic Lee
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_CODEC_RFX_DECODE_H
21
+ #define FREERDP_LIB_CODEC_RFX_DECODE_H
22
+
23
+ #include <winpr/wtypes.h>
24
+
25
+ #include <freerdp/codec/rfx.h>
26
+ #include <freerdp/api.h>
27
+
28
+ /* stride is bytes between rows in the output buffer. */
29
+ FREERDP_LOCAL BOOL rfx_decode_rgb(RFX_CONTEXT* WINPR_RESTRICT context,
30
+ const RFX_TILE* WINPR_RESTRICT tile,
31
+ BYTE* WINPR_RESTRICT rgb_buffer, UINT32 stride);
32
+
33
+ #endif /* FREERDP_LIB_CODEC_RFX_DECODE_H */
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/rfx_dwt.c ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RemoteFX Codec Library - DWT
4
+ *
5
+ * Copyright 2011 Vic Lee
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <winpr/assert.h>
21
+ #include <winpr/cast.h>
22
+
23
+ #include <freerdp/config.h>
24
+
25
+ #include <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+
29
+ #include "rfx_dwt.h"
30
+
31
+ static INLINE void rfx_dwt_2d_decode_block(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT idwt,
32
+ size_t subband_width)
33
+ {
34
+ const size_t total_width = subband_width << 1;
35
+
36
+ /* Inverse DWT in horizontal direction, results in 2 sub-bands in L, H order in tmp buffer idwt.
37
+ */
38
+ /* The 4 sub-bands are stored in HL(0), LH(1), HH(2), LL(3) order. */
39
+ /* The lower part L uses LL(3) and HL(0). */
40
+ /* The higher part H uses LH(1) and HH(2). */
41
+
42
+ const INT16* ll = buffer + subband_width * subband_width * 3;
43
+ const INT16* hl = buffer;
44
+ INT16* l_dst = idwt;
45
+
46
+ const INT16* lh = buffer + subband_width * subband_width;
47
+ const INT16* hh = buffer + subband_width * subband_width * 2;
48
+ INT16* h_dst = idwt + subband_width * subband_width * 2;
49
+
50
+ for (size_t y = 0; y < subband_width; y++)
51
+ {
52
+ /* Even coefficients */
53
+ l_dst[0] = WINPR_ASSERTING_INT_CAST(int16_t, ll[0] - ((hl[0] + hl[0] + 1) >> 1));
54
+ h_dst[0] = WINPR_ASSERTING_INT_CAST(int16_t, lh[0] - ((hh[0] + hh[0] + 1) >> 1));
55
+ for (size_t n = 1; n < subband_width; n++)
56
+ {
57
+ const size_t x = n << 1;
58
+ l_dst[x] = WINPR_ASSERTING_INT_CAST(int16_t, ll[n] - ((hl[n - 1] + hl[n] + 1) >> 1));
59
+ h_dst[x] = WINPR_ASSERTING_INT_CAST(int16_t, lh[n] - ((hh[n - 1] + hh[n] + 1) >> 1));
60
+ }
61
+
62
+ /* Odd coefficients */
63
+ size_t n = 0;
64
+ for (; n < subband_width - 1; n++)
65
+ {
66
+ const size_t x = n << 1;
67
+
68
+ const int ld = (hl[n] << 1) + ((l_dst[x] + l_dst[x + 2]) >> 1);
69
+ const int hd = (hh[n] << 1) + ((h_dst[x] + h_dst[x + 2]) >> 1);
70
+
71
+ l_dst[x + 1] = WINPR_ASSERTING_INT_CAST(INT16, ld);
72
+ h_dst[x + 1] = WINPR_ASSERTING_INT_CAST(INT16, hd);
73
+ }
74
+
75
+ const size_t x = n << 1;
76
+
77
+ const int ld = (hl[n] << 1) + (l_dst[x]);
78
+ const int hd = (hh[n] << 1) + (h_dst[x]);
79
+ l_dst[x + 1] = WINPR_ASSERTING_INT_CAST(INT16, ld);
80
+ h_dst[x + 1] = WINPR_ASSERTING_INT_CAST(INT16, hd);
81
+
82
+ ll += subband_width;
83
+ hl += subband_width;
84
+ l_dst += total_width;
85
+
86
+ lh += subband_width;
87
+ hh += subband_width;
88
+ h_dst += total_width;
89
+ }
90
+
91
+ /* Inverse DWT in vertical direction, results are stored in original buffer. */
92
+ for (size_t x = 0; x < total_width; x++)
93
+ {
94
+ const INT16* l = idwt + x;
95
+ const INT16* h = idwt + x + subband_width * total_width;
96
+ INT16* dst = buffer + x;
97
+
98
+ const int dd = *l - ((*h * 2 + 1) >> 1);
99
+ *dst = WINPR_ASSERTING_INT_CAST(INT16, dd);
100
+
101
+ for (size_t n = 1; n < subband_width; n++)
102
+ {
103
+ l += total_width;
104
+ h += total_width;
105
+
106
+ /* Even coefficients */
107
+ const int d2 = *l - ((*(h - total_width) + *h + 1) >> 1);
108
+ dst[2 * total_width] = WINPR_ASSERTING_INT_CAST(INT16, d2);
109
+
110
+ /* Odd coefficients */
111
+ const int d = (*(h - total_width) << 1) + ((*dst + dst[2 * total_width]) >> 1);
112
+ dst[total_width] = WINPR_ASSERTING_INT_CAST(INT16, d);
113
+
114
+ dst += 2 * total_width;
115
+ }
116
+
117
+ const int d = (*h << 1) + ((*dst * 2) >> 1);
118
+ dst[total_width] = WINPR_ASSERTING_INT_CAST(INT16, d);
119
+ }
120
+ }
121
+
122
+ void rfx_dwt_2d_decode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer)
123
+ {
124
+ WINPR_ASSERT(buffer);
125
+ WINPR_ASSERT(dwt_buffer);
126
+
127
+ rfx_dwt_2d_decode_block(&buffer[3840], dwt_buffer, 8);
128
+ rfx_dwt_2d_decode_block(&buffer[3072], dwt_buffer, 16);
129
+ rfx_dwt_2d_decode_block(&buffer[0], dwt_buffer, 32);
130
+ }
131
+
132
+ static void rfx_dwt_2d_encode_block(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt,
133
+ UINT32 subband_width)
134
+ {
135
+ INT16* src = NULL;
136
+ INT16* l = NULL;
137
+ INT16* h = NULL;
138
+ INT16* l_src = NULL;
139
+ INT16* h_src = NULL;
140
+ INT16* hl = NULL;
141
+ INT16* lh = NULL;
142
+ INT16* hh = NULL;
143
+ INT16* ll = NULL;
144
+
145
+ const UINT32 total_width = subband_width << 1;
146
+
147
+ /* DWT in vertical direction, results in 2 sub-bands in L, H order in tmp buffer dwt. */
148
+ for (UINT32 x = 0; x < total_width; x++)
149
+ {
150
+ for (UINT32 n = 0; n < subband_width; n++)
151
+ {
152
+ UINT32 y = n << 1;
153
+ l = dwt + 1ULL * n * total_width + x;
154
+ h = l + 1ULL * subband_width * total_width;
155
+ src = buffer + 1ULL * y * total_width + x;
156
+
157
+ /* H */
158
+ *h = WINPR_ASSERTING_INT_CAST(
159
+ int16_t, (src[total_width] -
160
+ ((src[0] + src[n < subband_width - 1 ? 2 * total_width : 0]) >> 1)) >>
161
+ 1);
162
+
163
+ /* L */
164
+ *l = WINPR_ASSERTING_INT_CAST(int16_t,
165
+ src[0] + (n == 0 ? *h : (*(h - total_width) + *h) >> 1));
166
+ }
167
+ }
168
+
169
+ /* DWT in horizontal direction, results in 4 sub-bands in HL(0), LH(1), HH(2), LL(3) order,
170
+ * stored in original buffer. */
171
+ /* The lower part L generates LL(3) and HL(0). */
172
+ /* The higher part H generates LH(1) and HH(2). */
173
+
174
+ ll = buffer + 3ULL * subband_width * subband_width;
175
+ hl = buffer;
176
+ l_src = dwt;
177
+
178
+ lh = buffer + 1ULL * subband_width * subband_width;
179
+ hh = buffer + 2ULL * subband_width * subband_width;
180
+ h_src = dwt + 2ULL * subband_width * subband_width;
181
+
182
+ for (size_t y = 0; y < subband_width; y++)
183
+ {
184
+ /* L */
185
+ for (UINT32 n = 0; n < subband_width; n++)
186
+ {
187
+ UINT32 x = n << 1;
188
+
189
+ /* HL */
190
+ hl[n] = WINPR_ASSERTING_INT_CAST(
191
+ int16_t,
192
+ (l_src[x + 1] - ((l_src[x] + l_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1);
193
+ /* LL */
194
+ ll[n] = WINPR_ASSERTING_INT_CAST(
195
+ int16_t, l_src[x] + (n == 0 ? hl[n] : (hl[n - 1] + hl[n]) >> 1));
196
+ }
197
+
198
+ /* H */
199
+ for (UINT32 n = 0; n < subband_width; n++)
200
+ {
201
+ UINT32 x = n << 1;
202
+
203
+ /* HH */
204
+ hh[n] = WINPR_ASSERTING_INT_CAST(
205
+ int16_t,
206
+ (h_src[x + 1] - ((h_src[x] + h_src[n < subband_width - 1 ? x + 2 : x]) >> 1)) >> 1);
207
+ /* LH */
208
+ lh[n] = WINPR_ASSERTING_INT_CAST(
209
+ int16_t, h_src[x] + (n == 0 ? hh[n] : (hh[n - 1] + hh[n]) >> 1));
210
+ }
211
+
212
+ ll += subband_width;
213
+ hl += subband_width;
214
+ l_src += total_width;
215
+
216
+ lh += subband_width;
217
+ hh += subband_width;
218
+ h_src += total_width;
219
+ }
220
+ }
221
+
222
+ void rfx_dwt_2d_encode(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer)
223
+ {
224
+ WINPR_ASSERT(buffer);
225
+ WINPR_ASSERT(dwt_buffer);
226
+
227
+ rfx_dwt_2d_encode_block(&buffer[0], dwt_buffer, 32);
228
+ rfx_dwt_2d_encode_block(&buffer[3072], dwt_buffer, 16);
229
+ rfx_dwt_2d_encode_block(&buffer[3840], dwt_buffer, 8);
230
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/rfx_quantization.h ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RemoteFX Codec Library - Quantization
4
+ *
5
+ * Copyright 2011 Vic Lee
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_CODEC_RFX_QUANTIZATION_H
21
+ #define FREERDP_LIB_CODEC_RFX_QUANTIZATION_H
22
+
23
+ #include <freerdp/codec/rfx.h>
24
+ #include <freerdp/api.h>
25
+
26
+ FREERDP_LOCAL void rfx_quantization_decode(INT16* WINPR_RESTRICT buffer,
27
+ const UINT32* WINPR_RESTRICT quantization_values);
28
+ FREERDP_LOCAL void rfx_quantization_encode(INT16* WINPR_RESTRICT buffer,
29
+ const UINT32* WINPR_RESTRICT quantization_values);
30
+
31
+ #endif /* FREERDP_LIB_CODEC_RFX_QUANTIZATION_H */
local-test-freerdp-full-01/afc-freerdp/libfreerdp/codec/rfx_types.h ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RemoteFX Codec Library
4
+ *
5
+ * Copyright 2011 Vic Lee
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_CODEC_RFX_TYPES_H
21
+ #define FREERDP_LIB_CODEC_RFX_TYPES_H
22
+
23
+ #include <freerdp/config.h>
24
+
25
+ #include <winpr/crt.h>
26
+ #include <winpr/pool.h>
27
+ #include <winpr/wlog.h>
28
+ #include <winpr/collections.h>
29
+
30
+ #include <freerdp/codec/rfx.h>
31
+ #include <freerdp/log.h>
32
+ #include <freerdp/utils/profiler.h>
33
+
34
+ #define RFX_TAG FREERDP_TAG("codec.rfx")
35
+ #ifdef WITH_DEBUG_RFX
36
+ #define DEBUG_RFX(...) WLog_DBG(RFX_TAG, __VA_ARGS__)
37
+ #else
38
+ #define DEBUG_RFX(...) \
39
+ do \
40
+ { \
41
+ } while (0)
42
+ #endif
43
+
44
+ #define RFX_DECODED_SYNC 0x00000001
45
+ #define RFX_DECODED_CONTEXT 0x00000002
46
+ #define RFX_DECODED_VERSIONS 0x00000004
47
+ #define RFX_DECODED_CHANNELS 0x00000008
48
+ #define RFX_DECODED_HEADERS 0x0000000F
49
+
50
+ typedef enum
51
+ {
52
+ RFX_STATE_INITIAL,
53
+ RFX_STATE_SERVER_UNINITIALIZED,
54
+ RFX_STATE_SEND_HEADERS,
55
+ RFX_STATE_SEND_FRAME_DATA,
56
+ RFX_STATE_FRAME_DATA_SENT,
57
+ RFX_STATE_FINAL
58
+ } RFX_STATE;
59
+
60
+ typedef struct S_RFX_TILE_COMPOSE_WORK_PARAM RFX_TILE_COMPOSE_WORK_PARAM;
61
+
62
+ typedef struct S_RFX_CONTEXT_PRIV RFX_CONTEXT_PRIV;
63
+ struct S_RFX_CONTEXT_PRIV
64
+ {
65
+ wLog* log;
66
+ wObjectPool* TilePool;
67
+
68
+ BOOL UseThreads;
69
+ PTP_WORK* workObjects;
70
+ RFX_TILE_COMPOSE_WORK_PARAM* tileWorkParams;
71
+
72
+ DWORD MinThreadCount;
73
+ DWORD MaxThreadCount;
74
+
75
+ PTP_POOL ThreadPool;
76
+ TP_CALLBACK_ENVIRON ThreadPoolEnv;
77
+
78
+ wBufferPool* BufferPool;
79
+
80
+ /* profilers */
81
+ PROFILER_DEFINE(prof_rfx_decode_rgb)
82
+ PROFILER_DEFINE(prof_rfx_decode_component)
83
+ PROFILER_DEFINE(prof_rfx_rlgr_decode)
84
+ PROFILER_DEFINE(prof_rfx_differential_decode)
85
+ PROFILER_DEFINE(prof_rfx_quantization_decode)
86
+ PROFILER_DEFINE(prof_rfx_dwt_2d_decode)
87
+ PROFILER_DEFINE(prof_rfx_ycbcr_to_rgb)
88
+
89
+ PROFILER_DEFINE(prof_rfx_encode_rgb)
90
+ PROFILER_DEFINE(prof_rfx_encode_component)
91
+ PROFILER_DEFINE(prof_rfx_rlgr_encode)
92
+ PROFILER_DEFINE(prof_rfx_differential_encode)
93
+ PROFILER_DEFINE(prof_rfx_quantization_encode)
94
+ PROFILER_DEFINE(prof_rfx_dwt_2d_encode)
95
+ PROFILER_DEFINE(prof_rfx_rgb_to_ycbcr)
96
+ PROFILER_DEFINE(prof_rfx_encode_format_rgb)
97
+ };
98
+
99
+ struct S_RFX_MESSAGE
100
+ {
101
+ UINT32 frameIdx;
102
+
103
+ /**
104
+ * The rects array represents the updated region of the frame. The UI
105
+ * requires to clip drawing destination base on the union of the rects.
106
+ */
107
+ UINT16 numRects;
108
+ RFX_RECT* rects;
109
+
110
+ /**
111
+ * The tiles array represents the actual frame data. Each tile is always
112
+ * 64x64. Note that only pixels inside the updated region (represented as
113
+ * rects described above) are valid. Pixels outside of the region may
114
+ * contain arbitrary data.
115
+ */
116
+ UINT16 numTiles;
117
+ size_t allocatedTiles;
118
+ RFX_TILE** tiles;
119
+
120
+ UINT16 numQuant;
121
+ UINT32* quantVals;
122
+
123
+ UINT32 tilesDataSize;
124
+
125
+ BOOL freeArray;
126
+ };
127
+
128
+ struct S_RFX_MESSAGE_LIST
129
+ {
130
+ struct S_RFX_MESSAGE* list;
131
+ size_t count;
132
+ RFX_CONTEXT* context;
133
+ };
134
+
135
+ struct S_RFX_CONTEXT
136
+ {
137
+ RFX_STATE state;
138
+
139
+ BOOL encoder;
140
+ UINT16 flags;
141
+ UINT16 properties;
142
+ UINT16 width;
143
+ UINT16 height;
144
+ RLGR_MODE mode;
145
+ UINT32 version;
146
+ UINT32 codec_id;
147
+ UINT32 codec_version;
148
+ UINT32 pixel_format;
149
+ BYTE bits_per_pixel;
150
+
151
+ /* color palette allocated by the application */
152
+ const BYTE* palette;
153
+
154
+ /* temporary data within a frame */
155
+ UINT32 frameIdx;
156
+ BYTE numQuant;
157
+ UINT32* quants;
158
+ BYTE quantIdxY;
159
+ BYTE quantIdxCb;
160
+ BYTE quantIdxCr;
161
+
162
+ /* decoded header blocks */
163
+ UINT32 decodedHeaderBlocks;
164
+ UINT16 expectedDataBlockType;
165
+ struct S_RFX_MESSAGE currentMessage;
166
+
167
+ /* routines */
168
+ void (*quantization_decode)(INT16* WINPR_RESTRICT buffer,
169
+ const UINT32* WINPR_RESTRICT quantization_values);
170
+ void (*quantization_encode)(INT16* WINPR_RESTRICT buffer,
171
+ const UINT32* WINPR_RESTRICT quantization_values);
172
+ void (*dwt_2d_decode)(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer);
173
+ void (*dwt_2d_extrapolate_decode)(INT16* WINPR_RESTRICT src, INT16* WINPR_RESTRICT temp);
174
+ void (*dwt_2d_encode)(INT16* WINPR_RESTRICT buffer, INT16* WINPR_RESTRICT dwt_buffer);
175
+ int (*rlgr_decode)(RLGR_MODE mode, const BYTE* WINPR_RESTRICT data, UINT32 data_size,
176
+ INT16* WINPR_RESTRICT buffer, UINT32 buffer_size);
177
+ int (*rlgr_encode)(RLGR_MODE mode, const INT16* WINPR_RESTRICT data, UINT32 data_size,
178
+ BYTE* WINPR_RESTRICT buffer, UINT32 buffer_size);
179
+
180
+ /* private definitions */
181
+ RFX_CONTEXT_PRIV* priv;
182
+ };
183
+
184
+ #endif /* FREERDP_LIB_CODEC_RFX_TYPES_H */
local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/CMakeLists.txt ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FreeRDP: A Remote Desktop Protocol Implementation
2
+ # libfreerdp-gdi cmake build script
3
+ #
4
+ # Copyright 2012 Marc-Andre Moreau <[email protected]>
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ set(MODULE_NAME "freerdp-gdi")
19
+ set(MODULE_PREFIX "FREERDP_GDI")
20
+
21
+ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
22
+
23
+ option(WITH_GFX_FRAME_DUMP "Dump GFX commands to directory" OFF)
24
+ if(WITH_GFX_FRAME_DUMP)
25
+ freerdp_definition_add(WITH_GFX_FRAME_DUMP)
26
+ endif()
27
+
28
+ file(GLOB ${MODULE_PREFIX}_SRCS LIST_DIRECTORIES false RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS "*.[ch]")
29
+
30
+ freerdp_module_add(${${MODULE_PREFIX}_SRCS})
31
+
32
+ if(BUILD_TESTING_INTERNAL)
33
+ add_subdirectory(test)
34
+ endif()
local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/bitmap.c ADDED
@@ -0,0 +1,678 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * GDI Bitmap Functions
4
+ *
5
+ * Copyright 2010-2011 Marc-Andre Moreau <[email protected]>
6
+ * Copyright 2016 Armin Novak <[email protected]>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <stdio.h>
25
+ #include <string.h>
26
+ #include <stdlib.h>
27
+
28
+ #include <freerdp/api.h>
29
+ #include <freerdp/freerdp.h>
30
+ #include <freerdp/gdi/gdi.h>
31
+ #include <freerdp/codec/color.h>
32
+
33
+ #include <freerdp/gdi/region.h>
34
+ #include <freerdp/gdi/bitmap.h>
35
+ #include <freerdp/log.h>
36
+ #include <freerdp/gdi/shape.h>
37
+
38
+ #include "brush.h"
39
+ #include "clipping.h"
40
+ #include "../gdi/gdi.h"
41
+
42
+ #define TAG FREERDP_TAG("gdi.bitmap")
43
+
44
+ /**
45
+ * Get pixel at the given coordinates. msdn{dd144909}
46
+ * @param hdc device context
47
+ * @param nXPos pixel x position
48
+ * @param nYPos pixel y position
49
+ * @return pixel color
50
+ */
51
+
52
+ UINT32 gdi_GetPixel(HGDI_DC hdc, UINT32 nXPos, UINT32 nYPos)
53
+ {
54
+ HGDI_BITMAP hBmp = (HGDI_BITMAP)hdc->selectedObject;
55
+ BYTE* data =
56
+ &(hBmp->data[(nYPos * hBmp->scanline) + nXPos * FreeRDPGetBytesPerPixel(hBmp->format)]);
57
+ return FreeRDPReadColor(data, hBmp->format);
58
+ }
59
+
60
+ BYTE* gdi_GetPointer(HGDI_BITMAP hBmp, UINT32 X, UINT32 Y)
61
+ {
62
+ UINT32 bpp = FreeRDPGetBytesPerPixel(hBmp->format);
63
+ return &hBmp->data[(Y * WINPR_ASSERTING_INT_CAST(uint32_t, hBmp->width) * bpp) + X * bpp];
64
+ }
65
+
66
+ /**
67
+ * Set pixel at the given coordinates. msdn{dd145078}
68
+ *
69
+ * @param hBmp device context
70
+ * @param X pixel x position
71
+ * @param Y pixel y position
72
+ * @param crColor new pixel color
73
+ * @return the color written
74
+ */
75
+
76
+ static INLINE UINT32 gdi_SetPixelBmp(HGDI_BITMAP hBmp, UINT32 X, UINT32 Y, UINT32 crColor)
77
+ {
78
+ BYTE* p = &hBmp->data[(Y * hBmp->scanline) + X * FreeRDPGetBytesPerPixel(hBmp->format)];
79
+ FreeRDPWriteColor(p, hBmp->format, crColor);
80
+ return crColor;
81
+ }
82
+
83
+ UINT32 gdi_SetPixel(HGDI_DC hdc, UINT32 X, UINT32 Y, UINT32 crColor)
84
+ {
85
+ HGDI_BITMAP hBmp = (HGDI_BITMAP)hdc->selectedObject;
86
+ return gdi_SetPixelBmp(hBmp, X, Y, crColor);
87
+ }
88
+
89
+ /**
90
+ * Create a new bitmap with the given width, height, color format and pixel buffer. msdn{dd183485}
91
+ *
92
+ * @param nWidth width
93
+ * @param nHeight height
94
+ * @param format the color format used
95
+ * @param data pixel buffer
96
+ * @return new bitmap
97
+ */
98
+
99
+ HGDI_BITMAP gdi_CreateBitmap(UINT32 nWidth, UINT32 nHeight, UINT32 format, BYTE* data)
100
+ {
101
+ return gdi_CreateBitmapEx(nWidth, nHeight, format, 0, data, winpr_aligned_free);
102
+ }
103
+
104
+ /**
105
+ * Create a new bitmap with the given width, height, color format and pixel buffer. msdn{dd183485}
106
+ *
107
+ * @param nWidth width
108
+ * @param nHeight height
109
+ * @param format the color format used
110
+ * @param data pixel buffer
111
+ * @param fkt_free The function used for deallocation of the buffer, NULL for none.
112
+ * @return new bitmap
113
+ */
114
+
115
+ HGDI_BITMAP gdi_CreateBitmapEx(UINT32 nWidth, UINT32 nHeight, UINT32 format, UINT32 stride,
116
+ BYTE* data, void (*fkt_free)(void*))
117
+ {
118
+ HGDI_BITMAP hBitmap = (HGDI_BITMAP)calloc(1, sizeof(GDI_BITMAP));
119
+
120
+ if (!hBitmap)
121
+ return NULL;
122
+
123
+ hBitmap->objectType = GDIOBJECT_BITMAP;
124
+ hBitmap->format = format;
125
+
126
+ if (stride > 0)
127
+ hBitmap->scanline = stride;
128
+ else
129
+ hBitmap->scanline = nWidth * FreeRDPGetBytesPerPixel(hBitmap->format);
130
+
131
+ hBitmap->width = WINPR_ASSERTING_INT_CAST(int, nWidth);
132
+ hBitmap->height = WINPR_ASSERTING_INT_CAST(int, nHeight);
133
+ hBitmap->data = data;
134
+ hBitmap->free = fkt_free;
135
+ return hBitmap;
136
+ }
137
+
138
+ /**
139
+ * Create a new bitmap of the given width and height compatible with the current device context.
140
+ * msdn{dd183488}
141
+ *
142
+ * @param hdc device context
143
+ * @param nWidth width
144
+ * @param nHeight height
145
+ *
146
+ * @return new bitmap
147
+ */
148
+
149
+ HGDI_BITMAP gdi_CreateCompatibleBitmap(HGDI_DC hdc, UINT32 nWidth, UINT32 nHeight)
150
+ {
151
+ HGDI_BITMAP hBitmap = (HGDI_BITMAP)calloc(1, sizeof(GDI_BITMAP));
152
+
153
+ if (!hBitmap)
154
+ return NULL;
155
+
156
+ hBitmap->objectType = GDIOBJECT_BITMAP;
157
+ hBitmap->format = hdc->format;
158
+ WINPR_ASSERT(nWidth <= INT32_MAX);
159
+ hBitmap->width = (INT32)nWidth;
160
+
161
+ WINPR_ASSERT(nHeight <= INT32_MAX);
162
+ hBitmap->height = (INT32)nHeight;
163
+
164
+ size_t size = 1ull * nWidth * nHeight * FreeRDPGetBytesPerPixel(hBitmap->format);
165
+ hBitmap->data = winpr_aligned_malloc(size, 16);
166
+ hBitmap->free = winpr_aligned_free;
167
+
168
+ if (!hBitmap->data)
169
+ {
170
+ free(hBitmap);
171
+ return NULL;
172
+ }
173
+
174
+ /* Initialize with 0xff */
175
+ memset(hBitmap->data, 0xff, size);
176
+ hBitmap->scanline = nWidth * FreeRDPGetBytesPerPixel(hBitmap->format);
177
+ return hBitmap;
178
+ }
179
+
180
+ static BOOL op_not(UINT32* stack, const UINT32* stackp)
181
+ {
182
+ if (!stack || !stackp)
183
+ return FALSE;
184
+
185
+ if (*stackp < 1)
186
+ return FALSE;
187
+
188
+ stack[(*stackp) - 1] = ~stack[(*stackp) - 1];
189
+ return TRUE;
190
+ }
191
+
192
+ static BOOL op_and(UINT32* stack, UINT32* stackp)
193
+ {
194
+ if (!stack || !stackp)
195
+ return FALSE;
196
+
197
+ if (*stackp < 2)
198
+ return FALSE;
199
+
200
+ (*stackp)--;
201
+ stack[(*stackp) - 1] &= stack[(*stackp)];
202
+ return TRUE;
203
+ }
204
+
205
+ static BOOL op_or(UINT32* stack, UINT32* stackp)
206
+ {
207
+ if (!stack || !stackp)
208
+ return FALSE;
209
+
210
+ if (*stackp < 2)
211
+ return FALSE;
212
+
213
+ (*stackp)--;
214
+ stack[(*stackp) - 1] |= stack[(*stackp)];
215
+ return TRUE;
216
+ }
217
+
218
+ static BOOL op_xor(UINT32* stack, UINT32* stackp)
219
+ {
220
+ if (!stack || !stackp)
221
+ return FALSE;
222
+
223
+ if (*stackp < 2)
224
+ return FALSE;
225
+
226
+ (*stackp)--;
227
+ stack[(*stackp) - 1] ^= stack[(*stackp)];
228
+ return TRUE;
229
+ }
230
+
231
+ static UINT32 process_rop(UINT32 src, UINT32 dst, UINT32 pat, const char* rop, UINT32 format)
232
+ {
233
+ UINT32 stack[10] = { 0 };
234
+ UINT32 stackp = 0;
235
+
236
+ while (*rop != '\0')
237
+ {
238
+ char op = *rop++;
239
+
240
+ switch (op)
241
+ {
242
+ case '0':
243
+ stack[stackp++] = FreeRDPGetColor(format, 0, 0, 0, 0xFF);
244
+ break;
245
+
246
+ case '1':
247
+ stack[stackp++] = FreeRDPGetColor(format, 0xFF, 0xFF, 0xFF, 0xFF);
248
+ break;
249
+
250
+ case 'D':
251
+ stack[stackp++] = dst;
252
+ break;
253
+
254
+ case 'S':
255
+ stack[stackp++] = src;
256
+ break;
257
+
258
+ case 'P':
259
+ stack[stackp++] = pat;
260
+ break;
261
+
262
+ case 'x':
263
+ op_xor(stack, &stackp);
264
+ break;
265
+
266
+ case 'a':
267
+ op_and(stack, &stackp);
268
+ break;
269
+
270
+ case 'o':
271
+ op_or(stack, &stackp);
272
+ break;
273
+
274
+ case 'n':
275
+ op_not(stack, &stackp);
276
+ break;
277
+
278
+ default:
279
+ break;
280
+ }
281
+ }
282
+
283
+ return stack[0];
284
+ }
285
+
286
+ static INLINE BOOL BitBlt_write(HGDI_DC hdcDest, HGDI_DC hdcSrc, INT32 nXDest, INT32 nYDest,
287
+ INT32 nXSrc, INT32 nYSrc, INT32 x, INT32 y, BOOL useSrc,
288
+ BOOL usePat, UINT32 style, const char* rop,
289
+ const gdiPalette* palette)
290
+ {
291
+ UINT32 dstColor = 0;
292
+ UINT32 colorA = 0;
293
+ UINT32 colorB = 0;
294
+ UINT32 colorC = 0;
295
+ const INT32 dstX = nXDest + x;
296
+ const INT32 dstY = nYDest + y;
297
+ BYTE* dstp = gdi_get_bitmap_pointer(hdcDest, dstX, dstY);
298
+
299
+ if (!dstp)
300
+ {
301
+ WLog_ERR(TAG, "dstp=%p", (const void*)dstp);
302
+ return FALSE;
303
+ }
304
+
305
+ colorA = FreeRDPReadColor(dstp, hdcDest->format);
306
+
307
+ if (useSrc)
308
+ {
309
+ const BYTE* srcp = gdi_get_bitmap_pointer(hdcSrc, nXSrc + x, nYSrc + y);
310
+
311
+ if (!srcp)
312
+ {
313
+ WLog_ERR(TAG, "srcp=%p", (const void*)srcp);
314
+ return FALSE;
315
+ }
316
+
317
+ colorC = FreeRDPReadColor(srcp, hdcSrc->format);
318
+ colorC = FreeRDPConvertColor(colorC, hdcSrc->format, hdcDest->format, palette);
319
+ }
320
+
321
+ if (usePat)
322
+ {
323
+ switch (style)
324
+ {
325
+ case GDI_BS_SOLID:
326
+ colorB = hdcDest->brush->color;
327
+ break;
328
+
329
+ case GDI_BS_HATCHED:
330
+ case GDI_BS_PATTERN:
331
+ {
332
+ const BYTE* patp =
333
+ gdi_get_brush_pointer(hdcDest, WINPR_ASSERTING_INT_CAST(uint32_t, nXDest + x),
334
+ WINPR_ASSERTING_INT_CAST(uint32_t, nYDest + y));
335
+
336
+ if (!patp)
337
+ {
338
+ WLog_ERR(TAG, "patp=%p", (const void*)patp);
339
+ return FALSE;
340
+ }
341
+
342
+ colorB = FreeRDPReadColor(patp, hdcDest->format);
343
+ }
344
+ break;
345
+
346
+ default:
347
+ break;
348
+ }
349
+ }
350
+
351
+ dstColor = process_rop(colorC, colorA, colorB, rop, hdcDest->format);
352
+ return FreeRDPWriteColor(dstp, hdcDest->format, dstColor);
353
+ }
354
+
355
+ static BOOL adjust_src_coordinates(HGDI_DC hdcSrc, INT32 nWidth, INT32 nHeight, INT32* px,
356
+ INT32* py)
357
+ {
358
+ HGDI_BITMAP hSrcBmp = NULL;
359
+ INT32 nXSrc = 0;
360
+ INT32 nYSrc = 0;
361
+
362
+ if (!hdcSrc || (nWidth < 0) || (nHeight < 0) || !px || !py)
363
+ return FALSE;
364
+
365
+ hSrcBmp = (HGDI_BITMAP)hdcSrc->selectedObject;
366
+ nXSrc = *px;
367
+ nYSrc = *py;
368
+
369
+ if (!hSrcBmp)
370
+ return FALSE;
371
+
372
+ if (nYSrc < 0)
373
+ {
374
+ nYSrc = 0;
375
+ nHeight = nHeight + nYSrc;
376
+ }
377
+
378
+ if ((nXSrc) < 0)
379
+ {
380
+ nXSrc = 0;
381
+ nWidth = nWidth + nXSrc;
382
+ }
383
+
384
+ if (hSrcBmp->width < (nXSrc + nWidth))
385
+ nXSrc = hSrcBmp->width - nWidth;
386
+
387
+ if (hSrcBmp->height < (nYSrc + nHeight))
388
+ nYSrc = hSrcBmp->height - nHeight;
389
+
390
+ if ((nXSrc < 0) || (nYSrc < 0))
391
+ return FALSE;
392
+
393
+ *px = nXSrc;
394
+ *py = nYSrc;
395
+ return TRUE;
396
+ }
397
+
398
+ static BOOL adjust_src_dst_coordinates(HGDI_DC hdcDest, INT32* pnXSrc, INT32* pnYSrc, INT32* pnXDst,
399
+ INT32* pnYDst, INT32* pnWidth, INT32* pnHeight)
400
+ {
401
+ HGDI_BITMAP hDstBmp = NULL;
402
+ volatile INT32 diffX = 0;
403
+ volatile INT32 diffY = 0;
404
+ volatile INT32 nXSrc = 0;
405
+ volatile INT32 nYSrc = 0;
406
+ volatile INT32 nXDst = 0;
407
+ volatile INT32 nYDst = 0;
408
+ volatile INT32 nWidth = 0;
409
+ volatile INT32 nHeight = 0;
410
+
411
+ if (!hdcDest || !pnXSrc || !pnYSrc || !pnXDst || !pnYDst || !pnWidth || !pnHeight)
412
+ return FALSE;
413
+
414
+ hDstBmp = (HGDI_BITMAP)hdcDest->selectedObject;
415
+ nXSrc = *pnXSrc;
416
+ nYSrc = *pnYSrc;
417
+ nXDst = *pnXDst;
418
+ nYDst = *pnYDst;
419
+ nWidth = *pnWidth;
420
+ nHeight = *pnHeight;
421
+
422
+ if (!hDstBmp)
423
+ return FALSE;
424
+
425
+ if (nXDst < 0)
426
+ {
427
+ nXSrc -= nXDst;
428
+ nWidth += nXDst;
429
+ nXDst = 0;
430
+ }
431
+
432
+ if (nYDst < 0)
433
+ {
434
+ nYSrc -= nYDst;
435
+ nHeight += nYDst;
436
+ nYDst = 0;
437
+ }
438
+
439
+ diffX = hDstBmp->width - nXDst - nWidth;
440
+
441
+ if (diffX < 0)
442
+ nWidth += diffX;
443
+
444
+ diffY = hDstBmp->height - nYDst - nHeight;
445
+
446
+ if (diffY < 0)
447
+ nHeight += diffY;
448
+
449
+ if ((nXDst < 0) || (nYDst < 0) || (nWidth < 0) || (nHeight < 0))
450
+ {
451
+ nXDst = 0;
452
+ nYDst = 0;
453
+ nWidth = 0;
454
+ nHeight = 0;
455
+ }
456
+
457
+ *pnXSrc = nXSrc;
458
+ *pnYSrc = nYSrc;
459
+ *pnXDst = nXDst;
460
+ *pnYDst = nYDst;
461
+ *pnWidth = nWidth;
462
+ *pnHeight = nHeight;
463
+ return TRUE;
464
+ }
465
+
466
+ static BOOL BitBlt_process(HGDI_DC hdcDest, INT32 nXDest, INT32 nYDest, INT32 nWidth, INT32 nHeight,
467
+ HGDI_DC hdcSrc, INT32 nXSrc, INT32 nYSrc, const char* rop,
468
+ const gdiPalette* palette)
469
+ {
470
+ UINT32 style = 0;
471
+ BOOL useSrc = FALSE;
472
+ BOOL usePat = FALSE;
473
+ const char* iter = rop;
474
+
475
+ while (*iter != '\0')
476
+ {
477
+ switch (*iter++)
478
+ {
479
+ case 'P':
480
+ usePat = TRUE;
481
+ break;
482
+
483
+ case 'S':
484
+ useSrc = TRUE;
485
+ break;
486
+
487
+ default:
488
+ break;
489
+ }
490
+ }
491
+
492
+ if (!hdcDest)
493
+ return FALSE;
494
+
495
+ if (!adjust_src_dst_coordinates(hdcDest, &nXSrc, &nYSrc, &nXDest, &nYDest, &nWidth, &nHeight))
496
+ return FALSE;
497
+
498
+ if (useSrc && !hdcSrc)
499
+ return FALSE;
500
+
501
+ if (useSrc)
502
+ {
503
+ if (!adjust_src_coordinates(hdcSrc, nWidth, nHeight, &nXSrc, &nYSrc))
504
+ return FALSE;
505
+ }
506
+
507
+ if (usePat)
508
+ {
509
+ style = gdi_GetBrushStyle(hdcDest);
510
+
511
+ switch (style)
512
+ {
513
+ case GDI_BS_SOLID:
514
+ case GDI_BS_HATCHED:
515
+ case GDI_BS_PATTERN:
516
+ break;
517
+
518
+ default:
519
+ WLog_ERR(TAG, "Invalid brush!!");
520
+ return FALSE;
521
+ }
522
+ }
523
+
524
+ if ((nXDest > nXSrc) && (nYDest > nYSrc))
525
+ {
526
+ for (INT32 y = nHeight - 1; y >= 0; y--)
527
+ {
528
+ for (INT32 x = nWidth - 1; x >= 0; x--)
529
+ {
530
+ if (!BitBlt_write(hdcDest, hdcSrc, nXDest, nYDest, nXSrc, nYSrc, x, y, useSrc,
531
+ usePat, style, rop, palette))
532
+ return FALSE;
533
+ }
534
+ }
535
+ }
536
+ else if (nXDest > nXSrc)
537
+ {
538
+ for (INT32 y = 0; y < nHeight; y++)
539
+ {
540
+ for (INT32 x = nWidth - 1; x >= 0; x--)
541
+ {
542
+ if (!BitBlt_write(hdcDest, hdcSrc, nXDest, nYDest, nXSrc, nYSrc, x, y, useSrc,
543
+ usePat, style, rop, palette))
544
+ return FALSE;
545
+ }
546
+ }
547
+ }
548
+ else if (nYDest > nYSrc)
549
+ {
550
+ for (INT32 y = nHeight - 1; y >= 0; y--)
551
+ {
552
+ for (INT32 x = 0; x < nWidth; x++)
553
+ {
554
+ if (!BitBlt_write(hdcDest, hdcSrc, nXDest, nYDest, nXSrc, nYSrc, x, y, useSrc,
555
+ usePat, style, rop, palette))
556
+ return FALSE;
557
+ }
558
+ }
559
+ }
560
+ else
561
+ {
562
+ for (INT32 y = 0; y < nHeight; y++)
563
+ {
564
+ for (INT32 x = 0; x < nWidth; x++)
565
+ {
566
+ if (!BitBlt_write(hdcDest, hdcSrc, nXDest, nYDest, nXSrc, nYSrc, x, y, useSrc,
567
+ usePat, style, rop, palette))
568
+ return FALSE;
569
+ }
570
+ }
571
+ }
572
+
573
+ return TRUE;
574
+ }
575
+
576
+ /**
577
+ * Perform a bit blit operation on the given pixel buffers.
578
+ * msdn{dd183370}
579
+ *
580
+ * @param hdcDest destination device context
581
+ * @param nXDest destination x1
582
+ * @param nYDest destination y1
583
+ * @param nWidth width
584
+ * @param nHeight height
585
+ * @param hdcSrc source device context
586
+ * @param nXSrc source x1
587
+ * @param nYSrc source y1
588
+ * @param rop raster operation code
589
+ * @return 0 on failure, non-zero otherwise
590
+ */
591
+ BOOL gdi_BitBlt(HGDI_DC hdcDest, INT32 nXDest, INT32 nYDest, INT32 nWidth, INT32 nHeight,
592
+ HGDI_DC hdcSrc, INT32 nXSrc, INT32 nYSrc, DWORD rop, const gdiPalette* palette)
593
+ {
594
+ HGDI_BITMAP hSrcBmp = NULL;
595
+ HGDI_BITMAP hDstBmp = NULL;
596
+
597
+ if (!hdcDest)
598
+ return FALSE;
599
+
600
+ if (!gdi_ClipCoords(hdcDest, &nXDest, &nYDest, &nWidth, &nHeight, &nXSrc, &nYSrc))
601
+ return TRUE;
602
+
603
+ /* Check which ROP should be performed.
604
+ * Some specific ROP are used heavily and are resource intensive,
605
+ * add optimized versions for these here.
606
+ *
607
+ * For all others fall back to the generic implementation.
608
+ */
609
+ switch (rop)
610
+ {
611
+ case GDI_SRCCOPY:
612
+ if (!hdcSrc)
613
+ return FALSE;
614
+
615
+ if (!adjust_src_dst_coordinates(hdcDest, &nXSrc, &nYSrc, &nXDest, &nYDest, &nWidth,
616
+ &nHeight))
617
+ return FALSE;
618
+
619
+ if (!adjust_src_coordinates(hdcSrc, nWidth, nHeight, &nXSrc, &nYSrc))
620
+ return FALSE;
621
+
622
+ hSrcBmp = (HGDI_BITMAP)hdcSrc->selectedObject;
623
+ hDstBmp = (HGDI_BITMAP)hdcDest->selectedObject;
624
+
625
+ if (!hSrcBmp || !hDstBmp)
626
+ return FALSE;
627
+
628
+ if (!freerdp_image_copy(
629
+ hDstBmp->data, hDstBmp->format, hDstBmp->scanline,
630
+ WINPR_ASSERTING_INT_CAST(UINT32, nXDest),
631
+ WINPR_ASSERTING_INT_CAST(UINT32, nYDest),
632
+ WINPR_ASSERTING_INT_CAST(UINT32, nWidth),
633
+ WINPR_ASSERTING_INT_CAST(UINT32, nHeight), hSrcBmp->data, hSrcBmp->format,
634
+ hSrcBmp->scanline, WINPR_ASSERTING_INT_CAST(UINT32, nXSrc),
635
+ WINPR_ASSERTING_INT_CAST(UINT32, nYSrc), palette, FREERDP_FLIP_NONE))
636
+ return FALSE;
637
+
638
+ break;
639
+
640
+ case GDI_DSTCOPY:
641
+ hSrcBmp = (HGDI_BITMAP)hdcDest->selectedObject;
642
+ hDstBmp = (HGDI_BITMAP)hdcDest->selectedObject;
643
+
644
+ if (!adjust_src_dst_coordinates(hdcDest, &nXSrc, &nYSrc, &nXDest, &nYDest, &nWidth,
645
+ &nHeight))
646
+ return FALSE;
647
+
648
+ if (!adjust_src_coordinates(hdcDest, nWidth, nHeight, &nXSrc, &nYSrc))
649
+ return FALSE;
650
+
651
+ if (!hSrcBmp || !hDstBmp)
652
+ return FALSE;
653
+
654
+ if (!freerdp_image_copy(
655
+ hDstBmp->data, hDstBmp->format, hDstBmp->scanline,
656
+ WINPR_ASSERTING_INT_CAST(UINT32, nXDest),
657
+ WINPR_ASSERTING_INT_CAST(UINT32, nYDest),
658
+ WINPR_ASSERTING_INT_CAST(UINT32, nWidth),
659
+ WINPR_ASSERTING_INT_CAST(UINT32, nHeight), hSrcBmp->data, hSrcBmp->format,
660
+ hSrcBmp->scanline, WINPR_ASSERTING_INT_CAST(UINT32, nXSrc),
661
+ WINPR_ASSERTING_INT_CAST(UINT32, nYSrc), palette, FREERDP_FLIP_NONE))
662
+ return FALSE;
663
+
664
+ break;
665
+
666
+ default:
667
+ if (!BitBlt_process(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc,
668
+ gdi_rop_to_string(rop), palette))
669
+ return FALSE;
670
+
671
+ break;
672
+ }
673
+
674
+ if (!gdi_InvalidateRegion(hdcDest, nXDest, nYDest, nWidth, nHeight))
675
+ return FALSE;
676
+
677
+ return TRUE;
678
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/brush.h ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * GDI Brush Functions
4
+ *
5
+ * Copyright 2010-2011 Marc-Andre Moreau <[email protected]>
6
+ * Copyright 2016 Armin Novak <[email protected]>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #ifndef FREERDP_LIB_GDI_BRUSH_H
23
+ #define FREERDP_LIB_GDI_BRUSH_H
24
+
25
+ #include <winpr/cast.h>
26
+
27
+ #include <freerdp/api.h>
28
+ #include <freerdp/gdi/gdi.h>
29
+
30
+ #ifdef __cplusplus
31
+ extern "C"
32
+ {
33
+ #endif
34
+
35
+ FREERDP_LOCAL const char* gdi_rop_to_string(UINT32 code);
36
+
37
+ FREERDP_LOCAL HGDI_BRUSH gdi_CreateSolidBrush(UINT32 crColor);
38
+ FREERDP_LOCAL HGDI_BRUSH gdi_CreatePatternBrush(HGDI_BITMAP hbmp);
39
+ FREERDP_LOCAL HGDI_BRUSH gdi_CreateHatchBrush(HGDI_BITMAP hbmp);
40
+
41
+ static INLINE UINT32 gdi_GetBrushStyle(HGDI_DC hdc)
42
+ {
43
+ if (!hdc || !hdc->brush)
44
+ return GDI_BS_NULL;
45
+
46
+ return WINPR_ASSERTING_INT_CAST(UINT32, hdc->brush->style);
47
+ }
48
+
49
+ #ifdef __cplusplus
50
+ }
51
+ #endif
52
+
53
+ #endif /* FREERDP_LIB_GDI_BRUSH_H */
local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/dc.c ADDED
@@ -0,0 +1,258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * GDI Device Context Functions
4
+ *
5
+ * Copyright 2010-2011 Marc-Andre Moreau <[email protected]>
6
+ * Copyright 2016 Armin Novak <[email protected]>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ /* Device Context Functions: http://msdn.microsoft.com/en-us/library/dd183554 */
23
+
24
+ #include <freerdp/config.h>
25
+
26
+ #include <stdio.h>
27
+ #include <stdlib.h>
28
+
29
+ #include <freerdp/freerdp.h>
30
+ #include <freerdp/gdi/gdi.h>
31
+
32
+ #include <freerdp/gdi/region.h>
33
+
34
+ #include <freerdp/gdi/dc.h>
35
+
36
+ /**
37
+ * @brief Get the current device context (a new one is created each time).
38
+ * msdn{dd144871}
39
+ *
40
+ * @return current device context
41
+ */
42
+
43
+ HGDI_DC gdi_GetDC(void)
44
+ {
45
+ HGDI_DC hDC = (HGDI_DC)calloc(1, sizeof(GDI_DC));
46
+
47
+ if (!hDC)
48
+ return NULL;
49
+
50
+ hDC->format = PIXEL_FORMAT_XRGB32;
51
+ hDC->drawMode = GDI_R2_BLACK;
52
+ hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0);
53
+
54
+ if (!hDC->clip)
55
+ {
56
+ free(hDC);
57
+ return NULL;
58
+ }
59
+
60
+ hDC->clip->null = TRUE;
61
+ hDC->hwnd = NULL;
62
+ return hDC;
63
+ }
64
+
65
+ /**
66
+ * @brief Create a device context.
67
+ * msdn{dd144871}
68
+ *
69
+ * @return new device context
70
+ */
71
+
72
+ HGDI_DC gdi_CreateDC(UINT32 format)
73
+ {
74
+ HGDI_DC hDC = NULL;
75
+
76
+ if (!(hDC = (HGDI_DC)calloc(1, sizeof(GDI_DC))))
77
+ return NULL;
78
+
79
+ hDC->drawMode = GDI_R2_BLACK;
80
+
81
+ if (!(hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0)))
82
+ goto fail;
83
+
84
+ hDC->clip->null = TRUE;
85
+ hDC->hwnd = NULL;
86
+ hDC->format = format;
87
+
88
+ if (!(hDC->hwnd = (HGDI_WND)calloc(1, sizeof(GDI_WND))))
89
+ goto fail;
90
+
91
+ if (!(hDC->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0)))
92
+ goto fail;
93
+
94
+ hDC->hwnd->invalid->null = TRUE;
95
+ hDC->hwnd->count = 32;
96
+
97
+ if (!(hDC->hwnd->cinvalid = (HGDI_RGN)calloc(hDC->hwnd->count, sizeof(GDI_RGN))))
98
+ goto fail;
99
+
100
+ hDC->hwnd->ninvalid = 0;
101
+ return hDC;
102
+ fail:
103
+ gdi_DeleteDC(hDC);
104
+ return NULL;
105
+ }
106
+
107
+ /**
108
+ * @brief Create a new device context compatible with the given device context.
109
+ * msdn{dd183489}
110
+ * @param hdc device context
111
+ * @return new compatible device context
112
+ */
113
+
114
+ HGDI_DC gdi_CreateCompatibleDC(HGDI_DC hdc)
115
+ {
116
+ HGDI_DC hDC = (HGDI_DC)calloc(1, sizeof(GDI_DC));
117
+
118
+ if (!hDC)
119
+ return NULL;
120
+
121
+ if (!(hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0)))
122
+ {
123
+ free(hDC);
124
+ return NULL;
125
+ }
126
+
127
+ hDC->clip->null = TRUE;
128
+ hDC->format = hdc->format;
129
+ hDC->drawMode = hdc->drawMode;
130
+ hDC->hwnd = NULL;
131
+ return hDC;
132
+ }
133
+
134
+ /**
135
+ * @brief Select a GDI object in the current device context.
136
+ * msdn{dd162957}
137
+ *
138
+ * @param hdc device context
139
+ * @param hgdiobject new selected GDI object
140
+ * @return previous selected GDI object
141
+ */
142
+
143
+ HGDIOBJECT gdi_SelectObject(HGDI_DC hdc, HGDIOBJECT hgdiobject)
144
+ {
145
+ HGDIOBJECT previousSelectedObject = hdc->selectedObject;
146
+
147
+ if (hgdiobject == NULL)
148
+ return NULL;
149
+
150
+ if (hgdiobject->objectType == GDIOBJECT_BITMAP)
151
+ {
152
+ hdc->selectedObject = hgdiobject;
153
+ }
154
+ else if (hgdiobject->objectType == GDIOBJECT_PEN)
155
+ {
156
+ previousSelectedObject = (HGDIOBJECT)hdc->pen;
157
+ hdc->pen = (HGDI_PEN)hgdiobject;
158
+ }
159
+ else if (hgdiobject->objectType == GDIOBJECT_BRUSH)
160
+ {
161
+ previousSelectedObject = (HGDIOBJECT)hdc->brush;
162
+ hdc->brush = (HGDI_BRUSH)hgdiobject;
163
+ }
164
+ else if (hgdiobject->objectType == GDIOBJECT_REGION)
165
+ {
166
+ hdc->selectedObject = hgdiobject;
167
+ previousSelectedObject = (HGDIOBJECT)COMPLEXREGION;
168
+ }
169
+ else if (hgdiobject->objectType == GDIOBJECT_RECT)
170
+ {
171
+ hdc->selectedObject = hgdiobject;
172
+ previousSelectedObject = (HGDIOBJECT)SIMPLEREGION;
173
+ }
174
+ else
175
+ {
176
+ /* Unknown GDI Object Type */
177
+ return NULL;
178
+ }
179
+
180
+ return previousSelectedObject;
181
+ }
182
+
183
+ /**
184
+ * @brief Delete a GDI object.
185
+ * msdn{dd183539}
186
+ * @param hgdiobject GDI object
187
+ * @return nonzero if successful, 0 otherwise
188
+ */
189
+
190
+ BOOL gdi_DeleteObject(HGDIOBJECT hgdiobject)
191
+ {
192
+ if (!hgdiobject)
193
+ return FALSE;
194
+
195
+ if (hgdiobject->objectType == GDIOBJECT_BITMAP)
196
+ {
197
+ HGDI_BITMAP hBitmap = (HGDI_BITMAP)hgdiobject;
198
+
199
+ if (hBitmap->data && hBitmap->free)
200
+ {
201
+ hBitmap->free(hBitmap->data);
202
+ hBitmap->data = NULL;
203
+ }
204
+
205
+ free(hBitmap);
206
+ }
207
+ else if (hgdiobject->objectType == GDIOBJECT_PEN)
208
+ {
209
+ HGDI_PEN hPen = (HGDI_PEN)hgdiobject;
210
+ free(hPen);
211
+ }
212
+ else if (hgdiobject->objectType == GDIOBJECT_BRUSH)
213
+ {
214
+ HGDI_BRUSH hBrush = (HGDI_BRUSH)hgdiobject;
215
+ free(hBrush);
216
+ }
217
+ else if (hgdiobject->objectType == GDIOBJECT_REGION)
218
+ {
219
+ free(hgdiobject);
220
+ }
221
+ else if (hgdiobject->objectType == GDIOBJECT_RECT)
222
+ {
223
+ free(hgdiobject);
224
+ }
225
+ else
226
+ {
227
+ /* Unknown GDI Object Type */
228
+ free(hgdiobject);
229
+ return FALSE;
230
+ }
231
+
232
+ return TRUE;
233
+ }
234
+
235
+ /**
236
+ * @brief Delete device context.
237
+ * msdn{dd183533}
238
+ * @param hdc device context
239
+ * @return nonzero if successful, 0 otherwise
240
+ */
241
+
242
+ BOOL gdi_DeleteDC(HGDI_DC hdc)
243
+ {
244
+ if (hdc)
245
+ {
246
+ if (hdc->hwnd)
247
+ {
248
+ free(hdc->hwnd->cinvalid);
249
+ free(hdc->hwnd->invalid);
250
+ free(hdc->hwnd);
251
+ }
252
+
253
+ free(hdc->clip);
254
+ free(hdc);
255
+ }
256
+
257
+ return TRUE;
258
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/gfx.c ADDED
@@ -0,0 +1,1967 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * GDI Graphics Pipeline
4
+ *
5
+ * Copyright 2014 Marc-Andre Moreau <[email protected]>
6
+ * Copyright 2016 Armin Novak <[email protected]>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include "../core/update.h"
25
+
26
+ #include <winpr/assert.h>
27
+ #include <winpr/cast.h>
28
+
29
+ #include <freerdp/api.h>
30
+ #include <freerdp/log.h>
31
+ #include <freerdp/gdi/gfx.h>
32
+ #include <freerdp/gdi/region.h>
33
+ #include <freerdp/utils/gfx.h>
34
+ #include <math.h>
35
+
36
+ #define TAG FREERDP_TAG("gdi")
37
+
38
+ static BOOL is_rect_valid(const RECTANGLE_16* rect, size_t width, size_t height)
39
+ {
40
+ if (!rect)
41
+ return FALSE;
42
+ if ((rect->left > rect->right) || (rect->right > width))
43
+ return FALSE;
44
+ if ((rect->top > rect->bottom) || (rect->bottom > height))
45
+ return FALSE;
46
+ return TRUE;
47
+ }
48
+
49
+ static BOOL is_within_surface(const gdiGfxSurface* surface, const RDPGFX_SURFACE_COMMAND* cmd)
50
+ {
51
+ RECTANGLE_16 rect;
52
+ if (!surface || !cmd)
53
+ return FALSE;
54
+ rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
55
+ rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
56
+ rect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
57
+ rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
58
+ if (!is_rect_valid(&rect, surface->width, surface->height))
59
+ {
60
+ WLog_ERR(TAG,
61
+ "Command rect %" PRIu32 "x%" PRIu32 "-%" PRIu32 "x%" PRIu32
62
+ " not within bounds of %" PRIu32 "x%" PRIu32,
63
+ rect.left, rect.top, cmd->width, cmd->height, surface->width, surface->height);
64
+ return FALSE;
65
+ }
66
+
67
+ return TRUE;
68
+ }
69
+
70
+ static DWORD gfx_align_scanline(DWORD widthInBytes, DWORD alignment)
71
+ {
72
+ const UINT32 align = alignment;
73
+ const UINT32 pad = align - (widthInBytes % alignment);
74
+ UINT32 scanline = widthInBytes;
75
+
76
+ if (align != pad)
77
+ scanline += pad;
78
+
79
+ return scanline;
80
+ }
81
+
82
+ /**
83
+ * Function description
84
+ *
85
+ * @return 0 on success, otherwise a Win32 error code
86
+ */
87
+ static UINT gdi_ResetGraphics(RdpgfxClientContext* context,
88
+ const RDPGFX_RESET_GRAPHICS_PDU* resetGraphics)
89
+ {
90
+ UINT rc = ERROR_INTERNAL_ERROR;
91
+ UINT16 count = 0;
92
+ UINT32 DesktopWidth = 0;
93
+ UINT32 DesktopHeight = 0;
94
+ UINT16* pSurfaceIds = NULL;
95
+ rdpGdi* gdi = NULL;
96
+ rdpUpdate* update = NULL;
97
+ rdpSettings* settings = NULL;
98
+
99
+ WINPR_ASSERT(context);
100
+ WINPR_ASSERT(resetGraphics);
101
+
102
+ gdi = (rdpGdi*)context->custom;
103
+ WINPR_ASSERT(gdi);
104
+
105
+ update = gdi->context->update;
106
+ WINPR_ASSERT(update);
107
+
108
+ settings = gdi->context->settings;
109
+ WINPR_ASSERT(settings);
110
+ EnterCriticalSection(&context->mux);
111
+ DesktopWidth = resetGraphics->width;
112
+ DesktopHeight = resetGraphics->height;
113
+
114
+ if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, DesktopWidth))
115
+ goto fail;
116
+ if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, DesktopHeight))
117
+ goto fail;
118
+
119
+ if (update)
120
+ {
121
+ WINPR_ASSERT(update->DesktopResize);
122
+ update->DesktopResize(gdi->context);
123
+ }
124
+
125
+ WINPR_ASSERT(context->GetSurfaceIds);
126
+ context->GetSurfaceIds(context, &pSurfaceIds, &count);
127
+
128
+ for (UINT32 index = 0; index < count; index++)
129
+ {
130
+ WINPR_ASSERT(context->GetSurfaceData);
131
+ gdiGfxSurface* surface =
132
+ (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
133
+
134
+ if (!surface)
135
+ continue;
136
+
137
+ memset(surface->data, 0xFF, (size_t)surface->scanline * surface->height);
138
+ region16_clear(&surface->invalidRegion);
139
+ }
140
+
141
+ free(pSurfaceIds);
142
+
143
+ if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))
144
+ {
145
+ const UINT32 width = (UINT32)MAX(0, gdi->width);
146
+ const UINT32 height = (UINT32)MAX(0, gdi->height);
147
+
148
+ if (!freerdp_client_codecs_reset(
149
+ context->codecs, freerdp_settings_get_codecs_flags(settings), width, height))
150
+ {
151
+ goto fail;
152
+ }
153
+ if (!freerdp_client_codecs_reset(
154
+ gdi->context->codecs, freerdp_settings_get_codecs_flags(settings), width, height))
155
+ {
156
+ goto fail;
157
+ }
158
+ }
159
+
160
+ rc = CHANNEL_RC_OK;
161
+ fail:
162
+ LeaveCriticalSection(&context->mux);
163
+ return rc;
164
+ }
165
+
166
+ static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)
167
+ {
168
+ UINT rc = ERROR_INTERNAL_ERROR;
169
+ UINT32 surfaceX = 0;
170
+ UINT32 surfaceY = 0;
171
+ RECTANGLE_16 surfaceRect;
172
+ const RECTANGLE_16* rects = NULL;
173
+ UINT32 nbRects = 0;
174
+ rdpUpdate* update = NULL;
175
+
176
+ WINPR_ASSERT(gdi);
177
+ WINPR_ASSERT(gdi->context);
178
+ WINPR_ASSERT(surface);
179
+
180
+ update = gdi->context->update;
181
+ WINPR_ASSERT(update);
182
+
183
+ if (gdi->suppressOutput)
184
+ return CHANNEL_RC_OK;
185
+
186
+ surfaceX = surface->outputOriginX;
187
+ surfaceY = surface->outputOriginY;
188
+ surfaceRect.left = 0;
189
+ surfaceRect.top = 0;
190
+ surfaceRect.right = (UINT16)MIN(UINT16_MAX, surface->mappedWidth);
191
+ surfaceRect.bottom = (UINT16)MIN(UINT16_MAX, surface->mappedHeight);
192
+ region16_intersect_rect(&(surface->invalidRegion), &(surface->invalidRegion), &surfaceRect);
193
+ const double sx = surface->outputTargetWidth / (double)surface->mappedWidth;
194
+ const double sy = surface->outputTargetHeight / (double)surface->mappedHeight;
195
+
196
+ if (!(rects = region16_rects(&surface->invalidRegion, &nbRects)) || !nbRects)
197
+ return CHANNEL_RC_OK;
198
+
199
+ if (!update_begin_paint(update))
200
+ goto fail;
201
+
202
+ for (UINT32 i = 0; i < nbRects; i++)
203
+ {
204
+ const UINT32 nXSrc = rects[i].left;
205
+ const UINT32 nYSrc = rects[i].top;
206
+ const UINT32 nXDst = (UINT32)MIN(surfaceX + nXSrc * sx, gdi->width - 1);
207
+ const UINT32 nYDst = (UINT32)MIN(surfaceY + nYSrc * sy, gdi->height - 1);
208
+ const UINT32 swidth = rects[i].right - rects[i].left;
209
+ const UINT32 sheight = rects[i].bottom - rects[i].top;
210
+ const UINT32 dwidth = MIN((UINT32)(swidth * sx), (UINT32)gdi->width - nXDst);
211
+ const UINT32 dheight = MIN((UINT32)(sheight * sy), (UINT32)gdi->height - nYDst);
212
+
213
+ if (!freerdp_image_scale(gdi->primary_buffer, gdi->dstFormat, gdi->stride, nXDst, nYDst,
214
+ dwidth, dheight, surface->data, surface->format, surface->scanline,
215
+ nXSrc, nYSrc, swidth, sheight))
216
+ {
217
+ rc = CHANNEL_RC_NULL_DATA;
218
+ goto fail;
219
+ }
220
+
221
+ gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,
222
+ (INT32)dheight);
223
+ }
224
+
225
+ rc = CHANNEL_RC_OK;
226
+ fail:
227
+
228
+ if (!update_end_paint(update))
229
+ rc = ERROR_INTERNAL_ERROR;
230
+
231
+ region16_clear(&(surface->invalidRegion));
232
+ return rc;
233
+ }
234
+
235
+ static UINT gdi_WindowUpdate(RdpgfxClientContext* context, gdiGfxSurface* surface)
236
+ {
237
+ WINPR_ASSERT(context);
238
+ WINPR_ASSERT(surface);
239
+ return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, surface);
240
+ }
241
+
242
+ static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)
243
+ {
244
+ UINT16 count = 0;
245
+ UINT status = ERROR_INTERNAL_ERROR;
246
+ UINT16* pSurfaceIds = NULL;
247
+ rdpGdi* gdi = NULL;
248
+
249
+ WINPR_ASSERT(context);
250
+
251
+ gdi = (rdpGdi*)context->custom;
252
+ WINPR_ASSERT(gdi);
253
+
254
+ EnterCriticalSection(&context->mux);
255
+
256
+ WINPR_ASSERT(context->GetSurfaceIds);
257
+ context->GetSurfaceIds(context, &pSurfaceIds, &count);
258
+ status = CHANNEL_RC_OK;
259
+
260
+ for (UINT32 index = 0; index < count; index++)
261
+ {
262
+ WINPR_ASSERT(context->GetSurfaceData);
263
+ gdiGfxSurface* surface =
264
+ (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);
265
+
266
+ if (!surface)
267
+ continue;
268
+
269
+ /* Already handled in UpdateSurfaceArea callbacks */
270
+ if (context->UpdateSurfaceArea)
271
+ {
272
+ if (surface->handleInUpdateSurfaceArea)
273
+ continue;
274
+ }
275
+
276
+ if (surface->outputMapped)
277
+ status = gdi_OutputUpdate(gdi, surface);
278
+ else if (surface->windowMapped)
279
+ status = gdi_WindowUpdate(context, surface);
280
+
281
+ if (status != CHANNEL_RC_OK)
282
+ break;
283
+ }
284
+
285
+ free(pSurfaceIds);
286
+ LeaveCriticalSection(&context->mux);
287
+ return status;
288
+ }
289
+
290
+ /**
291
+ * Function description
292
+ *
293
+ * @return 0 on success, otherwise a Win32 error code
294
+ */
295
+ static UINT gdi_StartFrame(RdpgfxClientContext* context, const RDPGFX_START_FRAME_PDU* startFrame)
296
+ {
297
+ rdpGdi* gdi = NULL;
298
+
299
+ WINPR_ASSERT(context);
300
+ WINPR_ASSERT(startFrame);
301
+
302
+ gdi = (rdpGdi*)context->custom;
303
+ WINPR_ASSERT(gdi);
304
+ gdi->inGfxFrame = TRUE;
305
+ gdi->frameId = startFrame->frameId;
306
+ return CHANNEL_RC_OK;
307
+ }
308
+
309
+ static UINT gdi_call_update_surfaces(RdpgfxClientContext* context)
310
+ {
311
+ WINPR_ASSERT(context);
312
+ return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaces, context);
313
+ }
314
+
315
+ /**
316
+ * Function description
317
+ *
318
+ * @return 0 on success, otherwise a Win32 error code
319
+ */
320
+ static UINT gdi_EndFrame(RdpgfxClientContext* context, const RDPGFX_END_FRAME_PDU* endFrame)
321
+ {
322
+ WINPR_ASSERT(context);
323
+ WINPR_ASSERT(endFrame);
324
+
325
+ rdpGdi* gdi = (rdpGdi*)context->custom;
326
+ WINPR_ASSERT(gdi);
327
+ const UINT status = gdi_call_update_surfaces(context);
328
+ gdi->inGfxFrame = FALSE;
329
+ return status;
330
+ }
331
+
332
+ static UINT gdi_interFrameUpdate(rdpGdi* gdi, RdpgfxClientContext* context)
333
+ {
334
+ WINPR_ASSERT(gdi);
335
+ UINT status = CHANNEL_RC_OK;
336
+ if (!gdi->inGfxFrame)
337
+ status = gdi_call_update_surfaces(context);
338
+ return status;
339
+ }
340
+
341
+ /**
342
+ * Function description
343
+ *
344
+ * @return 0 on success, otherwise a Win32 error code
345
+ */
346
+ static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,
347
+ const RDPGFX_SURFACE_COMMAND* cmd)
348
+ {
349
+ UINT status = CHANNEL_RC_OK;
350
+ gdiGfxSurface* surface = NULL;
351
+ RECTANGLE_16 invalidRect;
352
+ DWORD bpp = 0;
353
+ size_t size = 0;
354
+ WINPR_ASSERT(gdi);
355
+ WINPR_ASSERT(context);
356
+ WINPR_ASSERT(cmd);
357
+
358
+ WINPR_ASSERT(context->GetSurfaceData);
359
+ surface =
360
+ (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
361
+
362
+ if (!surface)
363
+ {
364
+ WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
365
+ return ERROR_NOT_FOUND;
366
+ }
367
+
368
+ if (!is_within_surface(surface, cmd))
369
+ return ERROR_INVALID_DATA;
370
+
371
+ bpp = FreeRDPGetBytesPerPixel(cmd->format);
372
+ size = 1ull * bpp * cmd->width * cmd->height;
373
+ if (cmd->length < size)
374
+ {
375
+ WLog_ERR(TAG, "Not enough data, got %" PRIu32 ", expected %" PRIuz, cmd->length, size);
376
+ return ERROR_INVALID_DATA;
377
+ }
378
+
379
+ if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline, cmd->left,
380
+ cmd->top, cmd->width, cmd->height, cmd->data, cmd->format, 0,
381
+ 0, 0, NULL, FREERDP_FLIP_NONE))
382
+ return ERROR_INTERNAL_ERROR;
383
+
384
+ invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
385
+ invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
386
+ invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
387
+ invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
388
+ region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
389
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
390
+ &invalidRect);
391
+
392
+ if (status != CHANNEL_RC_OK)
393
+ goto fail;
394
+
395
+ status = gdi_interFrameUpdate(gdi, context);
396
+
397
+ fail:
398
+ return status;
399
+ }
400
+
401
+ /**
402
+ * Function description
403
+ *
404
+ * @return 0 on success, otherwise a Win32 error code
405
+ */
406
+ static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,
407
+ const RDPGFX_SURFACE_COMMAND* cmd)
408
+ {
409
+ UINT status = ERROR_INTERNAL_ERROR;
410
+ gdiGfxSurface* surface = NULL;
411
+ REGION16 invalidRegion;
412
+ const RECTANGLE_16* rects = NULL;
413
+ UINT32 nrRects = 0;
414
+ WINPR_ASSERT(gdi);
415
+ WINPR_ASSERT(context);
416
+ WINPR_ASSERT(cmd);
417
+
418
+ WINPR_ASSERT(context->GetSurfaceData);
419
+ surface =
420
+ (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
421
+
422
+ if (!surface)
423
+ {
424
+ WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
425
+ return ERROR_NOT_FOUND;
426
+ }
427
+
428
+ WINPR_ASSERT(surface->codecs);
429
+ rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);
430
+ region16_init(&invalidRegion);
431
+
432
+ if (!rfx_process_message(surface->codecs->rfx, cmd->data, cmd->length, cmd->left, cmd->top,
433
+ surface->data, surface->format, surface->scanline, surface->height,
434
+ &invalidRegion))
435
+ {
436
+ WLog_ERR(TAG, "Failed to process RemoteFX message");
437
+ goto fail;
438
+ }
439
+
440
+ rects = region16_rects(&invalidRegion, &nrRects);
441
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
442
+ nrRects, rects);
443
+
444
+ if (status != CHANNEL_RC_OK)
445
+ goto fail;
446
+
447
+ for (UINT32 x = 0; x < nrRects; x++)
448
+ region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]);
449
+
450
+ status = gdi_interFrameUpdate(gdi, context);
451
+
452
+ fail:
453
+ region16_uninit(&invalidRegion);
454
+ return status;
455
+ }
456
+
457
+ /**
458
+ * Function description
459
+ *
460
+ * @return 0 on success, otherwise a Win32 error code
461
+ */
462
+ static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,
463
+ const RDPGFX_SURFACE_COMMAND* cmd)
464
+ {
465
+ INT32 rc = 0;
466
+ UINT status = CHANNEL_RC_OK;
467
+ gdiGfxSurface* surface = NULL;
468
+ RECTANGLE_16 invalidRect;
469
+ WINPR_ASSERT(gdi);
470
+ WINPR_ASSERT(context);
471
+ WINPR_ASSERT(cmd);
472
+
473
+ WINPR_ASSERT(context->GetSurfaceData);
474
+ surface =
475
+ (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
476
+
477
+ if (!surface)
478
+ {
479
+ WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
480
+ return ERROR_NOT_FOUND;
481
+ }
482
+
483
+ WINPR_ASSERT(surface->codecs);
484
+ rc = clear_decompress(surface->codecs->clear, cmd->data, cmd->length, cmd->width, cmd->height,
485
+ surface->data, surface->format, surface->scanline, cmd->left, cmd->top,
486
+ surface->width, surface->height, &gdi->palette);
487
+
488
+ if (rc < 0)
489
+ {
490
+ WLog_ERR(TAG, "clear_decompress failure: %" PRId32 "", rc);
491
+ return ERROR_INTERNAL_ERROR;
492
+ }
493
+
494
+ invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
495
+ invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
496
+ invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
497
+ invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
498
+ region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
499
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
500
+ &invalidRect);
501
+
502
+ if (status != CHANNEL_RC_OK)
503
+ goto fail;
504
+
505
+ status = gdi_interFrameUpdate(gdi, context);
506
+
507
+ fail:
508
+ return status;
509
+ }
510
+
511
+ /**
512
+ * Function description
513
+ *
514
+ * @return 0 on success, otherwise a Win32 error code
515
+ */
516
+ static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,
517
+ const RDPGFX_SURFACE_COMMAND* cmd)
518
+ {
519
+ UINT status = CHANNEL_RC_OK;
520
+ BYTE* DstData = NULL;
521
+ gdiGfxSurface* surface = NULL;
522
+ RECTANGLE_16 invalidRect;
523
+ WINPR_ASSERT(gdi);
524
+ WINPR_ASSERT(context);
525
+ WINPR_ASSERT(cmd);
526
+
527
+ WINPR_ASSERT(context->GetSurfaceData);
528
+ surface =
529
+ (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
530
+
531
+ if (!surface)
532
+ {
533
+ WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
534
+ return ERROR_NOT_FOUND;
535
+ }
536
+
537
+ DstData = surface->data;
538
+
539
+ if (!is_within_surface(surface, cmd))
540
+ return ERROR_INVALID_DATA;
541
+
542
+ if (!planar_decompress(surface->codecs->planar, cmd->data, cmd->length, cmd->width, cmd->height,
543
+ DstData, surface->format, surface->scanline, cmd->left, cmd->top,
544
+ cmd->width, cmd->height, FALSE))
545
+ return ERROR_INTERNAL_ERROR;
546
+
547
+ invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
548
+ invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
549
+ invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
550
+ invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
551
+ region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
552
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
553
+ &invalidRect);
554
+
555
+ if (status != CHANNEL_RC_OK)
556
+ goto fail;
557
+
558
+ status = gdi_interFrameUpdate(gdi, context);
559
+
560
+ fail:
561
+ return status;
562
+ }
563
+
564
+ /**
565
+ * Function description
566
+ *
567
+ * @return 0 on success, otherwise a Win32 error code
568
+ */
569
+ static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,
570
+ const RDPGFX_SURFACE_COMMAND* cmd)
571
+ {
572
+ #ifdef WITH_GFX_H264
573
+ INT32 rc = 0;
574
+ UINT status = CHANNEL_RC_OK;
575
+ gdiGfxSurface* surface = NULL;
576
+ RDPGFX_H264_METABLOCK* meta = NULL;
577
+ RDPGFX_AVC420_BITMAP_STREAM* bs = NULL;
578
+ WINPR_ASSERT(gdi);
579
+ WINPR_ASSERT(context);
580
+ WINPR_ASSERT(cmd);
581
+
582
+ WINPR_ASSERT(context->GetSurfaceData);
583
+ surface =
584
+ (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
585
+
586
+ if (!surface)
587
+ {
588
+ WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
589
+ return ERROR_NOT_FOUND;
590
+ }
591
+
592
+ if (!surface->h264)
593
+ {
594
+ surface->h264 = h264_context_new(FALSE);
595
+
596
+ if (!surface->h264)
597
+ {
598
+ WLog_ERR(TAG, "unable to create h264 context");
599
+ return ERROR_NOT_ENOUGH_MEMORY;
600
+ }
601
+
602
+ if (!h264_context_reset(surface->h264, surface->width, surface->height))
603
+ return ERROR_INTERNAL_ERROR;
604
+ }
605
+
606
+ if (!surface->h264)
607
+ return ERROR_NOT_SUPPORTED;
608
+
609
+ bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;
610
+
611
+ if (!bs)
612
+ return ERROR_INTERNAL_ERROR;
613
+
614
+ meta = &(bs->meta);
615
+ rc = avc420_decompress(surface->h264, bs->data, bs->length, surface->data, surface->format,
616
+ surface->scanline, surface->width, surface->height, meta->regionRects,
617
+ meta->numRegionRects);
618
+
619
+ if (rc < 0)
620
+ {
621
+ WLog_WARN(TAG, "avc420_decompress failure: %" PRId32 ", ignoring update.", rc);
622
+ return CHANNEL_RC_OK;
623
+ }
624
+
625
+ for (UINT32 i = 0; i < meta->numRegionRects; i++)
626
+ {
627
+ region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
628
+ &(meta->regionRects[i]));
629
+ }
630
+
631
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
632
+ meta->numRegionRects, meta->regionRects);
633
+
634
+ if (status != CHANNEL_RC_OK)
635
+ goto fail;
636
+
637
+ status = gdi_interFrameUpdate(gdi, context);
638
+
639
+ fail:
640
+ return status;
641
+ #else
642
+ return ERROR_NOT_SUPPORTED;
643
+ #endif
644
+ }
645
+
646
+ /**
647
+ * Function description
648
+ *
649
+ * @return 0 on success, otherwise a Win32 error code
650
+ */
651
+ static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,
652
+ const RDPGFX_SURFACE_COMMAND* cmd)
653
+ {
654
+ #ifdef WITH_GFX_H264
655
+ INT32 rc = 0;
656
+ UINT status = CHANNEL_RC_OK;
657
+ gdiGfxSurface* surface = NULL;
658
+ RDPGFX_AVC444_BITMAP_STREAM* bs = NULL;
659
+ RDPGFX_AVC420_BITMAP_STREAM* avc1 = NULL;
660
+ RDPGFX_H264_METABLOCK* meta1 = NULL;
661
+ RDPGFX_AVC420_BITMAP_STREAM* avc2 = NULL;
662
+ RDPGFX_H264_METABLOCK* meta2 = NULL;
663
+ WINPR_ASSERT(gdi);
664
+ WINPR_ASSERT(context);
665
+ WINPR_ASSERT(cmd);
666
+
667
+ WINPR_ASSERT(context->GetSurfaceData);
668
+ surface =
669
+ (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
670
+
671
+ if (!surface)
672
+ {
673
+ WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
674
+ return ERROR_NOT_FOUND;
675
+ }
676
+
677
+ if (!surface->h264)
678
+ {
679
+ surface->h264 = h264_context_new(FALSE);
680
+
681
+ if (!surface->h264)
682
+ {
683
+ WLog_ERR(TAG, "unable to create h264 context");
684
+ return ERROR_NOT_ENOUGH_MEMORY;
685
+ }
686
+
687
+ if (!h264_context_reset(surface->h264, surface->width, surface->height))
688
+ return ERROR_INTERNAL_ERROR;
689
+ }
690
+
691
+ if (!surface->h264)
692
+ return ERROR_NOT_SUPPORTED;
693
+
694
+ bs = (RDPGFX_AVC444_BITMAP_STREAM*)cmd->extra;
695
+
696
+ if (!bs)
697
+ return ERROR_INTERNAL_ERROR;
698
+
699
+ avc1 = &bs->bitstream[0];
700
+ avc2 = &bs->bitstream[1];
701
+ meta1 = &avc1->meta;
702
+ meta2 = &avc2->meta;
703
+ rc = avc444_decompress(surface->h264, bs->LC, meta1->regionRects, meta1->numRegionRects,
704
+ avc1->data, avc1->length, meta2->regionRects, meta2->numRegionRects,
705
+ avc2->data, avc2->length, surface->data, surface->format,
706
+ surface->scanline, surface->width, surface->height, cmd->codecId);
707
+
708
+ if (rc < 0)
709
+ {
710
+ WLog_WARN(TAG, "avc444_decompress failure: %" PRIu32 ", ignoring update.", status);
711
+ return CHANNEL_RC_OK;
712
+ }
713
+
714
+ for (UINT32 i = 0; i < meta1->numRegionRects; i++)
715
+ {
716
+ region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
717
+ &(meta1->regionRects[i]));
718
+ }
719
+
720
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
721
+ meta1->numRegionRects, meta1->regionRects);
722
+
723
+ if (status != CHANNEL_RC_OK)
724
+ goto fail;
725
+
726
+ for (UINT32 i = 0; i < meta2->numRegionRects; i++)
727
+ {
728
+ region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),
729
+ &(meta2->regionRects[i]));
730
+ }
731
+
732
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
733
+ meta2->numRegionRects, meta2->regionRects);
734
+
735
+ if (status != CHANNEL_RC_OK)
736
+ goto fail;
737
+
738
+ status = gdi_interFrameUpdate(gdi, context);
739
+
740
+ fail:
741
+ return status;
742
+ #else
743
+ return ERROR_NOT_SUPPORTED;
744
+ #endif
745
+ }
746
+
747
+ static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride, RECTANGLE_16* rect,
748
+ UINT32 startOffsetX, UINT32 count, BYTE a)
749
+ {
750
+ UINT32 written = 0;
751
+ BOOL first = TRUE;
752
+ const UINT32 bpp = FreeRDPGetBytesPerPixel(format);
753
+ WINPR_ASSERT(rect);
754
+
755
+ for (size_t y = rect->top; y < rect->bottom; y++)
756
+ {
757
+ BYTE* line = &data[y * stride];
758
+
759
+ for (size_t x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)
760
+ {
761
+ BYTE r = 0;
762
+ BYTE g = 0;
763
+ BYTE b = 0;
764
+
765
+ if (written == count)
766
+ return TRUE;
767
+
768
+ BYTE* src = &line[x * bpp];
769
+ UINT32 color = FreeRDPReadColor(src, format);
770
+ FreeRDPSplitColor(color, format, &r, &g, &b, NULL, NULL);
771
+ color = FreeRDPGetColor(format, r, g, b, a);
772
+ FreeRDPWriteColor(src, format, color);
773
+ written++;
774
+ }
775
+
776
+ first = FALSE;
777
+ }
778
+
779
+ return TRUE;
780
+ }
781
+ /**
782
+ * Function description
783
+ *
784
+ * @return 0 on success, otherwise a Win32 error code
785
+ */
786
+ static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,
787
+ const RDPGFX_SURFACE_COMMAND* cmd)
788
+ {
789
+ UINT status = CHANNEL_RC_OK;
790
+ UINT16 alphaSig = 0;
791
+ UINT16 compressed = 0;
792
+ gdiGfxSurface* surface = NULL;
793
+ RECTANGLE_16 invalidRect;
794
+ wStream buffer;
795
+ wStream* s = NULL;
796
+ WINPR_ASSERT(gdi);
797
+ WINPR_ASSERT(context);
798
+ WINPR_ASSERT(cmd);
799
+
800
+ s = Stream_StaticConstInit(&buffer, cmd->data, cmd->length);
801
+
802
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
803
+ return ERROR_INVALID_DATA;
804
+
805
+ WINPR_ASSERT(context->GetSurfaceData);
806
+ surface =
807
+ (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));
808
+
809
+ if (!surface)
810
+ {
811
+ WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
812
+ return ERROR_NOT_FOUND;
813
+ }
814
+
815
+ if (!is_within_surface(surface, cmd))
816
+ return ERROR_INVALID_DATA;
817
+
818
+ Stream_Read_UINT16(s, alphaSig);
819
+ Stream_Read_UINT16(s, compressed);
820
+
821
+ if (alphaSig != 0x414C)
822
+ return ERROR_INVALID_DATA;
823
+
824
+ if (compressed == 0)
825
+ {
826
+ if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, cmd->height, cmd->width))
827
+ return ERROR_INVALID_DATA;
828
+
829
+ for (size_t y = cmd->top; y < cmd->top + cmd->height; y++)
830
+ {
831
+ BYTE* line = &surface->data[y * surface->scanline];
832
+
833
+ for (size_t x = cmd->left; x < cmd->left + cmd->width; x++)
834
+ {
835
+ UINT32 color = 0;
836
+ BYTE r = 0;
837
+ BYTE g = 0;
838
+ BYTE b = 0;
839
+ BYTE a = 0;
840
+ BYTE* src = &line[x * FreeRDPGetBytesPerPixel(surface->format)];
841
+ Stream_Read_UINT8(s, a);
842
+ color = FreeRDPReadColor(src, surface->format);
843
+ FreeRDPSplitColor(color, surface->format, &r, &g, &b, NULL, NULL);
844
+ color = FreeRDPGetColor(surface->format, r, g, b, a);
845
+ FreeRDPWriteColor(src, surface->format, color);
846
+ }
847
+ }
848
+ }
849
+ else
850
+ {
851
+ UINT32 startOffsetX = 0;
852
+ RECTANGLE_16 rect = { 0 };
853
+ rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
854
+ rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
855
+ rect.right = (UINT16)MIN(UINT16_MAX, cmd->left + cmd->width);
856
+ rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->top + cmd->height);
857
+
858
+ while (rect.top < rect.bottom)
859
+ {
860
+ UINT32 count = 0;
861
+ BYTE a = 0;
862
+
863
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
864
+ return ERROR_INVALID_DATA;
865
+
866
+ Stream_Read_UINT8(s, a);
867
+ Stream_Read_UINT8(s, count);
868
+
869
+ if (count >= 0xFF)
870
+ {
871
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
872
+ return ERROR_INVALID_DATA;
873
+
874
+ Stream_Read_UINT16(s, count);
875
+
876
+ if (count >= 0xFFFF)
877
+ {
878
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
879
+ return ERROR_INVALID_DATA;
880
+
881
+ Stream_Read_UINT32(s, count);
882
+ }
883
+ }
884
+
885
+ if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,
886
+ startOffsetX, count, a))
887
+ return ERROR_INTERNAL_ERROR;
888
+
889
+ startOffsetX += count;
890
+
891
+ while (startOffsetX >= cmd->width)
892
+ {
893
+ startOffsetX -= cmd->width;
894
+ rect.top++;
895
+ }
896
+ }
897
+ }
898
+
899
+ invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);
900
+ invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);
901
+ invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);
902
+ invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);
903
+ region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
904
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,
905
+ &invalidRect);
906
+
907
+ if (status != CHANNEL_RC_OK)
908
+ goto fail;
909
+
910
+ status = gdi_interFrameUpdate(gdi, context);
911
+
912
+ fail:
913
+ return status;
914
+ }
915
+
916
+ #if defined(WITH_GFX_FRAME_DUMP)
917
+ static void dump_cmd(const RDPGFX_SURFACE_COMMAND* cmd, UINT32 frameId)
918
+ {
919
+ static UINT64 xxx = 0;
920
+ const char* path = "/tmp/dump/";
921
+ WINPR_ASSERT(cmd);
922
+ char fname[1024] = { 0 };
923
+
924
+ snprintf(fname, sizeof(fname), "%s/%08" PRIx64 ".raw", path, xxx++);
925
+ FILE* fp = fopen(fname, "w");
926
+ if (!fp)
927
+ return;
928
+ (void)fprintf(fp, "frameid: %" PRIu32 "\n", frameId);
929
+ (void)fprintf(fp, "surfaceId: %" PRIu32 "\n", cmd->surfaceId);
930
+ (void)fprintf(fp, "codecId: %" PRIu32 "\n", cmd->codecId);
931
+ (void)fprintf(fp, "contextId: %" PRIu32 "\n", cmd->contextId);
932
+ (void)fprintf(fp, "format: %" PRIu32 "\n", cmd->format);
933
+ (void)fprintf(fp, "left: %" PRIu32 "\n", cmd->left);
934
+ (void)fprintf(fp, "top: %" PRIu32 "\n", cmd->top);
935
+ (void)fprintf(fp, "right: %" PRIu32 "\n", cmd->right);
936
+ (void)fprintf(fp, "bottom: %" PRIu32 "\n", cmd->bottom);
937
+ (void)fprintf(fp, "width: %" PRIu32 "\n", cmd->width);
938
+ (void)fprintf(fp, "height: %" PRIu32 "\n", cmd->height);
939
+ (void)fprintf(fp, "length: %" PRIu32 "\n", cmd->length);
940
+
941
+ char* bdata = crypto_base64_encode_ex(cmd->data, cmd->length, FALSE);
942
+ (void)fprintf(fp, "data: %s\n", bdata);
943
+ free(bdata);
944
+ fclose(fp);
945
+ }
946
+ #endif
947
+
948
+ /**
949
+ * Function description
950
+ *
951
+ * @return 0 on success, otherwise a Win32 error code
952
+ */
953
+ static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,
954
+ const RDPGFX_SURFACE_COMMAND* cmd)
955
+ {
956
+ INT32 rc = 0;
957
+ UINT status = CHANNEL_RC_OK;
958
+ gdiGfxSurface* surface = NULL;
959
+ REGION16 invalidRegion;
960
+ const RECTANGLE_16* rects = NULL;
961
+ UINT32 nrRects = 0;
962
+ /**
963
+ * Note: Since this comes via a Wire-To-Surface-2 PDU the
964
+ * cmd's top/left/right/bottom/width/height members are always zero!
965
+ * The update region is determined during decompression.
966
+ */
967
+ WINPR_ASSERT(gdi);
968
+ WINPR_ASSERT(context);
969
+ WINPR_ASSERT(cmd);
970
+ const UINT16 surfaceId = (UINT16)MIN(UINT16_MAX, cmd->surfaceId);
971
+
972
+ WINPR_ASSERT(context->GetSurfaceData);
973
+ surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceId);
974
+
975
+ if (!surface)
976
+ {
977
+ WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);
978
+ return ERROR_NOT_FOUND;
979
+ }
980
+
981
+ if (!is_within_surface(surface, cmd))
982
+ return ERROR_INVALID_DATA;
983
+
984
+ WINPR_ASSERT(surface->codecs);
985
+ rc = progressive_create_surface_context(surface->codecs->progressive, surfaceId, surface->width,
986
+ surface->height);
987
+
988
+ if (rc < 0)
989
+ {
990
+ WLog_ERR(TAG, "progressive_create_surface_context failure: %" PRId32 "", rc);
991
+ return ERROR_INTERNAL_ERROR;
992
+ }
993
+
994
+ region16_init(&invalidRegion);
995
+
996
+ rc = progressive_decompress(surface->codecs->progressive, cmd->data, cmd->length, surface->data,
997
+ surface->format, surface->scanline, cmd->left, cmd->top,
998
+ &invalidRegion, surfaceId, gdi->frameId);
999
+
1000
+ if (rc < 0)
1001
+ {
1002
+ WLog_ERR(TAG, "progressive_decompress failure: %" PRId32 "", rc);
1003
+ region16_uninit(&invalidRegion);
1004
+ return ERROR_INTERNAL_ERROR;
1005
+ }
1006
+
1007
+ rects = region16_rects(&invalidRegion, &nrRects);
1008
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1009
+ nrRects, rects);
1010
+
1011
+ if (status != CHANNEL_RC_OK)
1012
+ goto fail;
1013
+
1014
+ for (UINT32 x = 0; x < nrRects; x++)
1015
+ region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]);
1016
+
1017
+ region16_uninit(&invalidRegion);
1018
+
1019
+ status = gdi_interFrameUpdate(gdi, context);
1020
+
1021
+ fail:
1022
+ return status;
1023
+ }
1024
+
1025
+ /**
1026
+ * Function description
1027
+ *
1028
+ * @return 0 on success, otherwise a Win32 error code
1029
+ */
1030
+ static UINT gdi_SurfaceCommand(RdpgfxClientContext* context, const RDPGFX_SURFACE_COMMAND* cmd)
1031
+ {
1032
+ UINT status = CHANNEL_RC_OK;
1033
+ rdpGdi* gdi = NULL;
1034
+
1035
+ if (!context || !cmd)
1036
+ return ERROR_INVALID_PARAMETER;
1037
+
1038
+ gdi = (rdpGdi*)context->custom;
1039
+
1040
+ EnterCriticalSection(&context->mux);
1041
+ const UINT16 codecId = WINPR_ASSERTING_INT_CAST(UINT16, cmd->codecId);
1042
+ WLog_Print(gdi->log, WLOG_TRACE,
1043
+ "surfaceId=%" PRIu32 ", codec=%s [%" PRIu32 "], contextId=%" PRIu32 ", format=%s, "
1044
+ "left=%" PRIu32 ", top=%" PRIu32 ", right=%" PRIu32 ", bottom=%" PRIu32
1045
+ ", width=%" PRIu32 ", height=%" PRIu32 " "
1046
+ "length=%" PRIu32 ", data=%p, extra=%p",
1047
+ cmd->surfaceId, rdpgfx_get_codec_id_string(codecId), cmd->codecId, cmd->contextId,
1048
+ FreeRDPGetColorFormatName(cmd->format), cmd->left, cmd->top, cmd->right, cmd->bottom,
1049
+ cmd->width, cmd->height, cmd->length, (void*)cmd->data, (void*)cmd->extra);
1050
+ #if defined(WITH_GFX_FRAME_DUMP)
1051
+ dump_cmd(cmd, gdi->frameId);
1052
+ #endif
1053
+
1054
+ switch (codecId)
1055
+ {
1056
+ case RDPGFX_CODECID_UNCOMPRESSED:
1057
+ status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);
1058
+ break;
1059
+
1060
+ case RDPGFX_CODECID_CAVIDEO:
1061
+ status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);
1062
+ break;
1063
+
1064
+ case RDPGFX_CODECID_CLEARCODEC:
1065
+ status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);
1066
+ break;
1067
+
1068
+ case RDPGFX_CODECID_PLANAR:
1069
+ status = gdi_SurfaceCommand_Planar(gdi, context, cmd);
1070
+ break;
1071
+
1072
+ case RDPGFX_CODECID_AVC420:
1073
+ status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);
1074
+ break;
1075
+
1076
+ case RDPGFX_CODECID_AVC444v2:
1077
+ case RDPGFX_CODECID_AVC444:
1078
+ status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);
1079
+ break;
1080
+
1081
+ case RDPGFX_CODECID_ALPHA:
1082
+ status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);
1083
+ break;
1084
+
1085
+ case RDPGFX_CODECID_CAPROGRESSIVE:
1086
+ status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);
1087
+ break;
1088
+
1089
+ case RDPGFX_CODECID_CAPROGRESSIVE_V2:
1090
+ WLog_WARN(TAG, "SurfaceCommand %s [0x%08" PRIX16 "] not implemented",
1091
+ rdpgfx_get_codec_id_string(codecId), codecId);
1092
+ break;
1093
+
1094
+ default:
1095
+ WLog_WARN(TAG, "Invalid SurfaceCommand %s [0x%08" PRIX16 "]",
1096
+ rdpgfx_get_codec_id_string(codecId), codecId);
1097
+ break;
1098
+ }
1099
+
1100
+ LeaveCriticalSection(&context->mux);
1101
+ return status;
1102
+ }
1103
+
1104
+ /**
1105
+ * Function description
1106
+ *
1107
+ * @return 0 on success, otherwise a Win32 error code
1108
+ */
1109
+ static UINT
1110
+ gdi_DeleteEncodingContext(RdpgfxClientContext* context,
1111
+ const RDPGFX_DELETE_ENCODING_CONTEXT_PDU* deleteEncodingContext)
1112
+ {
1113
+ WINPR_ASSERT(context);
1114
+ WINPR_ASSERT(deleteEncodingContext);
1115
+ WINPR_UNUSED(context);
1116
+ WINPR_UNUSED(deleteEncodingContext);
1117
+ return CHANNEL_RC_OK;
1118
+ }
1119
+
1120
+ /**
1121
+ * Function description
1122
+ *
1123
+ * @return 0 on success, otherwise a Win32 error code
1124
+ */
1125
+ static UINT gdi_CreateSurface(RdpgfxClientContext* context,
1126
+ const RDPGFX_CREATE_SURFACE_PDU* createSurface)
1127
+ {
1128
+ UINT rc = ERROR_INTERNAL_ERROR;
1129
+ gdiGfxSurface* surface = NULL;
1130
+ rdpGdi* gdi = NULL;
1131
+ WINPR_ASSERT(context);
1132
+ WINPR_ASSERT(createSurface);
1133
+ gdi = (rdpGdi*)context->custom;
1134
+ WINPR_ASSERT(gdi);
1135
+ WINPR_ASSERT(gdi->context);
1136
+ EnterCriticalSection(&context->mux);
1137
+ surface = (gdiGfxSurface*)calloc(1, sizeof(gdiGfxSurface));
1138
+
1139
+ if (!surface)
1140
+ goto fail;
1141
+
1142
+ if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))
1143
+ {
1144
+ WINPR_ASSERT(context->codecs);
1145
+ surface->codecs = context->codecs;
1146
+
1147
+ if (!surface->codecs)
1148
+ {
1149
+ free(surface);
1150
+ goto fail;
1151
+ }
1152
+ }
1153
+
1154
+ surface->surfaceId = createSurface->surfaceId;
1155
+ surface->width = gfx_align_scanline(createSurface->width, 16);
1156
+ surface->height = gfx_align_scanline(createSurface->height, 16);
1157
+ surface->mappedWidth = createSurface->width;
1158
+ surface->mappedHeight = createSurface->height;
1159
+ surface->outputTargetWidth = createSurface->width;
1160
+ surface->outputTargetHeight = createSurface->height;
1161
+
1162
+ switch (createSurface->pixelFormat)
1163
+ {
1164
+ case GFX_PIXEL_FORMAT_ARGB_8888:
1165
+ surface->format = PIXEL_FORMAT_BGRA32;
1166
+ break;
1167
+
1168
+ case GFX_PIXEL_FORMAT_XRGB_8888:
1169
+ surface->format = PIXEL_FORMAT_BGRX32;
1170
+ break;
1171
+
1172
+ default:
1173
+ free(surface);
1174
+ goto fail;
1175
+ }
1176
+
1177
+ surface->scanline = gfx_align_scanline(surface->width * 4UL, 16);
1178
+ surface->data = (BYTE*)winpr_aligned_malloc(1ull * surface->scanline * surface->height, 16);
1179
+
1180
+ if (!surface->data)
1181
+ {
1182
+ free(surface);
1183
+ goto fail;
1184
+ }
1185
+
1186
+ memset(surface->data, 0xFF, (size_t)surface->scanline * surface->height);
1187
+ region16_init(&surface->invalidRegion);
1188
+
1189
+ WINPR_ASSERT(context->SetSurfaceData);
1190
+ rc = context->SetSurfaceData(context, surface->surfaceId, (void*)surface);
1191
+ fail:
1192
+ LeaveCriticalSection(&context->mux);
1193
+ return rc;
1194
+ }
1195
+
1196
+ /**
1197
+ * Function description
1198
+ *
1199
+ * @return 0 on success, otherwise a Win32 error code
1200
+ */
1201
+ static UINT gdi_DeleteSurface(RdpgfxClientContext* context,
1202
+ const RDPGFX_DELETE_SURFACE_PDU* deleteSurface)
1203
+ {
1204
+ UINT rc = CHANNEL_RC_OK;
1205
+ UINT res = ERROR_INTERNAL_ERROR;
1206
+ rdpCodecs* codecs = NULL;
1207
+ gdiGfxSurface* surface = NULL;
1208
+ EnterCriticalSection(&context->mux);
1209
+
1210
+ WINPR_ASSERT(context->GetSurfaceData);
1211
+ surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);
1212
+
1213
+ if (surface)
1214
+ {
1215
+ if (surface->windowMapped)
1216
+ rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,
1217
+ surface->windowId);
1218
+
1219
+ #ifdef WITH_GFX_H264
1220
+ h264_context_free(surface->h264);
1221
+ #endif
1222
+ region16_uninit(&surface->invalidRegion);
1223
+ codecs = surface->codecs;
1224
+ winpr_aligned_free(surface->data);
1225
+ free(surface);
1226
+ }
1227
+
1228
+ WINPR_ASSERT(context->SetSurfaceData);
1229
+ res = context->SetSurfaceData(context, deleteSurface->surfaceId, NULL);
1230
+ if (res)
1231
+ rc = res;
1232
+
1233
+ if (codecs && codecs->progressive)
1234
+ progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);
1235
+
1236
+ LeaveCriticalSection(&context->mux);
1237
+ return rc;
1238
+ }
1239
+
1240
+ static BOOL intersect_rect(const RECTANGLE_16* rect, const gdiGfxSurface* surface,
1241
+ RECTANGLE_16* prect)
1242
+ {
1243
+ WINPR_ASSERT(rect);
1244
+ WINPR_ASSERT(surface);
1245
+ WINPR_ASSERT(prect);
1246
+
1247
+ if (rect->left > rect->right)
1248
+ return FALSE;
1249
+ if (rect->left > surface->width)
1250
+ return FALSE;
1251
+ if (rect->top > rect->bottom)
1252
+ return FALSE;
1253
+ if (rect->top > surface->height)
1254
+ return FALSE;
1255
+ prect->left = rect->left;
1256
+ prect->top = rect->top;
1257
+
1258
+ prect->right = MIN(rect->right, WINPR_ASSERTING_INT_CAST(UINT16, surface->width));
1259
+ prect->bottom = MIN(rect->bottom, WINPR_ASSERTING_INT_CAST(UINT16, surface->height));
1260
+ return TRUE;
1261
+ }
1262
+
1263
+ /**
1264
+ * Function description
1265
+ *
1266
+ * @return 0 on success, otherwise a Win32 error code
1267
+ */
1268
+ static UINT gdi_SolidFill(RdpgfxClientContext* context, const RDPGFX_SOLID_FILL_PDU* solidFill)
1269
+ {
1270
+ UINT status = ERROR_INTERNAL_ERROR;
1271
+ BYTE a = 0xff;
1272
+ RECTANGLE_16 invalidRect = { 0 };
1273
+ rdpGdi* gdi = (rdpGdi*)context->custom;
1274
+
1275
+ EnterCriticalSection(&context->mux);
1276
+
1277
+ WINPR_ASSERT(context->GetSurfaceData);
1278
+ gdiGfxSurface* surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);
1279
+
1280
+ if (!surface)
1281
+ goto fail;
1282
+
1283
+ const BYTE b = solidFill->fillPixel.B;
1284
+ const BYTE g = solidFill->fillPixel.G;
1285
+ const BYTE r = solidFill->fillPixel.R;
1286
+
1287
+ #if 0
1288
+ /* [MS-RDPEGFX] 3.3.5.4 Processing an RDPGFX_SOLIDFILL_PDU message
1289
+ * https://learn.microsoft.com/en-us/windows/win32/gdi/binary-raster-operations
1290
+ *
1291
+ * this sounds like the alpha value is always ignored.
1292
+ */
1293
+ if (FreeRDPColorHasAlpha(surface->format))
1294
+ a = solidFill->fillPixel.XA;
1295
+ #endif
1296
+
1297
+ const UINT32 color = FreeRDPGetColor(surface->format, r, g, b, a);
1298
+
1299
+ for (UINT16 index = 0; index < solidFill->fillRectCount; index++)
1300
+ {
1301
+ const RECTANGLE_16* rect = &(solidFill->fillRects[index]);
1302
+
1303
+ if (!intersect_rect(rect, surface, &invalidRect))
1304
+ goto fail;
1305
+
1306
+ const UINT32 nWidth = invalidRect.right - invalidRect.left;
1307
+ const UINT32 nHeight = invalidRect.bottom - invalidRect.top;
1308
+
1309
+ if (!freerdp_image_fill(surface->data, surface->format, surface->scanline, invalidRect.left,
1310
+ invalidRect.top, nWidth, nHeight, color))
1311
+ goto fail;
1312
+
1313
+ region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
1314
+ }
1315
+
1316
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,
1317
+ solidFill->fillRectCount, solidFill->fillRects);
1318
+
1319
+ if (status != CHANNEL_RC_OK)
1320
+ goto fail;
1321
+
1322
+ LeaveCriticalSection(&context->mux);
1323
+
1324
+ return gdi_interFrameUpdate(gdi, context);
1325
+ fail:
1326
+ LeaveCriticalSection(&context->mux);
1327
+ return status;
1328
+ }
1329
+
1330
+ /**
1331
+ * Function description
1332
+ *
1333
+ * @return 0 on success, otherwise a Win32 error code
1334
+ */
1335
+ static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,
1336
+ const RDPGFX_SURFACE_TO_SURFACE_PDU* surfaceToSurface)
1337
+ {
1338
+ UINT status = ERROR_INTERNAL_ERROR;
1339
+ BOOL sameSurface = 0;
1340
+ UINT32 nWidth = 0;
1341
+ UINT32 nHeight = 0;
1342
+ const RECTANGLE_16* rectSrc = NULL;
1343
+ RECTANGLE_16 invalidRect;
1344
+ gdiGfxSurface* surfaceSrc = NULL;
1345
+ gdiGfxSurface* surfaceDst = NULL;
1346
+ rdpGdi* gdi = (rdpGdi*)context->custom;
1347
+ EnterCriticalSection(&context->mux);
1348
+ rectSrc = &(surfaceToSurface->rectSrc);
1349
+
1350
+ WINPR_ASSERT(context->GetSurfaceData);
1351
+ surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);
1352
+ sameSurface =
1353
+ (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest) ? TRUE : FALSE;
1354
+
1355
+ if (!sameSurface)
1356
+ surfaceDst =
1357
+ (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);
1358
+ else
1359
+ surfaceDst = surfaceSrc;
1360
+
1361
+ if (!surfaceSrc || !surfaceDst)
1362
+ goto fail;
1363
+
1364
+ if (!is_rect_valid(rectSrc, surfaceSrc->width, surfaceSrc->height))
1365
+ goto fail;
1366
+
1367
+ nWidth = rectSrc->right - rectSrc->left;
1368
+ nHeight = rectSrc->bottom - rectSrc->top;
1369
+
1370
+ for (UINT16 index = 0; index < surfaceToSurface->destPtsCount; index++)
1371
+ {
1372
+ const RDPGFX_POINT16* destPt = &surfaceToSurface->destPts[index];
1373
+ const RECTANGLE_16 rect = { destPt->x, destPt->y,
1374
+ (UINT16)MIN(UINT16_MAX, destPt->x + nWidth),
1375
+ (UINT16)MIN(UINT16_MAX, destPt->y + nHeight) };
1376
+ if (!is_rect_valid(&rect, surfaceDst->width, surfaceDst->height))
1377
+ goto fail;
1378
+
1379
+ if (!freerdp_image_copy(surfaceDst->data, surfaceDst->format, surfaceDst->scanline,
1380
+ destPt->x, destPt->y, nWidth, nHeight, surfaceSrc->data,
1381
+ surfaceSrc->format, surfaceSrc->scanline, rectSrc->left,
1382
+ rectSrc->top, NULL, FREERDP_FLIP_NONE))
1383
+ goto fail;
1384
+
1385
+ invalidRect = rect;
1386
+ region16_union_rect(&surfaceDst->invalidRegion, &surfaceDst->invalidRegion, &invalidRect);
1387
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1388
+ surfaceDst->surfaceId, 1, &invalidRect);
1389
+
1390
+ if (status != CHANNEL_RC_OK)
1391
+ goto fail;
1392
+ }
1393
+
1394
+ LeaveCriticalSection(&context->mux);
1395
+
1396
+ return gdi_interFrameUpdate(gdi, context);
1397
+ fail:
1398
+ LeaveCriticalSection(&context->mux);
1399
+ return status;
1400
+ }
1401
+
1402
+ static void gdi_GfxCacheEntryFree(gdiGfxCacheEntry* entry)
1403
+ {
1404
+ if (!entry)
1405
+ return;
1406
+ free(entry->data);
1407
+ free(entry);
1408
+ }
1409
+
1410
+ static gdiGfxCacheEntry* gdi_GfxCacheEntryNew(UINT64 cacheKey, UINT32 width, UINT32 height,
1411
+ UINT32 format)
1412
+ {
1413
+ gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)calloc(1, sizeof(gdiGfxCacheEntry));
1414
+ if (!cacheEntry)
1415
+ goto fail;
1416
+
1417
+ cacheEntry->cacheKey = cacheKey;
1418
+ cacheEntry->width = width;
1419
+ cacheEntry->height = height;
1420
+ cacheEntry->format = format;
1421
+ cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * 4, 16);
1422
+
1423
+ if ((cacheEntry->width > 0) && (cacheEntry->height > 0))
1424
+ {
1425
+ cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);
1426
+
1427
+ if (!cacheEntry->data)
1428
+ goto fail;
1429
+ }
1430
+ return cacheEntry;
1431
+ fail:
1432
+ gdi_GfxCacheEntryFree(cacheEntry);
1433
+ return NULL;
1434
+ }
1435
+
1436
+ /**
1437
+ * Function description
1438
+ *
1439
+ * @return 0 on success, otherwise a Win32 error code
1440
+ */
1441
+ static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,
1442
+ const RDPGFX_SURFACE_TO_CACHE_PDU* surfaceToCache)
1443
+ {
1444
+ const RECTANGLE_16* rect = NULL;
1445
+ gdiGfxSurface* surface = NULL;
1446
+ gdiGfxCacheEntry* cacheEntry = NULL;
1447
+ UINT rc = ERROR_INTERNAL_ERROR;
1448
+ EnterCriticalSection(&context->mux);
1449
+ rect = &(surfaceToCache->rectSrc);
1450
+
1451
+ WINPR_ASSERT(context->GetSurfaceData);
1452
+ surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);
1453
+
1454
+ if (!surface)
1455
+ goto fail;
1456
+
1457
+ if (!is_rect_valid(rect, surface->width, surface->height))
1458
+ goto fail;
1459
+
1460
+ cacheEntry = gdi_GfxCacheEntryNew(surfaceToCache->cacheKey, (UINT32)(rect->right - rect->left),
1461
+ (UINT32)(rect->bottom - rect->top), surface->format);
1462
+
1463
+ if (!cacheEntry)
1464
+ goto fail;
1465
+
1466
+ if (!cacheEntry->data)
1467
+ goto fail;
1468
+
1469
+ if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1470
+ 0, 0, cacheEntry->width, cacheEntry->height, surface->data,
1471
+ surface->format, surface->scanline, rect->left, rect->top,
1472
+ NULL, FREERDP_FLIP_NONE))
1473
+ goto fail;
1474
+
1475
+ RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { surfaceToCache->cacheSlot };
1476
+ WINPR_ASSERT(context->EvictCacheEntry);
1477
+ context->EvictCacheEntry(context, &evict);
1478
+
1479
+ WINPR_ASSERT(context->SetCacheSlotData);
1480
+ rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (void*)cacheEntry);
1481
+ fail:
1482
+ if (rc != CHANNEL_RC_OK)
1483
+ gdi_GfxCacheEntryFree(cacheEntry);
1484
+ LeaveCriticalSection(&context->mux);
1485
+ return rc;
1486
+ }
1487
+
1488
+ /**
1489
+ * Function description
1490
+ *
1491
+ * @return 0 on success, otherwise a Win32 error code
1492
+ */
1493
+ static UINT gdi_CacheToSurface(RdpgfxClientContext* context,
1494
+ const RDPGFX_CACHE_TO_SURFACE_PDU* cacheToSurface)
1495
+ {
1496
+ UINT status = ERROR_INTERNAL_ERROR;
1497
+ gdiGfxSurface* surface = NULL;
1498
+ gdiGfxCacheEntry* cacheEntry = NULL;
1499
+ RECTANGLE_16 invalidRect;
1500
+ rdpGdi* gdi = (rdpGdi*)context->custom;
1501
+
1502
+ EnterCriticalSection(&context->mux);
1503
+
1504
+ WINPR_ASSERT(context->GetSurfaceData);
1505
+ surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);
1506
+
1507
+ WINPR_ASSERT(context->GetCacheSlotData);
1508
+ cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);
1509
+
1510
+ if (!surface || !cacheEntry)
1511
+ goto fail;
1512
+
1513
+ for (UINT16 index = 0; index < cacheToSurface->destPtsCount; index++)
1514
+ {
1515
+ const RDPGFX_POINT16* destPt = &cacheToSurface->destPts[index];
1516
+ const RECTANGLE_16 rect = { destPt->x, destPt->y,
1517
+ (UINT16)MIN(UINT16_MAX, destPt->x + cacheEntry->width),
1518
+ (UINT16)MIN(UINT16_MAX, destPt->y + cacheEntry->height) };
1519
+
1520
+ if (rectangle_is_empty(&rect))
1521
+ continue;
1522
+
1523
+ if (!is_rect_valid(&rect, surface->width, surface->height))
1524
+ goto fail;
1525
+
1526
+ if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline,
1527
+ destPt->x, destPt->y, cacheEntry->width,
1528
+ cacheEntry->height, cacheEntry->data, cacheEntry->format,
1529
+ cacheEntry->scanline, 0, 0, NULL, FREERDP_FLIP_NONE))
1530
+ goto fail;
1531
+
1532
+ invalidRect = rect;
1533
+ region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &invalidRect);
1534
+ status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,
1535
+ surface->surfaceId, 1, &invalidRect);
1536
+
1537
+ if (status != CHANNEL_RC_OK)
1538
+ goto fail;
1539
+ }
1540
+
1541
+ LeaveCriticalSection(&context->mux);
1542
+
1543
+ return gdi_interFrameUpdate(gdi, context);
1544
+
1545
+ fail:
1546
+ LeaveCriticalSection(&context->mux);
1547
+ return status;
1548
+ }
1549
+
1550
+ /**
1551
+ * Function description
1552
+ *
1553
+ * @return 0 on success, otherwise a Win32 error code
1554
+ */
1555
+ static UINT gdi_CacheImportReply(RdpgfxClientContext* context,
1556
+ const RDPGFX_CACHE_IMPORT_REPLY_PDU* cacheImportReply)
1557
+ {
1558
+ UINT16 count = 0;
1559
+ const UINT16* slots = NULL;
1560
+ UINT error = CHANNEL_RC_OK;
1561
+
1562
+ slots = cacheImportReply->cacheSlots;
1563
+ count = cacheImportReply->importedEntriesCount;
1564
+
1565
+ for (UINT16 index = 0; index < count; index++)
1566
+ {
1567
+ UINT16 cacheSlot = slots[index];
1568
+
1569
+ if (cacheSlot == 0)
1570
+ continue;
1571
+
1572
+ WINPR_ASSERT(context->GetCacheSlotData);
1573
+ gdiGfxCacheEntry* cacheEntry =
1574
+ (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1575
+
1576
+ if (cacheEntry)
1577
+ continue;
1578
+
1579
+ cacheEntry = gdi_GfxCacheEntryNew(cacheSlot, 0, 0, PIXEL_FORMAT_BGRX32);
1580
+
1581
+ if (!cacheEntry)
1582
+ return ERROR_INTERNAL_ERROR;
1583
+
1584
+ WINPR_ASSERT(context->SetCacheSlotData);
1585
+ error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);
1586
+
1587
+ if (error)
1588
+ {
1589
+ WLog_ERR(TAG, "CacheImportReply: SetCacheSlotData failed with error %" PRIu32 "",
1590
+ error);
1591
+ gdi_GfxCacheEntryFree(cacheEntry);
1592
+ break;
1593
+ }
1594
+ }
1595
+
1596
+ return error;
1597
+ }
1598
+
1599
+ static UINT gdi_ImportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1600
+ const PERSISTENT_CACHE_ENTRY* importCacheEntry)
1601
+ {
1602
+ UINT error = ERROR_INTERNAL_ERROR;
1603
+ gdiGfxCacheEntry* cacheEntry = NULL;
1604
+
1605
+ if (cacheSlot == 0)
1606
+ return CHANNEL_RC_OK;
1607
+
1608
+ cacheEntry = gdi_GfxCacheEntryNew(importCacheEntry->key64, importCacheEntry->width,
1609
+ importCacheEntry->height, PIXEL_FORMAT_BGRX32);
1610
+
1611
+ if (!cacheEntry)
1612
+ goto fail;
1613
+
1614
+ if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,
1615
+ 0, 0, cacheEntry->width, cacheEntry->height,
1616
+ importCacheEntry->data, PIXEL_FORMAT_BGRX32, 0, 0, 0, NULL,
1617
+ FREERDP_FLIP_NONE))
1618
+ goto fail;
1619
+
1620
+ RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { cacheSlot };
1621
+ WINPR_ASSERT(context->EvictCacheEntry);
1622
+ error = context->EvictCacheEntry(context, &evict);
1623
+ if (error != CHANNEL_RC_OK)
1624
+ goto fail;
1625
+
1626
+ WINPR_ASSERT(context->SetCacheSlotData);
1627
+ error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);
1628
+
1629
+ fail:
1630
+ if (error)
1631
+ {
1632
+ gdi_GfxCacheEntryFree(cacheEntry);
1633
+ WLog_ERR(TAG, "ImportCacheEntry: SetCacheSlotData failed with error %" PRIu32 "", error);
1634
+ }
1635
+
1636
+ return error;
1637
+ }
1638
+
1639
+ static UINT gdi_ExportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,
1640
+ PERSISTENT_CACHE_ENTRY* exportCacheEntry)
1641
+ {
1642
+ gdiGfxCacheEntry* cacheEntry = NULL;
1643
+
1644
+ WINPR_ASSERT(context->GetCacheSlotData);
1645
+ cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);
1646
+
1647
+ if (cacheEntry)
1648
+ {
1649
+ exportCacheEntry->key64 = cacheEntry->cacheKey;
1650
+ exportCacheEntry->width = (UINT16)MIN(UINT16_MAX, cacheEntry->width);
1651
+ exportCacheEntry->height = (UINT16)MIN(UINT16_MAX, cacheEntry->height);
1652
+ exportCacheEntry->size = cacheEntry->width * cacheEntry->height * 4;
1653
+ exportCacheEntry->flags = 0;
1654
+ exportCacheEntry->data = cacheEntry->data;
1655
+ return CHANNEL_RC_OK;
1656
+ }
1657
+
1658
+ return ERROR_NOT_FOUND;
1659
+ }
1660
+
1661
+ /**
1662
+ * Function description
1663
+ *
1664
+ * @return 0 on success, otherwise a Win32 error code
1665
+ */
1666
+ static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,
1667
+ const RDPGFX_EVICT_CACHE_ENTRY_PDU* evictCacheEntry)
1668
+ {
1669
+ gdiGfxCacheEntry* cacheEntry = NULL;
1670
+ UINT rc = ERROR_NOT_FOUND;
1671
+
1672
+ WINPR_ASSERT(context);
1673
+ WINPR_ASSERT(evictCacheEntry);
1674
+
1675
+ EnterCriticalSection(&context->mux);
1676
+
1677
+ WINPR_ASSERT(context->GetCacheSlotData);
1678
+ cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);
1679
+
1680
+ gdi_GfxCacheEntryFree(cacheEntry);
1681
+
1682
+ WINPR_ASSERT(context->SetCacheSlotData);
1683
+ rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot, NULL);
1684
+ LeaveCriticalSection(&context->mux);
1685
+ return rc;
1686
+ }
1687
+
1688
+ /**
1689
+ * Function description
1690
+ *
1691
+ * @return 0 on success, otherwise a Win32 error code
1692
+ */
1693
+ static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,
1694
+ const RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU* surfaceToOutput)
1695
+ {
1696
+ UINT rc = ERROR_INTERNAL_ERROR;
1697
+ gdiGfxSurface* surface = NULL;
1698
+ EnterCriticalSection(&context->mux);
1699
+
1700
+ WINPR_ASSERT(context->GetSurfaceData);
1701
+ surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1702
+
1703
+ if (!surface)
1704
+ goto fail;
1705
+
1706
+ if (surface->windowMapped)
1707
+ {
1708
+ WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");
1709
+ goto fail;
1710
+ }
1711
+
1712
+ surface->outputMapped = TRUE;
1713
+ surface->outputOriginX = surfaceToOutput->outputOriginX;
1714
+ surface->outputOriginY = surfaceToOutput->outputOriginY;
1715
+ surface->outputTargetWidth = surface->mappedWidth;
1716
+ surface->outputTargetHeight = surface->mappedHeight;
1717
+ region16_clear(&surface->invalidRegion);
1718
+ rc = CHANNEL_RC_OK;
1719
+ fail:
1720
+ LeaveCriticalSection(&context->mux);
1721
+ return rc;
1722
+ }
1723
+
1724
+ static UINT
1725
+ gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,
1726
+ const RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU* surfaceToOutput)
1727
+ {
1728
+ UINT rc = ERROR_INTERNAL_ERROR;
1729
+ gdiGfxSurface* surface = NULL;
1730
+ EnterCriticalSection(&context->mux);
1731
+
1732
+ WINPR_ASSERT(context->GetSurfaceData);
1733
+ surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);
1734
+
1735
+ if (!surface)
1736
+ goto fail;
1737
+
1738
+ if (surface->windowMapped)
1739
+ {
1740
+ WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");
1741
+ goto fail;
1742
+ }
1743
+
1744
+ surface->outputMapped = TRUE;
1745
+ surface->outputOriginX = surfaceToOutput->outputOriginX;
1746
+ surface->outputOriginY = surfaceToOutput->outputOriginY;
1747
+ surface->outputTargetWidth = surfaceToOutput->targetWidth;
1748
+ surface->outputTargetHeight = surfaceToOutput->targetHeight;
1749
+ region16_clear(&surface->invalidRegion);
1750
+ rc = CHANNEL_RC_OK;
1751
+ fail:
1752
+ LeaveCriticalSection(&context->mux);
1753
+ return rc;
1754
+ }
1755
+
1756
+ /**
1757
+ * Function description
1758
+ *
1759
+ * @return 0 on success, otherwise a Win32 error code
1760
+ */
1761
+ static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,
1762
+ const RDPGFX_MAP_SURFACE_TO_WINDOW_PDU* surfaceToWindow)
1763
+ {
1764
+ UINT rc = ERROR_INTERNAL_ERROR;
1765
+ gdiGfxSurface* surface = NULL;
1766
+ EnterCriticalSection(&context->mux);
1767
+
1768
+ WINPR_ASSERT(context->GetSurfaceData);
1769
+ surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
1770
+
1771
+ if (!surface)
1772
+ goto fail;
1773
+
1774
+ if (surface->outputMapped)
1775
+ {
1776
+ WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");
1777
+ goto fail;
1778
+ }
1779
+
1780
+ if (surface->windowMapped)
1781
+ {
1782
+ if (surface->windowId != surfaceToWindow->windowId)
1783
+ {
1784
+ WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,
1785
+ surface->windowId, surfaceToWindow->windowId);
1786
+ goto fail;
1787
+ }
1788
+ }
1789
+ surface->windowMapped = TRUE;
1790
+
1791
+ surface->windowId = surfaceToWindow->windowId;
1792
+ surface->mappedWidth = surfaceToWindow->mappedWidth;
1793
+ surface->mappedHeight = surfaceToWindow->mappedHeight;
1794
+ surface->outputTargetWidth = surfaceToWindow->mappedWidth;
1795
+ surface->outputTargetHeight = surfaceToWindow->mappedHeight;
1796
+ rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
1797
+ surfaceToWindow->surfaceId, surfaceToWindow->windowId);
1798
+ fail:
1799
+ LeaveCriticalSection(&context->mux);
1800
+ return rc;
1801
+ }
1802
+
1803
+ static UINT
1804
+ gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,
1805
+ const RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU* surfaceToWindow)
1806
+ {
1807
+ UINT rc = ERROR_INTERNAL_ERROR;
1808
+ gdiGfxSurface* surface = NULL;
1809
+ EnterCriticalSection(&context->mux);
1810
+
1811
+ WINPR_ASSERT(context->GetSurfaceData);
1812
+ surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);
1813
+
1814
+ if (!surface)
1815
+ goto fail;
1816
+
1817
+ if (surface->outputMapped)
1818
+ {
1819
+ WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");
1820
+ goto fail;
1821
+ }
1822
+
1823
+ if (surface->windowMapped)
1824
+ {
1825
+ if (surface->windowId != surfaceToWindow->windowId)
1826
+ {
1827
+ WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,
1828
+ surface->windowId, surfaceToWindow->windowId);
1829
+ goto fail;
1830
+ }
1831
+ }
1832
+ surface->windowMapped = TRUE;
1833
+
1834
+ surface->windowId = surfaceToWindow->windowId;
1835
+ surface->mappedWidth = surfaceToWindow->mappedWidth;
1836
+ surface->mappedHeight = surfaceToWindow->mappedHeight;
1837
+ surface->outputTargetWidth = surfaceToWindow->targetWidth;
1838
+ surface->outputTargetHeight = surfaceToWindow->targetHeight;
1839
+ rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,
1840
+ surfaceToWindow->surfaceId, surfaceToWindow->windowId);
1841
+ fail:
1842
+ LeaveCriticalSection(&context->mux);
1843
+ return rc;
1844
+ }
1845
+
1846
+ BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)
1847
+ {
1848
+ return gdi_graphics_pipeline_init_ex(gdi, gfx, NULL, NULL, NULL);
1849
+ }
1850
+
1851
+ BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,
1852
+ pcRdpgfxMapWindowForSurface map,
1853
+ pcRdpgfxUnmapWindowForSurface unmap,
1854
+ pcRdpgfxUpdateSurfaceArea update)
1855
+ {
1856
+ if (!gdi || !gfx || !gdi->context || !gdi->context->settings)
1857
+ return FALSE;
1858
+
1859
+ rdpContext* context = gdi->context;
1860
+ rdpSettings* settings = context->settings;
1861
+
1862
+ gdi->gfx = gfx;
1863
+ gfx->custom = (void*)gdi;
1864
+ gfx->ResetGraphics = gdi_ResetGraphics;
1865
+ gfx->StartFrame = gdi_StartFrame;
1866
+ gfx->EndFrame = gdi_EndFrame;
1867
+ gfx->SurfaceCommand = gdi_SurfaceCommand;
1868
+ gfx->DeleteEncodingContext = gdi_DeleteEncodingContext;
1869
+ gfx->CreateSurface = gdi_CreateSurface;
1870
+ gfx->DeleteSurface = gdi_DeleteSurface;
1871
+ gfx->SolidFill = gdi_SolidFill;
1872
+ gfx->SurfaceToSurface = gdi_SurfaceToSurface;
1873
+ gfx->SurfaceToCache = gdi_SurfaceToCache;
1874
+ gfx->CacheToSurface = gdi_CacheToSurface;
1875
+ gfx->CacheImportReply = gdi_CacheImportReply;
1876
+ gfx->ImportCacheEntry = gdi_ImportCacheEntry;
1877
+ gfx->ExportCacheEntry = gdi_ExportCacheEntry;
1878
+ gfx->EvictCacheEntry = gdi_EvictCacheEntry;
1879
+ gfx->MapSurfaceToOutput = gdi_MapSurfaceToOutput;
1880
+ gfx->MapSurfaceToWindow = gdi_MapSurfaceToWindow;
1881
+ gfx->MapSurfaceToScaledOutput = gdi_MapSurfaceToScaledOutput;
1882
+ gfx->MapSurfaceToScaledWindow = gdi_MapSurfaceToScaledWindow;
1883
+ gfx->UpdateSurfaces = gdi_UpdateSurfaces;
1884
+ gfx->MapWindowForSurface = map;
1885
+ gfx->UnmapWindowForSurface = unmap;
1886
+ gfx->UpdateSurfaceArea = update;
1887
+
1888
+ if (!freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
1889
+ {
1890
+ const UINT32 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
1891
+ const UINT32 h = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
1892
+ const UINT32 flags = freerdp_settings_get_uint32(settings, FreeRDP_ThreadingFlags);
1893
+
1894
+ gfx->codecs = freerdp_client_codecs_new(flags);
1895
+ if (!gfx->codecs)
1896
+ return FALSE;
1897
+ if (!freerdp_client_codecs_prepare(gfx->codecs, FREERDP_CODEC_ALL, w, h))
1898
+ return FALSE;
1899
+ }
1900
+ InitializeCriticalSection(&gfx->mux);
1901
+ PROFILER_CREATE(gfx->SurfaceProfiler, "GFX-PROFILER")
1902
+
1903
+ /**
1904
+ * gdi->graphicsReset will be removed in FreeRDP v3 from public headers,
1905
+ * since the EGFX Reset Graphics PDU seems to be optional.
1906
+ * There are still some clients that expect and check it and therefore
1907
+ * we simply initialize it with TRUE here for now.
1908
+ */
1909
+ gdi->graphicsReset = TRUE;
1910
+ if (freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))
1911
+ {
1912
+ gfx->UpdateSurfaceArea = NULL;
1913
+ gfx->UpdateSurfaces = NULL;
1914
+ gfx->SurfaceCommand = NULL;
1915
+ }
1916
+
1917
+ return TRUE;
1918
+ }
1919
+
1920
+ void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)
1921
+ {
1922
+ if (gdi)
1923
+ gdi->gfx = NULL;
1924
+
1925
+ if (!gfx)
1926
+ return;
1927
+
1928
+ gfx->custom = NULL;
1929
+ freerdp_client_codecs_free(gfx->codecs);
1930
+ gfx->codecs = NULL;
1931
+ DeleteCriticalSection(&gfx->mux);
1932
+ PROFILER_PRINT_HEADER
1933
+ PROFILER_PRINT(gfx->SurfaceProfiler)
1934
+ PROFILER_PRINT_FOOTER
1935
+ PROFILER_FREE(gfx->SurfaceProfiler)
1936
+ }
1937
+
1938
+ const char* rdpgfx_caps_version_str(UINT32 capsVersion)
1939
+ {
1940
+ switch (capsVersion)
1941
+ {
1942
+ case RDPGFX_CAPVERSION_8:
1943
+ return "RDPGFX_CAPVERSION_8";
1944
+ case RDPGFX_CAPVERSION_81:
1945
+ return "RDPGFX_CAPVERSION_81";
1946
+ case RDPGFX_CAPVERSION_10:
1947
+ return "RDPGFX_CAPVERSION_10";
1948
+ case RDPGFX_CAPVERSION_101:
1949
+ return "RDPGFX_CAPVERSION_101";
1950
+ case RDPGFX_CAPVERSION_102:
1951
+ return "RDPGFX_CAPVERSION_102";
1952
+ case RDPGFX_CAPVERSION_103:
1953
+ return "RDPGFX_CAPVERSION_103";
1954
+ case RDPGFX_CAPVERSION_104:
1955
+ return "RDPGFX_CAPVERSION_104";
1956
+ case RDPGFX_CAPVERSION_105:
1957
+ return "RDPGFX_CAPVERSION_105";
1958
+ case RDPGFX_CAPVERSION_106:
1959
+ return "RDPGFX_CAPVERSION_106";
1960
+ case RDPGFX_CAPVERSION_106_ERR:
1961
+ return "RDPGFX_CAPVERSION_106_ERR";
1962
+ case RDPGFX_CAPVERSION_107:
1963
+ return "RDPGFX_CAPVERSION_107";
1964
+ default:
1965
+ return "RDPGFX_CAPVERSION_UNKNOWN";
1966
+ }
1967
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/line.c ADDED
@@ -0,0 +1,316 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * GDI Line Functions
4
+ *
5
+ * Copyright 2010-2011 Marc-Andre Moreau <[email protected]>
6
+ * Copyright 2016 Armin Novak <[email protected]>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <stdio.h>
25
+ #include <string.h>
26
+ #include <stdlib.h>
27
+
28
+ #include <winpr/assert.h>
29
+
30
+ #include <freerdp/freerdp.h>
31
+ #include <freerdp/gdi/gdi.h>
32
+ #include <freerdp/gdi/pen.h>
33
+ #include <freerdp/gdi/bitmap.h>
34
+ #include <freerdp/gdi/region.h>
35
+
36
+ #include "drawing.h"
37
+ #include "clipping.h"
38
+ #include "line.h"
39
+
40
+ static BOOL gdi_rop_color(INT32 rop, BYTE* pixelPtr, UINT32 pen, UINT32 format)
41
+ {
42
+ WINPR_ASSERT(pixelPtr);
43
+ const UINT32 srcPixel = FreeRDPReadColor(pixelPtr, format);
44
+ UINT32 dstPixel = 0;
45
+
46
+ switch (rop)
47
+ {
48
+ case GDI_R2_BLACK: /* LineTo_BLACK */
49
+ dstPixel = FreeRDPGetColor(format, 0, 0, 0, 0xFF);
50
+ break;
51
+
52
+ case GDI_R2_NOTMERGEPEN: /* LineTo_NOTMERGEPEN */
53
+ dstPixel = ~(srcPixel | pen);
54
+ break;
55
+
56
+ case GDI_R2_MASKNOTPEN: /* LineTo_MASKNOTPEN */
57
+ dstPixel = srcPixel & ~pen;
58
+ break;
59
+
60
+ case GDI_R2_NOTCOPYPEN: /* LineTo_NOTCOPYPEN */
61
+ dstPixel = ~pen;
62
+ break;
63
+
64
+ case GDI_R2_MASKPENNOT: /* LineTo_MASKPENNOT */
65
+ dstPixel = pen & ~srcPixel;
66
+ break;
67
+
68
+ case GDI_R2_NOT: /* LineTo_NOT */
69
+ dstPixel = ~srcPixel;
70
+ break;
71
+
72
+ case GDI_R2_XORPEN: /* LineTo_XORPEN */
73
+ dstPixel = srcPixel ^ pen;
74
+ break;
75
+
76
+ case GDI_R2_NOTMASKPEN: /* LineTo_NOTMASKPEN */
77
+ dstPixel = ~(srcPixel & pen);
78
+ break;
79
+
80
+ case GDI_R2_MASKPEN: /* LineTo_MASKPEN */
81
+ dstPixel = srcPixel & pen;
82
+ break;
83
+
84
+ case GDI_R2_NOTXORPEN: /* LineTo_NOTXORPEN */
85
+ dstPixel = ~(srcPixel ^ pen);
86
+ break;
87
+
88
+ case GDI_R2_NOP: /* LineTo_NOP */
89
+ dstPixel = srcPixel;
90
+ break;
91
+
92
+ case GDI_R2_MERGENOTPEN: /* LineTo_MERGENOTPEN */
93
+ dstPixel = srcPixel | ~pen;
94
+ break;
95
+
96
+ case GDI_R2_COPYPEN: /* LineTo_COPYPEN */
97
+ dstPixel = pen;
98
+ break;
99
+
100
+ case GDI_R2_MERGEPENNOT: /* LineTo_MERGEPENNOT */
101
+ dstPixel = srcPixel | ~pen;
102
+ break;
103
+
104
+ case GDI_R2_MERGEPEN: /* LineTo_MERGEPEN */
105
+ dstPixel = srcPixel | pen;
106
+ break;
107
+
108
+ case GDI_R2_WHITE: /* LineTo_WHITE */
109
+ dstPixel = FreeRDPGetColor(format, 0xFF, 0xFF, 0xFF, 0xFF);
110
+ break;
111
+
112
+ default:
113
+ return FALSE;
114
+ }
115
+
116
+ return FreeRDPWriteColor(pixelPtr, format, dstPixel);
117
+ }
118
+
119
+ BOOL gdi_LineTo(HGDI_DC hdc, INT32 nXEnd, INT32 nYEnd)
120
+ {
121
+ INT32 e2 = 0;
122
+ UINT32 pen = 0;
123
+
124
+ WINPR_ASSERT(hdc);
125
+ const INT32 rop2 = gdi_GetROP2(hdc);
126
+
127
+ const INT32 x1 = hdc->pen->posX;
128
+ const INT32 y1 = hdc->pen->posY;
129
+ const INT32 x2 = WINPR_ASSERTING_INT_CAST(int32_t, nXEnd);
130
+ const INT32 y2 = WINPR_ASSERTING_INT_CAST(int32_t, nYEnd);
131
+ const INT32 dx = (x1 > x2) ? x1 - x2 : x2 - x1;
132
+ const INT32 dy = (y1 > y2) ? y1 - y2 : y2 - y1;
133
+ const INT32 sx = (x1 < x2) ? 1 : -1;
134
+ const INT32 sy = (y1 < y2) ? 1 : -1;
135
+ INT32 e = dx - dy;
136
+ INT32 x = x1;
137
+ INT32 y = y1;
138
+
139
+ WINPR_ASSERT(hdc->clip);
140
+ INT32 bx1 = 0;
141
+ INT32 by1 = 0;
142
+ INT32 bx2 = 0;
143
+ INT32 by2 = 0;
144
+ if (hdc->clip->null)
145
+ {
146
+ bx1 = (x1 < x2) ? x1 : x2;
147
+ by1 = (y1 < y2) ? y1 : y2;
148
+ bx2 = (x1 > x2) ? x1 : x2;
149
+ by2 = (y1 > y2) ? y1 : y2;
150
+ }
151
+ else
152
+ {
153
+ bx1 = hdc->clip->x;
154
+ by1 = hdc->clip->y;
155
+ bx2 = bx1 + hdc->clip->w - 1;
156
+ by2 = by1 + hdc->clip->h - 1;
157
+ }
158
+
159
+ HGDI_BITMAP bmp = (HGDI_BITMAP)hdc->selectedObject;
160
+ WINPR_ASSERT(bmp);
161
+
162
+ bx1 = MAX(bx1, 0);
163
+ by1 = MAX(by1, 0);
164
+ bx2 = MIN(bx2, bmp->width - 1);
165
+ by2 = MIN(by2, bmp->height - 1);
166
+
167
+ if (!gdi_InvalidateRegion(hdc, bx1, by1, bx2 - bx1 + 1, by2 - by1 + 1))
168
+ return FALSE;
169
+
170
+ pen = gdi_GetPenColor(hdc->pen, bmp->format);
171
+
172
+ while (1)
173
+ {
174
+ if (!(x == x2 && y == y2))
175
+ {
176
+ if ((x >= bx1 && x <= bx2) && (y >= by1 && y <= by2))
177
+ {
178
+ BYTE* pixel = gdi_GetPointer(bmp, WINPR_ASSERTING_INT_CAST(uint32_t, x),
179
+ WINPR_ASSERTING_INT_CAST(uint32_t, y));
180
+ WINPR_ASSERT(pixel);
181
+ gdi_rop_color(rop2, pixel, pen, bmp->format);
182
+ }
183
+ }
184
+ else
185
+ {
186
+ break;
187
+ }
188
+
189
+ e2 = 2 * e;
190
+
191
+ if (e2 > -dy)
192
+ {
193
+ e -= dy;
194
+ x += sx;
195
+ }
196
+
197
+ if (e2 < dx)
198
+ {
199
+ e += dx;
200
+ y += sy;
201
+ }
202
+ }
203
+
204
+ return TRUE;
205
+ }
206
+
207
+ /**
208
+ * Draw one or more straight lines
209
+ * @param hdc device context
210
+ * @param lppt array of points
211
+ * @param cCount number of points
212
+ * @return nonzero on success, 0 otherwise
213
+ */
214
+ BOOL gdi_PolylineTo(HGDI_DC hdc, GDI_POINT* lppt, DWORD cCount)
215
+ {
216
+ WINPR_ASSERT(hdc);
217
+ WINPR_ASSERT(lppt || (cCount == 0));
218
+
219
+ for (DWORD i = 0; i < cCount; i++)
220
+ {
221
+ if (!gdi_LineTo(hdc, lppt[i].x, lppt[i].y))
222
+ return FALSE;
223
+
224
+ if (!gdi_MoveToEx(hdc, lppt[i].x, lppt[i].y, NULL))
225
+ return FALSE;
226
+ }
227
+
228
+ return TRUE;
229
+ }
230
+
231
+ /**
232
+ * Draw one or more straight lines
233
+ * @param hdc device context
234
+ * @param lppt array of points
235
+ * @param cPoints number of points
236
+ * @return nonzero on success, 0 otherwise
237
+ */
238
+ BOOL gdi_Polyline(HGDI_DC hdc, GDI_POINT* lppt, UINT32 cPoints)
239
+ {
240
+ WINPR_ASSERT(hdc);
241
+ WINPR_ASSERT(lppt || (cPoints == 0));
242
+
243
+ if (cPoints > 0)
244
+ {
245
+ GDI_POINT pt = { 0 };
246
+
247
+ if (!gdi_MoveToEx(hdc, lppt[0].x, lppt[0].y, &pt))
248
+ return FALSE;
249
+
250
+ for (UINT32 i = 0; i < cPoints; i++)
251
+ {
252
+ if (!gdi_LineTo(hdc, lppt[i].x, lppt[i].y))
253
+ return FALSE;
254
+
255
+ if (!gdi_MoveToEx(hdc, lppt[i].x, lppt[i].y, NULL))
256
+ return FALSE;
257
+ }
258
+
259
+ if (!gdi_MoveToEx(hdc, pt.x, pt.y, NULL))
260
+ return FALSE;
261
+ }
262
+
263
+ return TRUE;
264
+ }
265
+
266
+ /**
267
+ * Draw multiple series of connected line segments
268
+ * @param hdc device context
269
+ * @param lppt array of points
270
+ * @param lpdwPolyPoints array of numbers of points per series
271
+ * @param cCount count of entries in lpdwPolyPoints
272
+ * @return nonzero on success, 0 otherwise
273
+ */
274
+ BOOL gdi_PolyPolyline(HGDI_DC hdc, GDI_POINT* lppt, const UINT32* lpdwPolyPoints, DWORD cCount)
275
+ {
276
+ DWORD j = 0;
277
+
278
+ WINPR_ASSERT(hdc);
279
+ WINPR_ASSERT(lppt || (cCount == 0));
280
+ WINPR_ASSERT(lpdwPolyPoints || (cCount == 0));
281
+
282
+ for (DWORD i = 0; i < cCount; i++)
283
+ {
284
+ const UINT32 cPoints = lpdwPolyPoints[i];
285
+
286
+ if (!gdi_Polyline(hdc, &lppt[j], cPoints))
287
+ return FALSE;
288
+
289
+ j += cPoints;
290
+ }
291
+
292
+ return TRUE;
293
+ }
294
+
295
+ /**
296
+ * Move pen from the current device context to a new position.
297
+ * @param hdc device context
298
+ * @param X x position
299
+ * @param Y y position
300
+ * @return nonzero on success, 0 otherwise
301
+ */
302
+
303
+ BOOL gdi_MoveToEx(HGDI_DC hdc, INT32 X, INT32 Y, HGDI_POINT lpPoint)
304
+ {
305
+ WINPR_ASSERT(hdc);
306
+
307
+ if (lpPoint != NULL)
308
+ {
309
+ lpPoint->x = hdc->pen->posX;
310
+ lpPoint->y = hdc->pen->posY;
311
+ }
312
+
313
+ hdc->pen->posX = X;
314
+ hdc->pen->posY = Y;
315
+ return TRUE;
316
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/shape.c ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * GDI Shape Functions
4
+ *
5
+ * Copyright 2010-2011 Marc-Andre Moreau <[email protected]>
6
+ * Copyright 2016 Armin Novak <[email protected]>
7
+ * Copyright 2016 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <stdio.h>
25
+ #include <string.h>
26
+ #include <stdlib.h>
27
+
28
+ #include <freerdp/freerdp.h>
29
+ #include <freerdp/gdi/gdi.h>
30
+
31
+ #include <freerdp/gdi/bitmap.h>
32
+ #include <freerdp/gdi/region.h>
33
+ #include <freerdp/gdi/shape.h>
34
+
35
+ #include <freerdp/log.h>
36
+
37
+ #include "clipping.h"
38
+ #include "../gdi/gdi.h"
39
+
40
+ #define TAG FREERDP_TAG("gdi.shape")
41
+
42
+ static void Ellipse_Bresenham(HGDI_DC hdc, int x1, int y1, int x2, int y2)
43
+ {
44
+ INT32 e = 0;
45
+ INT32 e2 = 0;
46
+ INT32 dx = 0;
47
+ INT32 dy = 0;
48
+ INT32 a = 0;
49
+ INT32 b = 0;
50
+ INT32 c = 0;
51
+ a = (x1 < x2) ? x2 - x1 : x1 - x2;
52
+ b = (y1 < y2) ? y2 - y1 : y1 - y2;
53
+ c = b & 1;
54
+ dx = 4 * (1 - a) * b * b;
55
+ dy = 4 * (c + 1) * a * a;
56
+ e = dx + dy + c * a * a;
57
+
58
+ if (x1 > x2)
59
+ {
60
+ x1 = x2;
61
+ x2 += a;
62
+ }
63
+
64
+ if (y1 > y2)
65
+ y1 = y2;
66
+
67
+ y1 += (b + 1) / 2;
68
+ y2 = y1 - c;
69
+ a *= 8 * a;
70
+ c = 8 * b * b;
71
+
72
+ do
73
+ {
74
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(UINT32, x2),
75
+ WINPR_ASSERTING_INT_CAST(UINT32, y1), 0);
76
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(UINT32, x1),
77
+ WINPR_ASSERTING_INT_CAST(UINT32, y1), 0);
78
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(UINT32, x1),
79
+ WINPR_ASSERTING_INT_CAST(UINT32, y2), 0);
80
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(UINT32, x2),
81
+ WINPR_ASSERTING_INT_CAST(UINT32, y2), 0);
82
+ e2 = 2 * e;
83
+
84
+ if (e2 >= dx)
85
+ {
86
+ x1++;
87
+ x2--;
88
+ e += dx += c;
89
+ }
90
+
91
+ if (e2 <= dy)
92
+ {
93
+ y1++;
94
+ y2--;
95
+ e += dy += a;
96
+ }
97
+ } while (x1 <= x2);
98
+
99
+ while (y1 - y2 < b)
100
+ {
101
+ y1++;
102
+ y2--;
103
+
104
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(uint32_t, x1 - 1),
105
+ WINPR_ASSERTING_INT_CAST(uint32_t, y1), 0);
106
+ gdi_SetPixel(hdc, WINPR_ASSERTING_INT_CAST(uint32_t, x1 - 1),
107
+ WINPR_ASSERTING_INT_CAST(uint32_t, y2), 0);
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Draw an ellipse
113
+ * msdn{dd162510}
114
+ *
115
+ * @param hdc device context
116
+ * @param nLeftRect x1
117
+ * @param nTopRect y1
118
+ * @param nRightRect x2
119
+ * @param nBottomRect y2
120
+ *
121
+ * @return nonzero if successful, 0 otherwise
122
+ */
123
+ BOOL gdi_Ellipse(HGDI_DC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
124
+ {
125
+ Ellipse_Bresenham(hdc, nLeftRect, nTopRect, nRightRect, nBottomRect);
126
+ return TRUE;
127
+ }
128
+
129
+ /**
130
+ * Fill a rectangle with the given brush.
131
+ * msdn{dd162719}
132
+ *
133
+ * @param hdc device context
134
+ * @param rect rectangle
135
+ * @param hbr brush
136
+ *
137
+ * @return nonzero if successful, 0 otherwise
138
+ */
139
+
140
+ BOOL gdi_FillRect(HGDI_DC hdc, const HGDI_RECT rect, HGDI_BRUSH hbr)
141
+ {
142
+ UINT32 color = 0;
143
+ UINT32 dstColor = 0;
144
+ BOOL monochrome = FALSE;
145
+ INT32 nXDest = 0;
146
+ INT32 nYDest = 0;
147
+ INT32 nWidth = 0;
148
+ INT32 nHeight = 0;
149
+ const BYTE* srcp = NULL;
150
+ DWORD formatSize = 0;
151
+ gdi_RectToCRgn(rect, &nXDest, &nYDest, &nWidth, &nHeight);
152
+
153
+ if (!hdc || !hbr)
154
+ return FALSE;
155
+
156
+ if (!gdi_ClipCoords(hdc, &nXDest, &nYDest, &nWidth, &nHeight, NULL, NULL))
157
+ return TRUE;
158
+
159
+ switch (hbr->style)
160
+ {
161
+ case GDI_BS_SOLID:
162
+ color = hbr->color;
163
+
164
+ for (INT32 x = 0; x < nWidth; x++)
165
+ {
166
+ BYTE* dstp = gdi_get_bitmap_pointer(hdc, nXDest + x, nYDest);
167
+
168
+ if (dstp)
169
+ FreeRDPWriteColor(dstp, hdc->format, color);
170
+ }
171
+
172
+ srcp = gdi_get_bitmap_pointer(hdc, nXDest, nYDest);
173
+ formatSize = FreeRDPGetBytesPerPixel(hdc->format);
174
+
175
+ for (INT32 y = 1; y < nHeight; y++)
176
+ {
177
+ BYTE* dstp = gdi_get_bitmap_pointer(hdc, nXDest, nYDest + y);
178
+ memcpy(dstp, srcp, 1ull * WINPR_ASSERTING_INT_CAST(size_t, nWidth) * formatSize);
179
+ }
180
+
181
+ break;
182
+
183
+ case GDI_BS_HATCHED:
184
+ case GDI_BS_PATTERN:
185
+ monochrome = (hbr->pattern->format == PIXEL_FORMAT_MONO);
186
+ formatSize = FreeRDPGetBytesPerPixel(hbr->pattern->format);
187
+
188
+ for (INT32 y = 0; y < nHeight; y++)
189
+ {
190
+ for (INT32 x = 0; x < nWidth; x++)
191
+ {
192
+ const size_t yOffset =
193
+ ((1ULL * WINPR_ASSERTING_INT_CAST(size_t, nYDest) +
194
+ WINPR_ASSERTING_INT_CAST(size_t, y)) *
195
+ WINPR_ASSERTING_INT_CAST(size_t, hbr->pattern->width) %
196
+ WINPR_ASSERTING_INT_CAST(size_t, hbr->pattern->height)) *
197
+ formatSize;
198
+ const size_t xOffset = ((1ULL * WINPR_ASSERTING_INT_CAST(size_t, nXDest) +
199
+ WINPR_ASSERTING_INT_CAST(size_t, x)) %
200
+ WINPR_ASSERTING_INT_CAST(size_t, hbr->pattern->width)) *
201
+ formatSize;
202
+ const BYTE* patp = &hbr->pattern->data[yOffset + xOffset];
203
+
204
+ if (monochrome)
205
+ {
206
+ if (*patp == 0)
207
+ dstColor = hdc->bkColor;
208
+ else
209
+ dstColor = hdc->textColor;
210
+ }
211
+ else
212
+ {
213
+ dstColor = FreeRDPReadColor(patp, hbr->pattern->format);
214
+ dstColor =
215
+ FreeRDPConvertColor(dstColor, hbr->pattern->format, hdc->format, NULL);
216
+ }
217
+
218
+ BYTE* dstp = gdi_get_bitmap_pointer(hdc, nXDest + x, nYDest + y);
219
+ if (dstp)
220
+ FreeRDPWriteColor(dstp, hdc->format, dstColor);
221
+ }
222
+ }
223
+
224
+ break;
225
+
226
+ default:
227
+ break;
228
+ }
229
+
230
+ if (!gdi_InvalidateRegion(hdc, nXDest, nYDest, nWidth, nHeight))
231
+ return FALSE;
232
+
233
+ return TRUE;
234
+ }
235
+
236
+ /**
237
+ * Draw a polygon
238
+ * msdn{dd162814}
239
+ * @param hdc device context
240
+ * @param lpPoints array of points
241
+ * @param nCount number of points
242
+ * @return nonzero if successful, 0 otherwise
243
+ */
244
+ BOOL gdi_Polygon(HGDI_DC hdc, GDI_POINT* lpPoints, int nCount)
245
+ {
246
+ WLog_ERR(TAG, "Not implemented!");
247
+ return FALSE;
248
+ }
249
+
250
+ /**
251
+ * Draw a series of closed polygons
252
+ * msdn{dd162818}
253
+ * @param hdc device context
254
+ * @param lpPoints array of series of points
255
+ * @param lpPolyCounts array of number of points in each series
256
+ * @param nCount count of number of points in lpPolyCounts
257
+ * @return nonzero if successful, 0 otherwise
258
+ */
259
+ BOOL gdi_PolyPolygon(HGDI_DC hdc, GDI_POINT* lpPoints, int* lpPolyCounts, int nCount)
260
+ {
261
+ WLog_ERR(TAG, "Not implemented!");
262
+ return FALSE;
263
+ }
264
+
265
+ BOOL gdi_Rectangle(HGDI_DC hdc, INT32 nXDst, INT32 nYDst, INT32 nWidth, INT32 nHeight)
266
+ {
267
+ UINT32 color = 0;
268
+
269
+ if (!gdi_ClipCoords(hdc, &nXDst, &nYDst, &nWidth, &nHeight, NULL, NULL))
270
+ return TRUE;
271
+
272
+ color = hdc->textColor;
273
+
274
+ for (INT32 y = 0; y < nHeight; y++)
275
+ {
276
+ BYTE* dstLeft = gdi_get_bitmap_pointer(hdc, nXDst, nYDst + y);
277
+ BYTE* dstRight = gdi_get_bitmap_pointer(hdc, nXDst + nWidth - 1, nYDst + y);
278
+
279
+ if (dstLeft)
280
+ FreeRDPWriteColor(dstLeft, hdc->format, color);
281
+
282
+ if (dstRight)
283
+ FreeRDPWriteColor(dstRight, hdc->format, color);
284
+ }
285
+
286
+ for (INT32 x = 0; x < nWidth; x++)
287
+ {
288
+ BYTE* dstTop = gdi_get_bitmap_pointer(hdc, nXDst + x, nYDst);
289
+ BYTE* dstBottom = gdi_get_bitmap_pointer(hdc, nXDst + x, nYDst + nHeight - 1);
290
+
291
+ if (dstTop)
292
+ FreeRDPWriteColor(dstTop, hdc->format, color);
293
+
294
+ if (dstBottom)
295
+ FreeRDPWriteColor(dstBottom, hdc->format, color);
296
+ }
297
+
298
+ return FALSE;
299
+ }
local-test-freerdp-full-01/afc-freerdp/libfreerdp/gdi/video.c ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Video Optimized Remoting Virtual Channel Extension for X11
4
+ *
5
+ * Copyright 2017 David Fort <[email protected]>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include "../core/update.h"
21
+
22
+ #include <winpr/assert.h>
23
+
24
+ #include <freerdp/client/geometry.h>
25
+ #include <freerdp/client/video.h>
26
+ #include <freerdp/gdi/gdi.h>
27
+ #include <freerdp/gdi/video.h>
28
+ #include <freerdp/gdi/region.h>
29
+
30
+ void gdi_video_geometry_init(rdpGdi* gdi, GeometryClientContext* geom)
31
+ {
32
+ WINPR_ASSERT(gdi);
33
+ WINPR_ASSERT(geom);
34
+
35
+ gdi->geometry = geom;
36
+
37
+ if (gdi->video)
38
+ {
39
+ VideoClientContext* video = gdi->video;
40
+
41
+ WINPR_ASSERT(video);
42
+ WINPR_ASSERT(video->setGeometry);
43
+ video->setGeometry(video, gdi->geometry);
44
+ }
45
+ }
46
+
47
+ void gdi_video_geometry_uninit(rdpGdi* gdi, GeometryClientContext* geom)
48
+ {
49
+ WINPR_ASSERT(gdi);
50
+ WINPR_ASSERT(geom);
51
+ WINPR_UNUSED(gdi);
52
+ WINPR_UNUSED(geom);
53
+ }
54
+
55
+ static VideoSurface* gdiVideoCreateSurface(VideoClientContext* video, UINT32 x, UINT32 y,
56
+ UINT32 width, UINT32 height)
57
+ {
58
+ return VideoClient_CreateCommonContext(sizeof(VideoSurface), x, y, width, height);
59
+ }
60
+
61
+ static BOOL gdiVideoShowSurface(VideoClientContext* video, const VideoSurface* surface,
62
+ UINT32 destinationWidth, UINT32 destinationHeight)
63
+ {
64
+ BOOL rc = FALSE;
65
+ rdpGdi* gdi = NULL;
66
+ rdpUpdate* update = NULL;
67
+
68
+ WINPR_ASSERT(video);
69
+ WINPR_ASSERT(surface);
70
+
71
+ gdi = (rdpGdi*)video->custom;
72
+ WINPR_ASSERT(gdi);
73
+ WINPR_ASSERT(gdi->context);
74
+
75
+ update = gdi->context->update;
76
+ WINPR_ASSERT(update);
77
+
78
+ if (!update_begin_paint(update))
79
+ goto fail;
80
+
81
+ if ((gdi->width < 0) || (gdi->height < 0))
82
+ goto fail;
83
+ else
84
+ {
85
+ const UINT32 nXSrc = surface->x;
86
+ const UINT32 nYSrc = surface->y;
87
+ const UINT32 nXDst = nXSrc;
88
+ const UINT32 nYDst = nYSrc;
89
+ const UINT32 width = (destinationWidth + surface->x < (UINT32)gdi->width)
90
+ ? destinationWidth
91
+ : (UINT32)gdi->width - surface->x;
92
+ const UINT32 height = (destinationHeight + surface->y < (UINT32)gdi->height)
93
+ ? destinationHeight
94
+ : (UINT32)gdi->height - surface->y;
95
+
96
+ WINPR_ASSERT(gdi->primary_buffer);
97
+ WINPR_ASSERT(gdi->primary);
98
+ WINPR_ASSERT(gdi->primary->hdc);
99
+
100
+ if (!freerdp_image_scale(gdi->primary_buffer, gdi->primary->hdc->format, gdi->stride, nXDst,
101
+ nYDst, width, height, surface->data, surface->format,
102
+ surface->scanline, 0, 0, surface->w, surface->h))
103
+ goto fail;
104
+
105
+ if ((nXDst > INT32_MAX) || (nYDst > INT32_MAX) || (width > INT32_MAX) ||
106
+ (height > INT32_MAX))
107
+ goto fail;
108
+
109
+ gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)width,
110
+ (INT32)height);
111
+ }
112
+
113
+ rc = TRUE;
114
+ fail:
115
+
116
+ if (!update_end_paint(update))
117
+ return FALSE;
118
+
119
+ return rc;
120
+ }
121
+
122
+ static BOOL gdiVideoDeleteSurface(VideoClientContext* video, VideoSurface* surface)
123
+ {
124
+ WINPR_UNUSED(video);
125
+ VideoClient_DestroyCommonContext(surface);
126
+ return TRUE;
127
+ }
128
+
129
+ void gdi_video_control_init(rdpGdi* gdi, VideoClientContext* video)
130
+ {
131
+ WINPR_ASSERT(gdi);
132
+ WINPR_ASSERT(video);
133
+
134
+ gdi->video = video;
135
+ video->custom = gdi;
136
+ video->createSurface = gdiVideoCreateSurface;
137
+ video->showSurface = gdiVideoShowSurface;
138
+ video->deleteSurface = gdiVideoDeleteSurface;
139
+ video->setGeometry(video, gdi->geometry);
140
+ }
141
+
142
+ void gdi_video_control_uninit(rdpGdi* gdi, VideoClientContext* video)
143
+ {
144
+ WINPR_ASSERT(gdi);
145
+ gdi->video = NULL;
146
+ }
147
+
148
+ static void gdi_video_timer(void* context, const TimerEventArgs* timer)
149
+ {
150
+ rdpContext* ctx = (rdpContext*)context;
151
+ rdpGdi* gdi = NULL;
152
+
153
+ WINPR_ASSERT(ctx);
154
+ WINPR_ASSERT(timer);
155
+
156
+ gdi = ctx->gdi;
157
+
158
+ if (gdi && gdi->video)
159
+ gdi->video->timer(gdi->video, timer->now);
160
+ }
161
+
162
+ void gdi_video_data_init(rdpGdi* gdi, VideoClientContext* video)
163
+ {
164
+ WINPR_ASSERT(gdi);
165
+ WINPR_ASSERT(gdi->context);
166
+ PubSub_SubscribeTimer(gdi->context->pubSub, gdi_video_timer);
167
+ }
168
+
169
+ void gdi_video_data_uninit(rdpGdi* gdi, VideoClientContext* context)
170
+ {
171
+ WINPR_ASSERT(gdi);
172
+ WINPR_ASSERT(gdi->context);
173
+ PubSub_UnsubscribeTimer(gdi->context->pubSub, gdi_video_timer);
174
+ }
175
+
176
+ VideoSurface* VideoClient_CreateCommonContext(size_t size, UINT32 x, UINT32 y, UINT32 w, UINT32 h)
177
+ {
178
+ VideoSurface* ret = NULL;
179
+
180
+ WINPR_ASSERT(size >= sizeof(VideoSurface));
181
+
182
+ ret = calloc(1, size);
183
+ if (!ret)
184
+ return NULL;
185
+
186
+ ret->format = PIXEL_FORMAT_BGRX32;
187
+ ret->x = x;
188
+ ret->y = y;
189
+ ret->w = w;
190
+ ret->h = h;
191
+ ret->alignedWidth = ret->w + 32 - ret->w % 16;
192
+ ret->alignedHeight = ret->h + 32 - ret->h % 16;
193
+
194
+ ret->scanline = ret->alignedWidth * FreeRDPGetBytesPerPixel(ret->format);
195
+ ret->data = winpr_aligned_malloc(1ull * ret->scanline * ret->alignedHeight, 64);
196
+ if (!ret->data)
197
+ goto fail;
198
+ return ret;
199
+ fail:
200
+ VideoClient_DestroyCommonContext(ret);
201
+ return NULL;
202
+ }
203
+
204
+ void VideoClient_DestroyCommonContext(VideoSurface* surface)
205
+ {
206
+ if (!surface)
207
+ return;
208
+ winpr_aligned_free(surface->data);
209
+ free(surface);
210
+ }