[impeller] fake image for fake tests. (flutter/engine#54974)

Simulator only change to allow toImage and toByteData to not fail when there is no metal context w/ impeller.
This commit is contained in:
Jonah Williams
2024-09-06 11:53:53 -07:00
committed by GitHub
parent ac26f2ceb4
commit 547dbe4c4c
7 changed files with 76 additions and 9 deletions

View File

@@ -9,7 +9,7 @@
#include <optional>
#include <string>
#include "flutter/fml/macros.h"
#include "flutter/fml/build_config.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkRefCnt.h"
@@ -118,6 +118,10 @@ class DlImage : public SkRefCnt {
/// image.
virtual std::optional<std::string> get_error() const;
#if FML_OS_IOS_SIMULATOR
virtual bool IsFakeImage() const { return false; }
#endif // FML_OS_IOS_SIMULATOR
bool Equals(const DlImage* other) const {
if (!other) {
return false;

View File

@@ -9,6 +9,17 @@
namespace impeller {
#if FML_OS_IOS_SIMULATOR
sk_sp<DlImageImpeller> DlImageImpeller::Make(std::shared_ptr<Texture> texture,
OwningContext owning_context,
bool is_fake_image) {
if (!texture && !is_fake_image) {
return nullptr;
}
return sk_sp<DlImageImpeller>(
new DlImageImpeller(std::move(texture), owning_context, is_fake_image));
}
#else
sk_sp<DlImageImpeller> DlImageImpeller::Make(std::shared_ptr<Texture> texture,
OwningContext owning_context) {
if (!texture) {
@@ -17,6 +28,7 @@ sk_sp<DlImageImpeller> DlImageImpeller::Make(std::shared_ptr<Texture> texture,
return sk_sp<DlImageImpeller>(
new DlImageImpeller(std::move(texture), owning_context));
}
#endif // FML_OS_IOS_SIMULATOR
sk_sp<DlImageImpeller> DlImageImpeller::MakeFromYUVTextures(
AiksContext* aiks_context,
@@ -45,8 +57,20 @@ sk_sp<DlImageImpeller> DlImageImpeller::MakeFromYUVTextures(
}
DlImageImpeller::DlImageImpeller(std::shared_ptr<Texture> texture,
OwningContext owning_context)
: texture_(std::move(texture)), owning_context_(owning_context) {}
OwningContext owning_context
#ifdef FML_OS_IOS_SIMULATOR
,
bool is_fake_image
#endif // FML_OS_IOS_SIMULATOR
)
: texture_(std::move(texture)),
owning_context_(owning_context)
#ifdef FML_OS_IOS_SIMULATOR
,
is_fake_image_(is_fake_image)
#endif // #ifdef FML_OS_IOS_SIMULATOR
{
}
// |DlImage|
DlImageImpeller::~DlImageImpeller() = default;

View File

@@ -16,7 +16,12 @@ class DlImageImpeller final : public flutter::DlImage {
public:
static sk_sp<DlImageImpeller> Make(
std::shared_ptr<Texture> texture,
OwningContext owning_context = OwningContext::kIO);
OwningContext owning_context = OwningContext::kIO
#if FML_OS_IOS_SIMULATOR
,
bool is_fake_image = false
#endif // FML_OS_IOS_SIMULATOR
);
static sk_sp<DlImageImpeller> MakeFromYUVTextures(
AiksContext* aiks_context,
@@ -51,12 +56,25 @@ class DlImageImpeller final : public flutter::DlImage {
// |DlImage|
OwningContext owning_context() const override { return owning_context_; }
#if FML_OS_IOS_SIMULATOR
// |DlImage|
bool IsFakeImage() const override { return is_fake_image_; }
#endif // FML_OS_IOS_SIMULATOR
private:
std::shared_ptr<Texture> texture_;
OwningContext owning_context_;
#if FML_OS_IOS_SIMULATOR
bool is_fake_image_ = false;
#endif // FML_OS_IOS_SIMULATOR
explicit DlImageImpeller(std::shared_ptr<Texture> texture,
OwningContext owning_context = OwningContext::kIO);
OwningContext owning_context = OwningContext::kIO
#if FML_OS_IOS_SIMULATOR
,
bool is_fake_image = false
#endif // FML_OS_IOS_SIMULATOR
);
DlImageImpeller(const DlImageImpeller&) = delete;

View File

@@ -52,8 +52,7 @@ int CanvasImage::colorSpace() {
return ImageEncodingImpeller::GetColorSpace(image_->impeller_texture());
#endif // IMPELLER_SUPPORTS_RENDERING
}
return -1;
return ColorSpace::kSRGB;
}
} // namespace flutter

View File

@@ -178,6 +178,14 @@ Dart_Handle EncodeImage(CanvasImage* canvas_image,
auto callback = std::make_unique<DartPersistentValue>(
tonic::DartState::Current(), callback_handle);
#if IMPELLER_SUPPORTS_RENDERING && FML_OS_IOS_SIMULATOR
if (canvas_image->image()->IsFakeImage()) {
sk_sp<SkData> data = SkData::MakeEmpty();
InvokeDataCallback(std::move(callback), data);
return Dart_Null();
}
#endif // IMPELLER_SUPPORTS_RENDERING && FML_OS_IOS_SIMULATOR
const auto& task_runners = UIDartState::Current()->GetTaskRunners();
// The static leak checker gets confused by the use of fml::MakeCopyable.

View File

@@ -207,7 +207,6 @@ Dart_Handle Picture::DoRasterizeToImage(const sk_sp<DisplayList>& display_list,
height, ui_task,
layer_tree = std::move(layer_tree)]() mutable {
auto picture_bounds = SkISize::Make(width, height);
sk_sp<DlImage> image;
sk_sp<DisplayList> snapshot_display_list = display_list;
if (layer_tree) {
FML_DCHECK(picture_bounds == layer_tree->frame_size());

View File

@@ -7,6 +7,7 @@
#include <algorithm>
#include "flutter/flow/surface.h"
#include "flutter/fml/build_config.h"
#include "flutter/fml/trace_event.h"
#include "flutter/impeller/display_list/dl_dispatcher.h"
#include "flutter/impeller/display_list/dl_image_impeller.h"
@@ -126,7 +127,6 @@ void SnapshotControllerImpeller::MakeRasterSnapshot(
sk_sp<DisplayList> display_list,
SkISize picture_size,
std::function<void(const sk_sp<DlImage>&)> callback) {
sk_sp<DlImage> result;
std::shared_ptr<const fml::SyncSwitch> sync_switch =
GetDelegate().GetIsGpuDisabledSyncSwitch();
sync_switch->Execute(
@@ -143,10 +143,25 @@ void SnapshotControllerImpeller::MakeRasterSnapshot(
},
[callback]() { callback(nullptr); });
} else {
#if FML_OS_IOS_SIMULATOR
callback(impeller::DlImageImpeller::Make(
nullptr, DlImage::OwningContext::kRaster,
/*is_fake_image=*/true));
#else
callback(nullptr);
#endif // FML_OS_IOS_SIMULATOR
}
})
.SetIfFalse([&] {
#if FML_OS_IOS_SIMULATOR
if (!GetDelegate().GetAiksContext()) {
callback(impeller::DlImageImpeller::Make(
nullptr, DlImage::OwningContext::kRaster,
/*is_fake_image=*/true));
return;
}
#endif
callback(DoMakeRasterSnapshot(display_list, picture_size,
GetDelegate().GetAiksContext()));
}));