1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 14:58:09 +00:00
trezor-firmware/core/embed/unix/display-unix.c

404 lines
12 KiB
C

/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <SDL.h>
#include <SDL_image.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "display-unix.h"
#include "display_interface.h"
#include "profile.h"
#define EMULATOR_BORDER 16
#if defined TREZOR_MODEL_T
#ifdef TREZOR_EMULATOR_RASPI
#define WINDOW_WIDTH 480
#define WINDOW_HEIGHT 320
#define TOUCH_OFFSET_X 110
#define TOUCH_OFFSET_Y 40
#else
#define WINDOW_WIDTH 400
#define WINDOW_HEIGHT 600
#define TOUCH_OFFSET_X 80
#define TOUCH_OFFSET_Y 110
#endif
#elif defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R
#define WINDOW_WIDTH 200
#define WINDOW_HEIGHT 340
#define TOUCH_OFFSET_X 36
#define TOUCH_OFFSET_Y 92
#else
#error Unknown Trezor model
#endif
static SDL_Window *WINDOW;
static SDL_Renderer *RENDERER;
// BUFFER_TO_DISPLAY will contain the actual pixels to be displayed,
// it will be filled from BUFFER by gamma-correcting the pixel colors.
// Screenshots will be taken with data from BUFFER.
static SDL_Surface *BUFFER;
static SDL_Surface *BUFFER_TO_DISPLAY;
static SDL_Texture *TEXTURE, *BACKGROUND;
static SDL_Surface *PREV_SAVED;
static int DISPLAY_BACKLIGHT = -1;
static int DISPLAY_ORIENTATION = -1;
float DISPLAY_GAMMA = 0.55f;
int sdl_display_res_x = DISPLAY_RESX, sdl_display_res_y = DISPLAY_RESY;
int sdl_touch_offset_x, sdl_touch_offset_y;
// Using RGB565 (16-bit) color format.
typedef uint16_t pixel_color;
// Will depend on SDL_VIDEODRIVER env variable
static bool DO_GAMMA_CORRECTION = true;
static pixel_color GAMMA_LUT[0x10000];
// this is just for compatibility with DMA2D using algorithms
uint8_t *const DISPLAY_DATA_ADDRESS = 0;
static struct {
struct {
uint16_t x, y;
} start;
struct {
uint16_t x, y;
} end;
struct {
uint16_t x, y;
} pos;
} PIXELWINDOW;
static pixel_color gamma_correct(pixel_color c, float gamma) {
// NOTE: 0x1f/31 and 0x3f/63 are maximum values of RGB components
// given the color is 16-bit (5 bits for R, 6 bits for G, 5 bits for B).
int r = (c >> 11) & 0x1f;
int g = (c >> 5) & 0x3f;
int b = c & 0x1f;
r = (int)round(pow(r / 31.0, gamma) * 31.0);
g = (int)round(pow(g / 63.0, gamma) * 63.0);
b = (int)round(pow(b / 31.0, gamma) * 31.0);
return (r << 11) | (g << 5) | b;
}
static void prepare_gamma_lut(float gamma) {
for (int i = 0; i < 0x10000; i++) {
GAMMA_LUT[i] = gamma_correct(i, gamma);
}
}
static void gamma_correct_buffer_to_display(void) {
// Gamma correct all the pixels in BUFFER_TO_DISPLAY.
pixel_color *pixels = (pixel_color *)BUFFER_TO_DISPLAY->pixels;
for (int y = 0; y < BUFFER_TO_DISPLAY->h; y++) {
for (int x = 0; x < BUFFER_TO_DISPLAY->w; x++) {
int index = y * BUFFER_TO_DISPLAY->pitch / 2 + x;
pixels[index] = GAMMA_LUT[pixels[index]];
}
}
}
float display_gamma(float gamma) {
float prev_gamma = DISPLAY_GAMMA;
if (gamma != 0) {
DISPLAY_GAMMA = gamma;
prepare_gamma_lut(gamma);
}
return prev_gamma;
}
void display_pixeldata(pixel_color c) {
#if defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R
// set to white if highest bits of all R, G, B values are set to 1
// bin(10000 100000 10000) = hex(0x8410)
// otherwise set to black
c = (c & 0x8410) ? 0xFFFF : 0x0000;
#endif
if (!RENDERER) {
display_init();
}
if (PIXELWINDOW.pos.x <= PIXELWINDOW.end.x &&
PIXELWINDOW.pos.y <= PIXELWINDOW.end.y) {
((pixel_color *)
BUFFER->pixels)[PIXELWINDOW.pos.x + PIXELWINDOW.pos.y * BUFFER->pitch /
sizeof(pixel_color)] = c;
}
PIXELWINDOW.pos.x++;
if (PIXELWINDOW.pos.x > PIXELWINDOW.end.x) {
PIXELWINDOW.pos.x = PIXELWINDOW.start.x;
PIXELWINDOW.pos.y++;
}
}
#define PIXELDATA(c) display_pixeldata(c)
void display_reset_state() {}
void display_init_seq(void) {}
void display_deinit(void) {
SDL_FreeSurface(PREV_SAVED);
SDL_FreeSurface(BUFFER);
SDL_FreeSurface(BUFFER_TO_DISPLAY);
if (BACKGROUND != NULL) {
SDL_DestroyTexture(BACKGROUND);
}
if (TEXTURE != NULL) {
SDL_DestroyTexture(TEXTURE);
}
if (RENDERER != NULL) {
SDL_DestroyRenderer(RENDERER);
}
if (WINDOW != NULL) {
SDL_DestroyWindow(WINDOW);
}
SDL_Quit();
}
void display_init(void) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("%s\n", SDL_GetError());
ensure(secfalse, "SDL_Init error");
}
atexit(display_deinit);
// Not doing gamma correction for "dummy" SDL driver
// (not to slow down device/UI tests)
char *sdl_env = getenv("SDL_VIDEODRIVER");
if (sdl_env && strcmp(sdl_env, "dummy") == 0) {
DO_GAMMA_CORRECTION = false;
} else {
DO_GAMMA_CORRECTION = true;
prepare_gamma_lut(DISPLAY_GAMMA);
}
char *window_title = NULL;
char *window_title_alloc = NULL;
if (asprintf(&window_title_alloc, "Trezor^emu: %s", profile_name()) > 0) {
window_title = window_title_alloc;
} else {
window_title = "Trezor^emu";
window_title_alloc = NULL;
}
WINDOW =
SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT,
#ifdef TREZOR_EMULATOR_RASPI
SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN
#else
SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI
#endif
);
free(window_title_alloc);
if (!WINDOW) {
printf("%s\n", SDL_GetError());
ensure(secfalse, "SDL_CreateWindow error");
}
RENDERER = SDL_CreateRenderer(WINDOW, -1, SDL_RENDERER_SOFTWARE);
if (!RENDERER) {
printf("%s\n", SDL_GetError());
SDL_DestroyWindow(WINDOW);
ensure(secfalse, "SDL_CreateRenderer error");
}
SDL_SetRenderDrawColor(RENDERER, 0, 0, 0, 255);
SDL_RenderClear(RENDERER);
BUFFER = SDL_CreateRGBSurface(0, MAX_DISPLAY_RESX, MAX_DISPLAY_RESY, 16,
0xF800, 0x07E0, 0x001F, 0x0000);
BUFFER_TO_DISPLAY =
SDL_CreateRGBSurface(0, MAX_DISPLAY_RESX, MAX_DISPLAY_RESY, 16, 0xF800,
0x07E0, 0x001F, 0x0000);
TEXTURE = SDL_CreateTexture(RENDERER, SDL_PIXELFORMAT_RGB565,
SDL_TEXTUREACCESS_STREAMING, DISPLAY_RESX,
DISPLAY_RESY);
SDL_SetTextureBlendMode(TEXTURE, SDL_BLENDMODE_BLEND);
#ifdef __APPLE__
// macOS Mojave SDL black screen workaround
SDL_PumpEvents();
SDL_SetWindowSize(WINDOW, WINDOW_WIDTH, WINDOW_HEIGHT);
#endif
#ifdef TREZOR_EMULATOR_RASPI
#include "background_raspi.h"
BACKGROUND = IMG_LoadTexture_RW(
RENDERER, SDL_RWFromMem(background_raspi_jpg, background_raspi_jpg_len),
0);
#else
#if defined TREZOR_MODEL_T
#include "background_T.h"
BACKGROUND = IMG_LoadTexture_RW(
RENDERER, SDL_RWFromMem(background_T_jpg, background_T_jpg_len), 0);
#elif defined TREZOR_MODEL_1
#include "background_1.h"
BACKGROUND = IMG_LoadTexture_RW(
RENDERER, SDL_RWFromMem(background_1_jpg, background_1_jpg_len), 0);
#elif defined TREZOR_MODEL_R
#include "background_R.h"
BACKGROUND = IMG_LoadTexture_RW(
RENDERER, SDL_RWFromMem(background_R_jpg, background_R_jpg_len), 0);
#endif
#endif
if (BACKGROUND) {
SDL_SetTextureBlendMode(BACKGROUND, SDL_BLENDMODE_NONE);
sdl_touch_offset_x = TOUCH_OFFSET_X;
sdl_touch_offset_y = TOUCH_OFFSET_Y;
} else {
SDL_SetWindowSize(WINDOW, DISPLAY_RESX + 2 * EMULATOR_BORDER,
DISPLAY_RESY + 2 * EMULATOR_BORDER);
sdl_touch_offset_x = EMULATOR_BORDER;
sdl_touch_offset_y = EMULATOR_BORDER;
}
DISPLAY_BACKLIGHT = 0;
#ifdef TREZOR_EMULATOR_RASPI
DISPLAY_ORIENTATION = 270;
SDL_ShowCursor(SDL_DISABLE);
#else
DISPLAY_ORIENTATION = 0;
#endif
}
void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
if (!RENDERER) {
display_init();
}
PIXELWINDOW.start.x = x0;
PIXELWINDOW.start.y = y0;
PIXELWINDOW.end.x = x1;
PIXELWINDOW.end.y = y1;
PIXELWINDOW.pos.x = x0;
PIXELWINDOW.pos.y = y0;
}
void display_sync(void) {}
void display_refresh(void) {
if (!RENDERER) {
display_init();
}
if (BACKGROUND) {
const SDL_Rect r = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
SDL_RenderCopy(RENDERER, BACKGROUND, NULL, &r);
} else {
SDL_RenderClear(RENDERER);
}
// Fill BUFFER_TO_DISPLAY with BUFFER data
SDL_BlitSurface(BUFFER, NULL, BUFFER_TO_DISPLAY, NULL);
#if defined TREZOR_MODEL_T
// Gamma-correcting the display buffer for model T when wanted
if (DO_GAMMA_CORRECTION) {
gamma_correct_buffer_to_display();
}
#endif
// Show the display buffer
SDL_UpdateTexture(TEXTURE, NULL, BUFFER_TO_DISPLAY->pixels,
BUFFER_TO_DISPLAY->pitch);
#define BACKLIGHT_NORMAL 150
SDL_SetTextureAlphaMod(TEXTURE,
MIN(255, 255 * DISPLAY_BACKLIGHT / BACKLIGHT_NORMAL));
if (BACKGROUND) {
const SDL_Rect r = {TOUCH_OFFSET_X, TOUCH_OFFSET_Y, DISPLAY_RESX,
DISPLAY_RESY};
SDL_RenderCopyEx(RENDERER, TEXTURE, NULL, &r, DISPLAY_ORIENTATION, NULL, 0);
} else {
const SDL_Rect r = {EMULATOR_BORDER, EMULATOR_BORDER, DISPLAY_RESX,
DISPLAY_RESY};
SDL_RenderCopyEx(RENDERER, TEXTURE, NULL, &r, DISPLAY_ORIENTATION, NULL, 0);
}
SDL_RenderPresent(RENDERER);
}
int display_orientation(int degrees) {
if (degrees != DISPLAY_ORIENTATION) {
#if defined TREZOR_MODEL_T
if (degrees == 0 || degrees == 90 || degrees == 180 || degrees == 270) {
#elif defined TREZOR_MODEL_1 || defined TREZOR_MODEL_R
if (degrees == 0 || degrees == 180) {
#else
#error Unknown Trezor model
#endif
DISPLAY_ORIENTATION = degrees;
display_refresh();
}
}
return DISPLAY_ORIENTATION;
}
int display_get_orientation(void) { return DISPLAY_ORIENTATION; }
int display_backlight(int val) {
#if defined TREZOR_MODEL_1
val = 255;
#endif
if (DISPLAY_BACKLIGHT != val && val >= 0 && val <= 255) {
DISPLAY_BACKLIGHT = val;
display_refresh();
}
return DISPLAY_BACKLIGHT;
}
const char *display_save(const char *prefix) {
if (!RENDERER) {
display_init();
}
static int count;
static char filename[256];
// take a cropped view of the screen contents
const SDL_Rect rect = {0, 0, DISPLAY_RESX, DISPLAY_RESY};
SDL_Surface *crop = SDL_CreateRGBSurface(
BUFFER->flags, rect.w, rect.h, BUFFER->format->BitsPerPixel,
BUFFER->format->Rmask, BUFFER->format->Gmask, BUFFER->format->Bmask,
BUFFER->format->Amask);
SDL_BlitSurface(BUFFER, &rect, crop, NULL);
// compare with previous screen, skip if equal
if (PREV_SAVED != NULL) {
if (memcmp(PREV_SAVED->pixels, crop->pixels, crop->pitch * crop->h) == 0) {
SDL_FreeSurface(crop);
return filename;
}
SDL_FreeSurface(PREV_SAVED);
}
// save to png
snprintf(filename, sizeof(filename), "%s%08d.png", prefix, count++);
IMG_SavePNG(crop, filename);
PREV_SAVED = crop;
return filename;
}
void display_clear_save(void) {
SDL_FreeSurface(PREV_SAVED);
PREV_SAVED = NULL;
}