GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_canvas_create.c Lines: 30 30 100.0 %
Date: 2026-03-06 19:21:09 Branches: 4 4 100.0 %

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