1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/*
* Copyright (C) 2017 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 <composer-vts/2.1/ComposerVts.h>
namespace android {
namespace hardware {
namespace graphics {
namespace composer {
namespace V2_1 {
namespace vts {
Composer::Composer(const sp<IComposer>& composer) : mComposer(composer) {
// ASSERT_* can only be used in functions returning void.
[this] {
ASSERT_NE(nullptr, mComposer.get()) << "failed to get composer service";
std::vector<IComposer::Capability> capabilities = getCapabilities();
mCapabilities.insert(capabilities.begin(), capabilities.end());
}();
}
sp<IComposer> Composer::getRaw() const {
return mComposer;
}
bool Composer::hasCapability(IComposer::Capability capability) const {
return mCapabilities.count(capability) > 0;
}
std::vector<IComposer::Capability> Composer::getCapabilities() {
std::vector<IComposer::Capability> capabilities;
mComposer->getCapabilities(
[&](const auto& tmpCapabilities) { capabilities = tmpCapabilities; });
return capabilities;
}
std::string Composer::dumpDebugInfo() {
std::string debugInfo;
mComposer->dumpDebugInfo([&](const auto& tmpDebugInfo) { debugInfo = tmpDebugInfo.c_str(); });
return debugInfo;
}
std::unique_ptr<ComposerClient> Composer::createClient() {
std::unique_ptr<ComposerClient> client;
mComposer->createClient([&](const auto& tmpError, const auto& tmpClient) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to create client";
client = std::make_unique<ComposerClient>(tmpClient);
});
return client;
}
ComposerClient::ComposerClient(const sp<IComposerClient>& client) : mClient(client) {}
ComposerClient::~ComposerClient() {
for (const auto& it : mDisplayResources) {
Display display = it.first;
const DisplayResource& resource = it.second;
for (auto layer : resource.layers) {
EXPECT_EQ(Error::NONE, mClient->destroyLayer(display, layer))
<< "failed to destroy layer " << layer;
}
if (resource.isVirtual) {
EXPECT_EQ(Error::NONE, mClient->destroyVirtualDisplay(display))
<< "failed to destroy virtual display " << display;
}
}
mDisplayResources.clear();
}
sp<IComposerClient> ComposerClient::getRaw() const {
return mClient;
}
void ComposerClient::registerCallback(const sp<IComposerCallback>& callback) {
mClient->registerCallback(callback);
}
uint32_t ComposerClient::getMaxVirtualDisplayCount() {
return mClient->getMaxVirtualDisplayCount();
}
Display ComposerClient::createVirtualDisplay(uint32_t width, uint32_t height,
PixelFormat formatHint, uint32_t outputBufferSlotCount,
PixelFormat* outFormat) {
Display display = 0;
mClient->createVirtualDisplay(
width, height, formatHint, outputBufferSlotCount,
[&](const auto& tmpError, const auto& tmpDisplay, const auto& tmpFormat) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to create virtual display";
display = tmpDisplay;
*outFormat = tmpFormat;
ASSERT_TRUE(mDisplayResources.insert({display, DisplayResource(true)}).second)
<< "duplicated virtual display id " << display;
});
return display;
}
void ComposerClient::destroyVirtualDisplay(Display display) {
Error error = mClient->destroyVirtualDisplay(display);
ASSERT_EQ(Error::NONE, error) << "failed to destroy virtual display " << display;
mDisplayResources.erase(display);
}
Layer ComposerClient::createLayer(Display display, uint32_t bufferSlotCount) {
Layer layer = 0;
mClient->createLayer(display, bufferSlotCount, [&](const auto& tmpError, const auto& tmpLayer) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to create layer";
layer = tmpLayer;
auto resourceIt = mDisplayResources.find(display);
if (resourceIt == mDisplayResources.end()) {
resourceIt = mDisplayResources.insert({display, DisplayResource(false)}).first;
}
ASSERT_TRUE(resourceIt->second.layers.insert(layer).second)
<< "duplicated layer id " << layer;
});
return layer;
}
void ComposerClient::destroyLayer(Display display, Layer layer) {
Error error = mClient->destroyLayer(display, layer);
ASSERT_EQ(Error::NONE, error) << "failed to destroy layer " << layer;
auto resourceIt = mDisplayResources.find(display);
ASSERT_NE(mDisplayResources.end(), resourceIt);
resourceIt->second.layers.erase(layer);
}
Config ComposerClient::getActiveConfig(Display display) {
Config config = 0;
mClient->getActiveConfig(display, [&](const auto& tmpError, const auto& tmpConfig) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to get active config";
config = tmpConfig;
});
return config;
}
bool ComposerClient::getClientTargetSupport(Display display, uint32_t width, uint32_t height,
PixelFormat format, Dataspace dataspace) {
Error error = mClient->getClientTargetSupport(display, width, height, format, dataspace);
return error == Error::NONE;
}
std::vector<ColorMode> ComposerClient::getColorModes(Display display) {
std::vector<ColorMode> modes;
mClient->getColorModes(display, [&](const auto& tmpError, const auto& tmpMode) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to get color mode";
modes = tmpMode;
});
return modes;
}
int32_t ComposerClient::getDisplayAttribute(Display display, Config config,
IComposerClient::Attribute attribute) {
int32_t value = 0;
mClient->getDisplayAttribute(
display, config, attribute, [&](const auto& tmpError, const auto& tmpValue) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to get display attribute";
value = tmpValue;
});
return value;
}
std::vector<Config> ComposerClient::getDisplayConfigs(Display display) {
std::vector<Config> configs;
mClient->getDisplayConfigs(display, [&](const auto& tmpError, const auto& tmpConfigs) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to get display configs";
configs = tmpConfigs;
});
return configs;
}
std::string ComposerClient::getDisplayName(Display display) {
std::string name;
mClient->getDisplayName(display, [&](const auto& tmpError, const auto& tmpName) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to get display name";
name = tmpName.c_str();
});
return name;
}
IComposerClient::DisplayType ComposerClient::getDisplayType(Display display) {
IComposerClient::DisplayType type = IComposerClient::DisplayType::INVALID;
mClient->getDisplayType(display, [&](const auto& tmpError, const auto& tmpType) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to get display type";
type = tmpType;
});
return type;
}
bool ComposerClient::getDozeSupport(Display display) {
bool support = false;
mClient->getDozeSupport(display, [&](const auto& tmpError, const auto& tmpSupport) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to get doze support";
support = tmpSupport;
});
return support;
}
std::vector<Hdr> ComposerClient::getHdrCapabilities(Display display, float* outMaxLuminance,
float* outMaxAverageLuminance,
float* outMinLuminance) {
std::vector<Hdr> types;
mClient->getHdrCapabilities(
display, [&](const auto& tmpError, const auto& tmpTypes, const auto& tmpMaxLuminance,
const auto& tmpMaxAverageLuminance, const auto& tmpMinLuminance) {
ASSERT_EQ(Error::NONE, tmpError) << "failed to get HDR capabilities";
types = tmpTypes;
*outMaxLuminance = tmpMaxLuminance;
*outMaxAverageLuminance = tmpMaxAverageLuminance;
*outMinLuminance = tmpMinLuminance;
});
return types;
}
void ComposerClient::setClientTargetSlotCount(Display display, uint32_t clientTargetSlotCount) {
Error error = mClient->setClientTargetSlotCount(display, clientTargetSlotCount);
ASSERT_EQ(Error::NONE, error) << "failed to set client target slot count";
}
void ComposerClient::setActiveConfig(Display display, Config config) {
Error error = mClient->setActiveConfig(display, config);
ASSERT_EQ(Error::NONE, error) << "failed to set active config";
}
void ComposerClient::setColorMode(Display display, ColorMode mode) {
Error error = mClient->setColorMode(display, mode);
ASSERT_EQ(Error::NONE, error) << "failed to set color mode";
}
void ComposerClient::setPowerMode(Display display, IComposerClient::PowerMode mode) {
Error error = mClient->setPowerMode(display, mode);
ASSERT_EQ(Error::NONE, error) << "failed to set power mode";
}
void ComposerClient::setVsyncEnabled(Display display, bool enabled) {
IComposerClient::Vsync vsync =
(enabled) ? IComposerClient::Vsync::ENABLE : IComposerClient::Vsync::DISABLE;
Error error = mClient->setVsyncEnabled(display, vsync);
ASSERT_EQ(Error::NONE, error) << "failed to set vsync mode";
// give the hwbinder thread some time to handle any pending vsync callback
if (!enabled) {
usleep(5 * 1000);
}
}
void ComposerClient::execute(TestCommandReader* reader, CommandWriterBase* writer) {
bool queueChanged = false;
uint32_t commandLength = 0;
hidl_vec<hidl_handle> commandHandles;
ASSERT_TRUE(writer->writeQueue(&queueChanged, &commandLength, &commandHandles));
if (queueChanged) {
auto ret = mClient->setInputCommandQueue(*writer->getMQDescriptor());
ASSERT_EQ(Error::NONE, static_cast<Error>(ret));
}
mClient->executeCommands(commandLength, commandHandles,
[&](const auto& tmpError, const auto& tmpOutQueueChanged,
const auto& tmpOutLength, const auto& tmpOutHandles) {
ASSERT_EQ(Error::NONE, tmpError);
if (tmpOutQueueChanged) {
mClient->getOutputCommandQueue(
[&](const auto& tmpError, const auto& tmpDescriptor) {
ASSERT_EQ(Error::NONE, tmpError);
reader->setMQDescriptor(tmpDescriptor);
});
}
ASSERT_TRUE(reader->readQueue(tmpOutLength, tmpOutHandles));
reader->parse();
});
reader->reset();
writer->reset();
}
NativeHandleWrapper::~NativeHandleWrapper() {
if (mHandle) {
mGralloc.freeBuffer(mHandle);
}
}
Gralloc::Gralloc() {
[this] {
ASSERT_NO_FATAL_FAILURE(mGralloc4 = std::make_shared<Gralloc4>(
/*aidlAllocatorServiceName*/ IAllocator::descriptor +
std::string("/default"),
/*hidlAllocatorServiceName*/ "default",
/*mapperServiceName*/ "default",
/*errOnFailure=*/false));
if (!mGralloc4->hasAllocator() || mGralloc4->getMapper() == nullptr) {
mGralloc4 = nullptr;
ASSERT_NO_FATAL_FAILURE(mGralloc3 = std::make_shared<Gralloc3>("default", "default",
/*errOnFailure=*/false));
if (mGralloc3->getAllocator() == nullptr || mGralloc3->getMapper() == nullptr) {
mGralloc3 = nullptr;
ASSERT_NO_FATAL_FAILURE(mGralloc2 = std::make_shared<Gralloc2>());
}
}
}();
}
const NativeHandleWrapper Gralloc::allocate(uint32_t width, uint32_t height, uint32_t layerCount,
PixelFormat format, uint64_t usage, bool import,
uint32_t* outStride) {
const native_handle_t* handle;
if (mGralloc4) {
IMapper4::BufferDescriptorInfo info{};
info.width = width;
info.height = height;
info.layerCount = layerCount;
info.format = static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(format);
info.usage = usage;
handle = mGralloc4->allocate(info, import, outStride);
} else if (mGralloc3) {
IMapper3::BufferDescriptorInfo info{};
info.width = width;
info.height = height;
info.layerCount = layerCount;
info.format = static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(format);
info.usage = usage;
handle = mGralloc3->allocate(info, import, outStride);
} else {
IMapper2::BufferDescriptorInfo info{};
info.width = width;
info.height = height;
info.layerCount = layerCount;
info.format = format;
info.usage = usage;
handle = mGralloc2->allocate(info, import, outStride);
}
return NativeHandleWrapper(*this, handle);
}
void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
const AccessRegion& accessRegionRect, int acquireFence) {
if (mGralloc4) {
IMapper4::Rect accessRegion;
accessRegion.left = accessRegionRect.left;
accessRegion.top = accessRegionRect.top;
accessRegion.width = accessRegionRect.width;
accessRegion.height = accessRegionRect.height;
return mGralloc4->lock(bufferHandle, cpuUsage, accessRegion, acquireFence);
} else if (mGralloc3) {
IMapper3::Rect accessRegion;
accessRegion.left = accessRegionRect.left;
accessRegion.top = accessRegionRect.top;
accessRegion.width = accessRegionRect.width;
accessRegion.height = accessRegionRect.height;
int32_t bytesPerPixel;
int32_t bytesPerStride;
return mGralloc3->lock(bufferHandle, cpuUsage, accessRegion, acquireFence, &bytesPerPixel,
&bytesPerStride);
} else {
IMapper2::Rect accessRegion;
accessRegion.left = accessRegionRect.left;
accessRegion.top = accessRegionRect.top;
accessRegion.width = accessRegionRect.width;
accessRegion.height = accessRegionRect.height;
return mGralloc2->lock(bufferHandle, cpuUsage, accessRegion, acquireFence);
}
}
int Gralloc::unlock(const native_handle_t* bufferHandle) {
if (mGralloc4) {
return mGralloc4->unlock(bufferHandle);
} else if (mGralloc3) {
return mGralloc3->unlock(bufferHandle);
} else {
return mGralloc2->unlock(bufferHandle);
}
}
void Gralloc::freeBuffer(const native_handle_t* bufferHandle) {
if (mGralloc4) {
mGralloc4->freeBuffer(bufferHandle);
} else if (mGralloc3) {
mGralloc3->freeBuffer(bufferHandle);
} else {
mGralloc2->freeBuffer(bufferHandle);
}
}
} // namespace vts
} // namespace V2_1
} // namespace composer
} // namespace graphics
} // namespace hardware
} // namespace android
|