| /* | |
| * Copyright 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include <common/include/common/test/FlagUtils.h> | |
| #include <compositionengine/impl/OutputCompositionState.h> | |
| #include <compositionengine/impl/planner/Planner.h> | |
| #include <compositionengine/mock/LayerFE.h> | |
| #include <compositionengine/mock/Output.h> | |
| #include <compositionengine/mock/OutputLayer.h> | |
| #include <gtest/gtest.h> | |
| #include <renderengine/mock/RenderEngine.h> | |
| #include "com_android_graphics_surfaceflinger_flags.h" | |
| #include "compositionengine/LayerFECompositionState.h" | |
| #include "compositionengine/impl/OutputLayerCompositionState.h" | |
| #include "gmock/gmock.h" | |
| namespace android::compositionengine::impl::planner { | |
| namespace { | |
| using testing::_; | |
| using testing::ByMove; | |
| using testing::Invoke; | |
| using testing::Return; | |
| using testing::ReturnRef; | |
| class PlannerTest : public testing::Test { | |
| protected: | |
| compositionengine::mock::Output mMockOutput; | |
| renderengine::mock::RenderEngine mRenderEngine; | |
| Planner mPlanner{mRenderEngine}; | |
| }; | |
| TEST_F(PlannerTest, plan_sourceCropChangePreventsFlattening) { | |
| SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::layer_caching_geometry_changes, | |
| true); | |
| constexpr size_t kNumLayers = 2; | |
| std::vector<std::unique_ptr<compositionengine::mock::OutputLayer>> mockLayers; | |
| std::vector<impl::OutputLayerCompositionState> layerStates(kNumLayers); | |
| std::vector<sp<compositionengine::mock::LayerFE>> layerFEs; | |
| std::vector<LayerFECompositionState> feStates(kNumLayers); | |
| const auto kUsageFlags = static_cast<uint64_t>( | |
| GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE); | |
| mPlanner.setDisplaySize({100, 100}); | |
| for (size_t i = 0; i < kNumLayers; ++i) { | |
| mockLayers.push_back(std::make_unique<compositionengine::mock::OutputLayer>()); | |
| auto& layer = *mockLayers.back(); | |
| layerStates[i].displayFrame = Rect(0, 0, 100, 100); | |
| layerStates[i].visibleRegion = Region(Rect(0, 0, 100, 100)); | |
| layerStates[i].sourceCrop = FloatRect{0.f, 0.f, 100.f, 100.f}; | |
| feStates[i].fps = 0.5f; // Low FPS (< 1.0) marks layer inactive immediately | |
| feStates[i].buffer = sp<GraphicBuffer>::make(100u, 100u, HAL_PIXEL_FORMAT_RGBA_8888, 1u, | |
| kUsageFlags, "test-buffer"); | |
| layerFEs.push_back(sp<compositionengine::mock::LayerFE>::make()); | |
| EXPECT_CALL(*layerFEs[i], getCompositionState()).WillRepeatedly(Return(&feStates[i])); | |
| EXPECT_CALL(*layerFEs[i], getSequence()).WillRepeatedly(Return(static_cast<int32_t>(i))); | |
| EXPECT_CALL(*layerFEs[i], getDebugName()).WillRepeatedly(Return("mock-layer")); | |
| EXPECT_CALL(*layerFEs[i], prepareClientComposition(_)) | |
| .WillRepeatedly(Return(LayerFE::LayerSettings{})); | |
| EXPECT_CALL(layer, getState()).WillRepeatedly(ReturnRef(layerStates[i])); | |
| EXPECT_CALL(layer, editState()).WillRepeatedly(ReturnRef(layerStates[i])); | |
| EXPECT_CALL(layer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFEs[i])); | |
| } | |
| EXPECT_CALL(mMockOutput, getOutputLayerCount()).WillRepeatedly(Return(mockLayers.size())); | |
| EXPECT_CALL(mMockOutput, getOutputLayerOrderedByZByIndex(_)) | |
| .WillRepeatedly(Invoke([&](size_t index) -> compositionengine::OutputLayer* { | |
| return mockLayers.at(index).get(); | |
| })); | |
| impl::OutputCompositionState outputState; | |
| outputState.dataspace = ui::Dataspace::SRGB; | |
| outputState.framebufferSpace = ProjectionSpace(ui::Size(100, 100), Rect(0, 0, 100, 100)); | |
| // Frame 1: Register initial layer stack (resets geometry) | |
| mPlanner.plan(mMockOutput.getOutputLayersOrderedByZ()); | |
| // Frame 2: Populate internal layers in Flattener | |
| mPlanner.plan(mMockOutput.getOutputLayersOrderedByZ()); | |
| // Frame 3: Build candidate cached set | |
| mPlanner.plan(mMockOutput.getOutputLayersOrderedByZ()); | |
| // Render the cached set | |
| EXPECT_CALL(mRenderEngine, drawLayers(_, _, _, _, _)) | |
| .WillOnce(Return(ByMove(ftl::yield<FenceResult>(Fence::NO_FENCE)))); | |
| mPlanner.renderCachedSets(outputState, std::nullopt, true); | |
| // Frame 4: Baseline check - layers are cached | |
| mPlanner.plan(mMockOutput.getOutputLayersOrderedByZ()); | |
| EXPECT_NE(nullptr, mockLayers[0]->getState().overrideInfo.buffer); | |
| EXPECT_NE(nullptr, mockLayers[1]->getState().overrideInfo.buffer); | |
| // Frame 5: Update source crop on layer 0 | |
| layerStates[0].sourceCrop = FloatRect(1.f, 1.f, 99.f, 99.f); | |
| mPlanner.plan(mMockOutput.getOutputLayersOrderedByZ()); | |
| // Verify cache is invalidated: neither layer has an override buffer | |
| EXPECT_EQ(nullptr, mockLayers[0]->getState().overrideInfo.buffer); | |
| EXPECT_EQ(nullptr, mockLayers[1]->getState().overrideInfo.buffer); | |
| } | |
| } // namespace | |
| } // namespace android::compositionengine::impl::planner |