From b4efc3450e1e2aad5dee25af1ad6e1d740091374 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 30 Oct 2023 10:34:15 -0700 Subject: [PATCH] Revert "[Impeller] remove image upload workarounds." (flutter/engine#47402) Reverts flutter/engine#47209 This actually did regress things, I benchmarked poorly! see https://flutter-flutter-perf.skia.org/e/?queries=device_type%3DPixel_7_Pro%26sub_result%3D90th_percentile_frame_rasterizer_time_millis%26sub_result%3D99th_percentile_frame_rasterizer_time_millis%26sub_result%3Daverage_frame_rasterizer_time_millis%26sub_result%3Dworst_frame_rasterizer_time_millis%26test%3Dnew_gallery_impeller__transition_perf I did check https://developer.arm.com/documentation/101897/0301/CPU-overheads/Vulkan-CPU-memory-mapping and we're following the expected advice there. Without a much deeper investigation though we may not have any idea what to do --- .../lib/ui/painting/image_decoder_impeller.cc | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc b/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc index 9be5f0c631..0238044298 100644 --- a/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc +++ b/engine/src/flutter/lib/ui/painting/image_decoder_impeller.cc @@ -10,13 +10,14 @@ #include "flutter/fml/make_copyable.h" #include "flutter/fml/trace_event.h" #include "flutter/impeller/core/allocator.h" +#include "flutter/impeller/core/texture.h" #include "flutter/impeller/display_list/dl_image_impeller.h" #include "flutter/impeller/renderer/command_buffer.h" #include "flutter/impeller/renderer/context.h" +#include "flutter/lib/ui/painting/image_decoder_skia.h" #include "impeller/base/strings.h" #include "impeller/display_list/skia_conversions.h" #include "impeller/geometry/size.h" - #include "third_party/skia/include/core/SkAlphaType.h" #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkColorSpace.h" @@ -31,6 +32,42 @@ namespace flutter { +class MallocDeviceBuffer : public impeller::DeviceBuffer { + public: + explicit MallocDeviceBuffer(impeller::DeviceBufferDescriptor desc) + : impeller::DeviceBuffer(desc) { + data_ = static_cast(malloc(desc.size)); + } + + ~MallocDeviceBuffer() override { free(data_); } + + bool SetLabel(const std::string& label) override { return true; } + + bool SetLabel(const std::string& label, impeller::Range range) override { + return true; + } + + uint8_t* OnGetContents() const override { return data_; } + + bool OnCopyHostBuffer(const uint8_t* source, + impeller::Range source_range, + size_t offset) override { + memcpy(data_ + offset, source + source_range.offset, source_range.length); + return true; + } + + private: + uint8_t* data_; + + FML_DISALLOW_COPY_AND_ASSIGN(MallocDeviceBuffer); +}; + +#ifdef FML_OS_ANDROID +static constexpr bool kShouldUseMallocDeviceBuffer = true; +#else +static constexpr bool kShouldUseMallocDeviceBuffer = false; +#endif // FML_OS_ANDROID + namespace { /** * Loads the gamut as a set of three points (triangle). @@ -485,7 +522,8 @@ void ImageDecoderImpeller::Decode(fml::RefPtr descriptor, gpu_disabled_switch]() { sk_sp image; std::string decode_error; - if (context->GetCapabilities()->SupportsBufferToTextureBlits()) { + if (!kShouldUseMallocDeviceBuffer && + context->GetCapabilities()->SupportsBufferToTextureBlits()) { std::tie(image, decode_error) = UploadTextureToPrivate( context, bitmap_result.device_buffer, bitmap_result.image_info, bitmap_result.sk_bitmap, gpu_disabled_switch); @@ -529,7 +567,9 @@ bool ImpellerAllocator::allocPixelRef(SkBitmap* bitmap) { (bitmap->width() * bitmap->bytesPerPixel()); std::shared_ptr device_buffer = - allocator_->CreateBuffer(descriptor); + kShouldUseMallocDeviceBuffer + ? std::make_shared(descriptor) + : allocator_->CreateBuffer(descriptor); struct ImpellerPixelRef final : public SkPixelRef { ImpellerPixelRef(int w, int h, void* s, size_t r)