refactor DST_STRIDE_RGBA

This commit is contained in:
rustdesk 2023-03-03 14:02:49 +08:00
parent 4351272297
commit b8941c15c0
8 changed files with 48 additions and 23 deletions

@ -1228,10 +1228,10 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: texture_rgba_renderer name: texture_rgba_renderer
sha256: fbb09b2c6b4ce71261927f9e7e4ea339af3e2f3f2b175f6fb921de1c66ec848d sha256: ec8d124e4c1d7dfff854ae34e95d7d9d877b8f9d291c383c67686e4b15cf538e
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.0.8" version: "0.0.12"
timing: timing:
dependency: transitive dependency: transitive
description: description:

@ -92,7 +92,7 @@ dependencies:
password_strength: ^0.2.0 password_strength: ^0.2.0
flutter_launcher_icons: ^0.11.0 flutter_launcher_icons: ^0.11.0
flutter_keyboard_visibility: ^5.4.0 flutter_keyboard_visibility: ^5.4.0
texture_rgba_renderer: ^0.0.8 texture_rgba_renderer: ^0.0.12
percent_indicator: ^4.2.2 percent_indicator: ^4.2.2
dev_dependencies: dev_dependencies:

@ -306,7 +306,7 @@ impl Decoder {
pub fn handle_video_frame( pub fn handle_video_frame(
&mut self, &mut self,
frame: &video_frame::Union, frame: &video_frame::Union,
fmt: ImageFormat, fmt: (ImageFormat, usize),
rgb: &mut Vec<u8>, rgb: &mut Vec<u8>,
) -> ResultType<bool> { ) -> ResultType<bool> {
match frame { match frame {
@ -352,7 +352,7 @@ impl Decoder {
fn handle_vp9s_video_frame( fn handle_vp9s_video_frame(
decoder: &mut VpxDecoder, decoder: &mut VpxDecoder,
vp9s: &EncodedVideoFrames, vp9s: &EncodedVideoFrames,
fmt: ImageFormat, fmt: (ImageFormat, usize),
rgb: &mut Vec<u8>, rgb: &mut Vec<u8>,
) -> ResultType<bool> { ) -> ResultType<bool> {
let mut last_frame = Image::new(); let mut last_frame = Image::new();
@ -369,7 +369,7 @@ impl Decoder {
if last_frame.is_null() { if last_frame.is_null() {
Ok(false) Ok(false)
} else { } else {
last_frame.to(fmt, 1, rgb); last_frame.to(fmt.0, fmt.1, rgb);
Ok(true) Ok(true)
} }
} }
@ -378,7 +378,7 @@ impl Decoder {
fn handle_hw_video_frame( fn handle_hw_video_frame(
decoder: &mut HwDecoder, decoder: &mut HwDecoder,
frames: &EncodedVideoFrames, frames: &EncodedVideoFrames,
fmt: ImageFormat, fmt: (ImageFormat, usize),
raw: &mut Vec<u8>, raw: &mut Vec<u8>,
i420: &mut Vec<u8>, i420: &mut Vec<u8>,
) -> ResultType<bool> { ) -> ResultType<bool> {
@ -398,7 +398,7 @@ impl Decoder {
fn handle_mediacodec_video_frame( fn handle_mediacodec_video_frame(
decoder: &mut MediaCodecDecoder, decoder: &mut MediaCodecDecoder,
frames: &EncodedVideoFrames, frames: &EncodedVideoFrames,
fmt: ImageFormat, fmt: (ImageFormat, usize),
raw: &mut Vec<u8>, raw: &mut Vec<u8>,
) -> ResultType<bool> { ) -> ResultType<bool> {
let mut ret = false; let mut ret = false;

@ -236,7 +236,13 @@ pub struct HwDecoderImage<'a> {
} }
impl HwDecoderImage<'_> { impl HwDecoderImage<'_> {
pub fn to_fmt(&self, fmt: ImageFormat, fmt_data: &mut Vec<u8>, i420: &mut Vec<u8>) -> ResultType<()> { // take dst_stride into account when you convert
pub fn to_fmt(
&self,
(fmt, dst_stride): (ImageFormat, usize),
fmt_data: &mut Vec<u8>,
i420: &mut Vec<u8>,
) -> ResultType<()> {
let frame = self.frame; let frame = self.frame;
match frame.pixfmt { match frame.pixfmt {
AVPixelFormat::AV_PIX_FMT_NV12 => hw::hw_nv12_to( AVPixelFormat::AV_PIX_FMT_NV12 => hw::hw_nv12_to(

@ -1,4 +1,4 @@
use hbb_common::{log, anyhow::Error, bail, ResultType}; use hbb_common::{anyhow::Error, bail, log, ResultType};
use ndk::media::media_codec::{MediaCodec, MediaCodecDirection, MediaFormat}; use ndk::media::media_codec::{MediaCodec, MediaCodecDirection, MediaFormat};
use std::ops::Deref; use std::ops::Deref;
use std::{ use std::{
@ -50,7 +50,13 @@ impl MediaCodecDecoder {
MediaCodecDecoders { h264, h265 } MediaCodecDecoders { h264, h265 }
} }
pub fn decode(&mut self, data: &[u8], fmt: ImageFormat, raw: &mut Vec<u8>) -> ResultType<bool> { // take dst_stride into account please
pub fn decode(
&mut self,
data: &[u8],
(fmt, dst_stride): (ImageFormat, usize),
raw: &mut Vec<u8>,
) -> ResultType<bool> {
match self.dequeue_input_buffer(Duration::from_millis(10))? { match self.dequeue_input_buffer(Duration::from_millis(10))? {
Some(mut input_buffer) => { Some(mut input_buffer) => {
let mut buf = input_buffer.buffer_mut(); let mut buf = input_buffer.buffer_mut();

@ -44,8 +44,7 @@ pub use helper::*;
use scrap::{ use scrap::{
codec::{Decoder, DecoderCfg}, codec::{Decoder, DecoderCfg},
record::{Recorder, RecorderContext}, record::{Recorder, RecorderContext},
VpxDecoderConfig, VpxVideoCodecId, ImageFormat, VpxDecoderConfig, VpxVideoCodecId,
ImageFormat,
}; };
use crate::{ use crate::{
@ -944,12 +943,11 @@ impl VideoHandler {
} }
match &vf.union { match &vf.union {
Some(frame) => { Some(frame) => {
// windows && flutter_texture_render, fmt is ImageFormat::ABGR let res = self.decoder.handle_video_frame(
#[cfg(all(target_os = "windows", feature = "flutter_texture_render"))] frame,
let fmt = ImageFormat::ABGR; (ImageFormat::ARGB, crate::DST_STRIDE_RGBA),
#[cfg(not(all(target_os = "windows", feature = "flutter_texture_render")))] &mut self.rgb,
let fmt = ImageFormat::ARGB; );
let res = self.decoder.handle_video_frame(frame, fmt, &mut self.rgb);
if self.record { if self.record {
self.recorder self.recorder
.lock() .lock()
@ -2038,7 +2036,7 @@ pub trait Interface: Send + Clone + 'static + Sized {
fn is_force_relay(&self) -> bool { fn is_force_relay(&self) -> bool {
self.get_login_config_handler().read().unwrap().force_relay self.get_login_config_handler().read().unwrap().force_relay
} }
fn swap_modifier_mouse(&self, _msg : &mut hbb_common::protos::message::MouseEvent) {} fn swap_modifier_mouse(&self, _msg: &mut hbb_common::protos::message::MouseEvent) {}
} }
/// Data used by the client interface. /// Data used by the client interface.

@ -39,6 +39,13 @@ pub const CLIPBOARD_INTERVAL: u64 = 333;
pub const SYNC_PEER_INFO_DISPLAYS: i32 = 1; pub const SYNC_PEER_INFO_DISPLAYS: i32 = 1;
#[cfg(all(target_os = "macos", feature = "flutter_texture_render"))]
// https://developer.apple.com/forums/thread/712709
// Memory alignment should be multiple of 64.
pub const DST_STRIDE_RGBA: usize = 64;
#[cfg(not(all(target_os = "macos", feature = "flutter_texture_render")))]
pub const DST_STRIDE_RGBA: usize = 1;
// the executable name of the portable version // the executable name of the portable version
pub const PORTABLE_APPNAME_RUNTIME_ENV_KEY: &str = "RUSTDESK_APPNAME"; pub const PORTABLE_APPNAME_RUNTIME_ENV_KEY: &str = "RUSTDESK_APPNAME";

@ -153,8 +153,13 @@ pub struct FlutterHandler {
} }
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
pub type FlutterRgbaRendererPluginOnRgba = pub type FlutterRgbaRendererPluginOnRgba = unsafe extern "C" fn(
unsafe extern "C" fn(texture_rgba: *mut c_void, buffer: *const u8, width: c_int, height: c_int); texture_rgba: *mut c_void,
buffer: *const u8,
width: c_int,
height: c_int,
dst_rgba_stride: c_int,
);
// Video Texture Renderer in Flutter // Video Texture Renderer in Flutter
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
@ -206,7 +211,9 @@ impl VideoRenderer {
self.width = width; self.width = width;
self.height = height; self.height = height;
self.data_len = if width > 0 && height > 0 { self.data_len = if width > 0 && height > 0 {
(width * height * 4) as usize let sa1 = crate::DST_STRIDE_RGBA - 1;
let row_bytes = (width as usize * 4 + sa1) & !sa1;
row_bytes * height as usize
} else { } else {
0 0
}; };
@ -223,6 +230,7 @@ impl VideoRenderer {
rgba.as_ptr() as _, rgba.as_ptr() as _,
self.width as _, self.width as _,
self.height as _, self.height as _,
crate::DST_STRIDE_RGBA as _,
) )
}; };
} }