Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fuchsia] Set blending mode on flatland images #30890

Merged
merged 1 commit into from
Jan 18, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ void FlatlandExternalViewEmbedder::SubmitFrame(
{static_cast<uint32_t>(surface_for_layer->GetSize().width()),
static_cast<uint32_t>(surface_for_layer->GetSize().height())});

// Flutter Embedder lacks an API to detect if a layer has alpha or not.
// For now, we assume any layer beyond the first has alpha.
flatland_->flatland()->SetImageBlendingFunction(
{surface_for_layer->GetImageId()},
flatland_layer_index == 0
? fuchsia::ui::composition::BlendMode::SRC
: fuchsia::ui::composition::BlendMode::SRC_OVER);

// Attach the FlatlandLayer to the main scene graph.
flatland_->flatland()->AddChild(
root_transform_id_,
Expand All @@ -267,7 +275,6 @@ void FlatlandExternalViewEmbedder::SubmitFrame(
}

// Reset for the next pass:
// +The next layer will not be the first layer.
flatland_layer_index++;
}
}
Expand Down
31 changes: 31 additions & 0 deletions shell/platform/fuchsia/flutter/tests/fakes/scenic/fake_flatland.cc
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,37 @@ void FakeFlatland::SetImageDestinationSize(
image->destination_size = size;
}

void FakeFlatland::SetImageBlendingFunction(
fuchsia::ui::composition::ContentId image_id,
fuchsia::ui::composition::BlendMode blend_mode) {
if (image_id.value == 0) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false)
<< "FakeFlatland::SetImageDestinationSize: ContentId 0 is invalid.";
return;
}

auto found_content = pending_graph_.content_map.find(image_id.value);
if (found_content == pending_graph_.content_map.end()) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetImageDestinationSize: ContentId "
<< image_id.value << " does not exist.";
return;
}

auto& content = found_content->second;
FML_CHECK(content);
FakeImage* image = std::get_if<FakeImage>(content.get());
if (image == nullptr) {
// TODO(fxb/85619): Raise a FlatlandError here
FML_CHECK(false) << "FakeFlatland::SetImageDestinationSize: ContentId "
<< image_id.value << " is not an Image.";
return;
}

image->blend_mode = blend_mode;
}

void FakeFlatland::SetViewportProperties(
fuchsia::ui::composition::ContentId viewport_id,
fuchsia::ui::composition::ViewportProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ class FakeFlatland
void SetImageDestinationSize(fuchsia::ui::composition::ContentId image_id,
fuchsia::math::SizeU size) override;

// |fuchsia::ui::composition::Flatland|
void SetImageBlendingFunction(
fuchsia::ui::composition::ContentId image_id,
fuchsia::ui::composition::BlendMode blend_mode) override;

// |fuchsia::ui::composition::Flatland|
void SetViewportProperties(
fuchsia::ui::composition::ContentId viewport_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ std::shared_ptr<FakeContent> CloneFakeContent(
.sample_region = image->sample_region,
.destination_size = image->destination_size,
.opacity = image->opacity,
.blend_mode = image->blend_mode,
.import_token = image->import_token,
.vmo_index = image->vmo_index,
});
Expand Down Expand Up @@ -124,8 +125,8 @@ bool FakeImage::operator==(const FakeImage& other) const {
return id == other.id && image_properties == other.image_properties &&
sample_region == other.sample_region &&
destination_size == other.destination_size &&
opacity == other.opacity && import_token == other.import_token &&
vmo_index == other.vmo_index;
opacity == other.opacity && blend_mode == other.blend_mode &&
import_token == other.import_token && vmo_index == other.vmo_index;
}

bool FakeTransform::operator==(const FakeTransform& other) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,16 @@ struct FakeImage {
constexpr static fuchsia::math::RectF kDefaultSampleRegion{};
constexpr static fuchsia::math::SizeU kDefaultDestinationSize{};
constexpr static float kDefaultOpacity{1.f};
constexpr static fuchsia::ui::composition::BlendMode kDefaultBlendMode{
fuchsia::ui::composition::BlendMode::SRC_OVER};

fuchsia::ui::composition::ContentId id{kInvalidContentId};

fuchsia::ui::composition::ImageProperties image_properties{};
fuchsia::math::RectF sample_region{kDefaultSampleRegion};
fuchsia::math::SizeU destination_size{kDefaultDestinationSize};
float opacity{kDefaultOpacity};
fuchsia::ui::composition::BlendMode blend_mode{kDefaultBlendMode};

zx_koid_t import_token{};
uint32_t vmo_index{0};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ using ::testing::VariantWith;
namespace flutter_runner::testing {
namespace {

constexpr static fuchsia::ui::composition::BlendMode kFirstLayerBlendMode{
fuchsia::ui::composition::BlendMode::SRC};
constexpr static fuchsia::ui::composition::BlendMode kUpperLayerBlendMode{
fuchsia::ui::composition::BlendMode::SRC_OVER};

class FakeSurfaceProducerSurface : public SurfaceProducerSurface {
public:
explicit FakeSurfaceProducerSurface(
Expand Down Expand Up @@ -226,7 +231,8 @@ Matcher<FakeGraph> IsFlutterGraph(
}

Matcher<std::shared_ptr<FakeTransform>> IsImageLayer(
const fuchsia::math::SizeU& layer_size) {
const fuchsia::math::SizeU& layer_size,
fuchsia::ui::composition::BlendMode blend_mode) {
return Pointee(FieldsAre(
/*id*/ _, FakeTransform::kDefaultTranslation,
FakeTransform::kDefaultClipBounds, FakeTransform::kDefaultOrientation,
Expand All @@ -235,7 +241,7 @@ Matcher<std::shared_ptr<FakeTransform>> IsImageLayer(
Pointee(VariantWith<FakeImage>(FieldsAre(
/*id*/ _, IsImageProperties(layer_size),
FakeImage::kDefaultSampleRegion, layer_size,
FakeImage::kDefaultOpacity,
FakeImage::kDefaultOpacity, blend_mode,
/*buffer_import_token*/ _, /*vmo_index*/ 0)))));
}

Expand Down Expand Up @@ -454,13 +460,13 @@ TEST_F(FlatlandExternalViewEmbedderTest, SimpleScene) {
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token,
view_ref));

// Pump the message loop. The scene updates should propagate to flatland.
// Pump the message loop. The scene updates should propagate to flatland.
loop().RunUntilIdle();
EXPECT_THAT(
fake_flatland().graph(),
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token, view_ref,
/*layers*/
{IsImageLayer(frame_size)}));
{IsImageLayer(frame_size, kFirstLayerBlendMode)}));
}

TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) {
Expand Down Expand Up @@ -549,9 +555,9 @@ TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) {
fake_flatland().graph(),
IsFlutterGraph(
parent_viewport_watcher, viewport_creation_token, view_ref, /*layers*/
{IsImageLayer(frame_size),
{IsImageLayer(frame_size, kFirstLayerBlendMode),
IsViewportLayer(child_view_token, child_view_size, {0, 0}),
IsImageLayer(frame_size)}));
IsImageLayer(frame_size, kUpperLayerBlendMode)}));

// Destroy the view. The scene graph shouldn't change yet.
external_view_embedder.DestroyView(
Expand All @@ -560,9 +566,9 @@ TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) {
fake_flatland().graph(),
IsFlutterGraph(
parent_viewport_watcher, viewport_creation_token, view_ref, /*layers*/
{IsImageLayer(frame_size),
{IsImageLayer(frame_size, kFirstLayerBlendMode),
IsViewportLayer(child_view_token, child_view_size, {0, 0}),
IsImageLayer(frame_size)}));
IsImageLayer(frame_size, kUpperLayerBlendMode)}));

// Draw another frame without the view. The scene graph shouldn't change yet.
DrawSimpleFrame(
Expand All @@ -581,16 +587,16 @@ TEST_F(FlatlandExternalViewEmbedderTest, SceneWithOneView) {
fake_flatland().graph(),
IsFlutterGraph(
parent_viewport_watcher, viewport_creation_token, view_ref, /*layers*/
{IsImageLayer(frame_size),
{IsImageLayer(frame_size, kFirstLayerBlendMode),
IsViewportLayer(child_view_token, child_view_size, {0, 0}),
IsImageLayer(frame_size)}));
IsImageLayer(frame_size, kUpperLayerBlendMode)}));

// Pump the message loop. The scene updates should propagate to flatland.
loop().RunUntilIdle();
EXPECT_THAT(fake_flatland().graph(),
IsFlutterGraph(parent_viewport_watcher, viewport_creation_token,
view_ref, /*layers*/
{IsImageLayer(frame_size)}));
{IsImageLayer(frame_size, kFirstLayerBlendMode)}));
}

} // namespace flutter_runner::testing