From b11a529c91ac0c252db48e76e7ef2f3108590dd6 Mon Sep 17 00:00:00 2001 From: amirh Date: Mon, 20 Nov 2017 14:42:42 -0800 Subject: [PATCH] Delete the native implementation for ui.decodeImageFromList. (flutter/engine#4375) The new ui.Codec API is a superset of the implementation in image_decoding.cc. While we are not using ui.decodeImageFromList in the framework anymore, we keep the dart function for backward compatability, and just make it a thin wrapper around the codec API. --- engine/src/flutter/lib/ui/BUILD.gn | 2 - engine/src/flutter/lib/ui/dart_ui.cc | 2 - engine/src/flutter/lib/ui/painting.dart | 16 ++- .../flutter/lib/ui/painting/image_decoding.cc | 118 ------------------ .../flutter/lib/ui/painting/image_decoding.h | 19 --- 5 files changed, 13 insertions(+), 144 deletions(-) delete mode 100644 engine/src/flutter/lib/ui/painting/image_decoding.cc delete mode 100644 engine/src/flutter/lib/ui/painting/image_decoding.h diff --git a/engine/src/flutter/lib/ui/BUILD.gn b/engine/src/flutter/lib/ui/BUILD.gn index cd11698523..b5d4d4dc94 100644 --- a/engine/src/flutter/lib/ui/BUILD.gn +++ b/engine/src/flutter/lib/ui/BUILD.gn @@ -24,8 +24,6 @@ source_set("ui") { "painting/gradient.h", "painting/image.cc", "painting/image.h", - "painting/image_decoding.cc", - "painting/image_decoding.h", "painting/image_filter.cc", "painting/image_filter.h", "painting/image_shader.cc", diff --git a/engine/src/flutter/lib/ui/dart_ui.cc b/engine/src/flutter/lib/ui/dart_ui.cc index c8dfb40232..6853bfdec8 100644 --- a/engine/src/flutter/lib/ui/dart_ui.cc +++ b/engine/src/flutter/lib/ui/dart_ui.cc @@ -12,7 +12,6 @@ #include "flutter/lib/ui/painting/frame_info.h" #include "flutter/lib/ui/painting/gradient.h" #include "flutter/lib/ui/painting/image.h" -#include "flutter/lib/ui/painting/image_decoding.h" #include "flutter/lib/ui/painting/image_filter.h" #include "flutter/lib/ui/painting/image_shader.h" #include "flutter/lib/ui/painting/mask_filter.h" @@ -58,7 +57,6 @@ void DartUI::InitForGlobal() { Codec::RegisterNatives(g_natives); DartRuntimeHooks::RegisterNatives(g_natives); FrameInfo::RegisterNatives(g_natives); - ImageDecoding::RegisterNatives(g_natives); ImageFilter::RegisterNatives(g_natives); ImageShader::RegisterNatives(g_natives); MaskFilter::RegisterNatives(g_natives); diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 6f4a5a6025..84a828150a 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -928,9 +928,19 @@ Future instantiateImageCodec(Uint8List list) { String _instantiateImageCodec(Uint8List list, _Callback callback) native "instantiateImageCodec"; -/// Convert an image file from a byte array into an [Image] object. -void decodeImageFromList(Uint8List list, ImageDecoderCallback callback) - native "decodeImageFromList"; +/// Loads a single image frame from a byte array into an [Image] object. +/// +/// This is a convenience wrapper around [instantiateImageCodec]. +/// Prefer using [instantiateImageCodec] which also supports multi frame images. +void decodeImageFromList(Uint8List list, ImageDecoderCallback callback) { + _decodeImageFromListAsync(list, callback); +} + +Future _decodeImageFromListAsync(Uint8List list, ImageDecoderCallback callback) async { + final Codec codec = await instantiateImageCodec(list); + final FrameInfo frameInfo = await codec.getNextFrame(); + callback(frameInfo.image); +} /// Determines the winding rule that decides how the interior of a [Path] is /// calculated. diff --git a/engine/src/flutter/lib/ui/painting/image_decoding.cc b/engine/src/flutter/lib/ui/painting/image_decoding.cc deleted file mode 100644 index b909e60d8e..0000000000 --- a/engine/src/flutter/lib/ui/painting/image_decoding.cc +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/lib/ui/painting/image_decoding.h" - -#include "flutter/common/threads.h" -#include "flutter/glue/trace_event.h" -#include "flutter/lib/ui/painting/image.h" -#include "flutter/lib/ui/painting/resource_context.h" -#include "lib/fxl/build_config.h" -#include "lib/fxl/functional/make_copyable.h" -#include "lib/tonic/dart_persistent_value.h" -#include "lib/tonic/dart_state.h" -#include "lib/tonic/logging/dart_invoke.h" -#include "lib/tonic/typed_data/uint8_list.h" -#include "third_party/skia/include/core/SkImageGenerator.h" - -using tonic::DartInvoke; -using tonic::DartPersistentValue; -using tonic::ToDart; - -namespace blink { -namespace { - -static constexpr const char* kDecodeImageTraceTag = "DecodeImage"; - -sk_sp DecodeImage(sk_sp buffer, size_t trace_id) { - TRACE_FLOW_STEP("flutter", kDecodeImageTraceTag, trace_id); - TRACE_EVENT0("blink", "DecodeImage"); - - if (buffer == nullptr || buffer->isEmpty()) { - return nullptr; - } - - GrContext* context = ResourceContext::Get(); - if (context) { - // This indicates that we do not want a "linear blending" decode. - sk_sp dstColorSpace = nullptr; - return SkImage::MakeCrossContextFromEncoded(context, std::move(buffer), - false, dstColorSpace.get()); - } else { - return SkImage::MakeFromEncoded(std::move(buffer)); - } -} - -void InvokeImageCallback(sk_sp image, - std::unique_ptr callback, - size_t trace_id) { - tonic::DartState* dart_state = callback->dart_state().get(); - if (!dart_state) { - TRACE_FLOW_END("flutter", kDecodeImageTraceTag, trace_id); - return; - } - tonic::DartState::Scope scope(dart_state); - if (!image) { - DartInvoke(callback->value(), {Dart_Null()}); - } else { - fxl::RefPtr resultImage = CanvasImage::Create(); - resultImage->set_image(std::move(image)); - DartInvoke(callback->value(), {ToDart(resultImage)}); - } - TRACE_FLOW_END("flutter", kDecodeImageTraceTag, trace_id); -} - -void DecodeImageAndInvokeImageCallback( - std::unique_ptr callback, - sk_sp buffer, - size_t trace_id) { - sk_sp image = DecodeImage(std::move(buffer), trace_id); - Threads::UI()->PostTask(fxl::MakeCopyable([ - callback = std::move(callback), image, trace_id - ]() mutable { InvokeImageCallback(image, std::move(callback), trace_id); })); -} - -void DecodeImageFromList(Dart_NativeArguments args) { - static size_t trace_counter = 1; - const size_t trace_id = trace_counter++; - TRACE_FLOW_BEGIN("flutter", kDecodeImageTraceTag, trace_id); - - Dart_Handle exception = nullptr; - - tonic::Uint8List list = - tonic::DartConverter::FromArguments(args, 0, exception); - if (exception) { - TRACE_FLOW_END("flutter", kDecodeImageTraceTag, trace_id); - Dart_ThrowException(exception); - return; - } - - Dart_Handle callback_handle = Dart_GetNativeArgument(args, 1); - if (!Dart_IsClosure(callback_handle)) { - TRACE_FLOW_END("flutter", kDecodeImageTraceTag, trace_id); - Dart_ThrowException(ToDart("Callback must be a function")); - return; - } - - auto buffer = SkData::MakeWithCopy(list.data(), list.num_elements()); - - Threads::IO()->PostTask(fxl::MakeCopyable([ - callback = std::make_unique( - tonic::DartState::Current(), callback_handle), - buffer = std::move(buffer), trace_id - ]() mutable { - DecodeImageAndInvokeImageCallback(std::move(callback), std::move(buffer), - trace_id); - })); -} - -} // namespace - -void ImageDecoding::RegisterNatives(tonic::DartLibraryNatives* natives) { - natives->Register({ - {"decodeImageFromList", DecodeImageFromList, 2, true}, - }); -} - -} // namespace blink diff --git a/engine/src/flutter/lib/ui/painting/image_decoding.h b/engine/src/flutter/lib/ui/painting/image_decoding.h deleted file mode 100644 index 994d40537f..0000000000 --- a/engine/src/flutter/lib/ui/painting/image_decoding.h +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_DECODING_H_ -#define FLUTTER_LIB_UI_PAINTING_IMAGE_DECODING_H_ - -#include "lib/tonic/dart_library_natives.h" - -namespace blink { - -class ImageDecoding { - public: - static void RegisterNatives(tonic::DartLibraryNatives* natives); -}; - -} // namespace blink - -#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_DECODING_H_