fix memory leak on macOS (#9)
Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
parent
f15e5eee20
commit
b91e1b2092
3 changed files with 49 additions and 35 deletions
|
|
@ -24,8 +24,8 @@ class MyApp extends StatefulWidget {
|
||||||
class _MyAppState extends State<MyApp> {
|
class _MyAppState extends State<MyApp> {
|
||||||
final _textureRgbaRendererPlugin = TextureRgbaRenderer();
|
final _textureRgbaRendererPlugin = TextureRgbaRenderer();
|
||||||
int textureId = -1;
|
int textureId = -1;
|
||||||
int height = 500;
|
int height = 768;
|
||||||
int width = 500;
|
int width = 1377;
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
var key = 0;
|
var key = 0;
|
||||||
int texturePtr = 0;
|
int texturePtr = 0;
|
||||||
|
|
@ -62,6 +62,7 @@ class _MyAppState extends State<MyApp> {
|
||||||
method = methodId;
|
method = methodId;
|
||||||
final rowBytes = (width * 4 + strideAlign - 1) & (~(strideAlign - 1));
|
final rowBytes = (width * 4 + strideAlign - 1) & (~(strideAlign - 1));
|
||||||
final picDataLength = rowBytes * height;
|
final picDataLength = rowBytes * height;
|
||||||
|
debugPrint('REMOVE ME =============================== rowBytes $rowBytes');
|
||||||
_timer?.cancel();
|
_timer?.cancel();
|
||||||
// 60 fps
|
// 60 fps
|
||||||
_timer =
|
_timer =
|
||||||
|
|
@ -142,8 +143,8 @@ class _MyAppState extends State<MyApp> {
|
||||||
final r = index / rowBytes;
|
final r = index / rowBytes;
|
||||||
final c = (index % rowBytes) / 4;
|
final c = (index % rowBytes) / 4;
|
||||||
final p = index & 0x03;
|
final p = index & 0x03;
|
||||||
final edgeH = (c >= 0 && c < 10) || ((c >= height - 10) && c < height);
|
final edgeH = (c >= 0 && c < 10) || ((c >= width - 10) && c < width);
|
||||||
final edgeW = (r >= 0 && r < 10) || ((r >= width - 10) && r < width);
|
final edgeW = (r >= 0 && r < 10) || ((r >= height - 10) && r < height);
|
||||||
if (edgeH || edgeW) {
|
if (edgeH || edgeW) {
|
||||||
if (p == 0 || p == 3) {
|
if (p == 0 || p == 3) {
|
||||||
return 255;
|
return 255;
|
||||||
|
|
|
||||||
|
|
@ -33,37 +33,51 @@ import CoreVideo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
@objc public func markFrameAvaliable(data: Data, width: Int, height: Int, stride_align: Int) -> Bool {
|
||||||
queue.sync {
|
data.withUnsafeBytes { buffer in
|
||||||
self.data = data.withUnsafeBytes { buffer in
|
markFrameAvaliableRaw(
|
||||||
var pixelBufferCopy: CVPixelBuffer!
|
buffer: buffer.baseAddress!.assumingMemoryBound(to: UInt8.self),
|
||||||
// macOS only support 32BGRA currently.
|
len: data.count,
|
||||||
let result = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, [
|
width: width,
|
||||||
kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA,
|
height: height,
|
||||||
kCVPixelBufferMetalCompatibilityKey: true,
|
stride_align: stride_align)
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,7 @@ extern "C" {
|
||||||
/// Keep the same name with Windows.
|
/// 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) {
|
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);
|
TextRgba* texture_rgba = (__bridge TextRgba *)(texture_rgba_ptr);
|
||||||
NSData* data = [NSData dataWithBytesNoCopy:(void*)buffer length:len freeWhenDone:FALSE];
|
[texture_rgba markFrameAvaliableRawWithBuffer:buffer len:len width:width height:height stride_align: stride_align];
|
||||||
[texture_rgba markFrameAvaliableWithData:data width:width height:height stride_align: stride_align];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue