Compare commits

...

10 commits

Author SHA1 Message Date
RustDesk
42797e0f03
Merge pull request #1 from 21pages/rm_switch_rgba
Remove switch rgba
2024-11-27 13:01:19 +09:00
21pages
c615e9507a windows: avoid unnecessary memory allocation
Signed-off-by: 21pages <sunboeasy@gmail.com>
2024-11-27 03:48:28 +08:00
21pages
96ed6df4a9 remove switch rgba
Signed-off-by: 21pages <sunboeasy@gmail.com>
2024-11-26 22:02:57 +08:00
Kingtous
c5922f891f bump: 0.0.16 2023-03-25 21:35:14 +08:00
Kingtous
bca854ff18 opt: remove len check 2023-03-25 21:34:25 +08:00
Kingtous
785c34f36b bump: 0.0.15 2023-03-23 23:34:09 +08:00
Kingtous
40fd713395 fix: remove unnecessary rgba class func pointer 2023-03-21 21:17:43 +08:00
Kingtous
937c10ffd9 bump: 0.0.13 2023-03-14 06:07:04 +00:00
fufesou
b91e1b2092
fix memory leak on macOS (#9)
Signed-off-by: fufesou <shuanglongchen@yeah.net>
2023-03-14 12:44:36 +08:00
Kingtous
f15e5eee20 bump: 0.0.12 2023-03-03 02:16:13 +00:00
16 changed files with 125 additions and 110 deletions

View file

@ -1,3 +1,18 @@
## 0.0.16
* opt: remove unneeded len check on Linux.
## 0.0.15
* opt: add the parameter check for len,width,height on Linux.
## 0.0.14
* fix: remove the `copy_pixels` function pointer initialization in texture rgba class.
## 0.0.13
* try to fix the memory leak on macOS.
## 0.0.12
* add `stride_align`.
## 0.0.11
* compatible for ubuntu 18.04 toolchain.

View file

@ -24,8 +24,8 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
final _textureRgbaRendererPlugin = TextureRgbaRenderer();
int textureId = -1;
int height = 500;
int width = 500;
int height = 768;
int width = 1377;
int cnt = 0;
var key = 0;
int texturePtr = 0;
@ -62,6 +62,7 @@ class _MyAppState extends State<MyApp> {
method = methodId;
final rowBytes = (width * 4 + strideAlign - 1) & (~(strideAlign - 1));
final picDataLength = rowBytes * height;
debugPrint('REMOVE ME =============================== rowBytes $rowBytes');
_timer?.cancel();
// 60 fps
_timer =
@ -142,8 +143,8 @@ class _MyAppState extends State<MyApp> {
final r = index / rowBytes;
final c = (index % rowBytes) / 4;
final p = index & 0x03;
final edgeH = (c >= 0 && c < 10) || ((c >= height - 10) && c < height);
final edgeW = (r >= 0 && r < 10) || ((r >= width - 10) && r < width);
final edgeH = (c >= 0 && c < 10) || ((c >= width - 10) && c < width);
final edgeW = (r >= 0 && r < 10) || ((r >= height - 10) && r < height);
if (edgeH || edgeW) {
if (p == 0 || p == 3) {
return 255;

View file

@ -6,7 +6,7 @@ packages:
description:
name: async
sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "2.10.0"
boolean_selector:
@ -14,7 +14,7 @@ packages:
description:
name: boolean_selector
sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
characters:
@ -22,7 +22,7 @@ packages:
description:
name: characters
sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
clock:
@ -30,7 +30,7 @@ packages:
description:
name: clock
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.1.1"
collection:
@ -38,7 +38,7 @@ packages:
description:
name: collection
sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.17.0"
cupertino_icons:
@ -46,7 +46,7 @@ packages:
description:
name: cupertino_icons
sha256: e35129dc44c9118cee2a5603506d823bab99c68393879edb440e0090d07586be
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.0.5"
fake_async:
@ -54,7 +54,7 @@ packages:
description:
name: fake_async
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.3.1"
ffi:
@ -62,7 +62,7 @@ packages:
description:
name: ffi
sha256: a38574032c5f1dd06c4aee541789906c12ccaab8ba01446e800d9c5b79c4a978
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
flutter:
@ -75,7 +75,7 @@ packages:
description:
name: flutter_lints
sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
flutter_test:
@ -88,7 +88,7 @@ packages:
description:
name: js
sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "0.6.5"
lints:
@ -96,7 +96,7 @@ packages:
description:
name: lints
sha256: "5e4a9cd06d447758280a8ac2405101e0e2094d2a1dbdd3756aec3fe7775ba593"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
matcher:
@ -104,7 +104,7 @@ packages:
description:
name: matcher
sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "0.12.13"
material_color_utilities:
@ -112,7 +112,7 @@ packages:
description:
name: material_color_utilities
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
meta:
@ -120,7 +120,7 @@ packages:
description:
name: meta
sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.8.0"
path:
@ -128,7 +128,7 @@ packages:
description:
name: path
sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.8.2"
plugin_platform_interface:
@ -136,7 +136,7 @@ packages:
description:
name: plugin_platform_interface
sha256: "6a2128648c854906c53fa8e33986fc0247a1116122f9534dd20e3ab9e16a32bc"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "2.1.4"
sky_engine:
@ -149,7 +149,7 @@ packages:
description:
name: source_span
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
stack_trace:
@ -157,7 +157,7 @@ packages:
description:
name: stack_trace
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
stream_channel:
@ -165,7 +165,7 @@ packages:
description:
name: stream_channel
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
string_scanner:
@ -173,7 +173,7 @@ packages:
description:
name: string_scanner
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
term_glyph:
@ -181,7 +181,7 @@ packages:
description:
name: term_glyph
sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
test_api:
@ -189,7 +189,7 @@ packages:
description:
name: test_api
sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "0.4.16"
texture_rgba_renderer:
@ -198,13 +198,13 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.10"
version: "0.0.13"
vector_math:
dependency: transitive
description:
name: vector_math
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
url: "https://pub.flutter-io.cn"
url: "https://pub.dev"
source: hosted
version: "2.1.4"
sdks:

View file

@ -1,9 +1,9 @@
import 'dart:ffi';
typedef F1 = Void Function(Pointer<Void> ptr, Pointer<Uint8> buffer, Int len,
Int32 width, Int32 height, Int stride_align);
Int32 width, Int32 height, Int strideAlign);
typedef F1Dart = void Function(Pointer<Void> ptr, Pointer<Uint8> buffer,
int len, int width, int height, int stride_align);
int len, int width, int height, int strideAlign);
class Native {
Native._();

View file

@ -24,9 +24,9 @@ class TextureRgbaRenderer {
/// Provide the rgba data to the texture.
Future<bool> onRgba(
int key, Uint8List data, int height, int width, int stride_align) {
int key, Uint8List data, int height, int width, int strideAlign) {
return TextureRgbaRendererPlatform.instance
.onRgba(key, data, height, width, stride_align);
.onRgba(key, data, height, width, strideAlign);
}
Future<int> getTexturePtr(int key) {

View file

@ -38,13 +38,13 @@ class MethodChannelTextureRgbaRenderer extends TextureRgbaRendererPlatform {
@override
Future<bool> onRgba(
int key, Uint8List data, int height, int width, int stride_align) async {
int key, Uint8List data, int height, int width, int strideAlign) async {
return await methodChannel.invokeMethod<bool>('onRgba', {
"data": data,
"height": height,
"width": width,
"key": key,
"stride_align": stride_align,
"stride_align": strideAlign,
}) ??
false;
}

View file

@ -40,7 +40,7 @@ abstract class TextureRgbaRendererPlatform extends PlatformInterface {
}
Future<bool> onRgba(
int key, Uint8List data, int height, int width, int stride_align) {
int key, Uint8List data, int height, int width, int strideAlign) {
throw UnimplementedError('onRgba() has not been implemented.');
}

View file

@ -39,21 +39,6 @@ G_DEFINE_TYPE(TextureRgba, texture_rgba,
(G_TYPE_CHECK_INSTANCE_CAST((obj), texture_rgba_get_type(), \
TextureRgba))
static inline void switch_rgba(uint8_t* pixels, int len, int height) {
uint8_t temp;
int byes_per_row = len / height;
int width = byes_per_row >> 2;
for (int i = 0; i < height; ++i) {
int row_idx_base = i * byes_per_row;
for (int k = 0; k < width; ++k) {
int idx = row_idx_base + k * 4;
temp = pixels[idx];
pixels[idx] = pixels[idx + 2];
pixels[idx + 2] = temp;
}
}
}
static void texture_rgba_terminate(TextureRgba* self) {
g_mutex_lock(&self->mutex);
@ -154,7 +139,7 @@ static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar)
static void texture_rgba_class_init(TextureRgbaClass *klass)
{
FL_PIXEL_BUFFER_TEXTURE_GET_CLASS(klass)->copy_pixels = texture_rgba_copy_pixels;
FL_PIXEL_BUFFER_TEXTURE_CLASS(klass)->copy_pixels = texture_rgba_copy_pixels;
}
static void texture_rgba_init(TextureRgba* self) {

View file

@ -142,6 +142,11 @@ void texture_rgba_renderer_plugin_register_with_registrar(FlPluginRegistrar *reg
extern "C" {
void FlutterRgbaRendererPluginOnRgba(void *texture_rgba_ptr, const uint8_t *buffer, int len, int width, int height, int stride_align) {
// Check whether the parameter is valid.
if (width == 0 || height == 0) {
g_warning("The width, height is not valid, ignoring this request.");
return;
}
TextureRgba* self = TEXTURE_RGBA(texture_rgba_ptr);
g_mutex_lock(&self->mutex);
// selfate has registered a texture_id,
@ -149,7 +154,6 @@ extern "C" {
// copy data to the texture.
auto copied_data = new uint8_t[len];
memcpy(copied_data, buffer, len);
switch_rgba(copied_data, len, height);
// It's safe to working on a non reading index
g_atomic_pointer_set(&self->buffer, copied_data);
g_atomic_int_set(&self->video_height, height);

View file

@ -32,38 +32,52 @@ import CoreVideo
return Unmanaged.passRetained(data!)
}
}
private func _markFrameAvaliable(buffer: UnsafePointer<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
var pixelBufferCopy: CVPixelBuffer?
// macOS only support 32BGRA currently.
let dict: [String: Any] = [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
kCVPixelBufferMetalCompatibilityKey as String: true,
kCVPixelBufferOpenGLCompatibilityKey as String: true,
// https://developer.apple.com/forums/thread/712709
kCVPixelBufferBytesPerRowAlignmentKey as String: 64
]
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, dict as CFDictionary, &pixelBufferCopy)
guard result == kCVReturnSuccess else {
return false
}
CVPixelBufferLockBaseAddress(pixelBufferCopy!, [])
let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)!
memcpy(ptr, buffer, len)
CVPixelBufferUnlockBaseAddress(pixelBufferCopy!, [])
self.data = pixelBufferCopy
self.width = width
self.height = height
if textureId != -1 && self.data != nil {
registry?.textureFrameAvailable(textureId)
return true
} else {
return false
}
}
@objc public func markFrameAvaliableRaw(buffer: UnsafePointer<UInt8>, len: Int, width: Int, height: Int, stride_align: Int) -> Bool {
queue.sync {
_markFrameAvaliable(buffer: buffer, len: len, width: width, height: height, stride_align: stride_align)
}
}
@objc public func markFrameAvaliable(data: Data, width: Int, height: Int, stride_align: Int) -> Bool {
queue.sync {
self.data = data.withUnsafeBytes { buffer in
var pixelBufferCopy: CVPixelBuffer!
// macOS only support 32BGRA currently.
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, [
kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA,
kCVPixelBufferMetalCompatibilityKey: true,
kCVPixelBufferOpenGLCompatibilityKey: true,
// https://developer.apple.com/forums/thread/712709
kCVPixelBufferBytesPerRowAlignmentKey: 64
] as CFDictionary, &pixelBufferCopy)
guard result == kCVReturnSuccess else {
return nil
}
CVPixelBufferLockBaseAddress(pixelBufferCopy, [])
let source = buffer.baseAddress!
let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)!
memcpy(ptr, source, data.count)
CVPixelBufferUnlockBaseAddress(pixelBufferCopy, [])
return pixelBufferCopy
}
self.width = width
self.height = height
if (textureId != -1 && self.data != nil) {
registry?.textureFrameAvailable(textureId)
return true
} else {
return false
}
data.withUnsafeBytes { buffer in
markFrameAvaliableRaw(
buffer: buffer.baseAddress!.assumingMemoryBound(to: UInt8.self),
len: data.count,
width: width,
height: height,
stride_align: stride_align)
}
}
}

View file

@ -15,8 +15,7 @@ extern "C" {
/// Keep the same name with Windows.
void FlutterRgbaRendererPluginOnRgba(void* texture_rgba_ptr, const uint8_t* buffer, int len, int width, int height, int stride_align) {
TextRgba* texture_rgba = (__bridge TextRgba *)(texture_rgba_ptr);
NSData* data = [NSData dataWithBytesNoCopy:(void*)buffer length:len freeWhenDone:FALSE];
[texture_rgba markFrameAvaliableWithData:data width:width height:height stride_align: stride_align];
[texture_rgba markFrameAvaliableRawWithBuffer:buffer len:len width:width height:height stride_align: stride_align];
}
#if __cplusplus

View file

@ -1,6 +1,6 @@
name: texture_rgba_renderer
description: A texture helper which brings a high level api to handle BGRA data.
version: 0.0.11
version: 0.0.16
homepage: https://github.com/Kingtous/flutter_texture_rgba_renderer
environment:

View file

@ -18,9 +18,10 @@ class MockTextureRgbaRendererPlatform
Future<int> createTexture(int key) => Future.value(1);
@override
Future<bool> onRgba(int key, Uint8List data, int height, int width) =>
Future<bool> onRgba(
int key, Uint8List data, int height, int width, int strideAlign) =>
Future.value(true);
@override
Future<int> getTexturePtr(int key) {
return Future.value(0);

View file

@ -16,24 +16,6 @@ TextureRgba::~TextureRgba()
texture_registrar_->UnregisterTexture(texture_id_);
}
void TextureRgba::switch_rgba(std::vector<uint8_t> &buffer, size_t height)
{
uint8_t temp;
size_t byes_per_row = buffer.size() / height;
size_t width = byes_per_row >> 2;
for (size_t i = 0; i < height; ++i)
{
size_t row_idx_base = i * byes_per_row;
for (size_t k = 0; k < width; ++k)
{
size_t idx = row_idx_base + k * 4;
temp = buffer[idx];
buffer[idx] = buffer[idx + 2];
buffer[idx + 2] = temp;
}
}
}
void TextureRgba::MarkVideoFrameAvailable(
std::vector<uint8_t> &buffer, size_t width, size_t height)
{
@ -45,7 +27,6 @@ void TextureRgba::MarkVideoFrameAvailable(
// Do not check bellow. Because the data may have paddings per row.
// buffer.size() != width * height * 4
switch_rgba(buffer, height);
const std::lock_guard<std::mutex> lock(mutex_);
int bg_index = fg_index_ ^ 1;
buffer.swap(buffer_tmp_[bg_index]);
@ -58,6 +39,23 @@ void TextureRgba::MarkVideoFrameAvailable(
}
}
void TextureRgba::MarkVideoFrameAvailable2(const uint8_t* buffer, const size_t buffer_length, size_t width, size_t height) {
if (!buffer || buffer_length < width * height * 4)
{
return;
}
const std::lock_guard<std::mutex> lock(mutex_);
int bg_index = fg_index_ ^ 1;
buffer_tmp_[bg_index].assign(buffer, buffer + buffer_length);
width_[bg_index] = width;
height_[bg_index] = height;
if (!buff_ready_)
{
buff_ready_ = true;
texture_registrar_->MarkTextureFrameAvailable(texture_id_);
}
}
// This function may be called
// event texture_registrar_->MarkTextureFrameAvailable(texture_id_); is not called before.
inline const FlutterDesktopPixelBuffer *TextureRgba::buffer()

View file

@ -13,13 +13,12 @@ public:
void MarkVideoFrameAvailable(std::vector<uint8_t>& buffer, size_t width, size_t height);
void MarkVideoFrameAvailable2(const uint8_t* buffer, size_t buffer_length, size_t width, size_t height);
int64_t texture_id() const { return texture_id_; };
const FlutterDesktopPixelBuffer* buffer();
private:
void switch_rgba(std::vector<uint8_t>& buffer, size_t height);
private:
FlutterDesktopPixelBuffer flutter_pixel_buffer_{};
flutter::TextureRegistrar* texture_registrar_ = nullptr;

View file

@ -25,6 +25,5 @@ void FlutterRgbaRendererPluginOnRgba(void *texture_rgba, const uint8_t *buffer,
}
TextureRgba *rgba = static_cast<TextureRgba *>(texture_rgba);
std::vector<uint8_t> data(buffer, buffer + len);
rgba->MarkVideoFrameAvailable(data, width, height);
rgba->MarkVideoFrameAvailable2(buffer, len, width, height);
}