fix memory leak on macOS (#9)

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-03-14 12:44:36 +08:00 • committed by GitHub
parent f15e5eee20
commit b91e1b2092
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 35 deletions

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

@ -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