blob: 61758b63f18659ce7cf4c21c58900e261ecae82f [file] [log] [blame]
Dan Stoza651bf312015-10-23 17:03:17 -07001/*
2 * Copyright 2015 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// #define LOG_NDEBUG 0
18
19#undef LOG_TAG
20#define LOG_TAG "HWC2"
21#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
23#include "HWC2.h"
Chia-I Wuaab99f52016-10-05 12:59:58 +080024#include "ComposerHal.h"
Dan Stoza651bf312015-10-23 17:03:17 -070025
Dan Stoza651bf312015-10-23 17:03:17 -070026#include <ui/Fence.h>
Dan Stoza5a423ea2017-02-16 14:10:39 -080027#include <ui/FloatRect.h>
Dan Stoza651bf312015-10-23 17:03:17 -070028#include <ui/GraphicBuffer.h>
29#include <ui/Region.h>
30
31#include <android/configuration.h>
32
33#include <inttypes.h>
Lloyd Pique3c085a02018-05-09 19:38:32 -070034#include <algorithm>
35#include <iterator>
Peiyong Lin2c327ac2018-04-19 22:06:34 -070036#include <set>
Dan Stoza651bf312015-10-23 17:03:17 -070037
Dan Stoza651bf312015-10-23 17:03:17 -070038using android::Fence;
Dan Stoza5a423ea2017-02-16 14:10:39 -080039using android::FloatRect;
Dan Stoza651bf312015-10-23 17:03:17 -070040using android::GraphicBuffer;
Dan Stoza7d7ae732016-03-16 12:23:40 -070041using android::HdrCapabilities;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070042using android::HdrMetadata;
Dan Stoza651bf312015-10-23 17:03:17 -070043using android::Rect;
44using android::Region;
45using android::sp;
Chia-I Wuaab99f52016-10-05 12:59:58 +080046using android::hardware::Return;
47using android::hardware::Void;
Dan Stoza651bf312015-10-23 17:03:17 -070048
49namespace HWC2 {
50
Chia-I Wuaab99f52016-10-05 12:59:58 +080051namespace Hwc2 = android::Hwc2;
Peiyong Lin34beb7a2018-03-28 11:57:12 -070052using android::ui::ColorMode;
53using android::ui::Dataspace;
54using android::ui::PixelFormat;
Peiyong Lin0e7a7912018-04-05 14:36:36 -070055using android::ui::RenderIntent;
Chia-I Wuaab99f52016-10-05 12:59:58 +080056
Steven Thomas94e35b92017-07-26 18:48:28 -070057namespace {
58
Peiyong Lin2c327ac2018-04-19 22:06:34 -070059inline bool hasMetadataKey(const std::set<Hwc2::PerFrameMetadataKey>& keys,
60 const Hwc2::PerFrameMetadataKey& key) {
61 return keys.find(key) != keys.end();
62}
63
Steven Thomas94e35b92017-07-26 18:48:28 -070064class ComposerCallbackBridge : public Hwc2::IComposerCallback {
65public:
66 ComposerCallbackBridge(ComposerCallback* callback, int32_t sequenceId)
Lloyd Pique715a2c12017-12-14 17:18:08 -080067 : mCallback(callback), mSequenceId(sequenceId) {}
Steven Thomas94e35b92017-07-26 18:48:28 -070068
69 Return<void> onHotplug(Hwc2::Display display,
70 IComposerCallback::Connection conn) override
71 {
72 HWC2::Connection connection = static_cast<HWC2::Connection>(conn);
Lloyd Pique715a2c12017-12-14 17:18:08 -080073 mCallback->onHotplugReceived(mSequenceId, display, connection);
Steven Thomas94e35b92017-07-26 18:48:28 -070074 return Void();
75 }
76
77 Return<void> onRefresh(Hwc2::Display display) override
78 {
79 mCallback->onRefreshReceived(mSequenceId, display);
80 return Void();
81 }
82
83 Return<void> onVsync(Hwc2::Display display, int64_t timestamp) override
84 {
85 mCallback->onVsyncReceived(mSequenceId, display, timestamp);
86 return Void();
87 }
88
Steven Thomas94e35b92017-07-26 18:48:28 -070089private:
90 ComposerCallback* mCallback;
91 int32_t mSequenceId;
Steven Thomas94e35b92017-07-26 18:48:28 -070092};
93
94} // namespace anonymous
95
96
Dan Stoza651bf312015-10-23 17:03:17 -070097// Device methods
98
Lloyd Piquea822d522017-12-20 16:42:57 -080099Device::Device(std::unique_ptr<android::Hwc2::Composer> composer) : mComposer(std::move(composer)) {
Dan Stoza651bf312015-10-23 17:03:17 -0700100 loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700101}
102
Steven Thomas94e35b92017-07-26 18:48:28 -0700103void Device::registerCallback(ComposerCallback* callback, int32_t sequenceId) {
104 if (mRegisteredCallback) {
105 ALOGW("Callback already registered. Ignored extra registration "
106 "attempt.");
107 return;
Dan Stoza651bf312015-10-23 17:03:17 -0700108 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700109 mRegisteredCallback = true;
110 sp<ComposerCallbackBridge> callbackBridge(
111 new ComposerCallbackBridge(callback, sequenceId));
112 mComposer->registerCallback(callbackBridge);
Dan Stoza651bf312015-10-23 17:03:17 -0700113}
114
115// Required by HWC2 device
116
117std::string Device::dump() const
118{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800119 return mComposer->dumpDebugInfo();
Dan Stoza651bf312015-10-23 17:03:17 -0700120}
121
122uint32_t Device::getMaxVirtualDisplayCount() const
123{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800124 return mComposer->getMaxVirtualDisplayCount();
Dan Stoza651bf312015-10-23 17:03:17 -0700125}
126
127Error Device::createVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700128 PixelFormat* format, Display** outDisplay)
Dan Stoza651bf312015-10-23 17:03:17 -0700129{
130 ALOGI("Creating virtual display");
131
132 hwc2_display_t displayId = 0;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800133 auto intError = mComposer->createVirtualDisplay(width, height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700134 format, &displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700135 auto error = static_cast<Error>(intError);
136 if (error != Error::None) {
137 return error;
138 }
139
Steven Thomas94e35b92017-07-26 18:48:28 -0700140 auto display = std::make_unique<Display>(
141 *mComposer.get(), mCapabilities, displayId, DisplayType::Virtual);
Lloyd Piquebc792092018-01-17 11:52:30 -0800142 display->setConnected(true);
Steven Thomas94e35b92017-07-26 18:48:28 -0700143 *outDisplay = display.get();
Steven Thomas94e35b92017-07-26 18:48:28 -0700144 mDisplays.emplace(displayId, std::move(display));
145 ALOGI("Created virtual display");
Dan Stoza651bf312015-10-23 17:03:17 -0700146 return Error::None;
147}
148
Steven Thomas94e35b92017-07-26 18:48:28 -0700149void Device::destroyDisplay(hwc2_display_t displayId)
Dan Stoza651bf312015-10-23 17:03:17 -0700150{
Steven Thomas94e35b92017-07-26 18:48:28 -0700151 ALOGI("Destroying display %" PRIu64, displayId);
152 mDisplays.erase(displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700153}
154
Steven Thomas94e35b92017-07-26 18:48:28 -0700155void Device::onHotplug(hwc2_display_t displayId, Connection connection) {
156 if (connection == Connection::Connected) {
Steven Thomas3bed0522018-03-20 15:40:48 -0700157 // If we get a hotplug connected event for a display we already have,
158 // destroy the display and recreate it. This will force us to requery
159 // the display params and recreate all layers on that display.
160 auto oldDisplay = getDisplayById(displayId);
161 if (oldDisplay != nullptr && oldDisplay->isConnected()) {
162 ALOGI("Hotplug connecting an already connected display."
163 " Clearing old display state.");
Dan Stoza651bf312015-10-23 17:03:17 -0700164 }
Steven Thomas3bed0522018-03-20 15:40:48 -0700165 mDisplays.erase(displayId);
166
167 DisplayType displayType;
168 auto intError = mComposer->getDisplayType(displayId,
169 reinterpret_cast<Hwc2::IComposerClient::DisplayType *>(
170 &displayType));
171 auto error = static_cast<Error>(intError);
172 if (error != Error::None) {
173 ALOGE("getDisplayType(%" PRIu64 ") failed: %s (%d). "
174 "Aborting hotplug attempt.",
175 displayId, to_string(error).c_str(), intError);
176 return;
177 }
178
179 auto newDisplay = std::make_unique<Display>(
180 *mComposer.get(), mCapabilities, displayId, displayType);
Lloyd Piquebc792092018-01-17 11:52:30 -0800181 newDisplay->setConnected(true);
Steven Thomas3bed0522018-03-20 15:40:48 -0700182 mDisplays.emplace(displayId, std::move(newDisplay));
Steven Thomas94e35b92017-07-26 18:48:28 -0700183 } else if (connection == Connection::Disconnected) {
184 // The display will later be destroyed by a call to
185 // destroyDisplay(). For now we just mark it disconnected.
186 auto display = getDisplayById(displayId);
187 if (display) {
188 display->setConnected(false);
189 } else {
190 ALOGW("Attempted to disconnect unknown display %" PRIu64,
191 displayId);
192 }
Dan Stoza651bf312015-10-23 17:03:17 -0700193 }
194}
195
196// Other Device methods
197
Steven Thomas94e35b92017-07-26 18:48:28 -0700198Display* Device::getDisplayById(hwc2_display_t id) {
199 auto iter = mDisplays.find(id);
200 return iter == mDisplays.end() ? nullptr : iter->second.get();
Dan Stoza651bf312015-10-23 17:03:17 -0700201}
202
203// Device initialization methods
204
205void Device::loadCapabilities()
206{
207 static_assert(sizeof(Capability) == sizeof(int32_t),
208 "Capability size has changed");
Chia-I Wuaab99f52016-10-05 12:59:58 +0800209 auto capabilities = mComposer->getCapabilities();
210 for (auto capability : capabilities) {
211 mCapabilities.emplace(static_cast<Capability>(capability));
212 }
Dan Stoza651bf312015-10-23 17:03:17 -0700213}
214
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700215Error Device::flushCommands()
216{
217 return static_cast<Error>(mComposer->executeCommands());
218}
219
Dan Stoza651bf312015-10-23 17:03:17 -0700220// Display methods
221
Steven Thomas94e35b92017-07-26 18:48:28 -0700222Display::Display(android::Hwc2::Composer& composer,
Lloyd Piquebc792092018-01-17 11:52:30 -0800223 const std::unordered_set<Capability>& capabilities, hwc2_display_t id,
224 DisplayType type)
225 : mComposer(composer),
226 mCapabilities(capabilities),
227 mId(id),
228 mIsConnected(false),
229 mType(type) {
Dan Stoza651bf312015-10-23 17:03:17 -0700230 ALOGV("Created display %" PRIu64, id);
231}
232
Steven Thomas94e35b92017-07-26 18:48:28 -0700233Display::~Display() {
234 mLayers.clear();
235
Chris Forbesceb67d12017-04-11 12:20:00 -0700236 if (mType == DisplayType::Virtual) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700237 ALOGV("Destroying virtual display");
238 auto intError = mComposer.destroyVirtualDisplay(mId);
239 auto error = static_cast<Error>(intError);
240 ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64
241 ") failed: %s (%d)", mId, to_string(error).c_str(), intError);
242 } else if (mType == DisplayType::Physical) {
243 auto error = setVsyncEnabled(HWC2::Vsync::Disable);
244 if (error != Error::None) {
245 ALOGE("~Display: Failed to disable vsync for display %" PRIu64
246 ": %s (%d)", mId, to_string(error).c_str(),
247 static_cast<int32_t>(error));
248 }
Dan Stoza651bf312015-10-23 17:03:17 -0700249 }
250}
251
252Display::Config::Config(Display& display, hwc2_config_t id)
253 : mDisplay(display),
254 mId(id),
255 mWidth(-1),
256 mHeight(-1),
257 mVsyncPeriod(-1),
258 mDpiX(-1),
259 mDpiY(-1) {}
260
261Display::Config::Builder::Builder(Display& display, hwc2_config_t id)
262 : mConfig(new Config(display, id)) {}
263
264float Display::Config::Builder::getDefaultDensity() {
265 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
266 // resolution displays get TV density. Maybe eventually we'll need to update
267 // it for 4k displays, though hopefully those will just report accurate DPI
268 // information to begin with. This is also used for virtual displays and
269 // older HWC implementations, so be careful about orientation.
270
271 auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight);
272 if (longDimension >= 1080) {
273 return ACONFIGURATION_DENSITY_XHIGH;
274 } else {
275 return ACONFIGURATION_DENSITY_TV;
276 }
277}
278
279// Required by HWC2 display
280
281Error Display::acceptChanges()
282{
Steven Thomas94e35b92017-07-26 18:48:28 -0700283 auto intError = mComposer.acceptDisplayChanges(mId);
Dan Stoza651bf312015-10-23 17:03:17 -0700284 return static_cast<Error>(intError);
285}
286
Steven Thomas94e35b92017-07-26 18:48:28 -0700287Error Display::createLayer(Layer** outLayer)
Dan Stoza651bf312015-10-23 17:03:17 -0700288{
Steven Thomas94e35b92017-07-26 18:48:28 -0700289 if (!outLayer) {
290 return Error::BadParameter;
291 }
Dan Stoza651bf312015-10-23 17:03:17 -0700292 hwc2_layer_t layerId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700293 auto intError = mComposer.createLayer(mId, &layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700294 auto error = static_cast<Error>(intError);
295 if (error != Error::None) {
296 return error;
297 }
298
Steven Thomas94e35b92017-07-26 18:48:28 -0700299 auto layer = std::make_unique<Layer>(
300 mComposer, mCapabilities, mId, layerId);
301 *outLayer = layer.get();
302 mLayers.emplace(layerId, std::move(layer));
303 return Error::None;
304}
305
306Error Display::destroyLayer(Layer* layer)
307{
308 if (!layer) {
309 return Error::BadParameter;
310 }
311 mLayers.erase(layer->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700312 return Error::None;
313}
314
315Error Display::getActiveConfig(
316 std::shared_ptr<const Display::Config>* outConfig) const
317{
318 ALOGV("[%" PRIu64 "] getActiveConfig", mId);
319 hwc2_config_t configId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700320 auto intError = mComposer.getActiveConfig(mId, &configId);
Dan Stoza651bf312015-10-23 17:03:17 -0700321 auto error = static_cast<Error>(intError);
322
323 if (error != Error::None) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800324 ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
325 *outConfig = nullptr;
Dan Stoza651bf312015-10-23 17:03:17 -0700326 return error;
327 }
328
329 if (mConfigs.count(configId) != 0) {
330 *outConfig = mConfigs.at(configId);
331 } else {
332 ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId,
333 configId);
334 // Return no error, but the caller needs to check for a null pointer to
335 // detect this case
336 *outConfig = nullptr;
337 }
338
339 return Error::None;
340}
341
Lloyd Pique3c085a02018-05-09 19:38:32 -0700342Error Display::getActiveConfigIndex(int* outIndex) const {
343 ALOGV("[%" PRIu64 "] getActiveConfigIndex", mId);
344 hwc2_config_t configId = 0;
345 auto intError = mComposer.getActiveConfig(mId, &configId);
346 auto error = static_cast<Error>(intError);
347
348 if (error != Error::None) {
349 ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
350 *outIndex = -1;
351 return error;
352 }
353
354 auto pos = mConfigs.find(configId);
355 if (pos != mConfigs.end()) {
356 *outIndex = std::distance(mConfigs.begin(), pos);
357 } else {
358 ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId, configId);
359 // Return no error, but the caller needs to check for a negative index
360 // to detect this case
361 *outIndex = -1;
362 }
363
364 return Error::None;
365}
366
Dan Stoza651bf312015-10-23 17:03:17 -0700367Error Display::getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700368 std::unordered_map<Layer*, Composition>* outTypes)
Dan Stoza651bf312015-10-23 17:03:17 -0700369{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800370 std::vector<Hwc2::Layer> layerIds;
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800371 std::vector<Hwc2::IComposerClient::Composition> types;
Steven Thomas94e35b92017-07-26 18:48:28 -0700372 auto intError = mComposer.getChangedCompositionTypes(
373 mId, &layerIds, &types);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800374 uint32_t numElements = layerIds.size();
375 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700376 error = static_cast<Error>(intError);
377 if (error != Error::None) {
378 return error;
379 }
380
381 outTypes->clear();
382 outTypes->reserve(numElements);
383 for (uint32_t element = 0; element < numElements; ++element) {
384 auto layer = getLayerById(layerIds[element]);
385 if (layer) {
386 auto type = static_cast<Composition>(types[element]);
387 ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
388 layer->getId(), to_string(type).c_str());
389 outTypes->emplace(layer, type);
390 } else {
391 ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
392 " on display %" PRIu64, layerIds[element], mId);
393 }
394 }
395
396 return Error::None;
397}
398
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700399Error Display::getColorModes(std::vector<ColorMode>* outModes) const
Dan Stoza076ac672016-03-14 10:47:53 -0700400{
Peiyong Linfd997e02018-03-28 15:29:00 -0700401 auto intError = mComposer.getColorModes(mId, outModes);
402 return static_cast<Error>(intError);
Dan Stoza076ac672016-03-14 10:47:53 -0700403}
404
Peiyong Lin2c327ac2018-04-19 22:06:34 -0700405Error Display::getSupportedPerFrameMetadata(int32_t* outSupportedPerFrameMetadata) const
406{
407 *outSupportedPerFrameMetadata = 0;
408 std::vector<Hwc2::PerFrameMetadataKey> tmpKeys;
409 auto intError = mComposer.getPerFrameMetadataKeys(mId, &tmpKeys);
410 auto error = static_cast<Error>(intError);
411 if (error != Error::None) {
412 return error;
413 }
414
415 // Check whether a specific metadata type is supported. A metadata type is considered
416 // supported if and only if all required fields are supported.
417
418 // SMPTE2086
419 std::set<Hwc2::PerFrameMetadataKey> keys(tmpKeys.begin(), tmpKeys.end());
420 if (hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X) &&
421 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y) &&
422 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X) &&
423 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y) &&
424 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X) &&
425 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y) &&
426 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::WHITE_POINT_X) &&
427 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::WHITE_POINT_Y) &&
428 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MAX_LUMINANCE) &&
429 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MIN_LUMINANCE)) {
430 *outSupportedPerFrameMetadata |= HdrMetadata::Type::SMPTE2086;
431 }
432 // CTA861_3
433 if (hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL) &&
434 hasMetadataKey(keys, Hwc2::PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL)) {
435 *outSupportedPerFrameMetadata |= HdrMetadata::Type::CTA861_3;
436 }
437
438 return Error::None;
439}
440
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700441Error Display::getRenderIntents(ColorMode colorMode,
442 std::vector<RenderIntent>* outRenderIntents) const
443{
444 auto intError = mComposer.getRenderIntents(mId, colorMode, outRenderIntents);
445 return static_cast<Error>(intError);
446}
447
448Error Display::getDataspaceSaturationMatrix(Dataspace dataspace, android::mat4* outMatrix)
449{
450 auto intError = mComposer.getDataspaceSaturationMatrix(dataspace, outMatrix);
451 return static_cast<Error>(intError);
452}
453
Dan Stoza651bf312015-10-23 17:03:17 -0700454std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const
455{
456 std::vector<std::shared_ptr<const Config>> configs;
457 for (const auto& element : mConfigs) {
458 configs.emplace_back(element.second);
459 }
460 return configs;
461}
462
463Error Display::getName(std::string* outName) const
464{
Steven Thomas94e35b92017-07-26 18:48:28 -0700465 auto intError = mComposer.getDisplayName(mId, outName);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800466 return static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700467}
468
469Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700470 std::unordered_map<Layer*, LayerRequest>* outLayerRequests)
Dan Stoza651bf312015-10-23 17:03:17 -0700471{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800472 uint32_t intDisplayRequests;
473 std::vector<Hwc2::Layer> layerIds;
474 std::vector<uint32_t> layerRequests;
Steven Thomas94e35b92017-07-26 18:48:28 -0700475 auto intError = mComposer.getDisplayRequests(
476 mId, &intDisplayRequests, &layerIds, &layerRequests);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800477 uint32_t numElements = layerIds.size();
478 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700479 if (error != Error::None) {
480 return error;
481 }
482
483 *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
484 outLayerRequests->clear();
485 outLayerRequests->reserve(numElements);
486 for (uint32_t element = 0; element < numElements; ++element) {
487 auto layer = getLayerById(layerIds[element]);
488 if (layer) {
489 auto layerRequest =
490 static_cast<LayerRequest>(layerRequests[element]);
491 outLayerRequests->emplace(layer, layerRequest);
492 } else {
493 ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
494 PRIu64, layerIds[element], mId);
495 }
496 }
497
498 return Error::None;
499}
500
501Error Display::getType(DisplayType* outType) const
502{
Chris Forbes016d73c2017-04-11 10:04:31 -0700503 *outType = mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700504 return Error::None;
505}
506
507Error Display::supportsDoze(bool* outSupport) const
508{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800509 bool intSupport = false;
Steven Thomas94e35b92017-07-26 18:48:28 -0700510 auto intError = mComposer.getDozeSupport(mId, &intSupport);
Dan Stoza651bf312015-10-23 17:03:17 -0700511 auto error = static_cast<Error>(intError);
512 if (error != Error::None) {
513 return error;
514 }
515 *outSupport = static_cast<bool>(intSupport);
516 return Error::None;
517}
518
Peiyong Lin62665892018-04-16 11:07:44 -0700519Error Display::getHdrCapabilities(HdrCapabilities* outCapabilities) const
Dan Stoza7d7ae732016-03-16 12:23:40 -0700520{
Dan Stoza7d7ae732016-03-16 12:23:40 -0700521 float maxLuminance = -1.0f;
522 float maxAverageLuminance = -1.0f;
523 float minLuminance = -1.0f;
Peiyong Lin62665892018-04-16 11:07:44 -0700524 std::vector<Hwc2::Hdr> types;
525 auto intError = mComposer.getHdrCapabilities(mId, &types,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800526 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800527 auto error = static_cast<HWC2::Error>(intError);
528
Dan Stoza7d7ae732016-03-16 12:23:40 -0700529 if (error != Error::None) {
530 return error;
531 }
532
Peiyong Lin62665892018-04-16 11:07:44 -0700533 *outCapabilities = HdrCapabilities(std::move(types),
Dan Stoza7d7ae732016-03-16 12:23:40 -0700534 maxLuminance, maxAverageLuminance, minLuminance);
535 return Error::None;
536}
537
Dan Stoza651bf312015-10-23 17:03:17 -0700538Error Display::getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700539 std::unordered_map<Layer*, sp<Fence>>* outFences) const
Dan Stoza651bf312015-10-23 17:03:17 -0700540{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800541 std::vector<Hwc2::Layer> layerIds;
542 std::vector<int> fenceFds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700543 auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800544 auto error = static_cast<Error>(intError);
545 uint32_t numElements = layerIds.size();
Dan Stoza651bf312015-10-23 17:03:17 -0700546 if (error != Error::None) {
547 return error;
548 }
549
Steven Thomas94e35b92017-07-26 18:48:28 -0700550 std::unordered_map<Layer*, sp<Fence>> releaseFences;
Dan Stoza651bf312015-10-23 17:03:17 -0700551 releaseFences.reserve(numElements);
552 for (uint32_t element = 0; element < numElements; ++element) {
553 auto layer = getLayerById(layerIds[element]);
554 if (layer) {
555 sp<Fence> fence(new Fence(fenceFds[element]));
Steven Thomas94e35b92017-07-26 18:48:28 -0700556 releaseFences.emplace(layer, fence);
Dan Stoza651bf312015-10-23 17:03:17 -0700557 } else {
558 ALOGE("getReleaseFences: invalid layer %" PRIu64
559 " found on display %" PRIu64, layerIds[element], mId);
Chia-I Wu5e74c652017-05-17 13:43:16 -0700560 for (; element < numElements; ++element) {
561 close(fenceFds[element]);
562 }
Dan Stoza651bf312015-10-23 17:03:17 -0700563 return Error::BadLayer;
564 }
565 }
566
567 *outFences = std::move(releaseFences);
568 return Error::None;
569}
570
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800571Error Display::present(sp<Fence>* outPresentFence)
Dan Stoza651bf312015-10-23 17:03:17 -0700572{
Naseer Ahmed847650b2016-06-17 11:14:25 -0400573 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700574 auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700575 auto error = static_cast<Error>(intError);
576 if (error != Error::None) {
577 return error;
578 }
579
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800580 *outPresentFence = new Fence(presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700581 return Error::None;
582}
583
584Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
585{
586 if (config->getDisplayId() != mId) {
587 ALOGE("setActiveConfig received config %u for the wrong display %"
588 PRIu64 " (expected %" PRIu64 ")", config->getId(),
589 config->getDisplayId(), mId);
590 return Error::BadConfig;
591 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700592 auto intError = mComposer.setActiveConfig(mId, config->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700593 return static_cast<Error>(intError);
594}
595
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400596Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700597 const sp<Fence>& acquireFence, Dataspace dataspace)
Dan Stoza651bf312015-10-23 17:03:17 -0700598{
Dan Stoza5cf424b2016-05-20 14:02:39 -0700599 // TODO: Properly encode client target surface damage
Dan Stoza651bf312015-10-23 17:03:17 -0700600 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700601 auto intError = mComposer.setClientTarget(mId, slot, target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700602 fenceFd, dataspace, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700603 return static_cast<Error>(intError);
604}
605
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700606Error Display::setColorMode(ColorMode mode, RenderIntent renderIntent)
Dan Stoza076ac672016-03-14 10:47:53 -0700607{
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700608 auto intError = mComposer.setColorMode(mId, mode, renderIntent);
Dan Stoza076ac672016-03-14 10:47:53 -0700609 return static_cast<Error>(intError);
610}
611
Dan Stoza5df2a862016-03-24 16:19:37 -0700612Error Display::setColorTransform(const android::mat4& matrix,
613 android_color_transform_t hint)
614{
Steven Thomas94e35b92017-07-26 18:48:28 -0700615 auto intError = mComposer.setColorTransform(mId,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800616 matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
Dan Stoza5df2a862016-03-24 16:19:37 -0700617 return static_cast<Error>(intError);
618}
619
Dan Stoza651bf312015-10-23 17:03:17 -0700620Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
621 const sp<Fence>& releaseFence)
622{
623 int32_t fenceFd = releaseFence->dup();
624 auto handle = buffer->getNativeBuffer()->handle;
Steven Thomas94e35b92017-07-26 18:48:28 -0700625 auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
Dan Stoza38628982016-07-13 15:48:58 -0700626 close(fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700627 return static_cast<Error>(intError);
628}
629
630Error Display::setPowerMode(PowerMode mode)
631{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800632 auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700633 auto intError = mComposer.setPowerMode(mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700634 return static_cast<Error>(intError);
635}
636
637Error Display::setVsyncEnabled(Vsync enabled)
638{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800639 auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
Steven Thomas94e35b92017-07-26 18:48:28 -0700640 auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
Dan Stoza651bf312015-10-23 17:03:17 -0700641 return static_cast<Error>(intError);
642}
643
644Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
645{
646 uint32_t numTypes = 0;
647 uint32_t numRequests = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700648 auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700649 auto error = static_cast<Error>(intError);
650 if (error != Error::None && error != Error::HasChanges) {
651 return error;
652 }
653
654 *outNumTypes = numTypes;
655 *outNumRequests = numRequests;
656 return error;
657}
658
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700659Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests,
660 sp<android::Fence>* outPresentFence, uint32_t* state) {
661
662 uint32_t numTypes = 0;
663 uint32_t numRequests = 0;
664 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700665 auto intError = mComposer.presentOrValidateDisplay(
666 mId, &numTypes, &numRequests, &presentFenceFd, state);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700667 auto error = static_cast<Error>(intError);
668 if (error != Error::None && error != Error::HasChanges) {
669 return error;
670 }
671
672 if (*state == 1) {
673 *outPresentFence = new Fence(presentFenceFd);
674 }
675
676 if (*state == 0) {
677 *outNumTypes = numTypes;
678 *outNumRequests = numRequests;
679 }
680 return error;
681}
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700682
Dan Stoza651bf312015-10-23 17:03:17 -0700683// For use by Device
684
Steven Thomas94e35b92017-07-26 18:48:28 -0700685void Display::setConnected(bool connected) {
Steven Thomasb6c6ad42018-01-29 12:22:00 -0800686 if (!mIsConnected && connected) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700687 mComposer.setClientTargetSlotCount(mId);
Steven Thomasb6c6ad42018-01-29 12:22:00 -0800688 if (mType == DisplayType::Physical) {
689 loadConfigs();
690 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700691 }
692 mIsConnected = connected;
693}
694
Dan Stoza651bf312015-10-23 17:03:17 -0700695int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute)
696{
697 int32_t value = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700698 auto intError = mComposer.getDisplayAttribute(mId, configId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800699 static_cast<Hwc2::IComposerClient::Attribute>(attribute),
700 &value);
Dan Stoza651bf312015-10-23 17:03:17 -0700701 auto error = static_cast<Error>(intError);
702 if (error != Error::None) {
703 ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId,
704 configId, to_string(attribute).c_str(),
705 to_string(error).c_str(), intError);
706 return -1;
707 }
708 return value;
709}
710
711void Display::loadConfig(hwc2_config_t configId)
712{
713 ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId);
714
715 auto config = Config::Builder(*this, configId)
716 .setWidth(getAttribute(configId, Attribute::Width))
717 .setHeight(getAttribute(configId, Attribute::Height))
718 .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod))
719 .setDpiX(getAttribute(configId, Attribute::DpiX))
720 .setDpiY(getAttribute(configId, Attribute::DpiY))
721 .build();
722 mConfigs.emplace(configId, std::move(config));
723}
724
725void Display::loadConfigs()
726{
727 ALOGV("[%" PRIu64 "] loadConfigs", mId);
728
Chia-I Wuaab99f52016-10-05 12:59:58 +0800729 std::vector<Hwc2::Config> configIds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700730 auto intError = mComposer.getDisplayConfigs(mId, &configIds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800731 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700732 if (error != Error::None) {
733 ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId,
734 to_string(error).c_str(), intError);
735 return;
736 }
737
738 for (auto configId : configIds) {
739 loadConfig(configId);
740 }
741}
742
Dan Stoza651bf312015-10-23 17:03:17 -0700743// Other Display methods
744
Steven Thomas94e35b92017-07-26 18:48:28 -0700745Layer* Display::getLayerById(hwc2_layer_t id) const
Dan Stoza651bf312015-10-23 17:03:17 -0700746{
747 if (mLayers.count(id) == 0) {
748 return nullptr;
749 }
750
Steven Thomas94e35b92017-07-26 18:48:28 -0700751 return mLayers.at(id).get();
Dan Stoza651bf312015-10-23 17:03:17 -0700752}
753
754// Layer methods
755
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700756Layer::Layer(android::Hwc2::Composer& composer, const std::unordered_set<Capability>& capabilities,
Steven Thomas94e35b92017-07-26 18:48:28 -0700757 hwc2_display_t displayId, hwc2_layer_t layerId)
758 : mComposer(composer),
759 mCapabilities(capabilities),
760 mDisplayId(displayId),
761 mId(layerId)
Dan Stoza651bf312015-10-23 17:03:17 -0700762{
Steven Thomas94e35b92017-07-26 18:48:28 -0700763 ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700764}
765
766Layer::~Layer()
767{
Steven Thomas94e35b92017-07-26 18:48:28 -0700768 auto intError = mComposer.destroyLayer(mDisplayId, mId);
769 auto error = static_cast<Error>(intError);
770 ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
771 " failed: %s (%d)", mDisplayId, mId, to_string(error).c_str(),
772 intError);
773 if (mLayerDestroyedListener) {
774 mLayerDestroyedListener(this);
Dan Stoza651bf312015-10-23 17:03:17 -0700775 }
776}
777
Steven Thomas94e35b92017-07-26 18:48:28 -0700778void Layer::setLayerDestroyedListener(std::function<void(Layer*)> listener) {
779 LOG_ALWAYS_FATAL_IF(mLayerDestroyedListener && listener,
780 "Attempt to set layer destroyed listener multiple times");
781 mLayerDestroyedListener = listener;
782}
783
Dan Stoza651bf312015-10-23 17:03:17 -0700784Error Layer::setCursorPosition(int32_t x, int32_t y)
785{
Steven Thomas94e35b92017-07-26 18:48:28 -0700786 auto intError = mComposer.setCursorPosition(mDisplayId, mId, x, y);
Dan Stoza651bf312015-10-23 17:03:17 -0700787 return static_cast<Error>(intError);
788}
789
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400790Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700791 const sp<Fence>& acquireFence)
792{
793 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700794 auto intError = mComposer.setLayerBuffer(mDisplayId, mId, slot, buffer,
795 fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700796 return static_cast<Error>(intError);
797}
798
799Error Layer::setSurfaceDamage(const Region& damage)
800{
801 // We encode default full-screen damage as INVALID_RECT upstream, but as 0
802 // rects for HWC
Chia-I Wuaab99f52016-10-05 12:59:58 +0800803 Hwc2::Error intError = Hwc2::Error::NONE;
Dan Stoza651bf312015-10-23 17:03:17 -0700804 if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700805 intError = mComposer.setLayerSurfaceDamage(mDisplayId,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800806 mId, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700807 } else {
808 size_t rectCount = 0;
809 auto rectArray = damage.getArray(&rectCount);
810
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800811 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700812 for (size_t rect = 0; rect < rectCount; ++rect) {
813 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
814 rectArray[rect].right, rectArray[rect].bottom});
815 }
816
Steven Thomas94e35b92017-07-26 18:48:28 -0700817 intError = mComposer.setLayerSurfaceDamage(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700818 }
819
820 return static_cast<Error>(intError);
821}
822
823Error Layer::setBlendMode(BlendMode mode)
824{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800825 auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700826 auto intError = mComposer.setLayerBlendMode(mDisplayId, mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700827 return static_cast<Error>(intError);
828}
829
830Error Layer::setColor(hwc_color_t color)
831{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800832 Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
Steven Thomas94e35b92017-07-26 18:48:28 -0700833 auto intError = mComposer.setLayerColor(mDisplayId, mId, hwcColor);
Dan Stoza651bf312015-10-23 17:03:17 -0700834 return static_cast<Error>(intError);
835}
836
837Error Layer::setCompositionType(Composition type)
838{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800839 auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
Steven Thomas94e35b92017-07-26 18:48:28 -0700840 auto intError = mComposer.setLayerCompositionType(
841 mDisplayId, mId, intType);
Dan Stoza651bf312015-10-23 17:03:17 -0700842 return static_cast<Error>(intError);
843}
844
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700845Error Layer::setDataspace(Dataspace dataspace)
Dan Stoza5df2a862016-03-24 16:19:37 -0700846{
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600847 if (dataspace == mDataSpace) {
848 return Error::None;
849 }
850 mDataSpace = dataspace;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700851 auto intError = mComposer.setLayerDataspace(mDisplayId, mId, mDataSpace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700852 return static_cast<Error>(intError);
853}
854
Peiyong Lin2c327ac2018-04-19 22:06:34 -0700855Error Layer::setPerFrameMetadata(const int32_t supportedPerFrameMetadata,
856 const android::HdrMetadata& metadata)
857{
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700858 if (metadata == mHdrMetadata) {
859 return Error::None;
860 }
861
862 mHdrMetadata = metadata;
Peiyong Lin2c327ac2018-04-19 22:06:34 -0700863 int validTypes = mHdrMetadata.validTypes & supportedPerFrameMetadata;
864 std::vector<Hwc2::PerFrameMetadata> perFrameMetadatas;
865 if (validTypes & HdrMetadata::SMPTE2086) {
866 perFrameMetadatas.insert(perFrameMetadatas.end(),
867 {{Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_X,
868 mHdrMetadata.smpte2086.displayPrimaryRed.x},
869 {Hwc2::PerFrameMetadataKey::DISPLAY_RED_PRIMARY_Y,
870 mHdrMetadata.smpte2086.displayPrimaryRed.y},
871 {Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_X,
872 mHdrMetadata.smpte2086.displayPrimaryGreen.x},
873 {Hwc2::PerFrameMetadataKey::DISPLAY_GREEN_PRIMARY_Y,
874 mHdrMetadata.smpte2086.displayPrimaryGreen.y},
875 {Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_X,
876 mHdrMetadata.smpte2086.displayPrimaryBlue.x},
877 {Hwc2::PerFrameMetadataKey::DISPLAY_BLUE_PRIMARY_Y,
878 mHdrMetadata.smpte2086.displayPrimaryBlue.y},
879 {Hwc2::PerFrameMetadataKey::WHITE_POINT_X,
880 mHdrMetadata.smpte2086.whitePoint.x},
881 {Hwc2::PerFrameMetadataKey::WHITE_POINT_Y,
882 mHdrMetadata.smpte2086.whitePoint.y},
883 {Hwc2::PerFrameMetadataKey::MAX_LUMINANCE,
884 mHdrMetadata.smpte2086.maxLuminance},
885 {Hwc2::PerFrameMetadataKey::MIN_LUMINANCE,
886 mHdrMetadata.smpte2086.minLuminance}});
887 }
888
889 if (validTypes & HdrMetadata::CTA861_3) {
890 perFrameMetadatas.insert(perFrameMetadatas.end(),
891 {{Hwc2::PerFrameMetadataKey::MAX_CONTENT_LIGHT_LEVEL,
892 mHdrMetadata.cta8613.maxContentLightLevel},
893 {Hwc2::PerFrameMetadataKey::MAX_FRAME_AVERAGE_LIGHT_LEVEL,
894 mHdrMetadata.cta8613.maxFrameAverageLightLevel}});
895 }
896
897 auto intError = mComposer.setLayerPerFrameMetadata(mDisplayId, mId, perFrameMetadatas);
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700898 return static_cast<Error>(intError);
899}
900
Dan Stoza651bf312015-10-23 17:03:17 -0700901Error Layer::setDisplayFrame(const Rect& frame)
902{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800903 Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800904 frame.right, frame.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700905 auto intError = mComposer.setLayerDisplayFrame(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700906 return static_cast<Error>(intError);
907}
908
909Error Layer::setPlaneAlpha(float alpha)
910{
Steven Thomas94e35b92017-07-26 18:48:28 -0700911 auto intError = mComposer.setLayerPlaneAlpha(mDisplayId, mId, alpha);
Dan Stoza651bf312015-10-23 17:03:17 -0700912 return static_cast<Error>(intError);
913}
914
915Error Layer::setSidebandStream(const native_handle_t* stream)
916{
Steven Thomas94e35b92017-07-26 18:48:28 -0700917 if (mCapabilities.count(Capability::SidebandStream) == 0) {
Dan Stoza09e7a272016-04-14 12:31:01 -0700918 ALOGE("Attempted to call setSidebandStream without checking that the "
919 "device supports sideband streams");
920 return Error::Unsupported;
921 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700922 auto intError = mComposer.setLayerSidebandStream(mDisplayId, mId, stream);
Dan Stoza651bf312015-10-23 17:03:17 -0700923 return static_cast<Error>(intError);
924}
925
926Error Layer::setSourceCrop(const FloatRect& crop)
927{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800928 Hwc2::IComposerClient::FRect hwcRect{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800929 crop.left, crop.top, crop.right, crop.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700930 auto intError = mComposer.setLayerSourceCrop(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700931 return static_cast<Error>(intError);
932}
933
934Error Layer::setTransform(Transform transform)
935{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800936 auto intTransform = static_cast<Hwc2::Transform>(transform);
Steven Thomas94e35b92017-07-26 18:48:28 -0700937 auto intError = mComposer.setLayerTransform(mDisplayId, mId, intTransform);
Dan Stoza651bf312015-10-23 17:03:17 -0700938 return static_cast<Error>(intError);
939}
940
941Error Layer::setVisibleRegion(const Region& region)
942{
943 size_t rectCount = 0;
944 auto rectArray = region.getArray(&rectCount);
945
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800946 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700947 for (size_t rect = 0; rect < rectCount; ++rect) {
948 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
949 rectArray[rect].right, rectArray[rect].bottom});
950 }
951
Steven Thomas94e35b92017-07-26 18:48:28 -0700952 auto intError = mComposer.setLayerVisibleRegion(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700953 return static_cast<Error>(intError);
954}
955
956Error Layer::setZOrder(uint32_t z)
957{
Steven Thomas94e35b92017-07-26 18:48:28 -0700958 auto intError = mComposer.setLayerZOrder(mDisplayId, mId, z);
Dan Stoza651bf312015-10-23 17:03:17 -0700959 return static_cast<Error>(intError);
960}
961
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500962Error Layer::setInfo(uint32_t type, uint32_t appId)
963{
Steven Thomas94e35b92017-07-26 18:48:28 -0700964 auto intError = mComposer.setLayerInfo(mDisplayId, mId, type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500965 return static_cast<Error>(intError);
966}
967
Dan Stoza651bf312015-10-23 17:03:17 -0700968} // namespace HWC2