Skip to content

Display and control your Android device

Rename sc_display to sc_texture

Romain Vimont77998f543f4b
diff --git a/app/meson.build b/app/meson.build
index 36b72cfd..c730ce07 100644
--- a/app/meson.build
+++ b/app/meson.build
@@ -15,7 +15,6 @@ src = [
'src/delay_buffer.c',
'src/demuxer.c',
'src/device_msg.c',
- 'src/display.c',
'src/events.c',
'src/icon.c',
'src/file_pusher.c',
@@ -33,6 +32,7 @@ src = [
'src/scrcpy.c',
'src/screen.c',
'src/server.c',
+ 'src/texture.c',
'src/version.c',
'src/hid/hid_gamepad.c',
'src/hid/hid_keyboard.c',
diff --git a/app/src/screen.c b/app/src/screen.c
index cad968b3..6a74e4ad 100644
--- a/app/src/screen.c
+++ b/app/src/screen.c
@@ -208,7 +208,7 @@ sc_screen_render(struct sc_screen *screen, bool update_content_rect) {
sc_sdl_render_clear(renderer);
bool ok = false;
- SDL_Texture *texture = screen->display.texture;
+ SDL_Texture *texture = screen->tex.texture;
if (!texture) {
LOGW("No texture to render");
goto end;
@@ -446,7 +446,7 @@ sc_screen_init(struct sc_screen *screen,
#endif
bool mipmaps = params->video;
- ok = sc_display_init(&screen->display, screen->renderer, mipmaps);
+ ok = sc_texture_init(&screen->tex, screen->renderer, mipmaps);
if (!ok) {
goto error_destroy_renderer;
}
@@ -454,7 +454,7 @@ sc_screen_init(struct sc_screen *screen,
ok = SDL_StartTextInput(screen->window);
if (!ok) {
LOGE("Could not enable text input: %s", SDL_GetError());
- goto error_destroy_display;
+ goto error_destroy_texture;
}
SDL_Surface *icon = scrcpy_icon_load();
@@ -468,17 +468,17 @@ sc_screen_init(struct sc_screen *screen,
} else {
// without video, the icon is used as window content, it must be present
LOGE("Could not load icon");
- goto error_destroy_display;
+ goto error_destroy_texture;
}
if (!params->video) {
assert(icon);
screen->content_size.width = icon->w;
screen->content_size.height = icon->h;
- ok = sc_display_set_texture_from_surface(&screen->display, icon);
+ ok = sc_texture_set_from_surface(&screen->tex, icon);
if (!ok) {
scrcpy_icon_destroy(icon);
- goto error_destroy_display;
+ goto error_destroy_texture;
}
}
@@ -489,7 +489,7 @@ sc_screen_init(struct sc_screen *screen,
screen->frame = av_frame_alloc();
if (!screen->frame) {
LOG_OOM();
- goto error_destroy_display;
+ goto error_destroy_texture;
}
struct sc_input_manager_params im_params = {
@@ -545,8 +545,8 @@ sc_screen_init(struct sc_screen *screen,
return true;
-error_destroy_display:
- sc_display_destroy(&screen->display);
+error_destroy_texture:
+ sc_texture_destroy(&screen->tex);
error_destroy_renderer:
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
if (screen->gl_context) {
@@ -615,7 +615,7 @@ sc_screen_destroy(struct sc_screen *screen) {
#ifndef NDEBUG
assert(!screen->open);
#endif
- sc_display_destroy(&screen->display);
+ sc_texture_destroy(&screen->tex);
av_frame_free(&screen->frame);
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
SDL_GL_DestroyContext(screen->gl_context);
@@ -719,7 +719,7 @@ sc_screen_apply_frame(struct sc_screen *screen) {
}
}
- bool ok = sc_display_set_texture_from_frame(&screen->display, frame);
+ bool ok = sc_texture_set_from_frame(&screen->tex, frame);
if (!ok) {
return false;
}
diff --git a/app/src/screen.h b/app/src/screen.h
index 40dd5f27..330e41a6 100644
--- a/app/src/screen.h
+++ b/app/src/screen.h
@@ -12,12 +12,12 @@
#include "controller.h"
#include "coords.h"
-#include "display.h"
#include "fps_counter.h"
#include "frame_buffer.h"
#include "input_manager.h"
#include "mouse_capture.h"
#include "options.h"
+#include "texture.h"
#include "trait/key_processor.h"
#include "trait/frame_sink.h"
#include "trait/mouse_processor.h"
@@ -36,7 +36,7 @@ struct sc_screen {
bool video;
bool camera;
- struct sc_display display;
+ struct sc_texture tex;
struct sc_input_manager im;
struct sc_mouse_capture mc; // only used in mouse relative mode
struct sc_frame_buffer fb;
diff --git a/app/src/display.c b/app/src/texture.c
similarity index 68%
rename from app/src/display.c
rename to app/src/texture.c
index 3294385d..1a88bda1 100644
--- a/app/src/display.c
+++ b/app/src/texture.c
@@ -1,4 +1,4 @@
-#include "display.h"
+#include "texture.h"
#include <assert.h>
#include <inttypes.h>
@@ -8,17 +8,16 @@
#include "util/log.h"
bool
-sc_display_init(struct sc_display *display, SDL_Renderer *renderer,
- bool mipmaps) {
+sc_texture_init(struct sc_texture *tex, SDL_Renderer *renderer, bool mipmaps) {
const char *renderer_name = SDL_GetRendererName(renderer);
LOGI("Renderer: %s", renderer_name ? renderer_name : "(unknown)");
- display->mipmaps = false;
+ tex->mipmaps = false;
// starts with "opengl"
bool use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6);
if (use_opengl) {
- struct sc_opengl *gl = &display->gl;
+ struct sc_opengl *gl = &tex->gl;
sc_opengl_init(gl);
LOGI("OpenGL version: %s", gl->version);
@@ -29,7 +28,7 @@ sc_display_init(struct sc_display *display, SDL_Renderer *renderer,
2, 0 /* OpenGL ES 2.0+ */);
if (supports_mipmaps) {
LOGI("Trilinear filtering enabled");
- display->mipmaps = true;
+ tex->mipmaps = true;
} else {
LOGW("Trilinear filtering disabled "
"(OpenGL 3.0+ or ES 2.0+ required)");
@@ -41,20 +40,20 @@ sc_display_init(struct sc_display *display, SDL_Renderer *renderer,
LOGD("Trilinear filtering disabled (not an OpenGL renderer)");
}
- display->renderer = renderer;
- display->texture = NULL;
+ tex->renderer = renderer;
+ tex->texture = NULL;
return true;
}
void
-sc_display_destroy(struct sc_display *display) {
- if (display->texture) {
- SDL_DestroyTexture(display->texture);
+sc_texture_destroy(struct sc_texture *tex) {
+ if (tex->texture) {
+ SDL_DestroyTexture(tex->texture);
}
}
static enum SDL_Colorspace
-sc_display_to_sdl_color_space(enum AVColorSpace color_space,
+sc_texture_to_sdl_color_space(enum AVColorSpace color_space,
enum AVColorRange color_range) {
bool full_range = color_range == AVCOL_RANGE_JPEG;
@@ -77,7 +76,7 @@ sc_display_to_sdl_color_space(enum AVColorSpace color_space,
}
static SDL_Texture *
-sc_display_create_frame_texture(struct sc_display *display,
+sc_texture_create_frame_texture(struct sc_texture *tex,
struct sc_size size,
enum AVColorSpace color_space,
enum AVColorRange color_range) {
@@ -87,7 +86,7 @@ sc_display_create_frame_texture(struct sc_display *display,
}
enum SDL_Colorspace sdl_color_space =
- sc_display_to_sdl_color_space(color_space, color_range);
+ sc_texture_to_sdl_color_space(color_space, color_range);
bool ok =
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER,
@@ -108,7 +107,7 @@ sc_display_create_frame_texture(struct sc_display *display,
return NULL;
}
- SDL_Renderer *renderer = display->renderer;
+ SDL_Renderer *renderer = tex->renderer;
SDL_Texture *texture = SDL_CreateTextureWithProperties(renderer, props);
SDL_DestroyProperties(props);
if (!texture) {
@@ -116,8 +115,8 @@ sc_display_create_frame_texture(struct sc_display *display,
return NULL;
}
- if (display->mipmaps) {
- struct sc_opengl *gl = &display->gl;
+ if (tex->mipmaps) {
+ struct sc_opengl *gl = &tex->gl;
SDL_PropertiesID props = SDL_GetTextureProperties(texture);
if (!props) {
@@ -126,7 +125,7 @@ sc_display_create_frame_texture(struct sc_display *display,
return NULL;
}
- const char *renderer_name = SDL_GetRendererName(display->renderer);
+ const char *renderer_name = SDL_GetRendererName(tex->renderer);
const char *key = !renderer_name || !strcmp(renderer_name, "opengl")
? SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER
: SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_NUMBER;
@@ -140,8 +139,8 @@ sc_display_create_frame_texture(struct sc_display *display,
}
assert(!(texture_id & ~0xFFFFFFFF)); // fits in uint32_t
- display->texture_id = texture_id;
- gl->BindTexture(GL_TEXTURE_2D, display->texture_id);
+ tex->texture_id = texture_id;
+ gl->BindTexture(GL_TEXTURE_2D, tex->texture_id);
// Enable trilinear filtering for downscaling
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
@@ -155,41 +154,39 @@ sc_display_create_frame_texture(struct sc_display *display,
}
bool
-sc_display_set_texture_from_frame(struct sc_display *display,
- const AVFrame *frame) {
+sc_texture_set_from_frame(struct sc_texture *tex, const AVFrame *frame) {
struct sc_size size = {frame->width, frame->height};
assert(size.width && size.height);
- if (!display->texture
- || display->texture_type != SC_TEXTURE_TYPE_FRAME
- || display->texture_size.width != size.width
- || display->texture_size.height != size.height) {
+ if (!tex->texture
+ || tex->texture_type != SC_TEXTURE_TYPE_FRAME
+ || tex->texture_size.width != size.width
+ || tex->texture_size.height != size.height) {
// Incompatible texture, recreate it
enum AVColorSpace color_space = frame->colorspace;
enum AVColorRange color_range = frame->color_range;
- if (display->texture) {
- SDL_DestroyTexture(display->texture);
+ if (tex->texture) {
+ SDL_DestroyTexture(tex->texture);
}
- display->texture = sc_display_create_frame_texture(display, size,
- color_space,
- color_range);
- if (!display->texture) {
+ tex->texture = sc_texture_create_frame_texture(tex, size, color_space,
+ color_range);
+ if (!tex->texture) {
return false;
}
- display->texture_size = size;
- display->texture_type = SC_TEXTURE_TYPE_FRAME;
+ tex->texture_size = size;
+ tex->texture_type = SC_TEXTURE_TYPE_FRAME;
LOGI("Texture: %" PRIu16 "x%" PRIu16, size.width, size.height);
}
- assert(display->texture);
- assert(display->texture_type == SC_TEXTURE_TYPE_FRAME);
+ assert(tex->texture);
+ assert(tex->texture_type == SC_TEXTURE_TYPE_FRAME);
- bool ok = SDL_UpdateYUVTexture(display->texture, NULL,
+ bool ok = SDL_UpdateYUVTexture(tex->texture, NULL,
frame->data[0], frame->linesize[0],
frame->data[1], frame->linesize[1],
frame->data[2], frame->linesize[2]);
@@ -198,11 +195,11 @@ sc_display_set_texture_from_frame(struct sc_display *display,
return false;
}
- if (display->mipmaps) {
- assert(display->texture_id);
- struct sc_opengl *gl = &display->gl;
+ if (tex->mipmaps) {
+ assert(tex->texture_id);
+ struct sc_opengl *gl = &tex->gl;
- gl->BindTexture(GL_TEXTURE_2D, display->texture_id);
+ gl->BindTexture(GL_TEXTURE_2D, tex->texture_id);
gl->GenerateMipmap(GL_TEXTURE_2D);
gl->BindTexture(GL_TEXTURE_2D, 0);
}
@@ -211,21 +208,20 @@ sc_display_set_texture_from_frame(struct sc_display *display,
}
bool
-sc_display_set_texture_from_surface(struct sc_display *display,
- SDL_Surface *surface) {
- if (display->texture) {
- SDL_DestroyTexture(display->texture);
+sc_texture_set_from_surface(struct sc_texture *tex, SDL_Surface *surface) {
+ if (tex->texture) {
+ SDL_DestroyTexture(tex->texture);
}
- display->texture = SDL_CreateTextureFromSurface(display->renderer, surface);
- if (!display->texture) {
+ tex->texture = SDL_CreateTextureFromSurface(tex->renderer, surface);
+ if (!tex->texture) {
LOGE("Could not create texture: %s", SDL_GetError());
return false;
}
- display->texture_size.width = surface->w;
- display->texture_size.height = surface->h;
- display->texture_type = SC_TEXTURE_TYPE_ICON;
+ tex->texture_size.width = surface->w;
+ tex->texture_size.height = surface->h;
+ tex->texture_type = SC_TEXTURE_TYPE_ICON;
return true;
}
diff --git a/app/src/display.h b/app/src/texture.h
similarity index 59%
rename from app/src/display.h
rename to app/src/texture.h
index 6fb64d7f..80587340 100644
--- a/app/src/display.h
+++ b/app/src/texture.h
@@ -16,7 +16,7 @@ enum sc_texture_type {
SC_TEXTURE_TYPE_ICON,
};
-struct sc_display {
+struct sc_texture {
SDL_Renderer *renderer; // owned by the caller
SDL_Texture *texture;
// Only valid if texture != NULL
@@ -30,18 +30,15 @@ struct sc_display {
};
bool
-sc_display_init(struct sc_display *display, SDL_Renderer *renderer,
- bool mipmaps);
+sc_texture_init(struct sc_texture *tex, SDL_Renderer *renderer, bool mipmaps);
void
-sc_display_destroy(struct sc_display *display);
+sc_texture_destroy(struct sc_texture *tex);
bool
-sc_display_set_texture_from_frame(struct sc_display *display,
- const AVFrame *frame);
+sc_texture_set_from_frame(struct sc_texture *tex, const AVFrame *frame);
bool
-sc_display_set_texture_from_surface(struct sc_display *display,
- SDL_Surface *surface);
+sc_texture_set_from_surface(struct sc_texture *tex, SDL_Surface *surface);
#endif