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 /**   Display Management (Display)                                        */
18 /**                                                                       */
19 /**************************************************************************/
20 
21 #define GX_SOURCE_CODE
22 
23 /* Include necessary system files.  */
24 
25 #include "gx_api.h"
26 #include "gx_display.h"
27 #include "gx_utility.h"
28 
29 #define ALPHAVAL(_c) (GX_UBYTE)(((_c) >> 12) & 0xf)
30 #define REDVAL(_c)   (GX_UBYTE)(((_c) >> 8) & 0xf)
31 #define GREENVAL(_c) (GX_UBYTE)(((_c) >> 4) & 0xf)
32 #define BLUEVAL(_c)  (GX_UBYTE)((_c) & 0xf)
33 
34 #define ASSEMBLECOLOR(_a, _r, _g, _b) \
35     ((((_a) & 0xf) << 12)   |         \
36      (((_r) & 0xf) << 8)   |          \
37      (((_g) & 0xf) << 4)    |         \
38      ((_b) & 0xf))
39 
40 
41 /**************************************************************************/
42 /*                                                                        */
43 /*  FUNCTION                                               RELEASE        */
44 /*                                                                        */
45 /*    _gx_display_driver_4444argb_canvas_blend            PORTABLE C      */
46 /*                                                           6.1          */
47 /*  AUTHOR                                                                */
48 /*                                                                        */
49 /*    Kenneth Maxwell, Microsoft Corporation                              */
50 /*                                                                        */
51 /*  DESCRIPTION                                                           */
52 /*                                                                        */
53 /*    Canvas blend function for 4444argb color foramt.                    */
54 /*                                                                        */
55 /*  INPUT                                                                 */
56 /*                                                                        */
57 /*   canvas                                 The canvas to blend to        */
58 /*   composite                              The canvas to blend from      */
59 /*                                                                        */
60 /*  OUTPUT                                                                */
61 /*                                                                        */
62 /*    None                                                                */
63 /*                                                                        */
64 /*  CALLS                                                                 */
65 /*                                                                        */
66 /*    _gx_utility_rectangle_shift           Adjust the rectangle          */
67 /*    _gx_utility_recttangle_overlap_detect Detect whether two areas      */
68 /*                                            overlap                     */
69 /*    REDVAL                                Extrace Red from canvas       */
70 /*    GREENVAL                              Extrace Green from canvas     */
71 /*    BLUEVAL                               Extrace Blue from canvas      */
72 /*    ASSEMBLECOLOR                         Compose the RGB color         */
73 /*                                                                        */
74 /*  CALLED BY                                                             */
75 /*                                                                        */
76 /*    GUIX Internal Code                                                  */
77 /*                                                                        */
78 /*  RELEASE HISTORY                                                       */
79 /*                                                                        */
80 /*    DATE              NAME                      DESCRIPTION             */
81 /*                                                                        */
82 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
83 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
84 /*                                            resulting in version 6.1    */
85 /*                                                                        */
86 /**************************************************************************/
_gx_display_driver_4444argb_canvas_blend(GX_CANVAS * canvas,GX_CANVAS * composite)87 VOID _gx_display_driver_4444argb_canvas_blend(GX_CANVAS *canvas, GX_CANVAS *composite)
88 {
89 GX_RECTANGLE dirty;
90 GX_RECTANGLE overlap;
91 USHORT      *read;
92 USHORT      *read_start;
93 USHORT      *write;
94 USHORT      *write_start;
95 USHORT       fcolor;
96 GX_UBYTE     falpha;
97 GX_UBYTE     fred;
98 GX_UBYTE     fgreen;
99 GX_UBYTE     fblue;
100 GX_UBYTE     balpha;
101 GX_UBYTE     bred;
102 GX_UBYTE     bgreen;
103 GX_UBYTE     bblue;
104 GX_UBYTE     inv_alpha;
105 GX_UBYTE     combined_alpha;
106 USHORT       bcolor;
107 INT          row;
108 INT          col;
109 
110     dirty.gx_rectangle_left = dirty.gx_rectangle_top = 0;
111     dirty.gx_rectangle_right = (GX_VALUE)(canvas -> gx_canvas_x_resolution - 1);
112     dirty.gx_rectangle_bottom = (GX_VALUE)(canvas -> gx_canvas_y_resolution - 1);
113 
114     _gx_utility_rectangle_shift(&dirty, canvas -> gx_canvas_display_offset_x, canvas -> gx_canvas_display_offset_y);
115 
116     if (_gx_utility_rectangle_overlap_detect(&dirty, &composite -> gx_canvas_dirty_area, &overlap))
117     {
118 
119         read_start = (USHORT *)canvas -> gx_canvas_memory;
120 
121         /* index into starting row */
122         read_start += (overlap.gx_rectangle_top - dirty.gx_rectangle_top) * canvas -> gx_canvas_x_resolution;
123 
124         /* index into pixel */
125 
126         read_start += overlap.gx_rectangle_left - dirty.gx_rectangle_left;
127 
128         /* calculate the write pointer */
129         write_start = (USHORT *)composite -> gx_canvas_memory;
130         write_start += overlap.gx_rectangle_top * composite -> gx_canvas_x_resolution;
131         write_start += overlap.gx_rectangle_left;
132 
133         for (row = overlap.gx_rectangle_top; row <= overlap.gx_rectangle_bottom; row++)
134         {
135             read = read_start;
136             write = write_start;
137 
138             for (col = overlap.gx_rectangle_left; col <= overlap.gx_rectangle_right; col++)
139             {
140                 /* read the foreground color */
141                 fcolor = *read++;
142 
143                 /* split foreground into red, green, and blue components */
144                 falpha = ALPHAVAL(fcolor);
145                 falpha = (GX_UBYTE)(falpha | (falpha << 4));
146                 fred = REDVAL(fcolor);
147                 fgreen = GREENVAL(fcolor);
148                 fblue = BLUEVAL(fcolor);
149 
150                 combined_alpha = (GX_UBYTE)((falpha * canvas -> gx_canvas_alpha) >> 8);
151 
152                 /* read background color */
153                 bcolor = *write;
154 
155                 /* split background color into alpha, red, green, and blue components */
156                 balpha = ALPHAVAL(bcolor);
157                 balpha = (GX_UBYTE)(balpha | (balpha << 4));
158                 bred = REDVAL(bcolor);
159                 bgreen = GREENVAL(bcolor);
160                 bblue = BLUEVAL(bcolor);
161 
162                 /* background alpha is inverse of foreground alpha */
163                 inv_alpha = (GX_UBYTE)(256 - combined_alpha);
164 
165                 /* blend foreground and background, each color channel */
166                 falpha = (GX_UBYTE)(((balpha * inv_alpha) + (falpha * combined_alpha)) >> 8);
167                 fred = (GX_UBYTE)(((bred * inv_alpha) + (fred * combined_alpha)) >> 8);
168                 fgreen = (GX_UBYTE)(((bgreen * inv_alpha) + (fgreen * combined_alpha)) >> 8);
169                 fblue = (GX_UBYTE)(((bblue * inv_alpha) + (fblue * combined_alpha)) >> 8);
170 
171                 /* re-assemble into 16-bit color and write it out */
172                 *write++ = (USHORT)ASSEMBLECOLOR(falpha, fred, fgreen, fblue);
173             }
174             write_start += composite -> gx_canvas_x_resolution;
175             read_start += canvas -> gx_canvas_x_resolution;
176         }
177     }
178 }
179 
180