1 /**
2  * Copyright (c) 2021, 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 "RenderEngineVts.h"
18 #include "renderengine/impl/ExternalTexture.h"
19 
20 namespace aidl::android::hardware::graphics::composer3::vts {
21 
22 using ::android::renderengine::DisplaySettings;
23 using ::android::renderengine::LayerSettings;
24 using ::android::renderengine::RenderEngineCreationArgs;
25 
TestRenderEngine(const RenderEngineCreationArgs & args)26 TestRenderEngine::TestRenderEngine(const RenderEngineCreationArgs& args) {
27     mFormat = static_cast<common::PixelFormat>(args.pixelFormat);
28     mRenderEngine = ::android::renderengine::RenderEngine::create(args);
29 }
30 
~TestRenderEngine()31 TestRenderEngine::~TestRenderEngine() {
32     mRenderEngine.release();
33 }
34 
setRenderLayers(std::vector<std::shared_ptr<TestLayer>> layers)35 void TestRenderEngine::setRenderLayers(std::vector<std::shared_ptr<TestLayer>> layers) {
36     sort(layers.begin(), layers.end(),
37          [](const std::shared_ptr<TestLayer>& lhs, const std::shared_ptr<TestLayer>& rhs) -> bool {
38              return lhs->getZOrder() < rhs->getZOrder();
39          });
40 
41     if (!mCompositionLayers.empty()) {
42         mCompositionLayers.clear();
43     }
44     for (auto& layer : layers) {
45         LayerSettings settings = layer->toRenderEngineLayerSettings();
46         mCompositionLayers.push_back(settings);
47     }
48 }
49 
initGraphicBuffer(uint32_t width,uint32_t height,uint32_t layerCount,uint64_t usage)50 void TestRenderEngine::initGraphicBuffer(uint32_t width, uint32_t height, uint32_t layerCount,
51                                          uint64_t usage) {
52     mGraphicBuffer = ::android::sp<::android::GraphicBuffer>::make(
53             width, height, static_cast<int32_t>(mFormat), layerCount, usage);
54 }
55 
drawLayers()56 void TestRenderEngine::drawLayers() {
57     ::android::base::unique_fd bufferFence;
58 
59     std::vector<::android::renderengine::LayerSettings> compositionLayers;
60     compositionLayers.reserve(mCompositionLayers.size());
61     std::transform(mCompositionLayers.begin(), mCompositionLayers.end(),
62                    std::back_insert_iterator(compositionLayers),
63                    [](::android::renderengine::LayerSettings& settings)
64                            -> ::android::renderengine::LayerSettings { return settings; });
65     auto texture = std::make_shared<::android::renderengine::impl::ExternalTexture>(
66             mGraphicBuffer, *mRenderEngine,
67             ::android::renderengine::impl::ExternalTexture::Usage::WRITEABLE);
68     auto result = mRenderEngine
69                           ->drawLayers(mDisplaySettings, compositionLayers, texture,
70                                        std::move(bufferFence))
71                           .get();
72     if (result.ok()) {
73         result.value()->waitForever(LOG_TAG);
74     }
75 }
76 
checkColorBuffer(const std::vector<Color> & expectedColors)77 void TestRenderEngine::checkColorBuffer(const std::vector<Color>& expectedColors) {
78     void* bufferData;
79     int32_t bytesPerPixel = -1;
80     int32_t bytesPerStride = -1;
81     ASSERT_EQ(0, mGraphicBuffer->lock(static_cast<uint32_t>(mGraphicBuffer->getUsage()),
82                                       &bufferData, &bytesPerPixel, &bytesPerStride));
83     const uint32_t stride = (bytesPerPixel > 0 && bytesPerStride > 0)
84                                     ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel)
85                                     : mGraphicBuffer->getStride();
86     ReadbackHelper::compareColorBuffers(expectedColors, bufferData, stride,
87                                         mGraphicBuffer->getWidth(), mGraphicBuffer->getHeight(),
88                                         mFormat);
89     ASSERT_EQ(::android::OK, mGraphicBuffer->unlock());
90 }
91 
checkColorBuffer(const::android::sp<::android::GraphicBuffer> & buffer)92 void TestRenderEngine::checkColorBuffer(const ::android::sp<::android::GraphicBuffer>& buffer) {
93     ASSERT_EQ(mGraphicBuffer->getWidth(), buffer->getWidth());
94     ASSERT_EQ(mGraphicBuffer->getHeight(), buffer->getHeight());
95     void* renderedBufferData;
96     int32_t bytesPerPixel = -1;
97     int32_t bytesPerStride = -1;
98     ASSERT_EQ(0, mGraphicBuffer->lock(static_cast<uint32_t>(mGraphicBuffer->getUsage()),
99                                       &renderedBufferData, &bytesPerPixel, &bytesPerStride));
100     const uint32_t renderedStride = (bytesPerPixel > 0 && bytesPerStride > 0)
101                                             ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel)
102                                             : mGraphicBuffer->getStride();
103 
104     void* bufferData;
105     ASSERT_EQ(0, buffer->lock(static_cast<uint32_t>(buffer->getUsage()), &bufferData,
106                               &bytesPerPixel, &bytesPerStride));
107     const uint32_t bufferStride = (bytesPerPixel > 0 && bytesPerStride > 0)
108                                           ? static_cast<uint32_t>(bytesPerStride / bytesPerPixel)
109                                           : buffer->getStride();
110 
111     ASSERT_EQ(renderedStride, bufferStride);
112 
113     ReadbackHelper::compareColorBuffers(renderedBufferData, bufferData, bufferStride,
114                                         mGraphicBuffer->getWidth(), mGraphicBuffer->getHeight(),
115                                         mFormat);
116     ASSERT_EQ(::android::OK, buffer->unlock());
117     ASSERT_EQ(::android::OK, mGraphicBuffer->unlock());
118 }
119 
120 }  // namespace aidl::android::hardware::graphics::composer3::vts
121