1 /*
2  * Copyright (C) 2018 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/ComposerVts.h>
18 
19 #include <VtsHalHidlTargetTestBase.h>
20 #include <composer-command-buffer/2.2/ComposerCommandBuffer.h>
21 #include <hidl/HidlTransportUtils.h>
22 
23 namespace android {
24 namespace hardware {
25 namespace graphics {
26 namespace composer {
27 namespace V2_2 {
28 namespace vts {
29 
30 using details::canCastInterface;
31 using details::getDescriptor;
32 
createClient()33 std::unique_ptr<ComposerClient> Composer::createClient() {
34     std::unique_ptr<ComposerClient> client;
35     getRaw()->createClient([&](const auto& tmpError, const auto& tmpClient) {
36         ASSERT_EQ(Error::NONE, tmpError) << "failed to create client";
37         ALOGV("tmpClient is a %s", getDescriptor(&(*tmpClient)).c_str());
38         ASSERT_TRUE(canCastInterface(
39             &(*tmpClient), "android.hardware.graphics.composer@2.2::IComposerClient", false))
40             << "Cannot create 2.2 IComposerClient";
41         client = std::make_unique<ComposerClient>(IComposerClient::castFrom(tmpClient, true));
42     });
43 
44     return client;
45 }
46 
getRaw() const47 sp<IComposerClient> ComposerClient::getRaw() const {
48     return mClient;
49 }
50 
getPerFrameMetadataKeys(Display display)51 std::vector<IComposerClient::PerFrameMetadataKey> ComposerClient::getPerFrameMetadataKeys(
52     Display display) {
53     std::vector<IComposerClient::PerFrameMetadataKey> keys;
54     mClient->getPerFrameMetadataKeys(display, [&](const auto& tmpError, const auto& tmpKeys) {
55         ASSERT_EQ(Error::NONE, tmpError) << "failed to get HDR metadata keys";
56         keys = tmpKeys;
57     });
58 
59     return keys;
60 }
61 
execute(V2_1::vts::TestCommandReader * reader,CommandWriterBase * writer)62 void ComposerClient::execute(V2_1::vts::TestCommandReader* reader, CommandWriterBase* writer) {
63     bool queueChanged = false;
64     uint32_t commandLength = 0;
65     hidl_vec<hidl_handle> commandHandles;
66     ASSERT_TRUE(writer->writeQueue(&queueChanged, &commandLength, &commandHandles));
67 
68     if (queueChanged) {
69         auto ret = mClient->setInputCommandQueue(*writer->getMQDescriptor());
70         ASSERT_EQ(Error::NONE, static_cast<Error>(ret));
71     }
72 
73     mClient->executeCommands(commandLength, commandHandles,
74                              [&](const auto& tmpError, const auto& tmpOutQueueChanged,
75                                  const auto& tmpOutLength, const auto& tmpOutHandles) {
76                                  ASSERT_EQ(Error::NONE, tmpError);
77 
78                                  if (tmpOutQueueChanged) {
79                                      mClient->getOutputCommandQueue(
80                                          [&](const auto& tmpError, const auto& tmpDescriptor) {
81                                              ASSERT_EQ(Error::NONE, tmpError);
82                                              reader->setMQDescriptor(tmpDescriptor);
83                                          });
84                                  }
85 
86                                  ASSERT_TRUE(reader->readQueue(tmpOutLength, tmpOutHandles));
87                                  reader->parse();
88                              });
89     reader->reset();
90     writer->reset();
91 }
92 
createVirtualDisplay_2_2(uint32_t width,uint32_t height,PixelFormat formatHint,uint32_t outputBufferSlotCount,PixelFormat * outFormat)93 Display ComposerClient::createVirtualDisplay_2_2(uint32_t width, uint32_t height,
94                                                  PixelFormat formatHint,
95                                                  uint32_t outputBufferSlotCount,
96                                                  PixelFormat* outFormat) {
97     Display display = 0;
98     mClient->createVirtualDisplay_2_2(
99         width, height, formatHint, outputBufferSlotCount,
100         [&](const auto& tmpError, const auto& tmpDisplay, const auto& tmpFormat) {
101             ASSERT_EQ(Error::NONE, tmpError) << "failed to create virtual display";
102             display = tmpDisplay;
103             *outFormat = tmpFormat;
104 
105             ASSERT_TRUE(mDisplayResources.insert({display, DisplayResource(true)}).second)
106                 << "duplicated virtual display id " << display;
107         });
108 
109     return display;
110 }
111 
getClientTargetSupport_2_2(Display display,uint32_t width,uint32_t height,PixelFormat format,Dataspace dataspace)112 bool ComposerClient::getClientTargetSupport_2_2(Display display, uint32_t width, uint32_t height,
113                                                 PixelFormat format, Dataspace dataspace) {
114     Error error = mClient->getClientTargetSupport_2_2(display, width, height, format, dataspace);
115     return error == Error::NONE;
116 }
117 
setPowerMode_2_2(Display display,IComposerClient::PowerMode mode)118 void ComposerClient::setPowerMode_2_2(Display display, IComposerClient::PowerMode mode) {
119     Error error = mClient->setPowerMode_2_2(display, mode);
120     ASSERT_TRUE(error == Error::NONE || error == Error::UNSUPPORTED) << "failed to set power mode";
121 }
122 
setReadbackBuffer(Display display,const native_handle_t * buffer,int32_t)123 void ComposerClient::setReadbackBuffer(Display display, const native_handle_t* buffer,
124                                        int32_t /* releaseFence */) {
125     // Ignoring fence, HIDL doesn't care
126     Error error = mClient->setReadbackBuffer(display, buffer, nullptr);
127     ASSERT_EQ(Error::NONE, error) << "failed to setReadbackBuffer";
128 }
129 
getReadbackBufferAttributes(Display display,PixelFormat * outPixelFormat,Dataspace * outDataspace)130 void ComposerClient::getReadbackBufferAttributes(Display display, PixelFormat* outPixelFormat,
131                                                  Dataspace* outDataspace) {
132     mClient->getReadbackBufferAttributes(
133         display,
134         [&](const auto& tmpError, const auto& tmpOutPixelFormat, const auto& tmpOutDataspace) {
135             ASSERT_EQ(Error::NONE, tmpError) << "failed to get readback buffer attributes";
136             *outPixelFormat = tmpOutPixelFormat;
137             *outDataspace = tmpOutDataspace;
138         });
139 }
140 
getReadbackBufferFence(Display display,int32_t * outFence)141 void ComposerClient::getReadbackBufferFence(Display display, int32_t* outFence) {
142     mClient->getReadbackBufferFence(display, [&](const auto& tmpError, const auto& tmpHandle) {
143         ASSERT_EQ(Error::NONE, tmpError) << "failed to get readback fence";
144         const native_handle_t* nativeFenceHandle = tmpHandle.getNativeHandle();
145         *outFence = dup(nativeFenceHandle->data[0]);
146     });
147 }
148 
getColorModes(Display display)149 std::vector<ColorMode> ComposerClient::getColorModes(Display display) {
150     std::vector<ColorMode> modes;
151     mClient->getColorModes_2_2(display, [&](const auto& tmpError, const auto& tmpModes) {
152         ASSERT_EQ(Error::NONE, tmpError) << "failed to get color modes";
153         modes = tmpModes;
154     });
155     return modes;
156 }
157 
getRenderIntents(Display display,ColorMode mode)158 std::vector<RenderIntent> ComposerClient::getRenderIntents(Display display, ColorMode mode) {
159     std::vector<RenderIntent> intents;
160     mClient->getRenderIntents(display, mode, [&](const auto& tmpError, const auto& tmpIntents) {
161         ASSERT_EQ(Error::NONE, tmpError) << "failed to get render intents";
162         intents = tmpIntents;
163     });
164     return intents;
165 }
166 
setColorMode(Display display,ColorMode mode,RenderIntent intent)167 void ComposerClient::setColorMode(Display display, ColorMode mode, RenderIntent intent) {
168     Error error = mClient->setColorMode_2_2(display, mode, intent);
169     ASSERT_TRUE(error == Error::NONE || error == Error::UNSUPPORTED) << "failed to set color mode";
170 }
171 
getDataspaceSaturationMatrix(Dataspace dataspace)172 std::array<float, 16> ComposerClient::getDataspaceSaturationMatrix(Dataspace dataspace) {
173     std::array<float, 16> matrix;
174     mClient->getDataspaceSaturationMatrix(
175         dataspace, [&](const auto& tmpError, const auto& tmpMatrix) {
176             ASSERT_EQ(Error::NONE, tmpError) << "failed to get datasapce saturation matrix";
177             std::copy_n(tmpMatrix.data(), matrix.size(), matrix.begin());
178         });
179 
180     return matrix;
181 }
182 
Gralloc()183 Gralloc::Gralloc() {
184     [this] {
185         ALOGD("Attempting to initialize gralloc3");
186         ASSERT_NO_FATAL_FAILURE(mGralloc3 = std::make_shared<Gralloc3>("default", "default",
187                                                                        /*errOnFailure=*/false));
188         if (mGralloc3->getMapper() == nullptr || mGralloc3->getAllocator() == nullptr) {
189             mGralloc3 = nullptr;
190             ALOGD("Failed to create gralloc3, initializing gralloc2_1");
191             mGralloc2_1 = std::make_shared<Gralloc2_1>(/*errOnFailure*/ false);
192             if (!mGralloc2_1->getMapper()) {
193                 mGralloc2_1 = nullptr;
194                 ALOGD("Failed to create gralloc2_1, initializing gralloc2");
195                 ASSERT_NO_FATAL_FAILURE(mGralloc2 = std::make_shared<Gralloc2>());
196             }
197         }
198     }();
199 }
200 
validateBufferSize(const native_handle_t * bufferHandle,uint32_t width,uint32_t height,uint32_t layerCount,PixelFormat format,uint64_t usage,uint32_t stride)201 bool Gralloc::validateBufferSize(const native_handle_t* bufferHandle, uint32_t width,
202                                  uint32_t height, uint32_t layerCount, PixelFormat format,
203                                  uint64_t usage, uint32_t stride) {
204     if (mGralloc3) {
205         IMapper3::BufferDescriptorInfo info{};
206         info.width = width;
207         info.height = height;
208         info.layerCount = layerCount;
209         info.format = static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(format);
210         info.usage = usage;
211         return mGralloc3->validateBufferSize(bufferHandle, info, stride);
212     } else if (mGralloc2_1) {
213         IMapper2_1::BufferDescriptorInfo info{};
214         info.width = width;
215         info.height = height;
216         info.layerCount = layerCount;
217         info.format = static_cast<android::hardware::graphics::common::V1_1::PixelFormat>(format);
218         info.usage = usage;
219         return mGralloc2_1->validateBufferSize(bufferHandle, info, stride);
220     } else {
221         return true;
222     }
223 }
224 
225 }  // namespace vts
226 }  // namespace V2_2
227 }  // namespace composer
228 }  // namespace graphics
229 }  // namespace hardware
230 }  // namespace android
231