1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** GUIX Component                                                        */
16 /**                                                                       */
17 /**   Canvas Management (Canvas)                                          */
18 /**                                                                       */
19 /**************************************************************************/
20 
21 #define GX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "gx_api.h"
27 #include "gx_system.h"
28 #include "gx_utility.h"
29 #include "gx_canvas.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _gx_canvas_create                                   PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Kenneth Maxwell, Microsoft Corporation                              */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function creates a canvas associated with the specified        */
45 /*    display.                                                            */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    canvas                                Canvas control block          */
50 /*    name                                  Name of canvas                */
51 /*    display                               Display control block         */
52 /*    type                                  Type of canvas                */
53 /*    width                                 Width of canvas               */
54 /*    height                                Height of canvas              */
55 /*    memory_area                           Memory area of canvas with    */
56 /*                                            each pixel of GX_COLOR      */
57 /*    memory_size                           Size of canvas memory area    */
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    status                                Completion status             */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*    memset                                Set control block and canvas  */
66 /*                                            memory to zero              */
67 /*    _gx_utility_rectangle_define          Define a rectangle            */
68 /*                                                                        */
69 /*  CALLED BY                                                             */
70 /*                                                                        */
71 /*    Application Code                                                    */
72 /*    _gx_animation_canvas_define                                         */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
79 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
80 /*                                            resulting in version 6.1    */
81 /*  12-31-2020     Kenneth Maxwell          Modified comment(s),          */
82 /*                                            resulting in version 6.1.3  */
83 /*  10-31-2023     Ting Zhu                 Modified comment(s),          */
84 /*                                            added initilization to the  */
85 /*                                            canvas members,             */
86 /*                                            resulting in version 6.3.0  */
87 /*                                                                        */
88 /**************************************************************************/
_gx_canvas_create(GX_CANVAS * canvas,GX_CONST GX_CHAR * name,GX_DISPLAY * display,UINT type,UINT width,UINT height,GX_COLOR * memory_area,ULONG memory_size)89 UINT  _gx_canvas_create(GX_CANVAS *canvas, GX_CONST GX_CHAR *name, GX_DISPLAY *display,
90                         UINT type, UINT width, UINT height, GX_COLOR *memory_area, ULONG memory_size)
91 {
92     /* Clear the canvas control block.  */
93     memset(canvas, 0, sizeof(GX_CANVAS));
94 
95     /* Setup the canvas.  */
96     canvas -> gx_canvas_display =           display;
97     canvas -> gx_canvas_memory =            memory_area;
98     canvas -> gx_canvas_memory_size =       memory_size;
99     canvas -> gx_canvas_alpha =             GX_ALPHA_VALUE_OPAQUE;
100     canvas -> gx_canvas_display_offset_x =  0;
101     canvas -> gx_canvas_display_offset_y =  0;
102     canvas -> gx_canvas_draw_count =        0;
103     canvas -> gx_canvas_draw_nesting =      0;
104     canvas -> gx_canvas_dirty_count =       0;
105     canvas -> gx_canvas_status =            type;
106     canvas -> gx_canvas_x_resolution =      (GX_VALUE)width;
107     canvas -> gx_canvas_y_resolution =      (GX_VALUE)height;
108 #ifdef GX_ENABLE_CANVAS_PARTIAL_FRAME_BUFFER
109     canvas -> gx_canvas_memory_width =      (GX_VALUE)width;
110     canvas -> gx_canvas_memory_height =     (GX_VALUE)height;
111 #endif
112     canvas -> gx_canvas_hardware_layer =    (GX_BYTE)-1;
113 
114     /* Determine if there is a memory area.  */
115     if (memory_area)
116     {
117         /* Yes, clear it!  */
118         memset(memory_area, 0, memory_size);
119     }
120 
121     /* Setup the dirty area.  */
122     _gx_utility_rectangle_define(&canvas -> gx_canvas_dirty_area, 0, 0, -1, -1);
123 
124     /* Now link the canvas control block on the list of created canvases.  */
125 
126     /* Load the canvas ID field in the canvas control block.  */
127     canvas -> gx_canvas_id =  GX_CANVAS_ID;
128 
129     /* Save the canvas name.  */
130     canvas -> gx_canvas_name =  name;
131 
132     /* If running on Win32, create padded memory, only if needed */
133 #ifdef GX_TARGET_WIN32
134     _win32_compatible_canvas_memory_allocate(canvas);
135 #endif
136 
137     /* initialize previous and next pointers */
138     canvas -> gx_canvas_created_previous =  GX_NULL;
139     canvas -> gx_canvas_created_next = GX_NULL;
140 
141     /* lock access to gx_system */
142 
143     GX_ENTER_CRITICAL
144 
145     /* Place the canvas on the list of created canvass.  First,
146        check for an empty list.  */
147     _gx_system_canvas_created_count++;
148 
149     if (_gx_system_canvas_created_count > 1)
150     {
151         /* Place the new canvas in the list.  */
152         _gx_system_canvas_created_list -> gx_canvas_created_previous = canvas;
153         canvas -> gx_canvas_created_next = _gx_system_canvas_created_list;
154     }
155 
156     /* point start of list at this new canvas */
157     _gx_system_canvas_created_list = canvas;
158 
159     /* unlock gx_system.  */
160     GX_EXIT_CRITICAL
161 
162     /* Return successful status.  */
163     return(GX_SUCCESS);
164 }
165 
166 #ifdef GX_TARGET_WIN32
_win32_compatible_canvas_memory_allocate(GX_CANVAS * canvas)167 VOID _win32_compatible_canvas_memory_allocate(GX_CANVAS *canvas)
168 {
169 USHORT      rotation;
170 INT         padded_width;
171 USHORT      row_byte_width;
172 INT         color_format = GX_COLOR_FORMAT_565RGB;
173 GX_DISPLAY *display = canvas -> gx_canvas_display;
174 
175     /* Windows bitmaps must be padded to an even multiple of 4 bytes in width.
176        When the GUIX canvas does not meet this requirement, we create a padded canvas memory
177        so that we can pass the padded canvas memory off to Windows for display as a bitmap.
178        This happens often when running at sub-byte color depth, but can occur at any color depth
179        if the display resolution is very odd. */
180 
181     padded_width = canvas -> gx_canvas_x_resolution;
182 
183     if (display == GX_NULL)
184     {
185         return;
186     }
187 
188     rotation = display -> gx_display_rotation_angle;
189 
190     padded_width = canvas -> gx_canvas_x_resolution;
191 
192     row_byte_width = display -> gx_display_driver_row_pitch_get(padded_width);
193 
194     while (row_byte_width % 4)
195     {
196         padded_width++;
197         row_byte_width = display -> gx_display_driver_row_pitch_get(padded_width);
198     }
199 
200     if ((padded_width != canvas -> gx_canvas_x_resolution) || rotation)
201     {
202         /* We are forced to create a padded buffer to hold Win32 compatible canvas memory. */
203         canvas -> gx_canvas_padded_memory = (GX_COLOR *)malloc(row_byte_width * canvas -> gx_canvas_y_resolution);
204     }
205 }
206 
207 #endif
208 
209