FreeRDP
Loading...
Searching...
No Matches
gdi/video.c
1
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
30void gdi_video_geometry_init(rdpGdi* gdi, GeometryClientContext* geom)
31{
32 WINPR_ASSERT(gdi);
33 WINPR_ASSERT(geom);
34
35 /* The geometry channel allows multiple instances, they all share the context of the plugin */
36 if (gdi->geometry != geom)
37 gdi->geometryInstances = 0;
38 gdi->geometryInstances++;
39 gdi->geometry = geom;
40
41 if (gdi->video)
42 {
43 VideoClientContext* video = gdi->video;
44
45 WINPR_ASSERT(video);
46 WINPR_ASSERT(video->setGeometry);
47 video->setGeometry(video, gdi->geometry);
48 }
49}
50
51void gdi_video_geometry_uninit(rdpGdi* gdi, GeometryClientContext* geom)
52{
53 WINPR_ASSERT(gdi);
54 WINPR_ASSERT(geom);
55
56 if (gdi->geometry != geom)
57 return;
58
59 if (gdi->geometryInstances > 0)
60 gdi->geometryInstances--;
61 if (gdi->geometryInstances > 0)
62 return;
63
64 /* The context is owned by the geometry plugin and freed with it (e.g. on a redirect or
65 * reconnect, which the gdi survives), so drop our reference and the one of the video channel.
66 */
67 gdi->geometry = nullptr;
68
69 if (gdi->video)
70 {
71 VideoClientContext* video = gdi->video;
72
73 WINPR_ASSERT(video);
74 WINPR_ASSERT(video->setGeometry);
75 video->setGeometry(video, nullptr);
76 }
77}
78
79static VideoSurface* gdiVideoCreateSurface(WINPR_ATTR_UNUSED VideoClientContext* video, UINT32 x,
80 UINT32 y, UINT32 width, UINT32 height)
81{
82 return VideoClient_CreateCommonContext(sizeof(VideoSurface), x, y, width, height);
83}
84
85static BOOL gdiVideoShowSurface(VideoClientContext* video, const VideoSurface* surface,
86 UINT32 destinationWidth, UINT32 destinationHeight)
87{
88 BOOL rc = FALSE;
89 rdpGdi* gdi = nullptr;
90 rdpUpdate* update = nullptr;
91
92 WINPR_ASSERT(video);
93 WINPR_ASSERT(surface);
94
95 gdi = (rdpGdi*)video->custom;
96 WINPR_ASSERT(gdi);
97 WINPR_ASSERT(gdi->context);
98
99 update = gdi->context->update;
100 WINPR_ASSERT(update);
101
102 if (!update_begin_paint(update))
103 goto fail;
104
105 if ((gdi->width < 0) || (gdi->height < 0))
106 goto fail;
107
108 /* the surface position is server controlled, it must start inside the desktop
109 * or the clipping below (and the destination offset) would wrap around */
110 if ((surface->x >= (UINT32)gdi->width) || (surface->y >= (UINT32)gdi->height))
111 goto fail;
112
113 {
114 const UINT32 nXSrc = surface->x;
115 const UINT32 nYSrc = surface->y;
116 const UINT32 nXDst = nXSrc;
117 const UINT32 nYDst = nYSrc;
118 const UINT32 width = MIN(destinationWidth, (UINT32)gdi->width - surface->x);
119 const UINT32 height = MIN(destinationHeight, (UINT32)gdi->height - surface->y);
120
121 WINPR_ASSERT(gdi->primary_buffer);
122 WINPR_ASSERT(gdi->primary);
123 WINPR_ASSERT(gdi->primary->hdc);
124
125 if (!freerdp_image_scale(gdi->primary_buffer, gdi->primary->hdc->format, gdi->stride, nXDst,
126 nYDst, width, height, surface->data, surface->format,
127 surface->scanline, 0, 0, surface->w, surface->h))
128 goto fail;
129
130 if ((nXDst > INT32_MAX) || (nYDst > INT32_MAX) || (width > INT32_MAX) ||
131 (height > INT32_MAX))
132 goto fail;
133
134 rc = gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)width,
135 (INT32)height);
136 }
137fail:
138
139 if (!update_end_paint(update))
140 return FALSE;
141
142 return rc;
143}
144
145static BOOL gdiVideoDeleteSurface(VideoClientContext* video, VideoSurface* surface)
146{
147 WINPR_UNUSED(video);
148 VideoClient_DestroyCommonContext(surface);
149 return TRUE;
150}
151
152void gdi_video_control_init(rdpGdi* gdi, VideoClientContext* video)
153{
154 WINPR_ASSERT(gdi);
155 WINPR_ASSERT(video);
156
157 gdi->video = video;
158 video->custom = gdi;
159 video->createSurface = gdiVideoCreateSurface;
160 video->showSurface = gdiVideoShowSurface;
161 video->deleteSurface = gdiVideoDeleteSurface;
162 video->setGeometry(video, gdi->geometry);
163}
164
165void gdi_video_control_uninit(rdpGdi* gdi, WINPR_ATTR_UNUSED VideoClientContext* video)
166{
167 WINPR_ASSERT(gdi);
168 gdi->video = nullptr;
169}
170
171void gdi_video_data_init(WINPR_ATTR_UNUSED rdpGdi* gdi, WINPR_ATTR_UNUSED VideoClientContext* video)
172{
173 WINPR_ASSERT(gdi);
174 WINPR_ASSERT(gdi->context);
175}
176
177void gdi_video_data_uninit(WINPR_ATTR_UNUSED rdpGdi* gdi,
178 WINPR_ATTR_UNUSED VideoClientContext* context)
179{
180 WINPR_ASSERT(gdi);
181 WINPR_ASSERT(gdi->context);
182}
183
184VideoSurface* VideoClient_CreateCommonContext(size_t size, UINT32 x, UINT32 y, UINT32 w, UINT32 h)
185{
186 if ((w == 0) || (h == 0))
187 return nullptr;
188
189 WINPR_ASSERT(size >= sizeof(VideoSurface));
190
191 const UINT32 alignedWidth = w + 32 - w % 16;
192 const UINT32 alignedHeight = h + 32 - h % 16;
193 if ((alignedWidth > UINT16_MAX) || (alignedHeight > UINT16_MAX))
194 return nullptr;
195
196 VideoSurface* ret = calloc(1, size);
197 if (!ret)
198 return nullptr;
199
200 ret->format = PIXEL_FORMAT_BGRX32;
201 ret->x = x;
202 ret->y = y;
203 ret->w = w;
204 ret->h = h;
205 ret->alignedWidth = alignedWidth;
206 ret->alignedHeight = alignedHeight;
207
208 ret->scanline = ret->alignedWidth * FreeRDPGetBytesPerPixel(ret->format);
209 ret->data = winpr_aligned_calloc(ret->scanline, ret->alignedHeight, 64);
210 if (!ret->data)
211 goto fail;
212 return ret;
213fail:
214 VideoClient_DestroyCommonContext(ret);
215 return nullptr;
216}
217
218void VideoClient_DestroyCommonContext(VideoSurface* surface)
219{
220 if (!surface)
221 return;
222 winpr_aligned_free(surface->data);
223 free(surface);
224}
an implementation of surface used by the video channel