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 #include "renderengine/impl/ExternalTexture.h"
19 
20 namespace android {
21 namespace hardware {
22 namespace graphics {
23 namespace composer {
24 namespace V2_2 {
25 namespace vts {
26 
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 
64     std::vector<renderengine::LayerSettings> compositionLayers;
65     compositionLayers.reserve(mCompositionLayers.size());
66     std::transform(mCompositionLayers.begin(), mCompositionLayers.end(),
67                    std::back_insert_iterator(compositionLayers),
68                    [](renderengine::LayerSettings& settings) -> renderengine::LayerSettings {
69                        return settings;
70                    });
71     auto texture = std::make_shared<renderengine::impl::ExternalTexture>(
72             mGraphicBuffer, *mRenderEngine, renderengine::impl::ExternalTexture::Usage::WRITEABLE);
73     auto result = mRenderEngine
74                           ->drawLayers(mDisplaySettings, compositionLayers, texture,
75                                        std::move(bufferFence))
76                           .get();
77     if (result.ok()) {
78         result.value()->waitForever(LOG_TAG);
79     }
80 }
81 
checkColorBuffer(std::vector<V2_2::IComposerClient::Color> & expectedColors)82 void TestRenderEngine::checkColorBuffer(std::vector<V2_2::IComposerClient::Color>& expectedColors) {
83     void* bufferData;
84     ASSERT_EQ(0, mGraphicBuffer->lock(mGraphicBuffer->getUsage(), &bufferData));
85     ReadbackHelper::compareColorBuffers(expectedColors, bufferData, mGraphicBuffer->getStride(),
86                                         mGraphicBuffer->getWidth(), mGraphicBuffer->getHeight(),
87                                         mFormat);
88     ASSERT_EQ(0, mGraphicBuffer->unlock());
89 }
90 
91 }  // namespace vts
92 }  // namespace V2_2
93 }  // namespace composer
94 }  // namespace graphics
95 }  // namespace hardware
96 }  // namespace android
97