David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include <stdbool.h> |
| 8 | #include <stddef.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <sys/mman.h> |
| 13 | #include <unistd.h> |
| 14 | #include <fcntl.h> |
| 15 | |
| 16 | #include <math.h> |
| 17 | #include <png.h> |
| 18 | |
| 19 | #include "util.h" |
| 20 | #include "splash.h" |
| 21 | #include "dbus_interface.h" |
| 22 | |
| 23 | #define MAX_SPLASH_IMAGES (30) |
| 24 | #define FILENAME_LENGTH (100) |
David Sodman | dd5b23e | 2015-02-18 21:36:38 -0800 | [diff] [blame^] | 25 | #define MAX_SPLASH_WAITTIME (8) |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 26 | |
| 27 | typedef union { |
| 28 | uint32_t *as_pixels; |
| 29 | png_byte *as_png_bytes; |
| 30 | char *address; |
| 31 | } splash_layout_t; |
| 32 | |
| 33 | typedef struct { |
| 34 | char filename[FILENAME_LENGTH]; |
| 35 | FILE *fp; |
| 36 | splash_layout_t layout; |
| 37 | png_uint_32 width; |
| 38 | png_uint_32 height; |
| 39 | png_uint_32 pitch; |
| 40 | } splash_image_t; |
| 41 | |
| 42 | struct _splash_t { |
| 43 | video_t *video; |
| 44 | int num_images; |
| 45 | splash_image_t images[MAX_SPLASH_IMAGES]; |
| 46 | int frame_interval; |
| 47 | uint32_t clear; |
| 48 | bool terminated; |
| 49 | bool devmode; |
| 50 | dbus_t *dbus; |
| 51 | }; |
| 52 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 53 | static void splash_rgb(png_struct *png, png_row_info *row_info, png_byte *data) |
| 54 | { |
| 55 | unsigned int i; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 56 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 57 | for (i = 0; i < row_info->rowbytes; i+= 4) { |
| 58 | uint8_t r, g, b, a; |
| 59 | uint32_t pixel; |
| 60 | |
| 61 | r = data[i + 0]; |
| 62 | g = data[i + 1]; |
| 63 | b = data[i + 2]; |
| 64 | a = data[i + 3]; |
| 65 | pixel = (a << 24) | (r << 16) | (g << 8) | b; |
| 66 | memcpy(data + i, &pixel, sizeof(pixel)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static int splash_load_image_from_file(splash_t* splash, splash_image_t* image) |
| 71 | { |
| 72 | png_struct *png; |
| 73 | png_info *info; |
| 74 | png_uint_32 width, height, pitch, row; |
| 75 | int bpp, color_type, interlace_mthd; |
| 76 | png_byte **rows; |
| 77 | |
| 78 | if (image->fp != NULL) |
| 79 | return 1; |
| 80 | |
| 81 | image->fp = fopen(image->filename, "rb"); |
| 82 | if (image->fp == NULL) |
| 83 | return 1; |
| 84 | |
| 85 | png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 86 | info = png_create_info_struct(png); |
| 87 | |
| 88 | if (info == NULL) |
| 89 | return 1; |
| 90 | |
| 91 | png_init_io(png, image->fp); |
| 92 | |
| 93 | if (setjmp(png_jmpbuf(png)) != 0) { |
| 94 | fclose(image->fp); |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | png_read_info(png, info); |
| 99 | png_get_IHDR(png, info, &width, &height, &bpp, &color_type, |
| 100 | &interlace_mthd, NULL, NULL); |
| 101 | |
| 102 | pitch = 4 * width; |
| 103 | |
| 104 | switch (color_type) |
| 105 | { |
| 106 | case PNG_COLOR_TYPE_PALETTE: |
| 107 | png_set_palette_to_rgb(png); |
| 108 | break; |
| 109 | |
| 110 | case PNG_COLOR_TYPE_GRAY: |
| 111 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 112 | png_set_gray_to_rgb(png); |
| 113 | } |
| 114 | |
| 115 | if (png_get_valid(png, info, PNG_INFO_tRNS)) |
| 116 | png_set_tRNS_to_alpha(png); |
| 117 | |
| 118 | switch (bpp) |
| 119 | { |
| 120 | default: |
| 121 | if (bpp < 8) |
| 122 | png_set_packing(png); |
| 123 | break; |
| 124 | case 16: |
| 125 | png_set_strip_16(png); |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | if (interlace_mthd != PNG_INTERLACE_NONE) |
| 130 | png_set_interlace_handling(png); |
| 131 | |
| 132 | png_set_filler(png, 0xff, PNG_FILLER_AFTER); |
| 133 | |
| 134 | png_set_read_user_transform_fn(png, splash_rgb); |
| 135 | png_read_update_info(png, info); |
| 136 | |
| 137 | rows = malloc(height * sizeof(*rows)); |
| 138 | image->layout.address = malloc(height * pitch); |
| 139 | |
| 140 | for (row = 0; row < height; row++) { |
| 141 | rows[row] = &image->layout.as_png_bytes[row * pitch]; |
| 142 | } |
| 143 | |
| 144 | png_read_image(png, rows); |
| 145 | free(rows); |
| 146 | |
| 147 | png_read_end(png, info); |
| 148 | fclose(image->fp); |
| 149 | png_destroy_read_struct(&png, &info, NULL); |
| 150 | |
| 151 | image->width = width; |
| 152 | image->height = height; |
| 153 | image->pitch = pitch; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int splash_image_show(splash_t *splash, |
| 159 | splash_image_t* image, |
| 160 | uint32_t *video_buffer) |
| 161 | { |
| 162 | uint32_t j; |
| 163 | uint32_t startx, starty; |
| 164 | buffer_properties_t *bp; |
| 165 | uint32_t *buffer; |
| 166 | |
| 167 | |
| 168 | bp = video_get_buffer_properties(splash->video); |
| 169 | startx = (bp->width - image->width) / 2; |
| 170 | starty = (bp->height - image->height) / 2; |
| 171 | |
| 172 | buffer = video_lock(splash->video); |
| 173 | |
| 174 | if (buffer != NULL) { |
| 175 | for (j = starty; j < starty + image->height; j++) { |
| 176 | memcpy(buffer + j * bp->pitch/4 + startx, |
| 177 | image->layout.address + (j - starty)*image->pitch, image->pitch); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | video_unlock(splash->video); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | splash_t* splash_init() |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 186 | { |
| 187 | splash_t* splash; |
David Sodman | 73e8227 | 2014-11-17 10:11:47 -0800 | [diff] [blame] | 188 | FILE *cookie_fp; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 189 | |
| 190 | splash = (splash_t*)calloc(1, sizeof(splash_t)); |
| 191 | if (splash == NULL) |
| 192 | return NULL; |
| 193 | |
| 194 | splash->num_images = 0; |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 195 | splash->video = video_init(); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 196 | |
David Sodman | 73e8227 | 2014-11-17 10:11:47 -0800 | [diff] [blame] | 197 | cookie_fp = fopen("/tmp/display_info.bin", "wb"); |
| 198 | if (cookie_fp) { |
| 199 | fwrite(&splash->video->internal_panel, sizeof(char), 1, cookie_fp); |
| 200 | fwrite(splash->video->edid, EDID_SIZE, 1, cookie_fp); |
| 201 | fclose(cookie_fp); |
| 202 | } |
| 203 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 204 | return splash; |
| 205 | } |
| 206 | |
| 207 | int splash_destroy(splash_t* splash) |
| 208 | { |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | int splash_set_frame_rate(splash_t *splash, int32_t rate) |
| 213 | { |
| 214 | if (rate <= 0 || rate > 120) |
| 215 | return 1; |
| 216 | |
| 217 | splash->frame_interval = rate; |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | int splash_set_clear(splash_t *splash, int32_t clear_color) |
| 222 | { |
| 223 | splash->clear = clear_color; |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | int splash_add_image(splash_t* splash, const char* filename) |
| 228 | { |
| 229 | if (splash->num_images >= MAX_SPLASH_IMAGES) |
| 230 | return 1; |
| 231 | |
| 232 | strcpy(splash->images[splash->num_images].filename, filename); |
| 233 | splash->num_images++; |
| 234 | return 0; |
| 235 | } |
| 236 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 237 | static void splash_clear_screen(splash_t *splash, uint32_t *video_buffer) |
| 238 | { |
| 239 | int i,j; |
| 240 | buffer_properties_t *bp; |
| 241 | |
| 242 | video_setmode(splash->video); |
| 243 | |
David Sodman | 4371e91 | 2014-12-08 14:14:35 -0800 | [diff] [blame] | 244 | /* After the mode is set, there is nothing splash |
| 245 | * needs master for |
| 246 | */ |
| 247 | video_release(splash->video); |
| 248 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 249 | bp = video_get_buffer_properties(splash->video); |
| 250 | |
| 251 | for (j = 0; j < bp->height; j++) { |
| 252 | for (i = 0; i < bp->width; i++) { |
| 253 | (video_buffer + bp->pitch/4 * j)[i] = splash->clear; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | int splash_run(splash_t* splash, dbus_t** dbus) |
| 259 | { |
| 260 | int i; |
| 261 | uint32_t* video_buffer; |
| 262 | int status; |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 263 | int64_t last_show_ms; |
| 264 | int64_t now_ms; |
| 265 | int64_t sleep_ms; |
| 266 | struct timespec sleep_spec; |
| 267 | int fd; |
| 268 | int num_written; |
| 269 | |
| 270 | status = 0; |
| 271 | |
| 272 | /* |
| 273 | * First draw the actual splash screen |
| 274 | */ |
| 275 | video_buffer = video_lock(splash->video); |
| 276 | if (video_buffer != NULL) { |
| 277 | splash_clear_screen(splash, video_buffer); |
| 278 | last_show_ms = -1; |
| 279 | for (i = 0; i < splash->num_images; i++) { |
| 280 | status = splash_load_image_from_file(splash, &splash->images[i]); |
| 281 | if (status != 0) { |
| 282 | LOG(WARNING, "splash_load_image_from_file failed: %d\n", status); |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | now_ms = get_monotonic_time_ms(); |
| 287 | if (last_show_ms > 0) { |
| 288 | sleep_ms = splash->frame_interval - (now_ms - last_show_ms); |
| 289 | if (sleep_ms > 0) { |
| 290 | sleep_spec.tv_sec = sleep_ms / MS_PER_SEC; |
| 291 | sleep_spec.tv_nsec = (sleep_ms % MS_PER_SEC) * NS_PER_MS; |
| 292 | nanosleep(&sleep_spec, NULL); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | now_ms = get_monotonic_time_ms(); |
| 297 | |
| 298 | status = splash_image_show(splash, &splash->images[i], video_buffer); |
| 299 | if (status != 0) { |
| 300 | LOG(WARNING, "splash_image_show failed: %d", status); |
| 301 | break; |
| 302 | } |
| 303 | last_show_ms = now_ms; |
| 304 | } |
| 305 | video_unlock(splash->video); |
| 306 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 307 | do { |
| 308 | *dbus = dbus_init(); |
| 309 | usleep(50000); |
| 310 | } while (*dbus == NULL); |
| 311 | |
| 312 | splash_set_dbus(splash, *dbus); |
| 313 | |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 314 | if (splash->devmode) { |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 315 | /* |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 316 | * Now set drm_master_relax so that we can transfer drm_master between |
| 317 | * chrome and frecon |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 318 | */ |
David Sodman | bf3f284 | 2014-11-12 08:26:58 -0800 | [diff] [blame] | 319 | fd = open("/sys/kernel/debug/dri/drm_master_relax", O_WRONLY); |
| 320 | if (fd != -1) { |
| 321 | num_written = write(fd, "Y", 1); |
| 322 | close(fd); |
| 323 | |
| 324 | /* |
| 325 | * If we can't set drm_master relax, then transitions between chrome |
| 326 | * and frecon won't work. No point in having frecon hold any resources |
| 327 | */ |
| 328 | if (num_written != 1) { |
| 329 | LOG(ERROR, "Unable to set drm_master_relax"); |
| 330 | splash->devmode = false; |
| 331 | } |
| 332 | } else { |
| 333 | LOG(ERROR, "unable to open drm_master_relax"); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 334 | } |
David Sodman | dd5b23e | 2015-02-18 21:36:38 -0800 | [diff] [blame^] | 335 | } else { |
| 336 | /* |
| 337 | * Below, we will wait for Chrome to appear above the splash |
| 338 | * image. If we are not in dev mode, wait and then exit |
| 339 | */ |
| 340 | sleep(MAX_SPLASH_WAITTIME); |
| 341 | exit(EXIT_SUCCESS); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 344 | |
David Sodman | 35d6bd8 | 2014-11-24 08:23:00 -0800 | [diff] [blame] | 345 | |
David Sodman | 003faed | 2014-11-03 09:02:10 -0800 | [diff] [blame] | 346 | (void)dbus_method_call0(splash->dbus, |
| 347 | kLibCrosServiceName, |
| 348 | kLibCrosServicePath, |
| 349 | kLibCrosServiceInterface, |
| 350 | kTakeDisplayOwnership); |
David Sodman | f348b0d | 2015-02-10 08:34:57 -0800 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * Finally, wait until chrome has drawn on top of the splash. In dev mode, |
| 354 | * wait a few seconds for chrome to show up. |
| 355 | */ |
| 356 | sleep(MAX_SPLASH_WAITTIME); |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 357 | return status; |
| 358 | } |
| 359 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 360 | void splash_set_dbus(splash_t* splash, dbus_t* dbus) |
| 361 | { |
| 362 | splash->dbus = dbus; |
| 363 | } |
| 364 | |
| 365 | void splash_set_devmode(splash_t* splash) |
| 366 | { |
| 367 | splash->devmode = true; |
| 368 | } |