Followup on post-merge comments for 9b03bcd7a (flutter/engine#4321)

This commit is contained in:
amirh
2017-11-03 11:44:23 -07:00
committed by GitHub
parent 55fb84e01b
commit 92a0de09f8
4 changed files with 31 additions and 20 deletions

View File

@@ -901,10 +901,15 @@ abstract class Codec extends NativeFieldWrapperClass2 {
}
/// Callback signature for [imageCodecFromList].
///
/// If parsing the data passed to [imageCodecFromList] failed, the result will
/// be null.
typedef void CodecCallback(Codec result);
/// Instatiates a [Codec] object for an image binary data.
void instantiateImageCodec(Uint8List list, CodecCallback callback)
///
/// Returns an error message if the instantiation has failed, null otherwise.
String instantiateImageCodec(Uint8List list, CodecCallback callback)
native "instantiateImageCodec";
/// Convert an image file from a byte array into an [Image] object.

View File

@@ -55,8 +55,6 @@ std::unique_ptr<SkCodec> InitCodec(sk_sp<SkData> buffer, size_t trace_id) {
}
void InvokeCodecCallback(std::unique_ptr<SkCodec> codec,
int frameCount,
int repetitionCount,
std::unique_ptr<DartPersistentValue> callback,
size_t trace_id) {
tonic::DartState* dart_state = callback->dart_state().get();
@@ -68,7 +66,8 @@ void InvokeCodecCallback(std::unique_ptr<SkCodec> codec,
if (!codec) {
DartInvoke(callback->value(), {Dart_Null()});
} else {
fxl::RefPtr<Codec> resultCodec = MultiFrameCodec::Create(std::move(codec));
fxl::RefPtr<Codec> resultCodec =
fxl::MakeRefCounted<MultiFrameCodec>(std::move(codec));
DartInvoke(callback->value(), {ToDart(resultCodec)});
}
TRACE_FLOW_END("flutter", kInitCodecTraceTag, trace_id);
@@ -79,14 +78,10 @@ void InitCodecAndInvokeCodecCallback(
sk_sp<SkData> buffer,
size_t trace_id) {
std::unique_ptr<SkCodec> codec = InitCodec(std::move(buffer), trace_id);
int frameCount = codec->getFrameCount();
int repetitionCount = codec->getRepetitionCount();
Threads::UI()->PostTask(fxl::MakeCopyable([
callback = std::move(callback), codec = std::move(codec), trace_id,
frameCount, repetitionCount
callback = std::move(callback), codec = std::move(codec), trace_id
]() mutable {
InvokeCodecCallback(std::move(codec), frameCount, repetitionCount,
std::move(callback), trace_id);
InvokeCodecCallback(std::move(codec), std::move(callback), trace_id);
}));
}
@@ -101,14 +96,14 @@ void InstantiateImageCodec(Dart_NativeArguments args) {
tonic::DartConverter<tonic::Uint8List>::FromArguments(args, 0, exception);
if (exception) {
TRACE_FLOW_END("flutter", kInitCodecTraceTag, trace_id);
Dart_ThrowException(exception);
Dart_SetReturnValue(args, exception);
return;
}
Dart_Handle callback_handle = Dart_GetNativeArgument(args, 1);
if (!Dart_IsClosure(callback_handle)) {
TRACE_FLOW_END("flutter", kInitCodecTraceTag, trace_id);
Dart_ThrowException(ToDart("Callback must be a function"));
Dart_SetReturnValue(args, ToDart("Callback must be a function"));
return;
}

View File

@@ -30,13 +30,7 @@ class Codec : public fxl::RefCountedThreadSafe<Codec>,
};
class MultiFrameCodec : public Codec {
FRIEND_MAKE_REF_COUNTED(MultiFrameCodec);
public:
static fxl::RefPtr<MultiFrameCodec> Create(std::unique_ptr<SkCodec> codec) {
return fxl::MakeRefCounted<MultiFrameCodec>(std::move(codec));
}
int frameCount() { return frameCount_; }
int repetitionCount() { return repetitionCount_; }
@@ -44,10 +38,14 @@ class MultiFrameCodec : public Codec {
private:
MultiFrameCodec(std::unique_ptr<SkCodec> codec);
~MultiFrameCodec() {}
const std::unique_ptr<SkCodec> codec_;
int frameCount_;
int repetitionCount_;
FRIEND_MAKE_REF_COUNTED(MultiFrameCodec);
FRIEND_REF_COUNTED_THREAD_SAFE(MultiFrameCodec);
};
} // namespace blink

View File

@@ -15,7 +15,7 @@ void main() {
test('Animation metadata', () async {
Uint8List data = await _getSkiaResource('alphabetAnim.gif').readAsBytes();
Completer<ui.Codec> completer = new Completer<ui.Codec>();
ui.instantiateImageCodec(data, completer.complete);
expect(ui.instantiateImageCodec(data, completer.complete), null);
ui.Codec codec = await completer.future;
expect(codec.frameCount, 13);
expect(codec.repetitionCount, 0);
@@ -23,11 +23,24 @@ void main() {
data = await _getSkiaResource('test640x479.gif').readAsBytes();
completer = new Completer<ui.Codec>();
ui.instantiateImageCodec(data, completer.complete);
expect(ui.instantiateImageCodec(data, completer.complete), null);
codec = await completer.future;
expect(codec.frameCount, 4);
expect(codec.repetitionCount, -1);
});
test('Fails when no callback provided', () async {
Uint8List data = await _getSkiaResource('alphabetAnim.gif').readAsBytes();
expect(ui.instantiateImageCodec(data, null), 'Callback must be a function');
});
test('Fails with invalid data', () async {
Uint8List data = new Uint8List.fromList([1, 2, 3]);
Completer<ui.Codec> completer = new Completer<ui.Codec>();
expect(ui.instantiateImageCodec(data, completer.complete), null);
ui.Codec codec = await completer.future;
expect(codec, null);
});
}
/// Returns a File handle to a file in the skia/resources directory.