Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Migrate Metal translation units to Obj-C++ #56592

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion display_list/testing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ source_set("display_list_surface_provider") {

if (surface_provider_include_metal) {
sources += [
"dl_test_surface_metal.cc",
"dl_test_surface_metal.h",
"dl_test_surface_metal.mm",
]
deps += [
"//flutter/impeller/display_list",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@ namespace testing {

class DlMetalSurfaceInstance : public DlSurfaceInstance {
public:
explicit DlMetalSurfaceInstance(
std::unique_ptr<TestMetalSurface> metal_surface)
explicit DlMetalSurfaceInstance(std::unique_ptr<TestMetalSurface> metal_surface)
: metal_surface_(std::move(metal_surface)) {}
~DlMetalSurfaceInstance() = default;

sk_sp<SkSurface> sk_surface() const override {
return metal_surface_->GetSurface();
}
sk_sp<SkSurface> sk_surface() const override { return metal_surface_->GetSurface(); }

private:
std::unique_ptr<TestMetalSurface> metal_surface_;
};

bool DlMetalSurfaceProvider::InitializeSurface(size_t width,
size_t height,
PixelFormat format) {
bool DlMetalSurfaceProvider::InitializeSurface(size_t width, size_t height, PixelFormat format) {
if (format != kN32PremulPixelFormat) {
return false;
}
Expand All @@ -39,8 +34,7 @@ bool DlMetalSurfaceProvider::InitializeSurface(size_t width,
return true;
}

std::shared_ptr<DlSurfaceInstance> DlMetalSurfaceProvider::GetPrimarySurface()
const {
std::shared_ptr<DlSurfaceInstance> DlMetalSurfaceProvider::GetPrimarySurface() const {
if (!metal_surface_) {
return nullptr;
}
Expand All @@ -51,51 +45,42 @@ std::shared_ptr<DlSurfaceInstance> DlMetalSurfaceProvider::MakeOffscreenSurface(
size_t width,
size_t height,
PixelFormat format) const {
auto surface =
TestMetalSurface::Create(*metal_context_, SkISize::Make(width, height));
auto surface = TestMetalSurface::Create(*metal_context_, SkISize::Make(width, height));
surface->GetSurface()->getCanvas()->clear(SK_ColorTRANSPARENT);
return std::make_shared<DlMetalSurfaceInstance>(std::move(surface));
}

class DlMetalPixelData : public DlPixelData {
public:
explicit DlMetalPixelData(
std::unique_ptr<impeller::testing::Screenshot> screenshot)
explicit DlMetalPixelData(std::unique_ptr<impeller::testing::Screenshot> screenshot)
: screenshot_(std::move(screenshot)),
addr_(reinterpret_cast<const uint32_t*>(screenshot_->GetBytes())),
ints_per_row_(screenshot_->GetBytesPerRow() / 4) {
FML_DCHECK(screenshot_->GetBytesPerRow() == ints_per_row_ * 4);
}
~DlMetalPixelData() override = default;

const uint32_t* addr32(int x, int y) const override {
return addr_ + (y * ints_per_row_) + x;
}
const uint32_t* addr32(int x, int y) const override { return addr_ + (y * ints_per_row_) + x; }
size_t width() const override { return screenshot_->GetWidth(); }
size_t height() const override { return screenshot_->GetHeight(); }
void write(const std::string& path) const override {
screenshot_->WriteToPNG(path);
}
void write(const std::string& path) const override { screenshot_->WriteToPNG(path); }

private:
std::unique_ptr<impeller::testing::Screenshot> screenshot_;
const uint32_t* addr_;
const uint32_t ints_per_row_;
};

sk_sp<DlPixelData> DlMetalSurfaceProvider::ImpellerSnapshot(
const sk_sp<DisplayList>& list,
int width,
int height) const {
sk_sp<DlPixelData> DlMetalSurfaceProvider::ImpellerSnapshot(const sk_sp<DisplayList>& list,
int width,
int height) const {
auto texture = DisplayListToTexture(list, {width, height}, *aiks_context_);
return sk_make_sp<DlMetalPixelData>(
snapshotter_->MakeScreenshot(*aiks_context_, texture));
return sk_make_sp<DlMetalPixelData>(snapshotter_->MakeScreenshot(*aiks_context_, texture));
}

sk_sp<DlImage> DlMetalSurfaceProvider::MakeImpellerImage(
const sk_sp<DisplayList>& list,
int width,
int height) const {
sk_sp<DlImage> DlMetalSurfaceProvider::MakeImpellerImage(const sk_sp<DisplayList>& list,
int width,
int height) const {
InitScreenShotter();
return impeller::DlImageImpeller::Make(
DisplayListToTexture(list, {width, height}, *aiks_context_));
Expand All @@ -105,8 +90,8 @@ void DlMetalSurfaceProvider::InitScreenShotter() const {
if (!snapshotter_) {
snapshotter_.reset(new MetalScreenshotter(/*enable_wide_gamut=*/false));
auto typographer = impeller::TypographerContextSkia::Make();
aiks_context_.reset(new impeller::AiksContext(
snapshotter_->GetPlayground().GetContext(), typographer));
aiks_context_.reset(
new impeller::AiksContext(snapshotter_->GetPlayground().GetContext(), typographer));
}
}

Expand Down
4 changes: 2 additions & 2 deletions shell/platform/embedder/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ if (enable_unittests) {

if (test_enable_metal) {
sources += [
"tests/embedder_test_compositor_metal.cc",
"tests/embedder_test_compositor_metal.h",
"tests/embedder_test_context_metal.cc",
"tests/embedder_test_compositor_metal.mm",
"tests/embedder_test_context_metal.h",
"tests/embedder_test_context_metal.mm",
"tests/embedder_test_metal.mm",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,26 @@
namespace flutter {
namespace testing {

EmbedderTestCompositorMetal::EmbedderTestCompositorMetal(
SkISize surface_size,
sk_sp<GrDirectContext> context)
EmbedderTestCompositorMetal::EmbedderTestCompositorMetal(SkISize surface_size,
sk_sp<GrDirectContext> context)
: EmbedderTestCompositor(surface_size, std::move(context)) {}

EmbedderTestCompositorMetal::~EmbedderTestCompositorMetal() = default;

bool EmbedderTestCompositorMetal::UpdateOffscrenComposition(
const FlutterLayer** layers,
size_t layers_count) {
bool EmbedderTestCompositorMetal::UpdateOffscrenComposition(const FlutterLayer** layers,
size_t layers_count) {
last_composition_ = nullptr;

const auto image_info = SkImageInfo::MakeN32Premul(surface_size_);

auto surface =
SkSurfaces::RenderTarget(context_.get(), // context
skgpu::Budgeted::kNo, // budgeted
image_info, // image info
1, // sample count
kTopLeft_GrSurfaceOrigin, // surface origin
nullptr, // surface properties
false // create mipmaps
);
auto surface = SkSurfaces::RenderTarget(context_.get(), // context
skgpu::Budgeted::kNo, // budgeted
image_info, // image info
1, // sample count
kTopLeft_GrSurfaceOrigin, // surface origin
nullptr, // surface properties
false // create mipmaps
);

if (!surface) {
FML_LOG(ERROR) << "Could not update the off-screen composition.";
Expand All @@ -60,23 +57,20 @@ bool EmbedderTestCompositorMetal::UpdateOffscrenComposition(
switch (layer->type) {
case kFlutterLayerContentTypeBackingStore:
layer_image =
reinterpret_cast<SkSurface*>(layer->backing_store->user_data)
->makeImageSnapshot();
reinterpret_cast<SkSurface*>(layer->backing_store->user_data)->makeImageSnapshot();
break;
case kFlutterLayerContentTypePlatformView:
layer_image =
platform_view_renderer_callback_
? platform_view_renderer_callback_(*layer, context_.get())
: nullptr;
layer_image = platform_view_renderer_callback_
? platform_view_renderer_callback_(*layer, context_.get())
: nullptr;
canvas_offset = SkIPoint::Make(layer->offset.x, layer->offset.y);
break;
};

// If the layer is not a platform view but the engine did not specify an
// image for the backing store, it is an error.
if (!layer_image && layer->type != kFlutterLayerContentTypePlatformView) {
FML_LOG(ERROR) << "Could not snapshot layer in test compositor: "
<< *layer;
FML_LOG(ERROR) << "Could not snapshot layer in test compositor: " << *layer;
return false;
}

Expand All @@ -85,8 +79,7 @@ bool EmbedderTestCompositorMetal::UpdateOffscrenComposition(
if (layer_image) {
// The image rendered by Flutter already has the correct offset and
// transformation applied. The layers offset is meant for the platform.
canvas->drawImage(layer_image.get(), canvas_offset.x(),
canvas_offset.y());
canvas->drawImage(layer_image.get(), canvas_offset.x(), canvas_offset.y());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ EmbedderTestContextType EmbedderTestContextMetal::GetContextType() const {

void EmbedderTestContextMetal::SetupCompositor() {
FML_CHECK(!compositor_) << "Already set up a compositor in this context.";
FML_CHECK(metal_surface_)
<< "Set up the Metal surface before setting up a compositor.";
compositor_ = std::make_unique<EmbedderTestCompositorMetal>(
surface_size_, metal_surface_->GetGrContext());
FML_CHECK(metal_surface_) << "Set up the Metal surface before setting up a compositor.";
compositor_ =
std::make_unique<EmbedderTestCompositorMetal>(surface_size_, metal_surface_->GetGrContext());
}

TestMetalContext* EmbedderTestContextMetal::GetTestMetalContext() {
Expand All @@ -51,8 +50,7 @@ TestMetalSurface* EmbedderTestContextMetal::GetTestMetalSurface() {
return metal_surface_.get();
}

void EmbedderTestContextMetal::SetPresentCallback(
PresentCallback present_callback) {
void EmbedderTestContextMetal::SetPresentCallback(PresentCallback present_callback) {
present_callback_ = std::move(present_callback);
}

Expand All @@ -71,11 +69,10 @@ void EmbedderTestContextMetal::SetExternalTextureCallback(
external_texture_frame_callback_ = std::move(external_texture_frame_callback);
}

bool EmbedderTestContextMetal::PopulateExternalTexture(
int64_t texture_id,
size_t w,
size_t h,
FlutterMetalExternalTexture* output) {
bool EmbedderTestContextMetal::PopulateExternalTexture(int64_t texture_id,
size_t w,
size_t h,
FlutterMetalExternalTexture* output) {
if (external_texture_frame_callback_ != nullptr) {
return external_texture_frame_callback_(texture_id, w, h, output);
} else {
Expand All @@ -88,8 +85,7 @@ void EmbedderTestContextMetal::SetNextDrawableCallback(
next_drawable_callback_ = std::move(next_drawable_callback);
}

FlutterMetalTexture EmbedderTestContextMetal::GetNextDrawable(
const FlutterFrameInfo* frame_info) {
FlutterMetalTexture EmbedderTestContextMetal::GetNextDrawable(const FlutterFrameInfo* frame_info) {
if (next_drawable_callback_ != nullptr) {
return next_drawable_callback_(frame_info);
}
Expand All @@ -98,8 +94,7 @@ FlutterMetalTexture EmbedderTestContextMetal::GetNextDrawable(
FlutterMetalTexture texture = {};
texture.struct_size = sizeof(FlutterMetalTexture);
texture.texture_id = texture_info.texture_id;
texture.texture =
reinterpret_cast<FlutterMetalTextureHandle>(texture_info.texture);
texture.texture = reinterpret_cast<FlutterMetalTextureHandle>(texture_info.texture);
return texture;
}

Expand Down
4 changes: 2 additions & 2 deletions testing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ if (is_mac || is_ios) {
sources = [
"test_metal_context.h",
"test_metal_context.mm",
"test_metal_surface.cc",
"test_metal_surface.h",
"test_metal_surface.mm",
"test_metal_surface_impl.h",
"test_metal_surface_impl.mm",
]
Expand Down Expand Up @@ -260,7 +260,7 @@ if (enable_unittests) {
cflags_objc = flutter_cflags_objc_arc
cflags_objcc = flutter_cflags_objcc_arc

sources += [ "test_metal_surface_unittests.cc" ]
sources += [ "test_metal_surface_unittests.mm" ]
deps += [ ":metal" ]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ bool TestMetalSurface::PlatformSupportsMetal() {
std::unique_ptr<TestMetalSurface> TestMetalSurface::Create(
const TestMetalContext& test_metal_context,
SkISize surface_size) {
return std::make_unique<TestMetalSurfaceImpl>(test_metal_context,
surface_size);
return std::make_unique<TestMetalSurfaceImpl>(test_metal_context, surface_size);
}

std::unique_ptr<TestMetalSurface> TestMetalSurface::Create(
const TestMetalContext& test_metal_context,
int64_t texture_id,
SkISize surface_size) {
return std::make_unique<TestMetalSurfaceImpl>(test_metal_context, texture_id,
surface_size);
return std::make_unique<TestMetalSurfaceImpl>(test_metal_context, texture_id, surface_size);
}

TestMetalSurface::TestMetalSurface() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ TEST(TestMetalSurface, CanCreateValidTestMetalSurface) {
}

TestMetalContext metal_context = TestMetalContext();
auto surface =
TestMetalSurface::Create(metal_context, SkISize::Make(100, 100));
auto surface = TestMetalSurface::Create(metal_context, SkISize::Make(100, 100));
ASSERT_NE(surface, nullptr);
ASSERT_TRUE(surface->IsValid());
ASSERT_NE(surface->GetSurface(), nullptr);
Expand Down