Skip to content

Display and control your Android device

Introduce interop abstraction

Romain Vimont73c6b5a0d202
diff --git a/app/meson.build b/app/meson.build
index 912e7e36..db35f514 100644
--- a/app/meson.build
+++ b/app/meson.build
@@ -39,6 +39,8 @@ src = [
'src/hid/hid_gamepad.c',
'src/hid/hid_keyboard.c',
'src/hid/hid_mouse.c',
+ 'src/interop/interop.c',
+ 'src/interop/interop_software.c',
'src/trait/frame_source.c',
'src/trait/packet_source.c',
'src/uhid/gamepad_uhid.c',
diff --git a/app/src/interop/interop.c b/app/src/interop/interop.c
new file mode 100644
index 00000000..f87d9db4
--- /dev/null
+++ b/app/src/interop/interop.c
@@ -0,0 +1,56 @@
+#include "interop.h"
+
+#include <assert.h>
+
+#include "interop_software.h"
+#include "util/log.h"
+
+struct sc_interop *
+sc_interop_new(enum AVHWDeviceType hw_type, SDL_Renderer *renderer,
+ struct sc_opengl *gl, bool mipmaps) {
+ switch (hw_type) {
+ case AV_HWDEVICE_TYPE_NONE: {
+ struct sc_interop_software *sw =
+ sc_interop_software_new(renderer, gl, mipmaps);
+ if (!sw) {
+ return NULL;
+ }
+ return &sw->interop;
+ }
+ default:
+ LOGE("hw_type not supported: %s", av_hwdevice_get_type_name(hw_type));
+ }
+ return NULL;
+}
+
+void
+sc_interop_delete(struct sc_interop *interop) {
+ assert(interop->ops && interop->ops->destroy);
+ interop->ops->destroy(interop);
+ free(interop);
+}
+
+SDL_Colorspace
+sc_interop_to_sdl_color_space(enum AVColorSpace color_space,
+ enum AVColorRange color_range) {
+ bool full_range = color_range == AVCOL_RANGE_JPEG;
+
+ switch (color_space) {
+ case AVCOL_SPC_BT709:
+ case AVCOL_SPC_RGB:
+ case AVCOL_SPC_UNSPECIFIED:
+ case AVCOL_SPC_YCGCO:
+ return full_range ? SDL_COLORSPACE_BT709_FULL
+ : SDL_COLORSPACE_BT709_LIMITED;
+ case AVCOL_SPC_BT470BG:
+ case AVCOL_SPC_SMPTE170M:
+ return full_range ? SDL_COLORSPACE_BT601_FULL
+ : SDL_COLORSPACE_BT601_LIMITED;
+ case AVCOL_SPC_BT2020_NCL:
+ case AVCOL_SPC_BT2020_CL:
+ return full_range ? SDL_COLORSPACE_BT2020_FULL
+ : SDL_COLORSPACE_BT2020_LIMITED;
+ default:
+ return SDL_COLORSPACE_JPEG;
+ }
+}
diff --git a/app/src/interop/interop.h b/app/src/interop/interop.h
new file mode 100644
index 00000000..4bc7dbfe
--- /dev/null
+++ b/app/src/interop/interop.h
@@ -0,0 +1,52 @@
+#ifndef SC_INTEROP_H
+#define SC_INTEROP_H
+
+#include "common.h"
+
+#include <stdbool.h>
+
+#include <SDL3/SDL.h>
+#include <libavutil/frame.h>
+#include <libavutil/hwcontext.h>
+#include <libavutil/pixfmt.h>
+
+#include "coords.h"
+#include "opengl.h"
+
+struct sc_interop {
+ const char *name;
+
+ enum AVPixelFormat pix_fmt;
+
+ SDL_Texture *texture;
+ struct sc_size frame_size; // only valid when texture != NULL
+
+ const struct sc_interop_ops *ops;
+};
+
+struct sc_interop_ops {
+ bool
+ (*import)(struct sc_interop *interop, const AVFrame *frame);
+
+ // Remove the current frame
+ void
+ (*reset)(struct sc_interop *interop);
+
+ void
+ (*destroy)(struct sc_interop *interop);
+};
+
+struct sc_interop *
+sc_interop_new(enum AVHWDeviceType hw_type, SDL_Renderer *renderer,
+ struct sc_opengl *gl, bool mipmaps);
+
+void
+sc_interop_delete(struct sc_interop *interop);
+
+// Helpers shared by the interop implementations
+
+SDL_Colorspace
+sc_interop_to_sdl_color_space(enum AVColorSpace color_space,
+ enum AVColorRange color_range);
+
+#endif
diff --git a/app/src/interop/interop_software.c b/app/src/interop/interop_software.c
new file mode 100644
index 00000000..043e1ccf
--- /dev/null
+++ b/app/src/interop/interop_software.c
@@ -0,0 +1,181 @@
+#include "interop_software.h"
+
+#include <assert.h>
+
+#include <SDL3/SDL.h>
+
+#include "opengl.h"
+#include "util/log.h"
+
+/** Downcast interop to sc_interop_software */
+#define DOWNCAST(INTEROP) \
+ container_of(INTEROP, struct sc_interop_software, interop)
+
+static bool
+sc_interop_software_create_texture(struct sc_interop_software *sw,
+ struct sc_size size,
+ SDL_Colorspace colorspace) {
+ LOGV("Creating new texture: size=%" PRIu16 "x%" PRIu16 " color_space=%d ",
+ size.width, size.height, colorspace);
+
+ SDL_PropertiesID props = SDL_CreateProperties();
+ if (!props) {
+ LOG_OOM();
+ return false;
+ }
+
+ bool ok =
+ SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER,
+ SDL_PIXELFORMAT_YV12);
+ ok &= SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER,
+ SDL_TEXTUREACCESS_STATIC);
+ ok &= SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER,
+ size.width);
+ ok &= SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER,
+ size.height);
+ ok &= SDL_SetNumberProperty(props,
+ SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER,
+ colorspace);
+
+ if (!ok) {
+ LOGE("Could not set texture properties");
+ SDL_DestroyProperties(props);
+ return false;
+ }
+
+ SDL_Texture *texture = SDL_CreateTextureWithProperties(sw->renderer, props);
+ SDL_DestroyProperties(props);
+ if (!texture) {
+ LOGD("Could not create texture: %s", SDL_GetError());
+ return false;
+ }
+
+ if (sw->mipmaps) {
+ // The properties are owned by the texture
+ SDL_PropertiesID props = SDL_GetTextureProperties(texture);
+ if (!props) {
+ LOGE("Could not get texture properties: %s", SDL_GetError());
+ SDL_DestroyTexture(texture);
+ return false;
+ }
+
+ // A YV12 texture is backed by one OpenGL texture per plane
+ static const char *const opengl_keys[3] = {
+ SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER,
+ SDL_PROP_TEXTURE_OPENGL_TEXTURE_U_NUMBER,
+ SDL_PROP_TEXTURE_OPENGL_TEXTURE_V_NUMBER,
+ };
+ static const char *const opengles2_keys[3] = {
+ SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_NUMBER,
+ SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_U_NUMBER,
+ SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_V_NUMBER,
+ };
+
+ const char *const *keys = sw->gl->is_opengles ? opengles2_keys
+ : opengl_keys;
+
+ for (unsigned i = 0; i < 3; ++i) {
+ int64_t texture_id = SDL_GetNumberProperty(props, keys[i], 0);
+ if (!texture_id) {
+ LOGE("Could not get texture id: %s", SDL_GetError());
+ SDL_DestroyTexture(texture);
+ return false;
+ }
+
+ assert(!(texture_id & ~0xFFFFFFFF)); // fits in uint32_t
+ sw->texture_ids[i] = texture_id;
+ }
+
+ sc_opengl_enable_mipmaps(sw->gl, sw->texture_ids, 3);
+ }
+
+ sw->interop.texture = texture;
+ sw->interop.frame_size = size;
+ LOGI("Texture: %" PRIu16 "x%" PRIu16, size.width, size.height);
+
+ return true;
+}
+
+static void
+sc_interop_software_reset(struct sc_interop *interop) {
+ if (interop->texture) {
+ SDL_DestroyTexture(interop->texture);
+ interop->texture = NULL;
+ }
+}
+
+static void
+sc_interop_software_destroy(struct sc_interop *interop) {
+ if (interop->texture) {
+ SDL_DestroyTexture(interop->texture);
+ }
+}
+
+static bool
+sc_interop_software_import(struct sc_interop *interop, const AVFrame *frame) {
+ struct sc_interop_software *sw = DOWNCAST(interop);
+
+ struct sc_size size = {frame->width, frame->height};
+ assert(size.width && size.height);
+
+ if (!interop->texture
+ || interop->frame_size.width != size.width
+ || interop->frame_size.height != size.height) {
+ // Incompatible texture, recreate it
+ sc_interop_software_reset(interop);
+
+ SDL_Colorspace colorspace =
+ sc_interop_to_sdl_color_space(frame->colorspace,
+ frame->color_range);
+ bool ok = sc_interop_software_create_texture(sw, size, colorspace);
+ if (!ok) {
+ return false;
+ }
+ }
+
+ assert(interop->texture);
+
+ bool ok = SDL_UpdateYUVTexture(interop->texture, NULL,
+ frame->data[0], frame->linesize[0],
+ frame->data[1], frame->linesize[1],
+ frame->data[2], frame->linesize[2]);
+ if (!ok) {
+ LOGD("Could not update texture: %s", SDL_GetError());
+ return false;
+ }
+
+ if (sw->mipmaps) {
+ sc_opengl_generate_mipmaps(sw->gl, sw->texture_ids, 3);
+ }
+
+ return true;
+}
+
+struct sc_interop_software *
+sc_interop_software_new(SDL_Renderer *renderer, struct sc_opengl *gl,
+ bool mipmaps) {
+ assert(!mipmaps || gl); // mipmaps implies gl
+
+ struct sc_interop_software *sw = malloc(sizeof(*sw));
+ if (!sw) {
+ LOG_OOM();
+ return NULL;
+ }
+
+ static const struct sc_interop_ops ops = {
+ .import = sc_interop_software_import,
+ .reset = sc_interop_software_reset,
+ .destroy = sc_interop_software_destroy,
+ };
+
+ sw->interop.name = "software";
+ sw->interop.pix_fmt = AV_PIX_FMT_YUV420P;
+ sw->interop.texture = NULL;
+ sw->interop.ops = &ops;
+
+ sw->renderer = renderer;
+ sw->gl = gl;
+ sw->mipmaps = mipmaps;
+
+ return sw;
+}
diff --git a/app/src/interop/interop_software.h b/app/src/interop/interop_software.h
new file mode 100644
index 00000000..5e8d9e9a
--- /dev/null
+++ b/app/src/interop/interop_software.h
@@ -0,0 +1,27 @@
+#ifndef SC_INTEROP_SOFTWARE_H
+#define SC_INTEROP_SOFTWARE_H
+
+#include "common.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <SDL3/SDL.h>
+
+#include "interop.h"
+#include "opengl.h"
+
+struct sc_interop_software {
+ struct sc_interop interop; // interop trait
+
+ SDL_Renderer *renderer; // owned by the screen
+
+ struct sc_opengl *gl;
+ bool mipmaps;
+ uint32_t texture_ids[3]; // only set if mipmaps is enabled
+};
+
+struct sc_interop_software *
+sc_interop_software_new(SDL_Renderer *renderer, struct sc_opengl *gl,
+ bool mipmaps);
+
+#endif
diff --git a/app/src/screen.c b/app/src/screen.c
index b00533a0..7c4e3f66 100644
--- a/app/src/screen.c
+++ b/app/src/screen.c
@@ -269,8 +269,9 @@ sc_screen_render(struct sc_screen *screen, bool update_content_rect) {
SDL_SetRenderDrawColor(renderer, bg.r, bg.g, bg.b, 0);
sc_sdl_render_clear(renderer);
- SDL_Texture *texture = screen->is_icon_active ? screen->icon_tex
- : screen->tex.texture;
+ SDL_Texture *texture = screen->is_icon_active
+ ? screen->icon_tex
+ : sc_texture_get(&screen->tex);
if (!texture) {
goto end;
}
@@ -632,7 +633,8 @@ sc_screen_init(struct sc_screen *screen,
#endif
bool mipmaps = params->video && params->mipmaps;
- ok = sc_texture_init(&screen->tex, screen->renderer, mipmaps);
+ ok = sc_texture_init(&screen->tex, screen->renderer, mipmaps,
+ AV_HWDEVICE_TYPE_NONE);
if (!ok) {
goto error_destroy_renderer;
}
diff --git a/app/src/texture.c b/app/src/texture.c
index 7a93e87f..a05fa97e 100644
--- a/app/src/texture.c
+++ b/app/src/texture.c
@@ -1,24 +1,27 @@
#include "texture.h"
#include <assert.h>
-#include <inttypes.h>
#include <string.h>
-#include <libavutil/pixfmt.h>
#include "util/log.h"
bool
-sc_texture_init(struct sc_texture *tex, SDL_Renderer *renderer, bool mipmaps) {
+sc_texture_init(struct sc_texture *tex, SDL_Renderer *renderer, bool mipmaps,
+ enum AVHWDeviceType hw_type) {
const char *renderer_name = SDL_GetRendererName(renderer);
LOGI("Renderer: %s", renderer_name ? renderer_name : "(unknown)");
+ tex->has_gl = false;
tex->mipmaps = false;
+ struct sc_opengl *gl = NULL;
+
// starts with "opengl"
bool use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6);
if (use_opengl) {
- struct sc_opengl *gl = &tex->gl;
+ gl = &tex->gl;
sc_opengl_init(gl);
+ tex->has_gl = true;
LOGI("OpenGL version: %s", gl->version);
@@ -41,182 +44,41 @@ sc_texture_init(struct sc_texture *tex, SDL_Renderer *renderer, bool mipmaps) {
}
tex->renderer = renderer;
- tex->texture = NULL;
+ tex->interop = sc_interop_new(hw_type, tex->renderer, gl, tex->mipmaps);
+ if (!tex->interop) {
+ return false;
+ }
+
return true;
}
void
sc_texture_destroy(struct sc_texture *tex) {
- if (tex->texture) {
- SDL_DestroyTexture(tex->texture);
- }
+ sc_interop_delete(tex->interop);
}
-static enum SDL_Colorspace
-sc_texture_to_sdl_color_space(enum AVColorSpace color_space,
- enum AVColorRange color_range) {
- bool full_range = color_range == AVCOL_RANGE_JPEG;
-
- switch (color_space) {
- case AVCOL_SPC_BT709:
- case AVCOL_SPC_RGB:
- case AVCOL_SPC_UNSPECIFIED:
- case AVCOL_SPC_YCGCO:
- return full_range ? SDL_COLORSPACE_BT709_FULL
- : SDL_COLORSPACE_BT709_LIMITED;
- case AVCOL_SPC_BT470BG:
- case AVCOL_SPC_SMPTE170M:
- return full_range ? SDL_COLORSPACE_BT601_FULL
- : SDL_COLORSPACE_BT601_LIMITED;
- case AVCOL_SPC_BT2020_NCL:
- case AVCOL_SPC_BT2020_CL:
- return full_range ? SDL_COLORSPACE_BT2020_FULL
- : SDL_COLORSPACE_BT2020_LIMITED;
- default:
- return SDL_COLORSPACE_JPEG;
- }
-}
-
-static SDL_Texture *
-sc_texture_create_frame_texture(struct sc_texture *tex,
- struct sc_size size,
- enum AVColorSpace color_space,
- enum AVColorRange color_range) {
- LOGV("Creating new texture: size=%" PRIu16 "x%" PRIu16 " color_space=%d "
- "color_range=%d", size.width, size.height, color_space, color_range);
-
- SDL_PropertiesID props = SDL_CreateProperties();
- if (!props) {
- LOG_OOM();
- return NULL;
- }
-
- enum SDL_Colorspace sdl_color_space =
- sc_texture_to_sdl_color_space(color_space, color_range);
-
- bool ok =
- SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER,
- SDL_PIXELFORMAT_YV12);
- ok &= SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER,
- SDL_TEXTUREACCESS_STATIC);
- ok &= SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER,
- size.width);
- ok &= SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER,
- size.height);
- ok &= SDL_SetNumberProperty(props,
- SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER,
- sdl_color_space);
-
- if (!ok) {
- LOGE("Could not set texture properties");
- SDL_DestroyProperties(props);
- return NULL;
- }
-
- SDL_Renderer *renderer = tex->renderer;
- SDL_Texture *texture = SDL_CreateTextureWithProperties(renderer, props);
- SDL_DestroyProperties(props);
- if (!texture) {
- LOGD("Could not create texture: %s", SDL_GetError());
- return NULL;
- }
-
- if (tex->mipmaps) {
- struct sc_opengl *gl = &tex->gl;
-
- // The properties are owned by the texture
- SDL_PropertiesID props = SDL_GetTextureProperties(texture);
- if (!props) {
- LOGE("Could not get texture properties: %s", SDL_GetError());
- SDL_DestroyTexture(texture);
- return NULL;
- }
-
- // A YV12 texture is backed by one OpenGL texture per plane
- static const char *const opengl_keys[3] = {
- SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER,
- SDL_PROP_TEXTURE_OPENGL_TEXTURE_U_NUMBER,
- SDL_PROP_TEXTURE_OPENGL_TEXTURE_V_NUMBER,
- };
- static const char *const opengles2_keys[3] = {
- SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_NUMBER,
- SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_U_NUMBER,
- SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_V_NUMBER,
- };
-
- const char *renderer_name = SDL_GetRendererName(tex->renderer);
- const char *const *keys = !renderer_name
- || !strcmp(renderer_name, "opengl")
- ? opengl_keys : opengles2_keys;
-
- for (unsigned i = 0; i < 3; ++i) {
- int64_t texture_id = SDL_GetNumberProperty(props, keys[i], 0);
- if (!texture_id) {
- LOGE("Could not get texture id: %s", SDL_GetError());
- SDL_DestroyTexture(texture);
- return NULL;
- }
-
- assert(!(texture_id & ~0xFFFFFFFF)); // fits in uint32_t
- tex->texture_ids[i] = texture_id;
- }
-
- sc_opengl_enable_mipmaps(gl, tex->texture_ids, 3);
- }
-
- return texture;
+SDL_Texture *
+sc_texture_get(struct sc_texture *tex) {
+ assert(tex->interop);
+ return tex->interop->texture;
}
bool
sc_texture_update(struct sc_texture *tex, const AVFrame *frame) {
-
- struct sc_size size = {frame->width, frame->height};
- assert(size.width && size.height);
-
- if (!tex->texture
- || 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 (tex->texture) {
- SDL_DestroyTexture(tex->texture);
- }
-
- tex->texture = sc_texture_create_frame_texture(tex, size, color_space,
- color_range);
- if (!tex->texture) {
- return false;
- }
-
- tex->texture_size = size;
-
- LOGI("Texture: %" PRIu16 "x%" PRIu16, size.width, size.height);
- }
-
- assert(tex->texture);
-
- 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]);
- if (!ok) {
- LOGD("Could not update texture: %s", SDL_GetError());
+ struct sc_interop *interop = tex->interop;
+ if (!interop) {
return false;
}
- if (tex->mipmaps) {
- sc_opengl_generate_mipmaps(&tex->gl, tex->texture_ids, 3);
- }
-
- return true;
+ assert(interop->ops && interop->ops->import);
+ return interop->ops->import(interop, frame);
}
void
sc_texture_reset(struct sc_texture *tex) {
- if (tex->texture) {
- SDL_DestroyTexture(tex->texture);
- tex->texture = NULL;
+ struct sc_interop *interop = tex->interop;
+ if (interop) {
+ assert(interop->ops && interop->ops->reset);
+ interop->ops->reset(interop);
}
}
diff --git a/app/src/texture.h b/app/src/texture.h
index a23d8678..926b2781 100644
--- a/app/src/texture.h
+++ b/app/src/texture.h
@@ -6,29 +6,32 @@
#include <stdbool.h>
#include <stdint.h>
#include <libavutil/frame.h>
+#include <libavutil/hwcontext.h>
#include <SDL3/SDL.h>
-#include "coords.h"
+#include "interop/interop.h"
#include "opengl.h"
struct sc_texture {
SDL_Renderer *renderer; // owned by the caller
- SDL_Texture *texture;
- // Only valid if texture != NULL
- struct sc_size texture_size;
struct sc_opengl gl;
-
+ bool has_gl;
bool mipmaps;
- uint32_t texture_ids[3]; // only set if mipmaps is enabled
+
+ struct sc_interop *interop;
};
bool
-sc_texture_init(struct sc_texture *tex, SDL_Renderer *renderer, bool mipmaps);
+sc_texture_init(struct sc_texture *tex, SDL_Renderer *renderer, bool mipmaps,
+ enum AVHWDeviceType hw_type);
void
sc_texture_destroy(struct sc_texture *tex);
+SDL_Texture *
+sc_texture_get(struct sc_texture *tex);
+
bool
sc_texture_update(struct sc_texture *tex, const AVFrame *frame);