/src/FreeRDP/libfreerdp/gdi/gfx.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * FreeRDP: A Remote Desktop Protocol Implementation  | 
3  |  |  * GDI Graphics Pipeline  | 
4  |  |  *  | 
5  |  |  * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>  | 
6  |  |  * Copyright 2016 Armin Novak <armin.novak@thincast.com>  | 
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 <freerdp/api.h>  | 
27  |  | #include <freerdp/log.h>  | 
28  |  | #include <freerdp/gdi/gfx.h>  | 
29  |  | #include <freerdp/gdi/region.h>  | 
30  |  | #include <freerdp/utils/gfx.h>  | 
31  |  | #include <math.h>  | 
32  |  |  | 
33  |  | #define TAG FREERDP_TAG("gdi") | 
34  |  |  | 
35  |  | static BOOL is_rect_valid(const RECTANGLE_16* rect, size_t width, size_t height)  | 
36  | 0  | { | 
37  | 0  |   if (!rect)  | 
38  | 0  |     return FALSE;  | 
39  | 0  |   if ((rect->left > rect->right) || (rect->right > width))  | 
40  | 0  |     return FALSE;  | 
41  | 0  |   if ((rect->top > rect->bottom) || (rect->bottom > height))  | 
42  | 0  |     return FALSE;  | 
43  | 0  |   return TRUE;  | 
44  | 0  | }  | 
45  |  |  | 
46  |  | static BOOL is_within_surface(const gdiGfxSurface* surface, const RDPGFX_SURFACE_COMMAND* cmd)  | 
47  | 0  | { | 
48  | 0  |   RECTANGLE_16 rect;  | 
49  | 0  |   if (!surface || !cmd)  | 
50  | 0  |     return FALSE;  | 
51  | 0  |   rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);  | 
52  | 0  |   rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);  | 
53  | 0  |   rect.right = (UINT16)MIN(UINT16_MAX, cmd->right);  | 
54  | 0  |   rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);  | 
55  | 0  |   if (!is_rect_valid(&rect, surface->width, surface->height))  | 
56  | 0  |   { | 
57  | 0  |     WLog_ERR(TAG,  | 
58  | 0  |              "Command rect %" PRIu32 "x%" PRIu32 "-%" PRIu32 "x%" PRIu32  | 
59  | 0  |              " not within bounds of %" PRIu32 "x%" PRIu32,  | 
60  | 0  |              rect.left, rect.top, cmd->width, cmd->height, surface->width, surface->height);  | 
61  | 0  |     return FALSE;  | 
62  | 0  |   }  | 
63  |  |  | 
64  | 0  |   return TRUE;  | 
65  | 0  | }  | 
66  |  |  | 
67  |  | static DWORD gfx_align_scanline(DWORD widthInBytes, DWORD alignment)  | 
68  | 0  | { | 
69  | 0  |   const UINT32 align = alignment;  | 
70  | 0  |   const UINT32 pad = align - (widthInBytes % alignment);  | 
71  | 0  |   UINT32 scanline = widthInBytes;  | 
72  |  | 
  | 
73  | 0  |   if (align != pad)  | 
74  | 0  |     scanline += pad;  | 
75  |  | 
  | 
76  | 0  |   return scanline;  | 
77  | 0  | }  | 
78  |  |  | 
79  |  | /**  | 
80  |  |  * Function description  | 
81  |  |  *  | 
82  |  |  * @return 0 on success, otherwise a Win32 error code  | 
83  |  |  */  | 
84  |  | static UINT gdi_ResetGraphics(RdpgfxClientContext* context,  | 
85  |  |                               const RDPGFX_RESET_GRAPHICS_PDU* resetGraphics)  | 
86  | 0  | { | 
87  | 0  |   UINT rc = ERROR_INTERNAL_ERROR;  | 
88  | 0  |   UINT16 count = 0;  | 
89  | 0  |   UINT32 DesktopWidth = 0;  | 
90  | 0  |   UINT32 DesktopHeight = 0;  | 
91  | 0  |   UINT16* pSurfaceIds = NULL;  | 
92  | 0  |   rdpGdi* gdi = NULL;  | 
93  | 0  |   rdpUpdate* update = NULL;  | 
94  | 0  |   rdpSettings* settings = NULL;  | 
95  |  | 
  | 
96  | 0  |   WINPR_ASSERT(context);  | 
97  | 0  |   WINPR_ASSERT(resetGraphics);  | 
98  |  |  | 
99  | 0  |   gdi = (rdpGdi*)context->custom;  | 
100  | 0  |   WINPR_ASSERT(gdi);  | 
101  |  |  | 
102  | 0  |   update = gdi->context->update;  | 
103  | 0  |   WINPR_ASSERT(update);  | 
104  |  |  | 
105  | 0  |   settings = gdi->context->settings;  | 
106  | 0  |   WINPR_ASSERT(settings);  | 
107  | 0  |   EnterCriticalSection(&context->mux);  | 
108  | 0  |   DesktopWidth = resetGraphics->width;  | 
109  | 0  |   DesktopHeight = resetGraphics->height;  | 
110  |  | 
  | 
111  | 0  |   if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopWidth, DesktopWidth))  | 
112  | 0  |     goto fail;  | 
113  | 0  |   if (!freerdp_settings_set_uint32(settings, FreeRDP_DesktopHeight, DesktopHeight))  | 
114  | 0  |     goto fail;  | 
115  |  |  | 
116  | 0  |   if (update)  | 
117  | 0  |   { | 
118  | 0  |     WINPR_ASSERT(update->DesktopResize);  | 
119  | 0  |     update->DesktopResize(gdi->context);  | 
120  | 0  |   }  | 
121  |  |  | 
122  | 0  |   WINPR_ASSERT(context->GetSurfaceIds);  | 
123  | 0  |   context->GetSurfaceIds(context, &pSurfaceIds, &count);  | 
124  |  | 
  | 
125  | 0  |   for (UINT32 index = 0; index < count; index++)  | 
126  | 0  |   { | 
127  | 0  |     WINPR_ASSERT(context->GetSurfaceData);  | 
128  | 0  |     gdiGfxSurface* surface =  | 
129  | 0  |         (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);  | 
130  |  | 
  | 
131  | 0  |     if (!surface)  | 
132  | 0  |       continue;  | 
133  |  |  | 
134  | 0  |     memset(surface->data, 0xFF, (size_t)surface->scanline * surface->height);  | 
135  | 0  |     region16_clear(&surface->invalidRegion);  | 
136  | 0  |   }  | 
137  |  |  | 
138  | 0  |   free(pSurfaceIds);  | 
139  |  | 
  | 
140  | 0  |   if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))  | 
141  | 0  |   { | 
142  | 0  |     const UINT32 width = (UINT32)MAX(0, gdi->width);  | 
143  | 0  |     const UINT32 height = (UINT32)MAX(0, gdi->height);  | 
144  |  | 
  | 
145  | 0  |     if (!freerdp_client_codecs_reset(  | 
146  | 0  |             context->codecs, freerdp_settings_get_codecs_flags(settings), width, height))  | 
147  | 0  |     { | 
148  | 0  |       goto fail;  | 
149  | 0  |     }  | 
150  | 0  |     if (!freerdp_client_codecs_reset(  | 
151  | 0  |             gdi->context->codecs, freerdp_settings_get_codecs_flags(settings), width, height))  | 
152  | 0  |     { | 
153  | 0  |       goto fail;  | 
154  | 0  |     }  | 
155  | 0  |   }  | 
156  |  |  | 
157  | 0  |   rc = CHANNEL_RC_OK;  | 
158  | 0  | fail:  | 
159  | 0  |   LeaveCriticalSection(&context->mux);  | 
160  | 0  |   return rc;  | 
161  | 0  | }  | 
162  |  |  | 
163  |  | static UINT gdi_OutputUpdate(rdpGdi* gdi, gdiGfxSurface* surface)  | 
164  | 0  | { | 
165  | 0  |   UINT rc = ERROR_INTERNAL_ERROR;  | 
166  | 0  |   UINT32 surfaceX = 0;  | 
167  | 0  |   UINT32 surfaceY = 0;  | 
168  | 0  |   RECTANGLE_16 surfaceRect;  | 
169  | 0  |   const RECTANGLE_16* rects = NULL;  | 
170  | 0  |   UINT32 nbRects = 0;  | 
171  | 0  |   rdpUpdate* update = NULL;  | 
172  |  | 
  | 
173  | 0  |   WINPR_ASSERT(gdi);  | 
174  | 0  |   WINPR_ASSERT(gdi->context);  | 
175  | 0  |   WINPR_ASSERT(surface);  | 
176  |  |  | 
177  | 0  |   update = gdi->context->update;  | 
178  | 0  |   WINPR_ASSERT(update);  | 
179  |  |  | 
180  | 0  |   if (gdi->suppressOutput)  | 
181  | 0  |     return CHANNEL_RC_OK;  | 
182  |  |  | 
183  | 0  |   surfaceX = surface->outputOriginX;  | 
184  | 0  |   surfaceY = surface->outputOriginY;  | 
185  | 0  |   surfaceRect.left = 0;  | 
186  | 0  |   surfaceRect.top = 0;  | 
187  | 0  |   surfaceRect.right = (UINT16)MIN(UINT16_MAX, surface->mappedWidth);  | 
188  | 0  |   surfaceRect.bottom = (UINT16)MIN(UINT16_MAX, surface->mappedHeight);  | 
189  | 0  |   region16_intersect_rect(&(surface->invalidRegion), &(surface->invalidRegion), &surfaceRect);  | 
190  | 0  |   const double sx = surface->outputTargetWidth / (double)surface->mappedWidth;  | 
191  | 0  |   const double sy = surface->outputTargetHeight / (double)surface->mappedHeight;  | 
192  |  | 
  | 
193  | 0  |   if (!(rects = region16_rects(&surface->invalidRegion, &nbRects)) || !nbRects)  | 
194  | 0  |     return CHANNEL_RC_OK;  | 
195  |  |  | 
196  | 0  |   if (!update_begin_paint(update))  | 
197  | 0  |     goto fail;  | 
198  |  |  | 
199  | 0  |   for (UINT32 i = 0; i < nbRects; i++)  | 
200  | 0  |   { | 
201  | 0  |     const UINT32 nXSrc = rects[i].left;  | 
202  | 0  |     const UINT32 nYSrc = rects[i].top;  | 
203  | 0  |     const UINT32 nXDst = (UINT32)MIN(surfaceX + nXSrc * sx, gdi->width - 1);  | 
204  | 0  |     const UINT32 nYDst = (UINT32)MIN(surfaceY + nYSrc * sy, gdi->height - 1);  | 
205  | 0  |     const UINT32 swidth = rects[i].right - rects[i].left;  | 
206  | 0  |     const UINT32 sheight = rects[i].bottom - rects[i].top;  | 
207  | 0  |     const UINT32 dwidth = MIN((UINT32)(swidth * sx), (UINT32)gdi->width - nXDst);  | 
208  | 0  |     const UINT32 dheight = MIN((UINT32)(sheight * sy), (UINT32)gdi->height - nYDst);  | 
209  |  | 
  | 
210  | 0  |     if (!freerdp_image_scale(gdi->primary_buffer, gdi->dstFormat, gdi->stride, nXDst, nYDst,  | 
211  | 0  |                              dwidth, dheight, surface->data, surface->format, surface->scanline,  | 
212  | 0  |                              nXSrc, nYSrc, swidth, sheight))  | 
213  | 0  |     { | 
214  | 0  |       rc = CHANNEL_RC_NULL_DATA;  | 
215  | 0  |       goto fail;  | 
216  | 0  |     }  | 
217  |  |  | 
218  | 0  |     gdi_InvalidateRegion(gdi->primary->hdc, (INT32)nXDst, (INT32)nYDst, (INT32)dwidth,  | 
219  | 0  |                          (INT32)dheight);  | 
220  | 0  |   }  | 
221  |  |  | 
222  | 0  |   rc = CHANNEL_RC_OK;  | 
223  | 0  | fail:  | 
224  |  | 
  | 
225  | 0  |   if (!update_end_paint(update))  | 
226  | 0  |     rc = ERROR_INTERNAL_ERROR;  | 
227  |  | 
  | 
228  | 0  |   region16_clear(&(surface->invalidRegion));  | 
229  | 0  |   return rc;  | 
230  | 0  | }  | 
231  |  |  | 
232  |  | static UINT gdi_WindowUpdate(RdpgfxClientContext* context, gdiGfxSurface* surface)  | 
233  | 0  | { | 
234  | 0  |   WINPR_ASSERT(context);  | 
235  | 0  |   WINPR_ASSERT(surface);  | 
236  | 0  |   return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateWindowFromSurface, context, surface);  | 
237  | 0  | }  | 
238  |  |  | 
239  |  | static UINT gdi_UpdateSurfaces(RdpgfxClientContext* context)  | 
240  | 0  | { | 
241  | 0  |   UINT16 count = 0;  | 
242  | 0  |   UINT status = ERROR_INTERNAL_ERROR;  | 
243  | 0  |   UINT16* pSurfaceIds = NULL;  | 
244  | 0  |   rdpGdi* gdi = NULL;  | 
245  |  | 
  | 
246  | 0  |   WINPR_ASSERT(context);  | 
247  |  |  | 
248  | 0  |   gdi = (rdpGdi*)context->custom;  | 
249  | 0  |   WINPR_ASSERT(gdi);  | 
250  |  |  | 
251  | 0  |   EnterCriticalSection(&context->mux);  | 
252  |  | 
  | 
253  | 0  |   WINPR_ASSERT(context->GetSurfaceIds);  | 
254  | 0  |   context->GetSurfaceIds(context, &pSurfaceIds, &count);  | 
255  | 0  |   status = CHANNEL_RC_OK;  | 
256  |  | 
  | 
257  | 0  |   for (UINT32 index = 0; index < count; index++)  | 
258  | 0  |   { | 
259  | 0  |     WINPR_ASSERT(context->GetSurfaceData);  | 
260  | 0  |     gdiGfxSurface* surface =  | 
261  | 0  |         (gdiGfxSurface*)context->GetSurfaceData(context, pSurfaceIds[index]);  | 
262  |  | 
  | 
263  | 0  |     if (!surface)  | 
264  | 0  |       continue;  | 
265  |  |  | 
266  |  |     /* Already handled in UpdateSurfaceArea callbacks */  | 
267  | 0  |     if (context->UpdateSurfaceArea)  | 
268  | 0  |     { | 
269  | 0  |       if (surface->handleInUpdateSurfaceArea)  | 
270  | 0  |         continue;  | 
271  | 0  |     }  | 
272  |  |  | 
273  | 0  |     if (surface->outputMapped)  | 
274  | 0  |       status = gdi_OutputUpdate(gdi, surface);  | 
275  | 0  |     else if (surface->windowMapped)  | 
276  | 0  |       status = gdi_WindowUpdate(context, surface);  | 
277  |  | 
  | 
278  | 0  |     if (status != CHANNEL_RC_OK)  | 
279  | 0  |       break;  | 
280  | 0  |   }  | 
281  |  |  | 
282  | 0  |   free(pSurfaceIds);  | 
283  | 0  |   LeaveCriticalSection(&context->mux);  | 
284  | 0  |   return status;  | 
285  | 0  | }  | 
286  |  |  | 
287  |  | /**  | 
288  |  |  * Function description  | 
289  |  |  *  | 
290  |  |  * @return 0 on success, otherwise a Win32 error code  | 
291  |  |  */  | 
292  |  | static UINT gdi_StartFrame(RdpgfxClientContext* context, const RDPGFX_START_FRAME_PDU* startFrame)  | 
293  | 0  | { | 
294  | 0  |   rdpGdi* gdi = NULL;  | 
295  |  | 
  | 
296  | 0  |   WINPR_ASSERT(context);  | 
297  | 0  |   WINPR_ASSERT(startFrame);  | 
298  |  |  | 
299  | 0  |   gdi = (rdpGdi*)context->custom;  | 
300  | 0  |   WINPR_ASSERT(gdi);  | 
301  | 0  |   gdi->inGfxFrame = TRUE;  | 
302  | 0  |   gdi->frameId = startFrame->frameId;  | 
303  | 0  |   return CHANNEL_RC_OK;  | 
304  | 0  | }  | 
305  |  |  | 
306  |  | static UINT gdi_call_update_surfaces(RdpgfxClientContext* context)  | 
307  | 0  | { | 
308  | 0  |   WINPR_ASSERT(context);  | 
309  | 0  |   return IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaces, context);  | 
310  | 0  | }  | 
311  |  |  | 
312  |  | /**  | 
313  |  |  * Function description  | 
314  |  |  *  | 
315  |  |  * @return 0 on success, otherwise a Win32 error code  | 
316  |  |  */  | 
317  |  | static UINT gdi_EndFrame(RdpgfxClientContext* context, const RDPGFX_END_FRAME_PDU* endFrame)  | 
318  | 0  | { | 
319  | 0  |   WINPR_ASSERT(context);  | 
320  | 0  |   WINPR_ASSERT(endFrame);  | 
321  |  |  | 
322  | 0  |   rdpGdi* gdi = (rdpGdi*)context->custom;  | 
323  | 0  |   WINPR_ASSERT(gdi);  | 
324  | 0  |   const UINT status = gdi_call_update_surfaces(context);  | 
325  | 0  |   gdi->inGfxFrame = FALSE;  | 
326  | 0  |   return status;  | 
327  | 0  | }  | 
328  |  |  | 
329  |  | static UINT gdi_interFrameUpdate(rdpGdi* gdi, RdpgfxClientContext* context)  | 
330  | 0  | { | 
331  | 0  |   WINPR_ASSERT(gdi);  | 
332  | 0  |   UINT status = CHANNEL_RC_OK;  | 
333  | 0  |   if (!gdi->inGfxFrame)  | 
334  | 0  |     status = gdi_call_update_surfaces(context);  | 
335  | 0  |   return status;  | 
336  | 0  | }  | 
337  |  |  | 
338  |  | /**  | 
339  |  |  * Function description  | 
340  |  |  *  | 
341  |  |  * @return 0 on success, otherwise a Win32 error code  | 
342  |  |  */  | 
343  |  | static UINT gdi_SurfaceCommand_Uncompressed(rdpGdi* gdi, RdpgfxClientContext* context,  | 
344  |  |                                             const RDPGFX_SURFACE_COMMAND* cmd)  | 
345  | 0  | { | 
346  | 0  |   UINT status = CHANNEL_RC_OK;  | 
347  | 0  |   gdiGfxSurface* surface = NULL;  | 
348  | 0  |   RECTANGLE_16 invalidRect;  | 
349  | 0  |   DWORD bpp = 0;  | 
350  | 0  |   size_t size = 0;  | 
351  | 0  |   WINPR_ASSERT(gdi);  | 
352  | 0  |   WINPR_ASSERT(context);  | 
353  | 0  |   WINPR_ASSERT(cmd);  | 
354  |  |  | 
355  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
356  | 0  |   surface =  | 
357  | 0  |       (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));  | 
358  |  | 
  | 
359  | 0  |   if (!surface)  | 
360  | 0  |   { | 
361  | 0  |     WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);  | 
362  | 0  |     return ERROR_NOT_FOUND;  | 
363  | 0  |   }  | 
364  |  |  | 
365  | 0  |   if (!is_within_surface(surface, cmd))  | 
366  | 0  |     return ERROR_INVALID_DATA;  | 
367  |  |  | 
368  | 0  |   bpp = FreeRDPGetBytesPerPixel(cmd->format);  | 
369  | 0  |   size = 1ull * bpp * cmd->width * cmd->height;  | 
370  | 0  |   if (cmd->length < size)  | 
371  | 0  |   { | 
372  | 0  |     WLog_ERR(TAG, "Not enough data, got %" PRIu32 ", expected %" PRIuz, cmd->length, size);  | 
373  | 0  |     return ERROR_INVALID_DATA;  | 
374  | 0  |   }  | 
375  |  |  | 
376  | 0  |   if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline, cmd->left,  | 
377  | 0  |                                      cmd->top, cmd->width, cmd->height, cmd->data, cmd->format, 0,  | 
378  | 0  |                                      0, 0, NULL, FREERDP_FLIP_NONE))  | 
379  | 0  |     return ERROR_INTERNAL_ERROR;  | 
380  |  |  | 
381  | 0  |   invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);  | 
382  | 0  |   invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);  | 
383  | 0  |   invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);  | 
384  | 0  |   invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);  | 
385  | 0  |   region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);  | 
386  | 0  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,  | 
387  | 0  |                         &invalidRect);  | 
388  |  | 
  | 
389  | 0  |   if (status != CHANNEL_RC_OK)  | 
390  | 0  |     goto fail;  | 
391  |  |  | 
392  | 0  |   status = gdi_interFrameUpdate(gdi, context);  | 
393  |  | 
  | 
394  | 0  | fail:  | 
395  | 0  |   return status;  | 
396  | 0  | }  | 
397  |  |  | 
398  |  | /**  | 
399  |  |  * Function description  | 
400  |  |  *  | 
401  |  |  * @return 0 on success, otherwise a Win32 error code  | 
402  |  |  */  | 
403  |  | static UINT gdi_SurfaceCommand_RemoteFX(rdpGdi* gdi, RdpgfxClientContext* context,  | 
404  |  |                                         const RDPGFX_SURFACE_COMMAND* cmd)  | 
405  | 0  | { | 
406  | 0  |   UINT status = ERROR_INTERNAL_ERROR;  | 
407  | 0  |   gdiGfxSurface* surface = NULL;  | 
408  | 0  |   REGION16 invalidRegion;  | 
409  | 0  |   const RECTANGLE_16* rects = NULL;  | 
410  | 0  |   UINT32 nrRects = 0;  | 
411  | 0  |   WINPR_ASSERT(gdi);  | 
412  | 0  |   WINPR_ASSERT(context);  | 
413  | 0  |   WINPR_ASSERT(cmd);  | 
414  |  |  | 
415  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
416  | 0  |   surface =  | 
417  | 0  |       (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));  | 
418  |  | 
  | 
419  | 0  |   if (!surface)  | 
420  | 0  |   { | 
421  | 0  |     WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);  | 
422  | 0  |     return ERROR_NOT_FOUND;  | 
423  | 0  |   }  | 
424  |  |  | 
425  | 0  |   WINPR_ASSERT(surface->codecs);  | 
426  | 0  |   rfx_context_set_pixel_format(surface->codecs->rfx, cmd->format);  | 
427  | 0  |   region16_init(&invalidRegion);  | 
428  |  | 
  | 
429  | 0  |   if (!rfx_process_message(surface->codecs->rfx, cmd->data, cmd->length, cmd->left, cmd->top,  | 
430  | 0  |                            surface->data, surface->format, surface->scanline, surface->height,  | 
431  | 0  |                            &invalidRegion))  | 
432  | 0  |   { | 
433  | 0  |     WLog_ERR(TAG, "Failed to process RemoteFX message");  | 
434  | 0  |     goto fail;  | 
435  | 0  |   }  | 
436  |  |  | 
437  | 0  |   rects = region16_rects(&invalidRegion, &nrRects);  | 
438  | 0  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,  | 
439  | 0  |                         nrRects, rects);  | 
440  |  | 
  | 
441  | 0  |   if (status != CHANNEL_RC_OK)  | 
442  | 0  |     goto fail;  | 
443  |  |  | 
444  | 0  |   for (UINT32 x = 0; x < nrRects; x++)  | 
445  | 0  |     region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]);  | 
446  |  | 
  | 
447  | 0  |   status = gdi_interFrameUpdate(gdi, context);  | 
448  |  | 
  | 
449  | 0  | fail:  | 
450  | 0  |   region16_uninit(&invalidRegion);  | 
451  | 0  |   return status;  | 
452  | 0  | }  | 
453  |  |  | 
454  |  | /**  | 
455  |  |  * Function description  | 
456  |  |  *  | 
457  |  |  * @return 0 on success, otherwise a Win32 error code  | 
458  |  |  */  | 
459  |  | static UINT gdi_SurfaceCommand_ClearCodec(rdpGdi* gdi, RdpgfxClientContext* context,  | 
460  |  |                                           const RDPGFX_SURFACE_COMMAND* cmd)  | 
461  | 0  | { | 
462  | 0  |   INT32 rc = 0;  | 
463  | 0  |   UINT status = CHANNEL_RC_OK;  | 
464  | 0  |   gdiGfxSurface* surface = NULL;  | 
465  | 0  |   RECTANGLE_16 invalidRect;  | 
466  | 0  |   WINPR_ASSERT(gdi);  | 
467  | 0  |   WINPR_ASSERT(context);  | 
468  | 0  |   WINPR_ASSERT(cmd);  | 
469  |  |  | 
470  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
471  | 0  |   surface =  | 
472  | 0  |       (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));  | 
473  |  | 
  | 
474  | 0  |   if (!surface)  | 
475  | 0  |   { | 
476  | 0  |     WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);  | 
477  | 0  |     return ERROR_NOT_FOUND;  | 
478  | 0  |   }  | 
479  |  |  | 
480  | 0  |   WINPR_ASSERT(surface->codecs);  | 
481  | 0  |   rc = clear_decompress(surface->codecs->clear, cmd->data, cmd->length, cmd->width, cmd->height,  | 
482  | 0  |                         surface->data, surface->format, surface->scanline, cmd->left, cmd->top,  | 
483  | 0  |                         surface->width, surface->height, &gdi->palette);  | 
484  |  | 
  | 
485  | 0  |   if (rc < 0)  | 
486  | 0  |   { | 
487  | 0  |     WLog_ERR(TAG, "clear_decompress failure: %" PRId32 "", rc);  | 
488  | 0  |     return ERROR_INTERNAL_ERROR;  | 
489  | 0  |   }  | 
490  |  |  | 
491  | 0  |   invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);  | 
492  | 0  |   invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);  | 
493  | 0  |   invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);  | 
494  | 0  |   invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);  | 
495  | 0  |   region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);  | 
496  | 0  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,  | 
497  | 0  |                         &invalidRect);  | 
498  |  | 
  | 
499  | 0  |   if (status != CHANNEL_RC_OK)  | 
500  | 0  |     goto fail;  | 
501  |  |  | 
502  | 0  |   status = gdi_interFrameUpdate(gdi, context);  | 
503  |  | 
  | 
504  | 0  | fail:  | 
505  | 0  |   return status;  | 
506  | 0  | }  | 
507  |  |  | 
508  |  | /**  | 
509  |  |  * Function description  | 
510  |  |  *  | 
511  |  |  * @return 0 on success, otherwise a Win32 error code  | 
512  |  |  */  | 
513  |  | static UINT gdi_SurfaceCommand_Planar(rdpGdi* gdi, RdpgfxClientContext* context,  | 
514  |  |                                       const RDPGFX_SURFACE_COMMAND* cmd)  | 
515  | 0  | { | 
516  | 0  |   UINT status = CHANNEL_RC_OK;  | 
517  | 0  |   BYTE* DstData = NULL;  | 
518  | 0  |   gdiGfxSurface* surface = NULL;  | 
519  | 0  |   RECTANGLE_16 invalidRect;  | 
520  | 0  |   WINPR_ASSERT(gdi);  | 
521  | 0  |   WINPR_ASSERT(context);  | 
522  | 0  |   WINPR_ASSERT(cmd);  | 
523  |  |  | 
524  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
525  | 0  |   surface =  | 
526  | 0  |       (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));  | 
527  |  | 
  | 
528  | 0  |   if (!surface)  | 
529  | 0  |   { | 
530  | 0  |     WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);  | 
531  | 0  |     return ERROR_NOT_FOUND;  | 
532  | 0  |   }  | 
533  |  |  | 
534  | 0  |   DstData = surface->data;  | 
535  |  | 
  | 
536  | 0  |   if (!is_within_surface(surface, cmd))  | 
537  | 0  |     return ERROR_INVALID_DATA;  | 
538  |  |  | 
539  | 0  |   if (!planar_decompress(surface->codecs->planar, cmd->data, cmd->length, cmd->width, cmd->height,  | 
540  | 0  |                          DstData, surface->format, surface->scanline, cmd->left, cmd->top,  | 
541  | 0  |                          cmd->width, cmd->height, FALSE))  | 
542  | 0  |     return ERROR_INTERNAL_ERROR;  | 
543  |  |  | 
544  | 0  |   invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);  | 
545  | 0  |   invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);  | 
546  | 0  |   invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);  | 
547  | 0  |   invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);  | 
548  | 0  |   region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);  | 
549  | 0  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,  | 
550  | 0  |                         &invalidRect);  | 
551  |  | 
  | 
552  | 0  |   if (status != CHANNEL_RC_OK)  | 
553  | 0  |     goto fail;  | 
554  |  |  | 
555  | 0  |   status = gdi_interFrameUpdate(gdi, context);  | 
556  |  | 
  | 
557  | 0  | fail:  | 
558  | 0  |   return status;  | 
559  | 0  | }  | 
560  |  |  | 
561  |  | /**  | 
562  |  |  * Function description  | 
563  |  |  *  | 
564  |  |  * @return 0 on success, otherwise a Win32 error code  | 
565  |  |  */  | 
566  |  | static UINT gdi_SurfaceCommand_AVC420(rdpGdi* gdi, RdpgfxClientContext* context,  | 
567  |  |                                       const RDPGFX_SURFACE_COMMAND* cmd)  | 
568  | 0  | { | 
569  |  | #ifdef WITH_GFX_H264  | 
570  |  |   INT32 rc = 0;  | 
571  |  |   UINT status = CHANNEL_RC_OK;  | 
572  |  |   gdiGfxSurface* surface = NULL;  | 
573  |  |   RDPGFX_H264_METABLOCK* meta = NULL;  | 
574  |  |   RDPGFX_AVC420_BITMAP_STREAM* bs = NULL;  | 
575  |  |   WINPR_ASSERT(gdi);  | 
576  |  |   WINPR_ASSERT(context);  | 
577  |  |   WINPR_ASSERT(cmd);  | 
578  |  |  | 
579  |  |   WINPR_ASSERT(context->GetSurfaceData);  | 
580  |  |   surface =  | 
581  |  |       (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));  | 
582  |  |  | 
583  |  |   if (!surface)  | 
584  |  |   { | 
585  |  |     WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);  | 
586  |  |     return ERROR_NOT_FOUND;  | 
587  |  |   }  | 
588  |  |  | 
589  |  |   if (!surface->h264)  | 
590  |  |   { | 
591  |  |     surface->h264 = h264_context_new(FALSE);  | 
592  |  |  | 
593  |  |     if (!surface->h264)  | 
594  |  |     { | 
595  |  |       WLog_ERR(TAG, "unable to create h264 context");  | 
596  |  |       return ERROR_NOT_ENOUGH_MEMORY;  | 
597  |  |     }  | 
598  |  |  | 
599  |  |     if (!h264_context_reset(surface->h264, surface->width, surface->height))  | 
600  |  |       return ERROR_INTERNAL_ERROR;  | 
601  |  |   }  | 
602  |  |  | 
603  |  |   if (!surface->h264)  | 
604  |  |     return ERROR_NOT_SUPPORTED;  | 
605  |  |  | 
606  |  |   bs = (RDPGFX_AVC420_BITMAP_STREAM*)cmd->extra;  | 
607  |  |  | 
608  |  |   if (!bs)  | 
609  |  |     return ERROR_INTERNAL_ERROR;  | 
610  |  |  | 
611  |  |   meta = &(bs->meta);  | 
612  |  |   rc = avc420_decompress(surface->h264, bs->data, bs->length, surface->data, surface->format,  | 
613  |  |                          surface->scanline, surface->width, surface->height, meta->regionRects,  | 
614  |  |                          meta->numRegionRects);  | 
615  |  |  | 
616  |  |   if (rc < 0)  | 
617  |  |   { | 
618  |  |     WLog_WARN(TAG, "avc420_decompress failure: %" PRId32 ", ignoring update.", rc);  | 
619  |  |     return CHANNEL_RC_OK;  | 
620  |  |   }  | 
621  |  |  | 
622  |  |   for (UINT32 i = 0; i < meta->numRegionRects; i++)  | 
623  |  |   { | 
624  |  |     region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),  | 
625  |  |                         &(meta->regionRects[i]));  | 
626  |  |   }  | 
627  |  |  | 
628  |  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,  | 
629  |  |                         meta->numRegionRects, meta->regionRects);  | 
630  |  |  | 
631  |  |   if (status != CHANNEL_RC_OK)  | 
632  |  |     goto fail;  | 
633  |  |  | 
634  |  |   status = gdi_interFrameUpdate(gdi, context);  | 
635  |  |  | 
636  |  | fail:  | 
637  |  |   return status;  | 
638  |  | #else  | 
639  | 0  |   return ERROR_NOT_SUPPORTED;  | 
640  | 0  | #endif  | 
641  | 0  | }  | 
642  |  |  | 
643  |  | /**  | 
644  |  |  * Function description  | 
645  |  |  *  | 
646  |  |  * @return 0 on success, otherwise a Win32 error code  | 
647  |  |  */  | 
648  |  | static UINT gdi_SurfaceCommand_AVC444(rdpGdi* gdi, RdpgfxClientContext* context,  | 
649  |  |                                       const RDPGFX_SURFACE_COMMAND* cmd)  | 
650  | 0  | { | 
651  |  | #ifdef WITH_GFX_H264  | 
652  |  |   INT32 rc = 0;  | 
653  |  |   UINT status = CHANNEL_RC_OK;  | 
654  |  |   gdiGfxSurface* surface = NULL;  | 
655  |  |   RDPGFX_AVC444_BITMAP_STREAM* bs = NULL;  | 
656  |  |   RDPGFX_AVC420_BITMAP_STREAM* avc1 = NULL;  | 
657  |  |   RDPGFX_H264_METABLOCK* meta1 = NULL;  | 
658  |  |   RDPGFX_AVC420_BITMAP_STREAM* avc2 = NULL;  | 
659  |  |   RDPGFX_H264_METABLOCK* meta2 = NULL;  | 
660  |  |   WINPR_ASSERT(gdi);  | 
661  |  |   WINPR_ASSERT(context);  | 
662  |  |   WINPR_ASSERT(cmd);  | 
663  |  |  | 
664  |  |   WINPR_ASSERT(context->GetSurfaceData);  | 
665  |  |   surface =  | 
666  |  |       (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));  | 
667  |  |  | 
668  |  |   if (!surface)  | 
669  |  |   { | 
670  |  |     WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);  | 
671  |  |     return ERROR_NOT_FOUND;  | 
672  |  |   }  | 
673  |  |  | 
674  |  |   if (!surface->h264)  | 
675  |  |   { | 
676  |  |     surface->h264 = h264_context_new(FALSE);  | 
677  |  |  | 
678  |  |     if (!surface->h264)  | 
679  |  |     { | 
680  |  |       WLog_ERR(TAG, "unable to create h264 context");  | 
681  |  |       return ERROR_NOT_ENOUGH_MEMORY;  | 
682  |  |     }  | 
683  |  |  | 
684  |  |     if (!h264_context_reset(surface->h264, surface->width, surface->height))  | 
685  |  |       return ERROR_INTERNAL_ERROR;  | 
686  |  |   }  | 
687  |  |  | 
688  |  |   if (!surface->h264)  | 
689  |  |     return ERROR_NOT_SUPPORTED;  | 
690  |  |  | 
691  |  |   bs = (RDPGFX_AVC444_BITMAP_STREAM*)cmd->extra;  | 
692  |  |  | 
693  |  |   if (!bs)  | 
694  |  |     return ERROR_INTERNAL_ERROR;  | 
695  |  |  | 
696  |  |   avc1 = &bs->bitstream[0];  | 
697  |  |   avc2 = &bs->bitstream[1];  | 
698  |  |   meta1 = &avc1->meta;  | 
699  |  |   meta2 = &avc2->meta;  | 
700  |  |   rc = avc444_decompress(surface->h264, bs->LC, meta1->regionRects, meta1->numRegionRects,  | 
701  |  |                          avc1->data, avc1->length, meta2->regionRects, meta2->numRegionRects,  | 
702  |  |                          avc2->data, avc2->length, surface->data, surface->format,  | 
703  |  |                          surface->scanline, surface->width, surface->height, cmd->codecId);  | 
704  |  |  | 
705  |  |   if (rc < 0)  | 
706  |  |   { | 
707  |  |     WLog_WARN(TAG, "avc444_decompress failure: %" PRIu32 ", ignoring update.", status);  | 
708  |  |     return CHANNEL_RC_OK;  | 
709  |  |   }  | 
710  |  |  | 
711  |  |   for (UINT32 i = 0; i < meta1->numRegionRects; i++)  | 
712  |  |   { | 
713  |  |     region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),  | 
714  |  |                         &(meta1->regionRects[i]));  | 
715  |  |   }  | 
716  |  |  | 
717  |  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,  | 
718  |  |                         meta1->numRegionRects, meta1->regionRects);  | 
719  |  |  | 
720  |  |   if (status != CHANNEL_RC_OK)  | 
721  |  |     goto fail;  | 
722  |  |  | 
723  |  |   for (UINT32 i = 0; i < meta2->numRegionRects; i++)  | 
724  |  |   { | 
725  |  |     region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion),  | 
726  |  |                         &(meta2->regionRects[i]));  | 
727  |  |   }  | 
728  |  |  | 
729  |  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,  | 
730  |  |                         meta2->numRegionRects, meta2->regionRects);  | 
731  |  |  | 
732  |  |   if (status != CHANNEL_RC_OK)  | 
733  |  |     goto fail;  | 
734  |  |  | 
735  |  |   status = gdi_interFrameUpdate(gdi, context);  | 
736  |  |  | 
737  |  | fail:  | 
738  |  |   return status;  | 
739  |  | #else  | 
740  | 0  |   return ERROR_NOT_SUPPORTED;  | 
741  | 0  | #endif  | 
742  | 0  | }  | 
743  |  |  | 
744  |  | static BOOL gdi_apply_alpha(BYTE* data, UINT32 format, UINT32 stride, RECTANGLE_16* rect,  | 
745  |  |                             UINT32 startOffsetX, UINT32 count, BYTE a)  | 
746  | 0  | { | 
747  | 0  |   UINT32 written = 0;  | 
748  | 0  |   BOOL first = TRUE;  | 
749  | 0  |   const UINT32 bpp = FreeRDPGetBytesPerPixel(format);  | 
750  | 0  |   WINPR_ASSERT(rect);  | 
751  |  |  | 
752  | 0  |   for (size_t y = rect->top; y < rect->bottom; y++)  | 
753  | 0  |   { | 
754  | 0  |     BYTE* line = &data[y * stride];  | 
755  |  | 
  | 
756  | 0  |     for (size_t x = first ? rect->left + startOffsetX : rect->left; x < rect->right; x++)  | 
757  | 0  |     { | 
758  | 0  |       BYTE r = 0;  | 
759  | 0  |       BYTE g = 0;  | 
760  | 0  |       BYTE b = 0;  | 
761  |  | 
  | 
762  | 0  |       if (written == count)  | 
763  | 0  |         return TRUE;  | 
764  |  |  | 
765  | 0  |       BYTE* src = &line[x * bpp];  | 
766  | 0  |       UINT32 color = FreeRDPReadColor(src, format);  | 
767  | 0  |       FreeRDPSplitColor(color, format, &r, &g, &b, NULL, NULL);  | 
768  | 0  |       color = FreeRDPGetColor(format, r, g, b, a);  | 
769  | 0  |       FreeRDPWriteColor(src, format, color);  | 
770  | 0  |       written++;  | 
771  | 0  |     }  | 
772  |  |  | 
773  | 0  |     first = FALSE;  | 
774  | 0  |   }  | 
775  |  |  | 
776  | 0  |   return TRUE;  | 
777  | 0  | }  | 
778  |  | /**  | 
779  |  |  * Function description  | 
780  |  |  *  | 
781  |  |  * @return 0 on success, otherwise a Win32 error code  | 
782  |  |  */  | 
783  |  | static UINT gdi_SurfaceCommand_Alpha(rdpGdi* gdi, RdpgfxClientContext* context,  | 
784  |  |                                      const RDPGFX_SURFACE_COMMAND* cmd)  | 
785  | 0  | { | 
786  | 0  |   UINT status = CHANNEL_RC_OK;  | 
787  | 0  |   UINT16 alphaSig = 0;  | 
788  | 0  |   UINT16 compressed = 0;  | 
789  | 0  |   gdiGfxSurface* surface = NULL;  | 
790  | 0  |   RECTANGLE_16 invalidRect;  | 
791  | 0  |   wStream buffer;  | 
792  | 0  |   wStream* s = NULL;  | 
793  | 0  |   WINPR_ASSERT(gdi);  | 
794  | 0  |   WINPR_ASSERT(context);  | 
795  | 0  |   WINPR_ASSERT(cmd);  | 
796  |  |  | 
797  | 0  |   s = Stream_StaticConstInit(&buffer, cmd->data, cmd->length);  | 
798  |  | 
  | 
799  | 0  |   if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))  | 
800  | 0  |     return ERROR_INVALID_DATA;  | 
801  |  |  | 
802  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
803  | 0  |   surface =  | 
804  | 0  |       (gdiGfxSurface*)context->GetSurfaceData(context, (UINT16)MIN(UINT16_MAX, cmd->surfaceId));  | 
805  |  | 
  | 
806  | 0  |   if (!surface)  | 
807  | 0  |   { | 
808  | 0  |     WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);  | 
809  | 0  |     return ERROR_NOT_FOUND;  | 
810  | 0  |   }  | 
811  |  |  | 
812  | 0  |   if (!is_within_surface(surface, cmd))  | 
813  | 0  |     return ERROR_INVALID_DATA;  | 
814  |  |  | 
815  | 0  |   Stream_Read_UINT16(s, alphaSig);  | 
816  | 0  |   Stream_Read_UINT16(s, compressed);  | 
817  |  | 
  | 
818  | 0  |   if (alphaSig != 0x414C)  | 
819  | 0  |     return ERROR_INVALID_DATA;  | 
820  |  |  | 
821  | 0  |   if (compressed == 0)  | 
822  | 0  |   { | 
823  | 0  |     if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, cmd->height, cmd->width))  | 
824  | 0  |       return ERROR_INVALID_DATA;  | 
825  |  |  | 
826  | 0  |     for (size_t y = cmd->top; y < cmd->top + cmd->height; y++)  | 
827  | 0  |     { | 
828  | 0  |       BYTE* line = &surface->data[y * surface->scanline];  | 
829  |  | 
  | 
830  | 0  |       for (size_t x = cmd->left; x < cmd->left + cmd->width; x++)  | 
831  | 0  |       { | 
832  | 0  |         UINT32 color = 0;  | 
833  | 0  |         BYTE r = 0;  | 
834  | 0  |         BYTE g = 0;  | 
835  | 0  |         BYTE b = 0;  | 
836  | 0  |         BYTE a = 0;  | 
837  | 0  |         BYTE* src = &line[x * FreeRDPGetBytesPerPixel(surface->format)];  | 
838  | 0  |         Stream_Read_UINT8(s, a);  | 
839  | 0  |         color = FreeRDPReadColor(src, surface->format);  | 
840  | 0  |         FreeRDPSplitColor(color, surface->format, &r, &g, &b, NULL, NULL);  | 
841  | 0  |         color = FreeRDPGetColor(surface->format, r, g, b, a);  | 
842  | 0  |         FreeRDPWriteColor(src, surface->format, color);  | 
843  | 0  |       }  | 
844  | 0  |     }  | 
845  | 0  |   }  | 
846  | 0  |   else  | 
847  | 0  |   { | 
848  | 0  |     UINT32 startOffsetX = 0;  | 
849  | 0  |     RECTANGLE_16 rect = { 0 }; | 
850  | 0  |     rect.left = (UINT16)MIN(UINT16_MAX, cmd->left);  | 
851  | 0  |     rect.top = (UINT16)MIN(UINT16_MAX, cmd->top);  | 
852  | 0  |     rect.right = (UINT16)MIN(UINT16_MAX, cmd->left + cmd->width);  | 
853  | 0  |     rect.bottom = (UINT16)MIN(UINT16_MAX, cmd->top + cmd->height);  | 
854  |  | 
  | 
855  | 0  |     while (rect.top < rect.bottom)  | 
856  | 0  |     { | 
857  | 0  |       UINT32 count = 0;  | 
858  | 0  |       BYTE a = 0;  | 
859  |  | 
  | 
860  | 0  |       if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))  | 
861  | 0  |         return ERROR_INVALID_DATA;  | 
862  |  |  | 
863  | 0  |       Stream_Read_UINT8(s, a);  | 
864  | 0  |       Stream_Read_UINT8(s, count);  | 
865  |  | 
  | 
866  | 0  |       if (count >= 0xFF)  | 
867  | 0  |       { | 
868  | 0  |         if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))  | 
869  | 0  |           return ERROR_INVALID_DATA;  | 
870  |  |  | 
871  | 0  |         Stream_Read_UINT16(s, count);  | 
872  |  | 
  | 
873  | 0  |         if (count >= 0xFFFF)  | 
874  | 0  |         { | 
875  | 0  |           if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))  | 
876  | 0  |             return ERROR_INVALID_DATA;  | 
877  |  |  | 
878  | 0  |           Stream_Read_UINT32(s, count);  | 
879  | 0  |         }  | 
880  | 0  |       }  | 
881  |  |  | 
882  | 0  |       if (!gdi_apply_alpha(surface->data, surface->format, surface->scanline, &rect,  | 
883  | 0  |                            startOffsetX, count, a))  | 
884  | 0  |         return ERROR_INTERNAL_ERROR;  | 
885  |  |  | 
886  | 0  |       startOffsetX += count;  | 
887  |  | 
  | 
888  | 0  |       while (startOffsetX >= cmd->width)  | 
889  | 0  |       { | 
890  | 0  |         startOffsetX -= cmd->width;  | 
891  | 0  |         rect.top++;  | 
892  | 0  |       }  | 
893  | 0  |     }  | 
894  | 0  |   }  | 
895  |  |  | 
896  | 0  |   invalidRect.left = (UINT16)MIN(UINT16_MAX, cmd->left);  | 
897  | 0  |   invalidRect.top = (UINT16)MIN(UINT16_MAX, cmd->top);  | 
898  | 0  |   invalidRect.right = (UINT16)MIN(UINT16_MAX, cmd->right);  | 
899  | 0  |   invalidRect.bottom = (UINT16)MIN(UINT16_MAX, cmd->bottom);  | 
900  | 0  |   region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);  | 
901  | 0  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId, 1,  | 
902  | 0  |                         &invalidRect);  | 
903  |  | 
  | 
904  | 0  |   if (status != CHANNEL_RC_OK)  | 
905  | 0  |     goto fail;  | 
906  |  |  | 
907  | 0  |   status = gdi_interFrameUpdate(gdi, context);  | 
908  |  | 
  | 
909  | 0  | fail:  | 
910  | 0  |   return status;  | 
911  | 0  | }  | 
912  |  |  | 
913  |  | #if defined(WITH_GFX_FRAME_DUMP)  | 
914  |  | static void dump_cmd(const RDPGFX_SURFACE_COMMAND* cmd, UINT32 frameId)  | 
915  |  | { | 
916  |  |   static UINT64 xxx = 0;  | 
917  |  |   const char* path = "/tmp/dump/";  | 
918  |  |   WINPR_ASSERT(cmd);  | 
919  |  |   char fname[1024] = { 0 }; | 
920  |  |  | 
921  |  |   snprintf(fname, sizeof(fname), "%s/%08" PRIx64 ".raw", path, xxx++);  | 
922  |  |   FILE* fp = fopen(fname, "w");  | 
923  |  |   if (!fp)  | 
924  |  |     return;  | 
925  |  |   (void)fprintf(fp, "frameid: %" PRIu32 "\n", frameId);  | 
926  |  |   (void)fprintf(fp, "surfaceId: %" PRIu32 "\n", cmd->surfaceId);  | 
927  |  |   (void)fprintf(fp, "codecId: %" PRIu32 "\n", cmd->codecId);  | 
928  |  |   (void)fprintf(fp, "contextId: %" PRIu32 "\n", cmd->contextId);  | 
929  |  |   (void)fprintf(fp, "format: %" PRIu32 "\n", cmd->format);  | 
930  |  |   (void)fprintf(fp, "left: %" PRIu32 "\n", cmd->left);  | 
931  |  |   (void)fprintf(fp, "top: %" PRIu32 "\n", cmd->top);  | 
932  |  |   (void)fprintf(fp, "right: %" PRIu32 "\n", cmd->right);  | 
933  |  |   (void)fprintf(fp, "bottom: %" PRIu32 "\n", cmd->bottom);  | 
934  |  |   (void)fprintf(fp, "width: %" PRIu32 "\n", cmd->width);  | 
935  |  |   (void)fprintf(fp, "height: %" PRIu32 "\n", cmd->height);  | 
936  |  |   (void)fprintf(fp, "length: %" PRIu32 "\n", cmd->length);  | 
937  |  |  | 
938  |  |   char* bdata = crypto_base64_encode_ex(cmd->data, cmd->length, FALSE);  | 
939  |  |   (void)fprintf(fp, "data: %s\n", bdata);  | 
940  |  |   free(bdata);  | 
941  |  |   fclose(fp);  | 
942  |  | }  | 
943  |  | #endif  | 
944  |  |  | 
945  |  | /**  | 
946  |  |  * Function description  | 
947  |  |  *  | 
948  |  |  * @return 0 on success, otherwise a Win32 error code  | 
949  |  |  */  | 
950  |  | static UINT gdi_SurfaceCommand_Progressive(rdpGdi* gdi, RdpgfxClientContext* context,  | 
951  |  |                                            const RDPGFX_SURFACE_COMMAND* cmd)  | 
952  | 0  | { | 
953  | 0  |   INT32 rc = 0;  | 
954  | 0  |   UINT status = CHANNEL_RC_OK;  | 
955  | 0  |   gdiGfxSurface* surface = NULL;  | 
956  | 0  |   REGION16 invalidRegion;  | 
957  | 0  |   const RECTANGLE_16* rects = NULL;  | 
958  | 0  |   UINT32 nrRects = 0;  | 
959  |  |   /**  | 
960  |  |    * Note: Since this comes via a Wire-To-Surface-2 PDU the  | 
961  |  |    * cmd's top/left/right/bottom/width/height members are always zero!  | 
962  |  |    * The update region is determined during decompression.  | 
963  |  |    */  | 
964  | 0  |   WINPR_ASSERT(gdi);  | 
965  | 0  |   WINPR_ASSERT(context);  | 
966  | 0  |   WINPR_ASSERT(cmd);  | 
967  | 0  |   const UINT16 surfaceId = (UINT16)MIN(UINT16_MAX, cmd->surfaceId);  | 
968  |  | 
  | 
969  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
970  | 0  |   surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceId);  | 
971  |  | 
  | 
972  | 0  |   if (!surface)  | 
973  | 0  |   { | 
974  | 0  |     WLog_ERR(TAG, "unable to retrieve surfaceData for surfaceId=%" PRIu32 "", cmd->surfaceId);  | 
975  | 0  |     return ERROR_NOT_FOUND;  | 
976  | 0  |   }  | 
977  |  |  | 
978  | 0  |   if (!is_within_surface(surface, cmd))  | 
979  | 0  |     return ERROR_INVALID_DATA;  | 
980  |  |  | 
981  | 0  |   WINPR_ASSERT(surface->codecs);  | 
982  | 0  |   rc = progressive_create_surface_context(surface->codecs->progressive, surfaceId, surface->width,  | 
983  | 0  |                                           surface->height);  | 
984  |  | 
  | 
985  | 0  |   if (rc < 0)  | 
986  | 0  |   { | 
987  | 0  |     WLog_ERR(TAG, "progressive_create_surface_context failure: %" PRId32 "", rc);  | 
988  | 0  |     return ERROR_INTERNAL_ERROR;  | 
989  | 0  |   }  | 
990  |  |  | 
991  | 0  |   region16_init(&invalidRegion);  | 
992  |  | 
  | 
993  | 0  |   rc = progressive_decompress(surface->codecs->progressive, cmd->data, cmd->length, surface->data,  | 
994  | 0  |                               surface->format, surface->scanline, cmd->left, cmd->top,  | 
995  | 0  |                               &invalidRegion, surfaceId, gdi->frameId);  | 
996  |  | 
  | 
997  | 0  |   if (rc < 0)  | 
998  | 0  |   { | 
999  | 0  |     WLog_ERR(TAG, "progressive_decompress failure: %" PRId32 "", rc);  | 
1000  | 0  |     region16_uninit(&invalidRegion);  | 
1001  | 0  |     return ERROR_INTERNAL_ERROR;  | 
1002  | 0  |   }  | 
1003  |  |  | 
1004  | 0  |   rects = region16_rects(&invalidRegion, &nrRects);  | 
1005  | 0  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,  | 
1006  | 0  |                         nrRects, rects);  | 
1007  |  | 
  | 
1008  | 0  |   if (status != CHANNEL_RC_OK)  | 
1009  | 0  |     goto fail;  | 
1010  |  |  | 
1011  | 0  |   for (UINT32 x = 0; x < nrRects; x++)  | 
1012  | 0  |     region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &rects[x]);  | 
1013  |  | 
  | 
1014  | 0  |   region16_uninit(&invalidRegion);  | 
1015  |  | 
  | 
1016  | 0  |   status = gdi_interFrameUpdate(gdi, context);  | 
1017  |  | 
  | 
1018  | 0  | fail:  | 
1019  | 0  |   return status;  | 
1020  | 0  | }  | 
1021  |  |  | 
1022  |  | /**  | 
1023  |  |  * Function description  | 
1024  |  |  *  | 
1025  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1026  |  |  */  | 
1027  |  | static UINT gdi_SurfaceCommand(RdpgfxClientContext* context, const RDPGFX_SURFACE_COMMAND* cmd)  | 
1028  | 0  | { | 
1029  | 0  |   UINT status = CHANNEL_RC_OK;  | 
1030  | 0  |   rdpGdi* gdi = NULL;  | 
1031  |  | 
  | 
1032  | 0  |   if (!context || !cmd)  | 
1033  | 0  |     return ERROR_INVALID_PARAMETER;  | 
1034  |  |  | 
1035  | 0  |   gdi = (rdpGdi*)context->custom;  | 
1036  |  | 
  | 
1037  | 0  |   EnterCriticalSection(&context->mux);  | 
1038  | 0  |   WLog_Print(gdi->log, WLOG_TRACE,  | 
1039  | 0  |              "surfaceId=%" PRIu32 ", codec=%s [%" PRIu32 "], contextId=%" PRIu32 ", format=%s, "  | 
1040  | 0  |              "left=%" PRIu32 ", top=%" PRIu32 ", right=%" PRIu32 ", bottom=%" PRIu32  | 
1041  | 0  |              ", width=%" PRIu32 ", height=%" PRIu32 " "  | 
1042  | 0  |              "length=%" PRIu32 ", data=%p, extra=%p",  | 
1043  | 0  |              cmd->surfaceId, rdpgfx_get_codec_id_string(cmd->codecId), cmd->codecId,  | 
1044  | 0  |              cmd->contextId, FreeRDPGetColorFormatName(cmd->format), cmd->left, cmd->top,  | 
1045  | 0  |              cmd->right, cmd->bottom, cmd->width, cmd->height, cmd->length, (void*)cmd->data,  | 
1046  | 0  |              (void*)cmd->extra);  | 
1047  |  | #if defined(WITH_GFX_FRAME_DUMP)  | 
1048  |  |   dump_cmd(cmd, gdi->frameId);  | 
1049  |  | #endif  | 
1050  |  | 
  | 
1051  | 0  |   switch (cmd->codecId)  | 
1052  | 0  |   { | 
1053  | 0  |     case RDPGFX_CODECID_UNCOMPRESSED:  | 
1054  | 0  |       status = gdi_SurfaceCommand_Uncompressed(gdi, context, cmd);  | 
1055  | 0  |       break;  | 
1056  |  |  | 
1057  | 0  |     case RDPGFX_CODECID_CAVIDEO:  | 
1058  | 0  |       status = gdi_SurfaceCommand_RemoteFX(gdi, context, cmd);  | 
1059  | 0  |       break;  | 
1060  |  |  | 
1061  | 0  |     case RDPGFX_CODECID_CLEARCODEC:  | 
1062  | 0  |       status = gdi_SurfaceCommand_ClearCodec(gdi, context, cmd);  | 
1063  | 0  |       break;  | 
1064  |  |  | 
1065  | 0  |     case RDPGFX_CODECID_PLANAR:  | 
1066  | 0  |       status = gdi_SurfaceCommand_Planar(gdi, context, cmd);  | 
1067  | 0  |       break;  | 
1068  |  |  | 
1069  | 0  |     case RDPGFX_CODECID_AVC420:  | 
1070  | 0  |       status = gdi_SurfaceCommand_AVC420(gdi, context, cmd);  | 
1071  | 0  |       break;  | 
1072  |  |  | 
1073  | 0  |     case RDPGFX_CODECID_AVC444v2:  | 
1074  | 0  |     case RDPGFX_CODECID_AVC444:  | 
1075  | 0  |       status = gdi_SurfaceCommand_AVC444(gdi, context, cmd);  | 
1076  | 0  |       break;  | 
1077  |  |  | 
1078  | 0  |     case RDPGFX_CODECID_ALPHA:  | 
1079  | 0  |       status = gdi_SurfaceCommand_Alpha(gdi, context, cmd);  | 
1080  | 0  |       break;  | 
1081  |  |  | 
1082  | 0  |     case RDPGFX_CODECID_CAPROGRESSIVE:  | 
1083  | 0  |       status = gdi_SurfaceCommand_Progressive(gdi, context, cmd);  | 
1084  | 0  |       break;  | 
1085  |  |  | 
1086  | 0  |     case RDPGFX_CODECID_CAPROGRESSIVE_V2:  | 
1087  | 0  |       WLog_WARN(TAG, "SurfaceCommand %s [0x%08" PRIX32 "] not implemented",  | 
1088  | 0  |                 rdpgfx_get_codec_id_string(cmd->codecId), cmd->codecId);  | 
1089  | 0  |       break;  | 
1090  |  |  | 
1091  | 0  |     default:  | 
1092  | 0  |       WLog_WARN(TAG, "Invalid SurfaceCommand %s [0x%08" PRIX32 "]",  | 
1093  | 0  |                 rdpgfx_get_codec_id_string(cmd->codecId), cmd->codecId);  | 
1094  | 0  |       break;  | 
1095  | 0  |   }  | 
1096  |  |  | 
1097  | 0  |   LeaveCriticalSection(&context->mux);  | 
1098  | 0  |   return status;  | 
1099  | 0  | }  | 
1100  |  |  | 
1101  |  | /**  | 
1102  |  |  * Function description  | 
1103  |  |  *  | 
1104  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1105  |  |  */  | 
1106  |  | static UINT  | 
1107  |  | gdi_DeleteEncodingContext(RdpgfxClientContext* context,  | 
1108  |  |                           const RDPGFX_DELETE_ENCODING_CONTEXT_PDU* deleteEncodingContext)  | 
1109  | 0  | { | 
1110  | 0  |   WINPR_ASSERT(context);  | 
1111  | 0  |   WINPR_ASSERT(deleteEncodingContext);  | 
1112  | 0  |   WINPR_UNUSED(context);  | 
1113  | 0  |   WINPR_UNUSED(deleteEncodingContext);  | 
1114  | 0  |   return CHANNEL_RC_OK;  | 
1115  | 0  | }  | 
1116  |  |  | 
1117  |  | /**  | 
1118  |  |  * Function description  | 
1119  |  |  *  | 
1120  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1121  |  |  */  | 
1122  |  | static UINT gdi_CreateSurface(RdpgfxClientContext* context,  | 
1123  |  |                               const RDPGFX_CREATE_SURFACE_PDU* createSurface)  | 
1124  | 0  | { | 
1125  | 0  |   UINT rc = ERROR_INTERNAL_ERROR;  | 
1126  | 0  |   gdiGfxSurface* surface = NULL;  | 
1127  | 0  |   rdpGdi* gdi = NULL;  | 
1128  | 0  |   WINPR_ASSERT(context);  | 
1129  | 0  |   WINPR_ASSERT(createSurface);  | 
1130  | 0  |   gdi = (rdpGdi*)context->custom;  | 
1131  | 0  |   WINPR_ASSERT(gdi);  | 
1132  | 0  |   WINPR_ASSERT(gdi->context);  | 
1133  | 0  |   EnterCriticalSection(&context->mux);  | 
1134  | 0  |   surface = (gdiGfxSurface*)calloc(1, sizeof(gdiGfxSurface));  | 
1135  |  | 
  | 
1136  | 0  |   if (!surface)  | 
1137  | 0  |     goto fail;  | 
1138  |  |  | 
1139  | 0  |   if (!freerdp_settings_get_bool(gdi->context->settings, FreeRDP_DeactivateClientDecoding))  | 
1140  | 0  |   { | 
1141  | 0  |     WINPR_ASSERT(context->codecs);  | 
1142  | 0  |     surface->codecs = context->codecs;  | 
1143  |  | 
  | 
1144  | 0  |     if (!surface->codecs)  | 
1145  | 0  |     { | 
1146  | 0  |       free(surface);  | 
1147  | 0  |       goto fail;  | 
1148  | 0  |     }  | 
1149  | 0  |   }  | 
1150  |  |  | 
1151  | 0  |   surface->surfaceId = createSurface->surfaceId;  | 
1152  | 0  |   surface->width = gfx_align_scanline(createSurface->width, 16);  | 
1153  | 0  |   surface->height = gfx_align_scanline(createSurface->height, 16);  | 
1154  | 0  |   surface->mappedWidth = createSurface->width;  | 
1155  | 0  |   surface->mappedHeight = createSurface->height;  | 
1156  | 0  |   surface->outputTargetWidth = createSurface->width;  | 
1157  | 0  |   surface->outputTargetHeight = createSurface->height;  | 
1158  |  | 
  | 
1159  | 0  |   switch (createSurface->pixelFormat)  | 
1160  | 0  |   { | 
1161  | 0  |     case GFX_PIXEL_FORMAT_ARGB_8888:  | 
1162  | 0  |       surface->format = PIXEL_FORMAT_BGRA32;  | 
1163  | 0  |       break;  | 
1164  |  |  | 
1165  | 0  |     case GFX_PIXEL_FORMAT_XRGB_8888:  | 
1166  | 0  |       surface->format = PIXEL_FORMAT_BGRX32;  | 
1167  | 0  |       break;  | 
1168  |  |  | 
1169  | 0  |     default:  | 
1170  | 0  |       free(surface);  | 
1171  | 0  |       goto fail;  | 
1172  | 0  |   }  | 
1173  |  |  | 
1174  | 0  |   surface->scanline = gfx_align_scanline(surface->width * 4UL, 16);  | 
1175  | 0  |   surface->data = (BYTE*)winpr_aligned_malloc(1ull * surface->scanline * surface->height, 16);  | 
1176  |  | 
  | 
1177  | 0  |   if (!surface->data)  | 
1178  | 0  |   { | 
1179  | 0  |     free(surface);  | 
1180  | 0  |     goto fail;  | 
1181  | 0  |   }  | 
1182  |  |  | 
1183  | 0  |   memset(surface->data, 0xFF, (size_t)surface->scanline * surface->height);  | 
1184  | 0  |   region16_init(&surface->invalidRegion);  | 
1185  |  | 
  | 
1186  | 0  |   WINPR_ASSERT(context->SetSurfaceData);  | 
1187  | 0  |   rc = context->SetSurfaceData(context, surface->surfaceId, (void*)surface);  | 
1188  | 0  | fail:  | 
1189  | 0  |   LeaveCriticalSection(&context->mux);  | 
1190  | 0  |   return rc;  | 
1191  | 0  | }  | 
1192  |  |  | 
1193  |  | /**  | 
1194  |  |  * Function description  | 
1195  |  |  *  | 
1196  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1197  |  |  */  | 
1198  |  | static UINT gdi_DeleteSurface(RdpgfxClientContext* context,  | 
1199  |  |                               const RDPGFX_DELETE_SURFACE_PDU* deleteSurface)  | 
1200  | 0  | { | 
1201  | 0  |   UINT rc = CHANNEL_RC_OK;  | 
1202  | 0  |   UINT res = ERROR_INTERNAL_ERROR;  | 
1203  | 0  |   rdpCodecs* codecs = NULL;  | 
1204  | 0  |   gdiGfxSurface* surface = NULL;  | 
1205  | 0  |   EnterCriticalSection(&context->mux);  | 
1206  |  | 
  | 
1207  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
1208  | 0  |   surface = (gdiGfxSurface*)context->GetSurfaceData(context, deleteSurface->surfaceId);  | 
1209  |  | 
  | 
1210  | 0  |   if (surface)  | 
1211  | 0  |   { | 
1212  | 0  |     if (surface->windowMapped)  | 
1213  | 0  |       rc = IFCALLRESULT(CHANNEL_RC_OK, context->UnmapWindowForSurface, context,  | 
1214  | 0  |                         surface->windowId);  | 
1215  |  | 
  | 
1216  |  | #ifdef WITH_GFX_H264  | 
1217  |  |     h264_context_free(surface->h264);  | 
1218  |  | #endif  | 
1219  | 0  |     region16_uninit(&surface->invalidRegion);  | 
1220  | 0  |     codecs = surface->codecs;  | 
1221  | 0  |     winpr_aligned_free(surface->data);  | 
1222  | 0  |     free(surface);  | 
1223  | 0  |   }  | 
1224  |  | 
  | 
1225  | 0  |   WINPR_ASSERT(context->SetSurfaceData);  | 
1226  | 0  |   res = context->SetSurfaceData(context, deleteSurface->surfaceId, NULL);  | 
1227  | 0  |   if (res)  | 
1228  | 0  |     rc = res;  | 
1229  |  | 
  | 
1230  | 0  |   if (codecs && codecs->progressive)  | 
1231  | 0  |     progressive_delete_surface_context(codecs->progressive, deleteSurface->surfaceId);  | 
1232  |  | 
  | 
1233  | 0  |   LeaveCriticalSection(&context->mux);  | 
1234  | 0  |   return rc;  | 
1235  | 0  | }  | 
1236  |  |  | 
1237  |  | static BOOL intersect_rect(const RECTANGLE_16* rect, const gdiGfxSurface* surface,  | 
1238  |  |                            RECTANGLE_16* prect)  | 
1239  | 0  | { | 
1240  | 0  |   WINPR_ASSERT(rect);  | 
1241  | 0  |   WINPR_ASSERT(surface);  | 
1242  | 0  |   WINPR_ASSERT(prect);  | 
1243  |  |  | 
1244  | 0  |   if (rect->left > rect->right)  | 
1245  | 0  |     return FALSE;  | 
1246  | 0  |   if (rect->left > surface->width)  | 
1247  | 0  |     return FALSE;  | 
1248  | 0  |   if (rect->top > rect->bottom)  | 
1249  | 0  |     return FALSE;  | 
1250  | 0  |   if (rect->top > surface->height)  | 
1251  | 0  |     return FALSE;  | 
1252  | 0  |   prect->left = rect->left;  | 
1253  | 0  |   prect->top = rect->top;  | 
1254  | 0  |   prect->right = MIN(rect->right, surface->width);  | 
1255  | 0  |   prect->bottom = MIN(rect->bottom, surface->height);  | 
1256  | 0  |   return TRUE;  | 
1257  | 0  | }  | 
1258  |  |  | 
1259  |  | /**  | 
1260  |  |  * Function description  | 
1261  |  |  *  | 
1262  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1263  |  |  */  | 
1264  |  | static UINT gdi_SolidFill(RdpgfxClientContext* context, const RDPGFX_SOLID_FILL_PDU* solidFill)  | 
1265  | 0  | { | 
1266  | 0  |   UINT status = ERROR_INTERNAL_ERROR;  | 
1267  | 0  |   BYTE a = 0xff;  | 
1268  | 0  |   RECTANGLE_16 invalidRect = { 0 }; | 
1269  | 0  |   rdpGdi* gdi = (rdpGdi*)context->custom;  | 
1270  |  | 
  | 
1271  | 0  |   EnterCriticalSection(&context->mux);  | 
1272  |  | 
  | 
1273  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
1274  | 0  |   gdiGfxSurface* surface = (gdiGfxSurface*)context->GetSurfaceData(context, solidFill->surfaceId);  | 
1275  |  | 
  | 
1276  | 0  |   if (!surface)  | 
1277  | 0  |     goto fail;  | 
1278  |  |  | 
1279  | 0  |   const BYTE b = solidFill->fillPixel.B;  | 
1280  | 0  |   const BYTE g = solidFill->fillPixel.G;  | 
1281  | 0  |   const BYTE r = solidFill->fillPixel.R;  | 
1282  |  | 
  | 
1283  |  | #if 0  | 
1284  |  |   /* [MS-RDPEGFX] 3.3.5.4 Processing an RDPGFX_SOLIDFILL_PDU message  | 
1285  |  |    * https://learn.microsoft.com/en-us/windows/win32/gdi/binary-raster-operations  | 
1286  |  |    *  | 
1287  |  |    * this sounds like the alpha value is always ignored.  | 
1288  |  |    */  | 
1289  |  |   if (FreeRDPColorHasAlpha(surface->format))  | 
1290  |  |     a = solidFill->fillPixel.XA;  | 
1291  |  | #endif  | 
1292  |  | 
  | 
1293  | 0  |   const UINT32 color = FreeRDPGetColor(surface->format, r, g, b, a);  | 
1294  |  | 
  | 
1295  | 0  |   for (UINT16 index = 0; index < solidFill->fillRectCount; index++)  | 
1296  | 0  |   { | 
1297  | 0  |     const RECTANGLE_16* rect = &(solidFill->fillRects[index]);  | 
1298  |  | 
  | 
1299  | 0  |     if (!intersect_rect(rect, surface, &invalidRect))  | 
1300  | 0  |       goto fail;  | 
1301  |  |  | 
1302  | 0  |     const UINT32 nWidth = invalidRect.right - invalidRect.left;  | 
1303  | 0  |     const UINT32 nHeight = invalidRect.bottom - invalidRect.top;  | 
1304  |  | 
  | 
1305  | 0  |     if (!freerdp_image_fill(surface->data, surface->format, surface->scanline, invalidRect.left,  | 
1306  | 0  |                             invalidRect.top, nWidth, nHeight, color))  | 
1307  | 0  |       goto fail;  | 
1308  |  |  | 
1309  | 0  |     region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);  | 
1310  | 0  |   }  | 
1311  |  |  | 
1312  | 0  |   status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context, surface->surfaceId,  | 
1313  | 0  |                         solidFill->fillRectCount, solidFill->fillRects);  | 
1314  |  | 
  | 
1315  | 0  |   if (status != CHANNEL_RC_OK)  | 
1316  | 0  |     goto fail;  | 
1317  |  |  | 
1318  | 0  |   LeaveCriticalSection(&context->mux);  | 
1319  |  | 
  | 
1320  | 0  |   return gdi_interFrameUpdate(gdi, context);  | 
1321  | 0  | fail:  | 
1322  | 0  |   LeaveCriticalSection(&context->mux);  | 
1323  | 0  |   return status;  | 
1324  | 0  | }  | 
1325  |  |  | 
1326  |  | /**  | 
1327  |  |  * Function description  | 
1328  |  |  *  | 
1329  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1330  |  |  */  | 
1331  |  | static UINT gdi_SurfaceToSurface(RdpgfxClientContext* context,  | 
1332  |  |                                  const RDPGFX_SURFACE_TO_SURFACE_PDU* surfaceToSurface)  | 
1333  | 0  | { | 
1334  | 0  |   UINT status = ERROR_INTERNAL_ERROR;  | 
1335  | 0  |   BOOL sameSurface = 0;  | 
1336  | 0  |   UINT32 nWidth = 0;  | 
1337  | 0  |   UINT32 nHeight = 0;  | 
1338  | 0  |   const RECTANGLE_16* rectSrc = NULL;  | 
1339  | 0  |   RECTANGLE_16 invalidRect;  | 
1340  | 0  |   gdiGfxSurface* surfaceSrc = NULL;  | 
1341  | 0  |   gdiGfxSurface* surfaceDst = NULL;  | 
1342  | 0  |   rdpGdi* gdi = (rdpGdi*)context->custom;  | 
1343  | 0  |   EnterCriticalSection(&context->mux);  | 
1344  | 0  |   rectSrc = &(surfaceToSurface->rectSrc);  | 
1345  |  | 
  | 
1346  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
1347  | 0  |   surfaceSrc = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdSrc);  | 
1348  | 0  |   sameSurface =  | 
1349  | 0  |       (surfaceToSurface->surfaceIdSrc == surfaceToSurface->surfaceIdDest) ? TRUE : FALSE;  | 
1350  |  | 
  | 
1351  | 0  |   if (!sameSurface)  | 
1352  | 0  |     surfaceDst =  | 
1353  | 0  |         (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToSurface->surfaceIdDest);  | 
1354  | 0  |   else  | 
1355  | 0  |     surfaceDst = surfaceSrc;  | 
1356  |  | 
  | 
1357  | 0  |   if (!surfaceSrc || !surfaceDst)  | 
1358  | 0  |     goto fail;  | 
1359  |  |  | 
1360  | 0  |   if (!is_rect_valid(rectSrc, surfaceSrc->width, surfaceSrc->height))  | 
1361  | 0  |     goto fail;  | 
1362  |  |  | 
1363  | 0  |   nWidth = rectSrc->right - rectSrc->left;  | 
1364  | 0  |   nHeight = rectSrc->bottom - rectSrc->top;  | 
1365  |  | 
  | 
1366  | 0  |   for (UINT16 index = 0; index < surfaceToSurface->destPtsCount; index++)  | 
1367  | 0  |   { | 
1368  | 0  |     const RDPGFX_POINT16* destPt = &surfaceToSurface->destPts[index];  | 
1369  | 0  |     const RECTANGLE_16 rect = { destPt->x, destPt->y, | 
1370  | 0  |                               (UINT16)MIN(UINT16_MAX, destPt->x + nWidth),  | 
1371  | 0  |                               (UINT16)MIN(UINT16_MAX, destPt->y + nHeight) };  | 
1372  | 0  |     if (!is_rect_valid(&rect, surfaceDst->width, surfaceDst->height))  | 
1373  | 0  |       goto fail;  | 
1374  |  |  | 
1375  | 0  |     if (surfaceDst == surfaceSrc)  | 
1376  | 0  |     { | 
1377  | 0  |       if (!freerdp_image_copy_overlap(  | 
1378  | 0  |               surfaceDst->data, surfaceDst->format, surfaceDst->scanline, destPt->x,  | 
1379  | 0  |               destPt->y, nWidth, nHeight, surfaceSrc->data, surfaceSrc->format,  | 
1380  | 0  |               surfaceSrc->scanline, rectSrc->left, rectSrc->top, NULL, FREERDP_FLIP_NONE))  | 
1381  | 0  |         goto fail;  | 
1382  | 0  |     }  | 
1383  | 0  |     else  | 
1384  | 0  |     { | 
1385  | 0  |       if (!freerdp_image_copy_no_overlap(  | 
1386  | 0  |               surfaceDst->data, surfaceDst->format, surfaceDst->scanline, destPt->x,  | 
1387  | 0  |               destPt->y, nWidth, nHeight, surfaceSrc->data, surfaceSrc->format,  | 
1388  | 0  |               surfaceSrc->scanline, rectSrc->left, rectSrc->top, NULL, FREERDP_FLIP_NONE))  | 
1389  | 0  |         goto fail;  | 
1390  | 0  |     }  | 
1391  |  |  | 
1392  | 0  |     invalidRect = rect;  | 
1393  | 0  |     region16_union_rect(&surfaceDst->invalidRegion, &surfaceDst->invalidRegion, &invalidRect);  | 
1394  | 0  |     status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,  | 
1395  | 0  |                           surfaceDst->surfaceId, 1, &invalidRect);  | 
1396  |  | 
  | 
1397  | 0  |     if (status != CHANNEL_RC_OK)  | 
1398  | 0  |       goto fail;  | 
1399  | 0  |   }  | 
1400  |  |  | 
1401  | 0  |   LeaveCriticalSection(&context->mux);  | 
1402  |  | 
  | 
1403  | 0  |   return gdi_interFrameUpdate(gdi, context);  | 
1404  | 0  | fail:  | 
1405  | 0  |   LeaveCriticalSection(&context->mux);  | 
1406  | 0  |   return status;  | 
1407  | 0  | }  | 
1408  |  |  | 
1409  |  | static void gdi_GfxCacheEntryFree(gdiGfxCacheEntry* entry)  | 
1410  | 0  | { | 
1411  | 0  |   if (!entry)  | 
1412  | 0  |     return;  | 
1413  | 0  |   free(entry->data);  | 
1414  | 0  |   free(entry);  | 
1415  | 0  | }  | 
1416  |  |  | 
1417  |  | static gdiGfxCacheEntry* gdi_GfxCacheEntryNew(UINT64 cacheKey, UINT32 width, UINT32 height,  | 
1418  |  |                                               UINT32 format)  | 
1419  | 0  | { | 
1420  | 0  |   gdiGfxCacheEntry* cacheEntry = (gdiGfxCacheEntry*)calloc(1, sizeof(gdiGfxCacheEntry));  | 
1421  | 0  |   if (!cacheEntry)  | 
1422  | 0  |     goto fail;  | 
1423  |  |  | 
1424  | 0  |   cacheEntry->cacheKey = cacheKey;  | 
1425  | 0  |   cacheEntry->width = width;  | 
1426  | 0  |   cacheEntry->height = height;  | 
1427  | 0  |   cacheEntry->format = format;  | 
1428  | 0  |   cacheEntry->scanline = gfx_align_scanline(cacheEntry->width * 4, 16);  | 
1429  |  | 
  | 
1430  | 0  |   if ((cacheEntry->width > 0) && (cacheEntry->height > 0))  | 
1431  | 0  |   { | 
1432  | 0  |     cacheEntry->data = (BYTE*)calloc(cacheEntry->height, cacheEntry->scanline);  | 
1433  |  | 
  | 
1434  | 0  |     if (!cacheEntry->data)  | 
1435  | 0  |       goto fail;  | 
1436  | 0  |   }  | 
1437  | 0  |   return cacheEntry;  | 
1438  | 0  | fail:  | 
1439  | 0  |   gdi_GfxCacheEntryFree(cacheEntry);  | 
1440  | 0  |   return NULL;  | 
1441  | 0  | }  | 
1442  |  |  | 
1443  |  | /**  | 
1444  |  |  * Function description  | 
1445  |  |  *  | 
1446  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1447  |  |  */  | 
1448  |  | static UINT gdi_SurfaceToCache(RdpgfxClientContext* context,  | 
1449  |  |                                const RDPGFX_SURFACE_TO_CACHE_PDU* surfaceToCache)  | 
1450  | 0  | { | 
1451  | 0  |   const RECTANGLE_16* rect = NULL;  | 
1452  | 0  |   gdiGfxSurface* surface = NULL;  | 
1453  | 0  |   gdiGfxCacheEntry* cacheEntry = NULL;  | 
1454  | 0  |   UINT rc = ERROR_INTERNAL_ERROR;  | 
1455  | 0  |   EnterCriticalSection(&context->mux);  | 
1456  | 0  |   rect = &(surfaceToCache->rectSrc);  | 
1457  |  | 
  | 
1458  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
1459  | 0  |   surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToCache->surfaceId);  | 
1460  |  | 
  | 
1461  | 0  |   if (!surface)  | 
1462  | 0  |     goto fail;  | 
1463  |  |  | 
1464  | 0  |   if (!is_rect_valid(rect, surface->width, surface->height))  | 
1465  | 0  |     goto fail;  | 
1466  |  |  | 
1467  | 0  |   cacheEntry = gdi_GfxCacheEntryNew(surfaceToCache->cacheKey, (UINT32)(rect->right - rect->left),  | 
1468  | 0  |                                     (UINT32)(rect->bottom - rect->top), surface->format);  | 
1469  |  | 
  | 
1470  | 0  |   if (!cacheEntry)  | 
1471  | 0  |     goto fail;  | 
1472  |  |  | 
1473  | 0  |   if (!cacheEntry->data)  | 
1474  | 0  |     goto fail;  | 
1475  |  |  | 
1476  | 0  |   if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,  | 
1477  | 0  |                                      0, 0, cacheEntry->width, cacheEntry->height, surface->data,  | 
1478  | 0  |                                      surface->format, surface->scanline, rect->left, rect->top,  | 
1479  | 0  |                                      NULL, FREERDP_FLIP_NONE))  | 
1480  | 0  |     goto fail;  | 
1481  |  |  | 
1482  | 0  |   RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { surfaceToCache->cacheSlot }; | 
1483  | 0  |   WINPR_ASSERT(context->EvictCacheEntry);  | 
1484  | 0  |   context->EvictCacheEntry(context, &evict);  | 
1485  |  | 
  | 
1486  | 0  |   WINPR_ASSERT(context->SetCacheSlotData);  | 
1487  | 0  |   rc = context->SetCacheSlotData(context, surfaceToCache->cacheSlot, (void*)cacheEntry);  | 
1488  | 0  | fail:  | 
1489  | 0  |   if (rc != CHANNEL_RC_OK)  | 
1490  | 0  |     gdi_GfxCacheEntryFree(cacheEntry);  | 
1491  | 0  |   LeaveCriticalSection(&context->mux);  | 
1492  | 0  |   return rc;  | 
1493  | 0  | }  | 
1494  |  |  | 
1495  |  | /**  | 
1496  |  |  * Function description  | 
1497  |  |  *  | 
1498  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1499  |  |  */  | 
1500  |  | static UINT gdi_CacheToSurface(RdpgfxClientContext* context,  | 
1501  |  |                                const RDPGFX_CACHE_TO_SURFACE_PDU* cacheToSurface)  | 
1502  | 0  | { | 
1503  | 0  |   UINT status = ERROR_INTERNAL_ERROR;  | 
1504  | 0  |   gdiGfxSurface* surface = NULL;  | 
1505  | 0  |   gdiGfxCacheEntry* cacheEntry = NULL;  | 
1506  | 0  |   RECTANGLE_16 invalidRect;  | 
1507  | 0  |   rdpGdi* gdi = (rdpGdi*)context->custom;  | 
1508  |  | 
  | 
1509  | 0  |   EnterCriticalSection(&context->mux);  | 
1510  |  | 
  | 
1511  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
1512  | 0  |   surface = (gdiGfxSurface*)context->GetSurfaceData(context, cacheToSurface->surfaceId);  | 
1513  |  | 
  | 
1514  | 0  |   WINPR_ASSERT(context->GetCacheSlotData);  | 
1515  | 0  |   cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheToSurface->cacheSlot);  | 
1516  |  | 
  | 
1517  | 0  |   if (!surface || !cacheEntry)  | 
1518  | 0  |     goto fail;  | 
1519  |  |  | 
1520  | 0  |   for (UINT16 index = 0; index < cacheToSurface->destPtsCount; index++)  | 
1521  | 0  |   { | 
1522  | 0  |     const RDPGFX_POINT16* destPt = &cacheToSurface->destPts[index];  | 
1523  | 0  |     const RECTANGLE_16 rect = { destPt->x, destPt->y, | 
1524  | 0  |                               (UINT16)MIN(UINT16_MAX, destPt->x + cacheEntry->width),  | 
1525  | 0  |                               (UINT16)MIN(UINT16_MAX, destPt->y + cacheEntry->height) };  | 
1526  |  | 
  | 
1527  | 0  |     if (rectangle_is_empty(&rect))  | 
1528  | 0  |       continue;  | 
1529  |  |  | 
1530  | 0  |     if (!is_rect_valid(&rect, surface->width, surface->height))  | 
1531  | 0  |       goto fail;  | 
1532  |  |  | 
1533  | 0  |     if (!freerdp_image_copy_no_overlap(surface->data, surface->format, surface->scanline,  | 
1534  | 0  |                                        destPt->x, destPt->y, cacheEntry->width,  | 
1535  | 0  |                                        cacheEntry->height, cacheEntry->data, cacheEntry->format,  | 
1536  | 0  |                                        cacheEntry->scanline, 0, 0, NULL, FREERDP_FLIP_NONE))  | 
1537  | 0  |       goto fail;  | 
1538  |  |  | 
1539  | 0  |     invalidRect = rect;  | 
1540  | 0  |     region16_union_rect(&surface->invalidRegion, &surface->invalidRegion, &invalidRect);  | 
1541  | 0  |     status = IFCALLRESULT(CHANNEL_RC_OK, context->UpdateSurfaceArea, context,  | 
1542  | 0  |                           surface->surfaceId, 1, &invalidRect);  | 
1543  |  | 
  | 
1544  | 0  |     if (status != CHANNEL_RC_OK)  | 
1545  | 0  |       goto fail;  | 
1546  | 0  |   }  | 
1547  |  |  | 
1548  | 0  |   LeaveCriticalSection(&context->mux);  | 
1549  |  | 
  | 
1550  | 0  |   return gdi_interFrameUpdate(gdi, context);  | 
1551  |  |  | 
1552  | 0  | fail:  | 
1553  | 0  |   LeaveCriticalSection(&context->mux);  | 
1554  | 0  |   return status;  | 
1555  | 0  | }  | 
1556  |  |  | 
1557  |  | /**  | 
1558  |  |  * Function description  | 
1559  |  |  *  | 
1560  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1561  |  |  */  | 
1562  |  | static UINT gdi_CacheImportReply(RdpgfxClientContext* context,  | 
1563  |  |                                  const RDPGFX_CACHE_IMPORT_REPLY_PDU* cacheImportReply)  | 
1564  | 0  | { | 
1565  | 0  |   UINT16 count = 0;  | 
1566  | 0  |   const UINT16* slots = NULL;  | 
1567  | 0  |   UINT error = CHANNEL_RC_OK;  | 
1568  |  | 
  | 
1569  | 0  |   slots = cacheImportReply->cacheSlots;  | 
1570  | 0  |   count = cacheImportReply->importedEntriesCount;  | 
1571  |  | 
  | 
1572  | 0  |   for (UINT16 index = 0; index < count; index++)  | 
1573  | 0  |   { | 
1574  | 0  |     UINT16 cacheSlot = slots[index];  | 
1575  |  | 
  | 
1576  | 0  |     if (cacheSlot == 0)  | 
1577  | 0  |       continue;  | 
1578  |  |  | 
1579  | 0  |     WINPR_ASSERT(context->GetCacheSlotData);  | 
1580  | 0  |     gdiGfxCacheEntry* cacheEntry =  | 
1581  | 0  |         (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);  | 
1582  |  | 
  | 
1583  | 0  |     if (cacheEntry)  | 
1584  | 0  |       continue;  | 
1585  |  |  | 
1586  | 0  |     cacheEntry = gdi_GfxCacheEntryNew(cacheSlot, 0, 0, PIXEL_FORMAT_BGRX32);  | 
1587  |  | 
  | 
1588  | 0  |     if (!cacheEntry)  | 
1589  | 0  |       return ERROR_INTERNAL_ERROR;  | 
1590  |  |  | 
1591  | 0  |     WINPR_ASSERT(context->SetCacheSlotData);  | 
1592  | 0  |     error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);  | 
1593  |  | 
  | 
1594  | 0  |     if (error)  | 
1595  | 0  |     { | 
1596  | 0  |       WLog_ERR(TAG, "CacheImportReply: SetCacheSlotData failed with error %" PRIu32 "",  | 
1597  | 0  |                error);  | 
1598  | 0  |       gdi_GfxCacheEntryFree(cacheEntry);  | 
1599  | 0  |       break;  | 
1600  | 0  |     }  | 
1601  | 0  |   }  | 
1602  |  |  | 
1603  | 0  |   return error;  | 
1604  | 0  | }  | 
1605  |  |  | 
1606  |  | static UINT gdi_ImportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,  | 
1607  |  |                                  const PERSISTENT_CACHE_ENTRY* importCacheEntry)  | 
1608  | 0  | { | 
1609  | 0  |   UINT error = ERROR_INTERNAL_ERROR;  | 
1610  | 0  |   gdiGfxCacheEntry* cacheEntry = NULL;  | 
1611  |  | 
  | 
1612  | 0  |   if (cacheSlot == 0)  | 
1613  | 0  |     return CHANNEL_RC_OK;  | 
1614  |  |  | 
1615  | 0  |   cacheEntry = gdi_GfxCacheEntryNew(importCacheEntry->key64, importCacheEntry->width,  | 
1616  | 0  |                                     importCacheEntry->height, PIXEL_FORMAT_BGRX32);  | 
1617  |  | 
  | 
1618  | 0  |   if (!cacheEntry)  | 
1619  | 0  |     goto fail;  | 
1620  |  |  | 
1621  | 0  |   if (!freerdp_image_copy_no_overlap(cacheEntry->data, cacheEntry->format, cacheEntry->scanline,  | 
1622  | 0  |                                      0, 0, cacheEntry->width, cacheEntry->height,  | 
1623  | 0  |                                      importCacheEntry->data, PIXEL_FORMAT_BGRX32, 0, 0, 0, NULL,  | 
1624  | 0  |                                      FREERDP_FLIP_NONE))  | 
1625  | 0  |     goto fail;  | 
1626  |  |  | 
1627  | 0  |   RDPGFX_EVICT_CACHE_ENTRY_PDU evict = { cacheSlot }; | 
1628  | 0  |   WINPR_ASSERT(context->EvictCacheEntry);  | 
1629  | 0  |   error = context->EvictCacheEntry(context, &evict);  | 
1630  | 0  |   if (error != CHANNEL_RC_OK)  | 
1631  | 0  |     goto fail;  | 
1632  |  |  | 
1633  | 0  |   WINPR_ASSERT(context->SetCacheSlotData);  | 
1634  | 0  |   error = context->SetCacheSlotData(context, cacheSlot, (void*)cacheEntry);  | 
1635  |  | 
  | 
1636  | 0  | fail:  | 
1637  | 0  |   if (error)  | 
1638  | 0  |   { | 
1639  | 0  |     gdi_GfxCacheEntryFree(cacheEntry);  | 
1640  | 0  |     WLog_ERR(TAG, "ImportCacheEntry: SetCacheSlotData failed with error %" PRIu32 "", error);  | 
1641  | 0  |   }  | 
1642  |  | 
  | 
1643  | 0  |   return error;  | 
1644  | 0  | }  | 
1645  |  |  | 
1646  |  | static UINT gdi_ExportCacheEntry(RdpgfxClientContext* context, UINT16 cacheSlot,  | 
1647  |  |                                  PERSISTENT_CACHE_ENTRY* exportCacheEntry)  | 
1648  | 0  | { | 
1649  | 0  |   gdiGfxCacheEntry* cacheEntry = NULL;  | 
1650  |  | 
  | 
1651  | 0  |   WINPR_ASSERT(context->GetCacheSlotData);  | 
1652  | 0  |   cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, cacheSlot);  | 
1653  |  | 
  | 
1654  | 0  |   if (cacheEntry)  | 
1655  | 0  |   { | 
1656  | 0  |     exportCacheEntry->key64 = cacheEntry->cacheKey;  | 
1657  | 0  |     exportCacheEntry->width = (UINT16)MIN(UINT16_MAX, cacheEntry->width);  | 
1658  | 0  |     exportCacheEntry->height = (UINT16)MIN(UINT16_MAX, cacheEntry->height);  | 
1659  | 0  |     exportCacheEntry->size = cacheEntry->width * cacheEntry->height * 4;  | 
1660  | 0  |     exportCacheEntry->flags = 0;  | 
1661  | 0  |     exportCacheEntry->data = cacheEntry->data;  | 
1662  | 0  |     return CHANNEL_RC_OK;  | 
1663  | 0  |   }  | 
1664  |  |  | 
1665  | 0  |   return ERROR_NOT_FOUND;  | 
1666  | 0  | }  | 
1667  |  |  | 
1668  |  | /**  | 
1669  |  |  * Function description  | 
1670  |  |  *  | 
1671  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1672  |  |  */  | 
1673  |  | static UINT gdi_EvictCacheEntry(RdpgfxClientContext* context,  | 
1674  |  |                                 const RDPGFX_EVICT_CACHE_ENTRY_PDU* evictCacheEntry)  | 
1675  | 0  | { | 
1676  | 0  |   gdiGfxCacheEntry* cacheEntry = NULL;  | 
1677  | 0  |   UINT rc = ERROR_NOT_FOUND;  | 
1678  |  | 
  | 
1679  | 0  |   WINPR_ASSERT(context);  | 
1680  | 0  |   WINPR_ASSERT(evictCacheEntry);  | 
1681  |  |  | 
1682  | 0  |   EnterCriticalSection(&context->mux);  | 
1683  |  | 
  | 
1684  | 0  |   WINPR_ASSERT(context->GetCacheSlotData);  | 
1685  | 0  |   cacheEntry = (gdiGfxCacheEntry*)context->GetCacheSlotData(context, evictCacheEntry->cacheSlot);  | 
1686  |  | 
  | 
1687  | 0  |   gdi_GfxCacheEntryFree(cacheEntry);  | 
1688  |  | 
  | 
1689  | 0  |   WINPR_ASSERT(context->SetCacheSlotData);  | 
1690  | 0  |   rc = context->SetCacheSlotData(context, evictCacheEntry->cacheSlot, NULL);  | 
1691  | 0  |   LeaveCriticalSection(&context->mux);  | 
1692  | 0  |   return rc;  | 
1693  | 0  | }  | 
1694  |  |  | 
1695  |  | /**  | 
1696  |  |  * Function description  | 
1697  |  |  *  | 
1698  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1699  |  |  */  | 
1700  |  | static UINT gdi_MapSurfaceToOutput(RdpgfxClientContext* context,  | 
1701  |  |                                    const RDPGFX_MAP_SURFACE_TO_OUTPUT_PDU* surfaceToOutput)  | 
1702  | 0  | { | 
1703  | 0  |   UINT rc = ERROR_INTERNAL_ERROR;  | 
1704  | 0  |   gdiGfxSurface* surface = NULL;  | 
1705  | 0  |   EnterCriticalSection(&context->mux);  | 
1706  |  | 
  | 
1707  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
1708  | 0  |   surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);  | 
1709  |  | 
  | 
1710  | 0  |   if (!surface)  | 
1711  | 0  |     goto fail;  | 
1712  |  |  | 
1713  | 0  |   if (surface->windowMapped)  | 
1714  | 0  |   { | 
1715  | 0  |     WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");  | 
1716  | 0  |     goto fail;  | 
1717  | 0  |   }  | 
1718  |  |  | 
1719  | 0  |   surface->outputMapped = TRUE;  | 
1720  | 0  |   surface->outputOriginX = surfaceToOutput->outputOriginX;  | 
1721  | 0  |   surface->outputOriginY = surfaceToOutput->outputOriginY;  | 
1722  | 0  |   surface->outputTargetWidth = surface->mappedWidth;  | 
1723  | 0  |   surface->outputTargetHeight = surface->mappedHeight;  | 
1724  | 0  |   region16_clear(&surface->invalidRegion);  | 
1725  | 0  |   rc = CHANNEL_RC_OK;  | 
1726  | 0  | fail:  | 
1727  | 0  |   LeaveCriticalSection(&context->mux);  | 
1728  | 0  |   return rc;  | 
1729  | 0  | }  | 
1730  |  |  | 
1731  |  | static UINT  | 
1732  |  | gdi_MapSurfaceToScaledOutput(RdpgfxClientContext* context,  | 
1733  |  |                              const RDPGFX_MAP_SURFACE_TO_SCALED_OUTPUT_PDU* surfaceToOutput)  | 
1734  | 0  | { | 
1735  | 0  |   UINT rc = ERROR_INTERNAL_ERROR;  | 
1736  | 0  |   gdiGfxSurface* surface = NULL;  | 
1737  | 0  |   EnterCriticalSection(&context->mux);  | 
1738  |  | 
  | 
1739  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
1740  | 0  |   surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToOutput->surfaceId);  | 
1741  |  | 
  | 
1742  | 0  |   if (!surface)  | 
1743  | 0  |     goto fail;  | 
1744  |  |  | 
1745  | 0  |   if (surface->windowMapped)  | 
1746  | 0  |   { | 
1747  | 0  |     WLog_WARN(TAG, "surface already windowMapped when trying to set outputMapped");  | 
1748  | 0  |     goto fail;  | 
1749  | 0  |   }  | 
1750  |  |  | 
1751  | 0  |   surface->outputMapped = TRUE;  | 
1752  | 0  |   surface->outputOriginX = surfaceToOutput->outputOriginX;  | 
1753  | 0  |   surface->outputOriginY = surfaceToOutput->outputOriginY;  | 
1754  | 0  |   surface->outputTargetWidth = surfaceToOutput->targetWidth;  | 
1755  | 0  |   surface->outputTargetHeight = surfaceToOutput->targetHeight;  | 
1756  | 0  |   region16_clear(&surface->invalidRegion);  | 
1757  | 0  |   rc = CHANNEL_RC_OK;  | 
1758  | 0  | fail:  | 
1759  | 0  |   LeaveCriticalSection(&context->mux);  | 
1760  | 0  |   return rc;  | 
1761  | 0  | }  | 
1762  |  |  | 
1763  |  | /**  | 
1764  |  |  * Function description  | 
1765  |  |  *  | 
1766  |  |  * @return 0 on success, otherwise a Win32 error code  | 
1767  |  |  */  | 
1768  |  | static UINT gdi_MapSurfaceToWindow(RdpgfxClientContext* context,  | 
1769  |  |                                    const RDPGFX_MAP_SURFACE_TO_WINDOW_PDU* surfaceToWindow)  | 
1770  | 0  | { | 
1771  | 0  |   UINT rc = ERROR_INTERNAL_ERROR;  | 
1772  | 0  |   gdiGfxSurface* surface = NULL;  | 
1773  | 0  |   EnterCriticalSection(&context->mux);  | 
1774  |  | 
  | 
1775  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
1776  | 0  |   surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);  | 
1777  |  | 
  | 
1778  | 0  |   if (!surface)  | 
1779  | 0  |     goto fail;  | 
1780  |  |  | 
1781  | 0  |   if (surface->outputMapped)  | 
1782  | 0  |   { | 
1783  | 0  |     WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");  | 
1784  | 0  |     goto fail;  | 
1785  | 0  |   }  | 
1786  |  |  | 
1787  | 0  |   if (surface->windowMapped)  | 
1788  | 0  |   { | 
1789  | 0  |     if (surface->windowId != surfaceToWindow->windowId)  | 
1790  | 0  |     { | 
1791  | 0  |       WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,  | 
1792  | 0  |                 surface->windowId, surfaceToWindow->windowId);  | 
1793  | 0  |       goto fail;  | 
1794  | 0  |     }  | 
1795  | 0  |   }  | 
1796  | 0  |   surface->windowMapped = TRUE;  | 
1797  |  | 
  | 
1798  | 0  |   surface->windowId = surfaceToWindow->windowId;  | 
1799  | 0  |   surface->mappedWidth = surfaceToWindow->mappedWidth;  | 
1800  | 0  |   surface->mappedHeight = surfaceToWindow->mappedHeight;  | 
1801  | 0  |   surface->outputTargetWidth = surfaceToWindow->mappedWidth;  | 
1802  | 0  |   surface->outputTargetHeight = surfaceToWindow->mappedHeight;  | 
1803  | 0  |   rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,  | 
1804  | 0  |                     surfaceToWindow->surfaceId, surfaceToWindow->windowId);  | 
1805  | 0  | fail:  | 
1806  | 0  |   LeaveCriticalSection(&context->mux);  | 
1807  | 0  |   return rc;  | 
1808  | 0  | }  | 
1809  |  |  | 
1810  |  | static UINT  | 
1811  |  | gdi_MapSurfaceToScaledWindow(RdpgfxClientContext* context,  | 
1812  |  |                              const RDPGFX_MAP_SURFACE_TO_SCALED_WINDOW_PDU* surfaceToWindow)  | 
1813  | 0  | { | 
1814  | 0  |   UINT rc = ERROR_INTERNAL_ERROR;  | 
1815  | 0  |   gdiGfxSurface* surface = NULL;  | 
1816  | 0  |   EnterCriticalSection(&context->mux);  | 
1817  |  | 
  | 
1818  | 0  |   WINPR_ASSERT(context->GetSurfaceData);  | 
1819  | 0  |   surface = (gdiGfxSurface*)context->GetSurfaceData(context, surfaceToWindow->surfaceId);  | 
1820  |  | 
  | 
1821  | 0  |   if (!surface)  | 
1822  | 0  |     goto fail;  | 
1823  |  |  | 
1824  | 0  |   if (surface->outputMapped)  | 
1825  | 0  |   { | 
1826  | 0  |     WLog_WARN(TAG, "surface already outputMapped when trying to set windowMapped");  | 
1827  | 0  |     goto fail;  | 
1828  | 0  |   }  | 
1829  |  |  | 
1830  | 0  |   if (surface->windowMapped)  | 
1831  | 0  |   { | 
1832  | 0  |     if (surface->windowId != surfaceToWindow->windowId)  | 
1833  | 0  |     { | 
1834  | 0  |       WLog_WARN(TAG, "surface windowId mismatch, has %" PRIu64 ", expected %" PRIu64,  | 
1835  | 0  |                 surface->windowId, surfaceToWindow->windowId);  | 
1836  | 0  |       goto fail;  | 
1837  | 0  |     }  | 
1838  | 0  |   }  | 
1839  | 0  |   surface->windowMapped = TRUE;  | 
1840  |  | 
  | 
1841  | 0  |   surface->windowId = surfaceToWindow->windowId;  | 
1842  | 0  |   surface->mappedWidth = surfaceToWindow->mappedWidth;  | 
1843  | 0  |   surface->mappedHeight = surfaceToWindow->mappedHeight;  | 
1844  | 0  |   surface->outputTargetWidth = surfaceToWindow->targetWidth;  | 
1845  | 0  |   surface->outputTargetHeight = surfaceToWindow->targetHeight;  | 
1846  | 0  |   rc = IFCALLRESULT(CHANNEL_RC_OK, context->MapWindowForSurface, context,  | 
1847  | 0  |                     surfaceToWindow->surfaceId, surfaceToWindow->windowId);  | 
1848  | 0  | fail:  | 
1849  | 0  |   LeaveCriticalSection(&context->mux);  | 
1850  | 0  |   return rc;  | 
1851  | 0  | }  | 
1852  |  |  | 
1853  |  | BOOL gdi_graphics_pipeline_init(rdpGdi* gdi, RdpgfxClientContext* gfx)  | 
1854  | 0  | { | 
1855  | 0  |   return gdi_graphics_pipeline_init_ex(gdi, gfx, NULL, NULL, NULL);  | 
1856  | 0  | }  | 
1857  |  |  | 
1858  |  | BOOL gdi_graphics_pipeline_init_ex(rdpGdi* gdi, RdpgfxClientContext* gfx,  | 
1859  |  |                                    pcRdpgfxMapWindowForSurface map,  | 
1860  |  |                                    pcRdpgfxUnmapWindowForSurface unmap,  | 
1861  |  |                                    pcRdpgfxUpdateSurfaceArea update)  | 
1862  | 0  | { | 
1863  | 0  |   if (!gdi || !gfx || !gdi->context || !gdi->context->settings)  | 
1864  | 0  |     return FALSE;  | 
1865  |  |  | 
1866  | 0  |   rdpContext* context = gdi->context;  | 
1867  | 0  |   rdpSettings* settings = context->settings;  | 
1868  |  | 
  | 
1869  | 0  |   gdi->gfx = gfx;  | 
1870  | 0  |   gfx->custom = (void*)gdi;  | 
1871  | 0  |   gfx->ResetGraphics = gdi_ResetGraphics;  | 
1872  | 0  |   gfx->StartFrame = gdi_StartFrame;  | 
1873  | 0  |   gfx->EndFrame = gdi_EndFrame;  | 
1874  | 0  |   gfx->SurfaceCommand = gdi_SurfaceCommand;  | 
1875  | 0  |   gfx->DeleteEncodingContext = gdi_DeleteEncodingContext;  | 
1876  | 0  |   gfx->CreateSurface = gdi_CreateSurface;  | 
1877  | 0  |   gfx->DeleteSurface = gdi_DeleteSurface;  | 
1878  | 0  |   gfx->SolidFill = gdi_SolidFill;  | 
1879  | 0  |   gfx->SurfaceToSurface = gdi_SurfaceToSurface;  | 
1880  | 0  |   gfx->SurfaceToCache = gdi_SurfaceToCache;  | 
1881  | 0  |   gfx->CacheToSurface = gdi_CacheToSurface;  | 
1882  | 0  |   gfx->CacheImportReply = gdi_CacheImportReply;  | 
1883  | 0  |   gfx->ImportCacheEntry = gdi_ImportCacheEntry;  | 
1884  | 0  |   gfx->ExportCacheEntry = gdi_ExportCacheEntry;  | 
1885  | 0  |   gfx->EvictCacheEntry = gdi_EvictCacheEntry;  | 
1886  | 0  |   gfx->MapSurfaceToOutput = gdi_MapSurfaceToOutput;  | 
1887  | 0  |   gfx->MapSurfaceToWindow = gdi_MapSurfaceToWindow;  | 
1888  | 0  |   gfx->MapSurfaceToScaledOutput = gdi_MapSurfaceToScaledOutput;  | 
1889  | 0  |   gfx->MapSurfaceToScaledWindow = gdi_MapSurfaceToScaledWindow;  | 
1890  | 0  |   gfx->UpdateSurfaces = gdi_UpdateSurfaces;  | 
1891  | 0  |   gfx->MapWindowForSurface = map;  | 
1892  | 0  |   gfx->UnmapWindowForSurface = unmap;  | 
1893  | 0  |   gfx->UpdateSurfaceArea = update;  | 
1894  |  | 
  | 
1895  | 0  |   if (!freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))  | 
1896  | 0  |   { | 
1897  | 0  |     const UINT32 w = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);  | 
1898  | 0  |     const UINT32 h = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);  | 
1899  | 0  |     const UINT32 flags = freerdp_settings_get_uint32(settings, FreeRDP_ThreadingFlags);  | 
1900  |  | 
  | 
1901  | 0  |     gfx->codecs = freerdp_client_codecs_new(flags);  | 
1902  | 0  |     if (!gfx->codecs)  | 
1903  | 0  |       return FALSE;  | 
1904  | 0  |     if (!freerdp_client_codecs_prepare(gfx->codecs, FREERDP_CODEC_ALL, w, h))  | 
1905  | 0  |       return FALSE;  | 
1906  | 0  |   }  | 
1907  | 0  |   InitializeCriticalSection(&gfx->mux);  | 
1908  | 0  |   PROFILER_CREATE(gfx->SurfaceProfiler, "GFX-PROFILER")  | 
1909  |  |  | 
1910  |  |   /**  | 
1911  |  |    * gdi->graphicsReset will be removed in FreeRDP v3 from public headers,  | 
1912  |  |    * since the EGFX Reset Graphics PDU seems to be optional.  | 
1913  |  |    * There are still some clients that expect and check it and therefore  | 
1914  |  |    * we simply initialize it with TRUE here for now.  | 
1915  |  |    */  | 
1916  | 0  |   gdi->graphicsReset = TRUE;  | 
1917  | 0  |   if (freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding))  | 
1918  | 0  |   { | 
1919  | 0  |     gfx->UpdateSurfaceArea = NULL;  | 
1920  | 0  |     gfx->UpdateSurfaces = NULL;  | 
1921  | 0  |     gfx->SurfaceCommand = NULL;  | 
1922  | 0  |   }  | 
1923  |  | 
  | 
1924  | 0  |   return TRUE;  | 
1925  | 0  | }  | 
1926  |  |  | 
1927  |  | void gdi_graphics_pipeline_uninit(rdpGdi* gdi, RdpgfxClientContext* gfx)  | 
1928  | 0  | { | 
1929  | 0  |   if (gdi)  | 
1930  | 0  |     gdi->gfx = NULL;  | 
1931  |  | 
  | 
1932  | 0  |   if (!gfx)  | 
1933  | 0  |     return;  | 
1934  |  |  | 
1935  | 0  |   gfx->custom = NULL;  | 
1936  | 0  |   freerdp_client_codecs_free(gfx->codecs);  | 
1937  | 0  |   gfx->codecs = NULL;  | 
1938  | 0  |   DeleteCriticalSection(&gfx->mux);  | 
1939  | 0  |   PROFILER_PRINT_HEADER  | 
1940  | 0  |   PROFILER_PRINT(gfx->SurfaceProfiler)  | 
1941  | 0  |   PROFILER_PRINT_FOOTER  | 
1942  | 0  |   PROFILER_FREE(gfx->SurfaceProfiler)  | 
1943  | 0  | }  | 
1944  |  |  | 
1945  |  | const char* rdpgfx_caps_version_str(UINT32 capsVersion)  | 
1946  | 0  | { | 
1947  | 0  |   switch (capsVersion)  | 
1948  | 0  |   { | 
1949  | 0  |     case RDPGFX_CAPVERSION_8:  | 
1950  | 0  |       return "RDPGFX_CAPVERSION_8";  | 
1951  | 0  |     case RDPGFX_CAPVERSION_81:  | 
1952  | 0  |       return "RDPGFX_CAPVERSION_81";  | 
1953  | 0  |     case RDPGFX_CAPVERSION_10:  | 
1954  | 0  |       return "RDPGFX_CAPVERSION_10";  | 
1955  | 0  |     case RDPGFX_CAPVERSION_101:  | 
1956  | 0  |       return "RDPGFX_CAPVERSION_101";  | 
1957  | 0  |     case RDPGFX_CAPVERSION_102:  | 
1958  | 0  |       return "RDPGFX_CAPVERSION_102";  | 
1959  | 0  |     case RDPGFX_CAPVERSION_103:  | 
1960  | 0  |       return "RDPGFX_CAPVERSION_103";  | 
1961  | 0  |     case RDPGFX_CAPVERSION_104:  | 
1962  | 0  |       return "RDPGFX_CAPVERSION_104";  | 
1963  | 0  |     case RDPGFX_CAPVERSION_105:  | 
1964  | 0  |       return "RDPGFX_CAPVERSION_105";  | 
1965  | 0  |     case RDPGFX_CAPVERSION_106:  | 
1966  | 0  |       return "RDPGFX_CAPVERSION_106";  | 
1967  | 0  |     case RDPGFX_CAPVERSION_106_ERR:  | 
1968  | 0  |       return "RDPGFX_CAPVERSION_106_ERR";  | 
1969  | 0  |     case RDPGFX_CAPVERSION_107:  | 
1970  | 0  |       return "RDPGFX_CAPVERSION_107";  | 
1971  | 0  |     default:  | 
1972  | 0  |       return "RDPGFX_CAPVERSION_UNKNOWN";  | 
1973  | 0  |   }  | 
1974  | 0  | }  |