2018-02-08 18:14:13 +08:00
|
|
|
#ifndef SCREEN_H
|
|
|
|
#define SCREEN_H
|
|
|
|
|
2021-01-09 02:24:51 +08:00
|
|
|
#include "common.h"
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
#include <stdbool.h>
|
2018-02-08 18:14:13 +08:00
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
2021-01-09 02:15:29 +08:00
|
|
|
#include "coords.h"
|
2020-04-11 22:08:23 +08:00
|
|
|
#include "opengl.h"
|
2019-03-02 22:16:55 +08:00
|
|
|
|
|
|
|
struct video_buffer;
|
2018-02-08 18:14:13 +08:00
|
|
|
|
|
|
|
struct screen {
|
2021-02-16 01:28:41 +08:00
|
|
|
struct video_buffer *vb;
|
2018-02-08 18:14:13 +08:00
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
SDL_Texture *texture;
|
2020-04-11 22:08:23 +08:00
|
|
|
struct sc_opengl gl;
|
2018-02-08 18:14:13 +08:00
|
|
|
struct size frame_size;
|
2020-04-08 20:11:23 +08:00
|
|
|
struct size content_size; // rotated frame_size
|
2020-05-11 09:11:13 +08:00
|
|
|
|
|
|
|
bool resize_pending; // resize requested while fullscreen or maximized
|
|
|
|
// The content size the last time the window was not maximized or
|
|
|
|
// fullscreen (meaningful only when resize_pending is true)
|
|
|
|
struct size windowed_content_size;
|
|
|
|
|
2020-04-08 05:03:23 +08:00
|
|
|
// client rotation: 0, 1, 2 or 3 (x90 degrees counterclockwise)
|
|
|
|
unsigned rotation;
|
2020-04-18 00:44:24 +08:00
|
|
|
// rectangle of the content (excluding black borders)
|
|
|
|
struct SDL_Rect rect;
|
2019-03-03 06:52:22 +08:00
|
|
|
bool has_frame;
|
|
|
|
bool fullscreen;
|
2019-11-12 04:26:35 +08:00
|
|
|
bool maximized;
|
2019-03-03 06:52:22 +08:00
|
|
|
bool no_window;
|
2020-04-11 22:08:23 +08:00
|
|
|
bool mipmaps;
|
2018-02-08 18:14:13 +08:00
|
|
|
};
|
|
|
|
|
2019-11-12 04:26:35 +08:00
|
|
|
#define SCREEN_INITIALIZER { \
|
2021-02-16 01:28:41 +08:00
|
|
|
.vb = NULL, \
|
2019-11-12 04:26:35 +08:00
|
|
|
.window = NULL, \
|
|
|
|
.renderer = NULL, \
|
|
|
|
.texture = NULL, \
|
2020-04-11 22:08:23 +08:00
|
|
|
.gl = {0}, \
|
2019-11-12 04:26:35 +08:00
|
|
|
.frame_size = { \
|
2020-04-08 20:11:23 +08:00
|
|
|
.width = 0, \
|
|
|
|
.height = 0, \
|
|
|
|
}, \
|
|
|
|
.content_size = { \
|
|
|
|
.width = 0, \
|
2019-11-12 04:26:35 +08:00
|
|
|
.height = 0, \
|
|
|
|
}, \
|
2020-05-11 09:11:13 +08:00
|
|
|
.resize_pending = false, \
|
|
|
|
.windowed_content_size = { \
|
2019-11-12 04:26:35 +08:00
|
|
|
.width = 0, \
|
|
|
|
.height = 0, \
|
|
|
|
}, \
|
2020-04-08 05:03:23 +08:00
|
|
|
.rotation = 0, \
|
2020-04-18 00:44:24 +08:00
|
|
|
.rect = { \
|
|
|
|
.x = 0, \
|
|
|
|
.y = 0, \
|
|
|
|
.w = 0, \
|
|
|
|
.h = 0, \
|
|
|
|
}, \
|
2019-11-12 04:26:35 +08:00
|
|
|
.has_frame = false, \
|
|
|
|
.fullscreen = false, \
|
|
|
|
.maximized = false, \
|
|
|
|
.no_window = false, \
|
2020-04-11 22:08:23 +08:00
|
|
|
.mipmaps = false, \
|
2018-02-08 18:14:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize default values
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2021-02-16 01:28:41 +08:00
|
|
|
screen_init(struct screen *screen, struct video_buffer *vb);
|
2018-02-08 18:14:13 +08:00
|
|
|
|
Improve startup time
On startup, the client has to:
1. listen on a port
2. push and start the server to the device
3. wait for the server to connect (accept)
4. read device name and size
5. initialize SDL
6. initialize the window and renderer
7. show the window
From the execution of the app_process command to start the server on the
device, to the execution of the java main method, it takes ~800ms. As a
consequence, step 3 also takes ~800ms on the client.
Once complete, the client initializes SDL, which takes ~500ms.
These two expensive actions are executed sequentially:
HOST DEVICE
listen on port | |
push/start the server |----------------->|| app_process loads the jar
accept the connection . ^ ||
. | ||
. | WASTE ||
. | OF ||
. | TIME ||
. | ||
. | ||
. v X execution of our java main
connection accepted |<-----------------| connect to the host
init SDL || |
|| ,----------------| send frames
|| |,---------------|
|| ||,--------------|
|| |||,-------------|
|| ||||,------------|
init window/renderer | |||||,-----------|
display frames |<++++++-----------|
(many frames skipped)
The rationale for step 3 occuring before step 5 is that initializing
SDL replaces the SIGTERM handler to receive the event in the event loop,
so pressing Ctrl+C during step 5 would not work (since it blocks the
event loop).
But this is not so important; let's parallelize the SDL initialization
with the app_process execution (we'll just add a timeout to the
connection):
HOST DEVICE
listen on port | |
push/start the server |----------------->||app_process loads the jar
init SDL || ||
|| ||
|| ||
|| ||
|| ||
|| ||
accept the connection . ||
. X execution of our java main
connection accepted |<-----------------| connect to the host
init window/renderer | |
display frames |<-----------------| send frames
|<-----------------|
In addition, show the window only once the first frame is available to
avoid flickering (opening a black window for 100~200ms).
Note: the window and renderer are initialized after the connection is
accepted because they use the device information received from the
device.
2018-02-09 20:50:54 +08:00
|
|
|
// initialize screen, create window, renderer and texture (window is hidden)
|
2020-06-20 04:04:06 +08:00
|
|
|
// window_x and window_y accept SC_WINDOW_POSITION_UNDEFINED
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-06-24 01:02:34 +08:00
|
|
|
screen_init_rendering(struct screen *screen, const char *window_title,
|
2019-08-29 13:25:17 +08:00
|
|
|
struct size frame_size, bool always_on_top,
|
2019-11-04 01:00:11 +08:00
|
|
|
int16_t window_x, int16_t window_y, uint16_t window_width,
|
2020-04-08 16:17:12 +08:00
|
|
|
uint16_t window_height, bool window_borderless,
|
2020-04-12 05:55:29 +08:00
|
|
|
uint8_t rotation, bool mipmaps);
|
2018-02-08 18:14:13 +08:00
|
|
|
|
Improve startup time
On startup, the client has to:
1. listen on a port
2. push and start the server to the device
3. wait for the server to connect (accept)
4. read device name and size
5. initialize SDL
6. initialize the window and renderer
7. show the window
From the execution of the app_process command to start the server on the
device, to the execution of the java main method, it takes ~800ms. As a
consequence, step 3 also takes ~800ms on the client.
Once complete, the client initializes SDL, which takes ~500ms.
These two expensive actions are executed sequentially:
HOST DEVICE
listen on port | |
push/start the server |----------------->|| app_process loads the jar
accept the connection . ^ ||
. | ||
. | WASTE ||
. | OF ||
. | TIME ||
. | ||
. | ||
. v X execution of our java main
connection accepted |<-----------------| connect to the host
init SDL || |
|| ,----------------| send frames
|| |,---------------|
|| ||,--------------|
|| |||,-------------|
|| ||||,------------|
init window/renderer | |||||,-----------|
display frames |<++++++-----------|
(many frames skipped)
The rationale for step 3 occuring before step 5 is that initializing
SDL replaces the SIGTERM handler to receive the event in the event loop,
so pressing Ctrl+C during step 5 would not work (since it blocks the
event loop).
But this is not so important; let's parallelize the SDL initialization
with the app_process execution (we'll just add a timeout to the
connection):
HOST DEVICE
listen on port | |
push/start the server |----------------->||app_process loads the jar
init SDL || ||
|| ||
|| ||
|| ||
|| ||
|| ||
accept the connection . ||
. X execution of our java main
connection accepted |<-----------------| connect to the host
init window/renderer | |
display frames |<-----------------| send frames
|<-----------------|
In addition, show the window only once the first frame is available to
avoid flickering (opening a black window for 100~200ms).
Note: the window and renderer are initialized after the connection is
accepted because they use the device information received from the
device.
2018-02-09 20:50:54 +08:00
|
|
|
// show the window
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
screen_show_window(struct screen *screen);
|
Improve startup time
On startup, the client has to:
1. listen on a port
2. push and start the server to the device
3. wait for the server to connect (accept)
4. read device name and size
5. initialize SDL
6. initialize the window and renderer
7. show the window
From the execution of the app_process command to start the server on the
device, to the execution of the java main method, it takes ~800ms. As a
consequence, step 3 also takes ~800ms on the client.
Once complete, the client initializes SDL, which takes ~500ms.
These two expensive actions are executed sequentially:
HOST DEVICE
listen on port | |
push/start the server |----------------->|| app_process loads the jar
accept the connection . ^ ||
. | ||
. | WASTE ||
. | OF ||
. | TIME ||
. | ||
. | ||
. v X execution of our java main
connection accepted |<-----------------| connect to the host
init SDL || |
|| ,----------------| send frames
|| |,---------------|
|| ||,--------------|
|| |||,-------------|
|| ||||,------------|
init window/renderer | |||||,-----------|
display frames |<++++++-----------|
(many frames skipped)
The rationale for step 3 occuring before step 5 is that initializing
SDL replaces the SIGTERM handler to receive the event in the event loop,
so pressing Ctrl+C during step 5 would not work (since it blocks the
event loop).
But this is not so important; let's parallelize the SDL initialization
with the app_process execution (we'll just add a timeout to the
connection):
HOST DEVICE
listen on port | |
push/start the server |----------------->||app_process loads the jar
init SDL || ||
|| ||
|| ||
|| ||
|| ||
|| ||
accept the connection . ||
. X execution of our java main
connection accepted |<-----------------| connect to the host
init window/renderer | |
display frames |<-----------------| send frames
|<-----------------|
In addition, show the window only once the first frame is available to
avoid flickering (opening a black window for 100~200ms).
Note: the window and renderer are initialized after the connection is
accepted because they use the device information received from the
device.
2018-02-09 20:50:54 +08:00
|
|
|
|
2018-02-08 18:14:13 +08:00
|
|
|
// destroy window, renderer and texture (if any)
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
screen_destroy(struct screen *screen);
|
2018-02-08 18:14:13 +08:00
|
|
|
|
2018-02-09 18:14:47 +08:00
|
|
|
// resize if necessary and write the rendered frame into the texture
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2021-02-16 01:28:41 +08:00
|
|
|
screen_update_frame(struct screen *screen);
|
2018-02-08 18:14:13 +08:00
|
|
|
|
|
|
|
// render the texture to the renderer
|
2020-04-18 00:44:24 +08:00
|
|
|
//
|
|
|
|
// Set the update_content_rect flag if the window or content size may have
|
|
|
|
// changed, so that the content rectangle is recomputed
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2020-04-18 00:44:24 +08:00
|
|
|
screen_render(struct screen *screen, bool update_content_rect);
|
2018-02-08 18:14:13 +08:00
|
|
|
|
|
|
|
// switch the fullscreen mode
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
screen_switch_fullscreen(struct screen *screen);
|
2018-02-08 18:14:13 +08:00
|
|
|
|
|
|
|
// resize window to optimal size (remove black borders)
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
screen_resize_to_fit(struct screen *screen);
|
2018-02-08 18:14:13 +08:00
|
|
|
|
|
|
|
// resize window to 1:1 (pixel-perfect)
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
screen_resize_to_pixel_perfect(struct screen *screen);
|
2018-02-08 18:14:13 +08:00
|
|
|
|
2020-04-08 05:03:23 +08:00
|
|
|
// set the display rotation (0, 1, 2 or 3, x90 degrees counterclockwise)
|
|
|
|
void
|
|
|
|
screen_set_rotation(struct screen *screen, unsigned rotation);
|
|
|
|
|
2019-10-20 21:32:33 +08:00
|
|
|
// react to window events
|
|
|
|
void
|
|
|
|
screen_handle_window_event(struct screen *screen, const SDL_WindowEvent *event);
|
|
|
|
|
2020-04-18 00:43:29 +08:00
|
|
|
// convert point from window coordinates to frame coordinates
|
|
|
|
// x and y are expressed in pixels
|
|
|
|
struct point
|
2020-06-25 14:54:40 +08:00
|
|
|
screen_convert_window_to_frame_coords(struct screen *screen,
|
|
|
|
int32_t x, int32_t y);
|
|
|
|
|
|
|
|
// convert point from drawable coordinates to frame coordinates
|
|
|
|
// x and y are expressed in pixels
|
|
|
|
struct point
|
|
|
|
screen_convert_drawable_to_frame_coords(struct screen *screen,
|
|
|
|
int32_t x, int32_t y);
|
2020-04-18 00:43:29 +08:00
|
|
|
|
2020-04-18 00:44:24 +08:00
|
|
|
// Convert coordinates from window to drawable.
|
|
|
|
// Events are expressed in window coordinates, but content is expressed in
|
|
|
|
// drawable coordinates. They are the same if HiDPI scaling is 1, but differ
|
|
|
|
// otherwise.
|
|
|
|
void
|
|
|
|
screen_hidpi_scale_coords(struct screen *screen, int32_t *x, int32_t *y);
|
|
|
|
|
2018-02-08 18:14:13 +08:00
|
|
|
#endif
|