-
-
Notifications
You must be signed in to change notification settings - Fork 269
iOS copy native replay screenshot in-memory to native #2530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3c54d06
iOS copy native replay screenshot in-memory to native
vaind 0bebd52
formatting
vaind e420459
fix tests on web
vaind 0db8eb3
chore: changelog
vaind 4893a36
cleanups
vaind f485ade
fix wasm test
vaind cd51c10
objc syntax error
vaind 6c0fec0
comment
vaind 6543a63
Merge branch 'main' into enha/replay-copy-image-in-memory
vaind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:ffi'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:meta/meta.dart'; | ||
import 'package:ffi/ffi.dart' as pkg_ffi; | ||
|
||
@internal | ||
@immutable | ||
class NativeMemory { | ||
final Pointer<Uint8> pointer; | ||
final int length; | ||
|
||
const NativeMemory._(this.pointer, this.length); | ||
|
||
factory NativeMemory.fromUint8List(Uint8List source) { | ||
final length = source.length; | ||
final ptr = pkg_ffi.malloc.allocate<Uint8>(length); | ||
if (length > 0) { | ||
ptr.asTypedList(length).setAll(0, source); | ||
} | ||
return NativeMemory._(ptr, length); | ||
} | ||
|
||
factory NativeMemory.fromJson(Map<dynamic, dynamic> json) { | ||
final length = json['length'] as int; | ||
final ptr = Pointer<Uint8>.fromAddress(json['address'] as int); | ||
return NativeMemory._(ptr, length); | ||
} | ||
|
||
/// Frees the underlying native memory. | ||
/// You must not use this object after freeing. | ||
/// | ||
/// Currently, we only need to do this in tests because there's no native | ||
/// counterpart to free the memory. | ||
@visibleForTesting | ||
void free() => pkg_ffi.malloc.free(pointer); | ||
|
||
Uint8List asTypedList() => pointer.asTypedList(length); | ||
|
||
Map<String, int> toJson() => { | ||
'address': pointer.address, | ||
'length': length, | ||
}; | ||
} | ||
|
||
@internal | ||
extension Uint8ListNativeMemory on Uint8List { | ||
NativeMemory toNativeMemory() => NativeMemory.fromUint8List(this); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@TestOn('vm') | ||
library flutter_test; | ||
|
||
import 'dart:typed_data'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'native_memory_web_mock.dart' | ||
if (dart.library.io) 'package:sentry_flutter/src/native/native_memory.dart'; | ||
|
||
void main() { | ||
final testSrcList = Uint8List.fromList([1, 2, 3]); | ||
|
||
test('empty list', () async { | ||
final sut = NativeMemory.fromUint8List(Uint8List.fromList([])); | ||
expect(sut.length, 0); | ||
expect(sut.pointer.address, greaterThan(0)); | ||
expect(sut.asTypedList(), isEmpty); | ||
sut.free(); | ||
}); | ||
|
||
test('non-empty list', () async { | ||
final sut = NativeMemory.fromUint8List(testSrcList); | ||
expect(sut.length, 3); | ||
expect(sut.pointer.address, greaterThan(0)); | ||
expect(sut.asTypedList(), testSrcList); | ||
sut.free(); | ||
}); | ||
|
||
test('json', () async { | ||
final sut = NativeMemory.fromUint8List(testSrcList); | ||
final json = sut.toJson(); | ||
expect(json['address'], greaterThan(0)); | ||
expect(json['length'], 3); | ||
expect(json.entries, hasLength(2)); | ||
|
||
final sut2 = NativeMemory.fromJson(json); | ||
expect(sut2.toJson(), json); | ||
expect(sut2.asTypedList(), testSrcList); | ||
|
||
expect(sut.pointer, sut2.pointer); | ||
expect(sut.length, sut2.length); | ||
sut2.free(); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'dart:math'; | ||
import 'dart:typed_data'; | ||
|
||
// This is just a mock so `flutter test --platform chrome` works. | ||
// See https://github.com/flutter/flutter/issues/160675 | ||
class NativeMemory { | ||
final Pointer<Uint8> pointer; | ||
final int length; | ||
|
||
const NativeMemory._(this.pointer, this.length); | ||
|
||
factory NativeMemory.fromUint8List(Uint8List source) { | ||
return NativeMemory._(Pointer<Uint8>._store(source), source.length); | ||
} | ||
|
||
factory NativeMemory.fromJson(Map<dynamic, dynamic> json) { | ||
return NativeMemory._( | ||
Pointer<Uint8>._load(json['address'] as int), json['length'] as int); | ||
} | ||
|
||
void free() {} | ||
|
||
Uint8List asTypedList() => _memory[pointer.address]!; | ||
|
||
Map<String, int> toJson() => { | ||
'address': pointer.address, | ||
'length': length, | ||
}; | ||
} | ||
|
||
class Pointer<T> { | ||
final int address; | ||
|
||
const Pointer(this.address); | ||
|
||
factory Pointer._store(Uint8List data) { | ||
final address = Random().nextInt(999999); | ||
_memory[address] = data; | ||
return Pointer(address); | ||
} | ||
|
||
factory Pointer._load(int address) { | ||
return Pointer(address); | ||
} | ||
|
||
/// Equality for Pointers only depends on their address. | ||
@override | ||
bool operator ==(Object other) { | ||
if (other is! Pointer) return false; | ||
return address == other.address; | ||
} | ||
|
||
/// The hash code for a Pointer only depends on its address. | ||
@override | ||
int get hashCode => address.hashCode; | ||
} | ||
|
||
class Uint8 {} | ||
|
||
final _memory = <int, Uint8List>{}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.