1 /* 2 * Common display-related functions. 3 * 4 * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "display.h" 21 #include "utils.h" 22 23 24 25 /* Initialise a display configuration. */ 26 27 void init_display(display_config_t *cfg, uint8_t *framebuffer, 28 uint32_t line_length, uint32_t line_count, 29 int frames, uint8_t **screen_starts) 30 { 31 /* Framebuffer address. */ 32 33 cfg->framebuffer = framebuffer; 34 35 /* Frame allocation limits. */ 36 37 cfg->total_lines = (line_count + 1) * frames; 38 cfg->max_frames = frames; 39 40 /* Screen size and dimensions. */ 41 42 cfg->screen_size = line_length * line_count; 43 cfg->line_length = line_length; 44 cfg->line_count = line_count; 45 46 /* Set the number of frames and the current frame. */ 47 48 cfg->frames = frames; 49 cfg->frame = 0; 50 51 /* Set the pointer to a collection of screen start pointers, one per 52 frame. */ 53 54 cfg->screen_starts = screen_starts; 55 56 init_frames(cfg); 57 init_display_properties(cfg); 58 } 59 60 /* Initialise the display constraints. */ 61 62 void init_display_properties(display_config_t *cfg) 63 { 64 /* Fixed address of the frame. */ 65 66 cfg->frame_start = display_get_frame_start(cfg, cfg->frame); 67 68 /* Floating address of the screen contents. */ 69 70 if (cfg->screen_starts) 71 cfg->screen_start = cfg->screen_starts[cfg->frame]; 72 73 /* Without any way of recording the address, just reset the start. */ 74 75 else 76 cfg->screen_start = cfg->frame_start; 77 78 /* Fixed limit of the frame. */ 79 80 cfg->screen_limit = cfg->frame_start + cfg->screen_size; 81 82 /* Recalculate the line multiplier. */ 83 84 cfg->line_multiplier = cfg->scanlines / cfg->line_count; 85 } 86 87 /* Initialise the screen start addresses for all frames. */ 88 89 void init_frames(display_config_t *cfg) 90 { 91 int frame; 92 93 for (frame = 0; frame < cfg->frames; frame++) 94 cfg->screen_starts[frame] = display_get_frame_start(cfg, frame); 95 } 96 97 98 99 /* Select a frame in the framebuffer. */ 100 101 void display_select_frame(display_config_t *cfg, int frame) 102 { 103 if ((frame < 0) || (frame >= cfg->frames)) 104 return; 105 106 /* Store the current frame's screen start, if possible. */ 107 108 if (cfg->screen_starts) 109 cfg->screen_starts[cfg->frame] = cfg->screen_start; 110 111 /* Update the frame details. */ 112 113 cfg->frame = frame; 114 115 /* Set the screen start offset when switching frames. */ 116 117 init_display_properties(cfg); 118 } 119 120 /* Set the number of frames in the framebuffer memory. */ 121 122 void display_set_frames(display_config_t *cfg, int frames) 123 { 124 if ((frames <= 0) || (frames > cfg->max_frames)) 125 return; 126 127 /* Recalculate the number of lines. */ 128 129 cfg->line_count = (cfg->total_lines - cfg->max_frames) / frames; 130 131 /* Recalculate the screen size. */ 132 133 cfg->screen_size = cfg->line_count * cfg->line_length; 134 135 /* Set the number of frames and the current frame. */ 136 137 cfg->frames = frames; 138 cfg->frame = 0; 139 140 init_frames(cfg); 141 init_display_properties(cfg); 142 } 143 144 145 146 /* Return the start address of the given frame. */ 147 148 uint8_t *display_get_frame_start(display_config_t *cfg, int frame) 149 { 150 return cfg->framebuffer + (cfg->screen_size + cfg->line_length) * frame; 151 } 152 153 /* Return the line data position for the given pixel. */ 154 155 int display_get_position(display_config_t *cfg, int x) 156 { 157 int cell, offset, pos; 158 159 if (cfg->line_channels < 2) 160 return x; 161 162 /* Determine which cell is providing the position and the offset of the 163 pixel within the cell. */ 164 165 cell = x / cfg->cell_size; 166 offset = x % cfg->cell_size; 167 168 /* Determine the resulting position within the divided-up data. */ 169 170 pos = (cell / 2) * cfg->cell_size + offset; 171 172 /* Return the final position within the entire data. All cells in 173 odd-numbered positions occur in the first half, all even-numbered cells 174 in the second half. */ 175 176 return cell % 2 ? pos + cfg->line_length / 2 : pos; 177 } 178 179 /* Return the screen start offset. */ 180 181 uint32_t display_get_start_offset(display_config_t *cfg) 182 { 183 return cfg->screen_start - cfg->frame_start; 184 } 185 186 187 188 /* Copying from/to the display to/from a backing store. */ 189 190 void display_copy(display_config_t *cfg, uint8_t *store, 191 int width, int height, int ystep, 192 int x, int y, int key, int to_display) 193 { 194 display_copy_section(cfg, store, width, height, 195 0, 0, width, height, ystep, 196 x, y, key, to_display); 197 } 198 199 /* Copying from/to the display to/from a backing store region. */ 200 201 void display_copy_section(display_config_t *cfg, uint8_t *store, 202 int width, int height, 203 int xstart, int ystart, int xsize, int ysize, int ystep, 204 int x, int y, int key, int to_display) 205 { 206 int sx, sy, dx, dy; 207 uint8_t *storeline = store + ystart * width, 208 *displayline = display_wrap_pointer(cfg, cfg->screen_start + y * cfg->line_length), 209 pixel; 210 211 /* Define the limits of the copying in the store. */ 212 213 int xlimit = xstart + xsize, ylimit = ystart + ysize; 214 215 if (xlimit > width) 216 xlimit = width; 217 218 if (ylimit > height) 219 ylimit = height; 220 221 /* Perform the copying between the store and display. */ 222 223 for (sy = ystart, dy = y; (sy < ylimit) && (dy < cfg->line_count); sy += ystep, dy++) 224 { 225 for (sx = xstart, dx = x; (sx < xlimit) && (dx < cfg->line_length); sx++, dx++) 226 { 227 if (to_display) 228 { 229 pixel = storeline[sx]; 230 if ((key < 0) || (pixel != key)) 231 displayline[display_get_position(cfg, dx)] = pixel; 232 } 233 else 234 storeline[sx] = displayline[display_get_position(cfg, dx)]; 235 } 236 237 storeline += width * ystep; 238 displayline = display_wrap_pointer(cfg, displayline + cfg->line_length); 239 } 240 } 241 242 /* Scroll the display. */ 243 244 void display_scroll(display_config_t *cfg, int x, int y) 245 { 246 /* Move the screen start by the given number of bytes and lines, wrapping 247 around the start and end of the framebuffer. */ 248 249 cfg->screen_start = display_wrap_pointer(cfg, cfg->screen_start + x + 250 y * cfg->line_length); 251 } 252 253 /* Provide a pattern to test the line data. */ 254 255 void display_test_linedata(display_config_t *cfg) 256 { 257 int x, y; 258 uint8_t *linedata = cfg->screen_start; 259 260 for (y = 0; y < cfg->line_count; y++) 261 { 262 for (x = 0; x < cfg->line_length; x++) 263 { 264 /* Pixel: I0RRGGBB = Y0YYYYXX */ 265 266 linedata[display_get_position(cfg, x)] = (x % 2) ? 267 (((y / (cfg->line_count / 32)) & 0b1) << 7) | 268 (((y / (cfg->line_count / 16)) & 0b1111) << 2) | 269 ((x / (cfg->line_length / 4)) & 0b11) : 270 0x00; 271 } 272 273 linedata = display_wrap_pointer(cfg, linedata + cfg->line_length); 274 } 275 } 276 277 /* Wrap the screen pointer to the current frame. */ 278 279 uint8_t *display_wrap_pointer(display_config_t *cfg, uint8_t *ptr) 280 { 281 return wrap_pointer(ptr, cfg->frame_start, cfg->screen_limit); 282 }