1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <composer-vts/2.2/RenderEngineVts.h>
18 
19 namespace android {
20 namespace hardware {
21 namespace graphics {
22 namespace composer {
23 namespace V2_2 {
24 namespace vts {
25 
26 using mapper::V2_1::IMapper;
27 using renderengine::DisplaySettings;
28 using renderengine::LayerSettings;
29 using renderengine::RenderEngineCreationArgs;
30 
TestRenderEngine(const RenderEngineCreationArgs & args)31 TestRenderEngine::TestRenderEngine(const RenderEngineCreationArgs& args) {
32     mFormat = static_cast<common::V1_1::PixelFormat>(args.pixelFormat);
33     mRenderEngine = renderengine::RenderEngine::create(args);
34 }
35 
~TestRenderEngine()36 TestRenderEngine::~TestRenderEngine() {
37     mRenderEngine.release();
38 }
39 
setRenderLayers(std::vector<std::shared_ptr<TestLayer>> layers)40 void TestRenderEngine::setRenderLayers(std::vector<std::shared_ptr<TestLayer>> layers) {
41     sort(layers.begin(), layers.end(),
42          [](const std::shared_ptr<TestLayer>& lhs, const std::shared_ptr<TestLayer>& rhs) -> bool {
43              return lhs->mZOrder < rhs->mZOrder;
44          });
45 
46     if (!mCompositionLayers.empty()) {
47         mCompositionLayers.clear();
48     }
49     for (auto& layer : layers) {
50         LayerSettings settings = layer->toRenderEngineLayerSettings();
51         mCompositionLayers.push_back(settings);
52     }
53 }
54 
initGraphicBuffer(uint32_t width,uint32_t height,uint32_t layerCount,uint64_t usage)55 void TestRenderEngine::initGraphicBuffer(uint32_t width, uint32_t height, uint32_t layerCount,
56                                          uint64_t usage) {
57     mGraphicBuffer =
58             new GraphicBuffer(width, height, static_cast<int32_t>(mFormat), layerCount, usage);
59 }
60 
drawLayers()61 void TestRenderEngine::drawLayers() {
62     base::unique_fd bufferFence;
63     base::unique_fd readyFence;
64 
65     std::vector<const renderengine::LayerSettings*> compositionLayerPointers;
66     compositionLayerPointers.reserve(mCompositionLayers.size());
67     std::transform(mCompositionLayers.begin(), mCompositionLayers.end(),
68                    std::back_insert_iterator(compositionLayerPointers),
69                    [](renderengine::LayerSettings& settings) -> renderengine::LayerSettings* {
70                        return &settings;
71                    });
72     auto texture = std::make_shared<renderengine::ExternalTexture>(
73             mGraphicBuffer, *mRenderEngine, renderengine::ExternalTexture::Usage::WRITEABLE);
74     mRenderEngine->drawLayers(mDisplaySettings, compositionLayerPointers, texture, true,
75                               std::move(bufferFence), &readyFence);
76     int fd = readyFence.release();
77     if (fd != -1) {
78         ASSERT_EQ(0, sync_wait(fd, -1));
79         ASSERT_EQ(0, close(fd));
80     }
81 }
82 
checkColorBuffer(std::vector<V2_2::IComposerClient::Color> & expectedColors)83 void TestRenderEngine::checkColorBuffer(std::vector<V2_2::IComposerClient::Color>& expectedColors) {
84     void* bufferData;
85     ASSERT_EQ(0, mGraphicBuffer->lock(mGraphicBuffer->getUsage(), &bufferData));
86     ReadbackHelper::compareColorBuffers(expectedColors, bufferData, mGraphicBuffer->getStride(),
87                                         mGraphicBuffer->getWidth(), mGraphicBuffer->getHeight(),
88                                         mFormat);
89     ASSERT_EQ(0, mGraphicBuffer->unlock());
90 }
91 
92 }  // namespace vts
93 }  // namespace V2_2
94 }  // namespace composer
95 }  // namespace graphics
96 }  // namespace hardware
97 }  // namespace android
98