Commit 5c30fbb8ec4aa364d5e441c86d7b5776d6c94fb0

Authored by Heinrich Schuchardt
Committed by Anatolij Gustschin
1 parent d7a75d3cd7

dm: video: use constants to refer to colors

Use constants to refer to colors.
Adjust initialization of foreground and background color to avoid
setting reserved bits.
Consistently u32 instead of unsigned for color bit mask.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

Showing 4 changed files with 89 additions and 31 deletions Side-by-side Diff

drivers/video/vidconsole-uclass.c
... ... @@ -15,6 +15,15 @@
15 15 #include <video_console.h>
16 16 #include <video_font.h> /* Get font data, width and height */
17 17  
  18 +/*
  19 + * Structure to describe a console color
  20 + */
  21 +struct vid_rgb {
  22 + u32 r;
  23 + u32 g;
  24 + u32 b;
  25 +};
  26 +
18 27 /* By default we scroll by a single line */
19 28 #ifndef CONFIG_CONSOLE_SCROLL_LINES
20 29 #define CONFIG_CONSOLE_SCROLL_LINES 1
... ... @@ -108,11 +117,7 @@
108 117 video_sync(dev->parent);
109 118 }
110 119  
111   -static const struct {
112   - unsigned r;
113   - unsigned g;
114   - unsigned b;
115   -} colors[] = {
  120 +static const struct vid_rgb colors[VID_COLOR_COUNT] = {
116 121 { 0x00, 0x00, 0x00 }, /* black */
117 122 { 0xff, 0x00, 0x00 }, /* red */
118 123 { 0x00, 0xff, 0x00 }, /* green */
119 124  
120 125  
121 126  
... ... @@ -123,22 +128,26 @@
123 128 { 0xff, 0xff, 0xff }, /* white */
124 129 };
125 130  
126   -static void set_color(struct video_priv *priv, unsigned idx, unsigned *c)
  131 +u32 vid_console_color(struct video_priv *priv, unsigned int idx)
127 132 {
128 133 switch (priv->bpix) {
129 134 case VIDEO_BPP16:
130   - *c = ((colors[idx].r >> 3) << 11) |
131   - ((colors[idx].g >> 2) << 5) |
132   - ((colors[idx].b >> 3) << 0);
133   - break;
  135 + return ((colors[idx].r >> 3) << 11) |
  136 + ((colors[idx].g >> 2) << 5) |
  137 + ((colors[idx].b >> 3) << 0);
134 138 case VIDEO_BPP32:
135   - *c = (colors[idx].r << 16) |
136   - (colors[idx].g << 8) |
137   - (colors[idx].b << 0);
138   - break;
  139 + return (colors[idx].r << 16) |
  140 + (colors[idx].g << 8) |
  141 + (colors[idx].b << 0);
139 142 default:
140   - /* unsupported, leave current color in place */
141   - break;
  143 + /*
  144 + * For unknown bit arrangements just support
  145 + * black and white.
  146 + */
  147 + if (idx)
  148 + return 0xffffff; /* white */
  149 + else
  150 + return 0x000000; /* black */
142 151 }
143 152 }
144 153  
145 154  
146 155  
... ... @@ -270,17 +279,17 @@
270 279  
271 280 switch (val) {
272 281 case 30 ... 37:
273   - /* fg color */
274   - set_color(vid_priv, val - 30,
275   - (unsigned *)&vid_priv->colour_fg);
  282 + /* foreground color */
  283 + vid_priv->colour_fg = vid_console_color(
  284 + vid_priv, val - 30);
276 285 break;
277 286 case 40 ... 47:
278   - /* bg color */
279   - set_color(vid_priv, val - 40,
280   - (unsigned *)&vid_priv->colour_bg);
  287 + /* background color */
  288 + vid_priv->colour_bg = vid_console_color(
  289 + vid_priv, val - 40);
281 290 break;
282 291 default:
283   - /* unknown/unsupported */
  292 + /* ignore unsupported SGR parameter */
284 293 break;
285 294 }
286 295 }
drivers/video/video-uclass.c
... ... @@ -114,6 +114,17 @@
114 114 }
115 115 }
116 116  
  117 +void video_set_default_colors(struct video_priv *priv)
  118 +{
  119 +#ifdef CONFIG_SYS_WHITE_ON_BLACK
  120 + priv->colour_fg = vid_console_color(priv, VID_WHITE);
  121 + priv->colour_bg = vid_console_color(priv, VID_BLACK);
  122 +#else
  123 + priv->colour_fg = vid_console_color(priv, VID_BLACK);
  124 + priv->colour_bg = vid_console_color(priv, VID_WHITE);
  125 +#endif
  126 +}
  127 +
117 128 /* Flush video activity to the caches */
118 129 void video_sync(struct udevice *vid)
119 130 {
... ... @@ -203,12 +214,8 @@
203 214 priv->line_length = priv->xsize * VNBYTES(priv->bpix);
204 215 priv->fb_size = priv->line_length * priv->ysize;
205 216  
206   - /* Set up colours - we could in future support other colours */
207   -#ifdef CONFIG_SYS_WHITE_ON_BLACK
208   - priv->colour_fg = 0xffffff;
209   -#else
210   - priv->colour_bg = 0xffffff;
211   -#endif
  217 + /* Set up colors */
  218 + video_set_default_colors(priv);
212 219  
213 220 if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
214 221 video_clear(dev);
... ... @@ -84,8 +84,8 @@
84 84 void *fb;
85 85 int fb_size;
86 86 int line_length;
87   - int colour_fg;
88   - int colour_bg;
  87 + u32 colour_fg;
  88 + u32 colour_bg;
89 89 bool flush_dcache;
90 90 ushort *cmap;
91 91 };
... ... @@ -182,6 +182,13 @@
182 182 * @param flush non-zero to flush cache after update, 0 to skip
183 183 */
184 184 void video_set_flush_dcache(struct udevice *dev, bool flush);
  185 +
  186 +/**
  187 + * Set default colors and attributes
  188 + *
  189 + * @priv device information
  190 + */
  191 +void video_set_default_colors(struct video_priv *priv);
185 192  
186 193 #endif /* CONFIG_DM_VIDEO */
187 194  
include/video_console.h
... ... @@ -7,11 +7,29 @@
7 7 #ifndef __video_console_h
8 8 #define __video_console_h
9 9  
  10 +#include <video.h>
  11 +
10 12 #define VID_FRAC_DIV 256
11 13  
12 14 #define VID_TO_PIXEL(x) ((x) / VID_FRAC_DIV)
13 15 #define VID_TO_POS(x) ((x) * VID_FRAC_DIV)
14 16  
  17 +/*
  18 + * The 8 colors supported by the console
  19 + */
  20 +enum color_idx {
  21 + VID_BLACK = 0,
  22 + VID_RED,
  23 + VID_GREEN,
  24 + VID_YELLOW,
  25 + VID_BLUE,
  26 + VID_MAGENTA,
  27 + VID_CYAN,
  28 + VID_WHITE,
  29 +
  30 + VID_COLOR_COUNT
  31 +};
  32 +
15 33 /**
16 34 * struct vidconsole_priv - uclass-private data about a console device
17 35 *
... ... @@ -195,6 +213,23 @@
195 213 */
196 214 void vidconsole_position_cursor(struct udevice *dev, unsigned col,
197 215 unsigned row);
  216 +
  217 +#ifdef CONFIG_DM_VIDEO
  218 +
  219 +/**
  220 + * vid_console_color() - convert a color code to a pixel's internal
  221 + * representation
  222 + *
  223 + * The caller has to guarantee that the color index is less than
  224 + * VID_COLOR_COUNT.
  225 + *
  226 + * @priv private data of the console device
  227 + * @idx color index
  228 + * @return color value
  229 + */
  230 +u32 vid_console_color(struct video_priv *priv, unsigned int idx);
  231 +
  232 +#endif
198 233  
199 234 #endif