blob: 070b6915024eec03b20d89cad8fb7d1b324fd8b5 [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
Dan Stoza09e7a272016-04-14 12:31:01 -070033#include <algorithm>
Dan Stoza651bf312015-10-23 17:03:17 -070034#include <inttypes.h>
35
Dan Stoza651bf312015-10-23 17:03:17 -070036using android::Fence;
Dan Stoza5a423ea2017-02-16 14:10:39 -080037using android::FloatRect;
Dan Stoza651bf312015-10-23 17:03:17 -070038using android::GraphicBuffer;
Dan Stoza7d7ae732016-03-16 12:23:40 -070039using android::HdrCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -070040using android::Rect;
41using android::Region;
42using android::sp;
Chia-I Wuaab99f52016-10-05 12:59:58 +080043using android::hardware::Return;
44using android::hardware::Void;
Dan Stoza651bf312015-10-23 17:03:17 -070045
46namespace HWC2 {
47
Chia-I Wuaab99f52016-10-05 12:59:58 +080048namespace Hwc2 = android::Hwc2;
49
Steven Thomas94e35b92017-07-26 18:48:28 -070050namespace {
51
52class ComposerCallbackBridge : public Hwc2::IComposerCallback {
53public:
54 ComposerCallbackBridge(ComposerCallback* callback, int32_t sequenceId)
Lloyd Pique715a2c12017-12-14 17:18:08 -080055 : mCallback(callback), mSequenceId(sequenceId) {}
Steven Thomas94e35b92017-07-26 18:48:28 -070056
57 Return<void> onHotplug(Hwc2::Display display,
58 IComposerCallback::Connection conn) override
59 {
60 HWC2::Connection connection = static_cast<HWC2::Connection>(conn);
Lloyd Pique715a2c12017-12-14 17:18:08 -080061 mCallback->onHotplugReceived(mSequenceId, display, connection);
Steven Thomas94e35b92017-07-26 18:48:28 -070062 return Void();
63 }
64
65 Return<void> onRefresh(Hwc2::Display display) override
66 {
67 mCallback->onRefreshReceived(mSequenceId, display);
68 return Void();
69 }
70
71 Return<void> onVsync(Hwc2::Display display, int64_t timestamp) override
72 {
73 mCallback->onVsyncReceived(mSequenceId, display, timestamp);
74 return Void();
75 }
76
Steven Thomas94e35b92017-07-26 18:48:28 -070077private:
78 ComposerCallback* mCallback;
79 int32_t mSequenceId;
Steven Thomas94e35b92017-07-26 18:48:28 -070080};
81
82} // namespace anonymous
83
84
Dan Stoza651bf312015-10-23 17:03:17 -070085// Device methods
86
Kalle Raitaa099a242017-01-11 11:17:29 -080087Device::Device(const std::string& serviceName)
88 : mComposer(std::make_unique<Hwc2::Composer>(serviceName)),
Dan Stoza651bf312015-10-23 17:03:17 -070089 mCapabilities(),
90 mDisplays(),
Steven Thomas94e35b92017-07-26 18:48:28 -070091 mRegisteredCallback(false)
Dan Stoza651bf312015-10-23 17:03:17 -070092{
93 loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -070094}
95
Steven Thomas94e35b92017-07-26 18:48:28 -070096void Device::registerCallback(ComposerCallback* callback, int32_t sequenceId) {
97 if (mRegisteredCallback) {
98 ALOGW("Callback already registered. Ignored extra registration "
99 "attempt.");
100 return;
Dan Stoza651bf312015-10-23 17:03:17 -0700101 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700102 mRegisteredCallback = true;
103 sp<ComposerCallbackBridge> callbackBridge(
104 new ComposerCallbackBridge(callback, sequenceId));
105 mComposer->registerCallback(callbackBridge);
Dan Stoza651bf312015-10-23 17:03:17 -0700106}
107
108// Required by HWC2 device
109
110std::string Device::dump() const
111{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800112 return mComposer->dumpDebugInfo();
Dan Stoza651bf312015-10-23 17:03:17 -0700113}
114
115uint32_t Device::getMaxVirtualDisplayCount() const
116{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800117 return mComposer->getMaxVirtualDisplayCount();
Dan Stoza651bf312015-10-23 17:03:17 -0700118}
119
120Error Device::createVirtualDisplay(uint32_t width, uint32_t height,
Steven Thomas94e35b92017-07-26 18:48:28 -0700121 android_pixel_format_t* format, Display** outDisplay)
Dan Stoza651bf312015-10-23 17:03:17 -0700122{
123 ALOGI("Creating virtual display");
124
125 hwc2_display_t displayId = 0;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800126 auto intFormat = static_cast<Hwc2::PixelFormat>(*format);
127 auto intError = mComposer->createVirtualDisplay(width, height,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800128 &intFormat, &displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700129 auto error = static_cast<Error>(intError);
130 if (error != Error::None) {
131 return error;
132 }
133
Steven Thomas94e35b92017-07-26 18:48:28 -0700134 auto display = std::make_unique<Display>(
135 *mComposer.get(), mCapabilities, displayId, DisplayType::Virtual);
136 *outDisplay = display.get();
Dan Stoza5cf424b2016-05-20 14:02:39 -0700137 *format = static_cast<android_pixel_format_t>(intFormat);
Steven Thomas94e35b92017-07-26 18:48:28 -0700138 mDisplays.emplace(displayId, std::move(display));
139 ALOGI("Created virtual display");
Dan Stoza651bf312015-10-23 17:03:17 -0700140 return Error::None;
141}
142
Steven Thomas94e35b92017-07-26 18:48:28 -0700143void Device::destroyDisplay(hwc2_display_t displayId)
Dan Stoza651bf312015-10-23 17:03:17 -0700144{
Steven Thomas94e35b92017-07-26 18:48:28 -0700145 ALOGI("Destroying display %" PRIu64, displayId);
146 mDisplays.erase(displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700147}
148
Steven Thomas94e35b92017-07-26 18:48:28 -0700149void Device::onHotplug(hwc2_display_t displayId, Connection connection) {
150 if (connection == Connection::Connected) {
151 auto display = getDisplayById(displayId);
152 if (display) {
153 if (display->isConnected()) {
154 ALOGW("Attempt to hotplug connect display %" PRIu64
155 " , which is already connected.", displayId);
156 } else {
157 display->setConnected(true);
158 }
159 } else {
160 DisplayType displayType;
161 auto intError = mComposer->getDisplayType(displayId,
162 reinterpret_cast<Hwc2::IComposerClient::DisplayType *>(
163 &displayType));
164 auto error = static_cast<Error>(intError);
165 if (error != Error::None) {
166 ALOGE("getDisplayType(%" PRIu64 ") failed: %s (%d). "
167 "Aborting hotplug attempt.",
168 displayId, to_string(error).c_str(), intError);
169 return;
170 }
Dan Stoza651bf312015-10-23 17:03:17 -0700171
Steven Thomas94e35b92017-07-26 18:48:28 -0700172 auto newDisplay = std::make_unique<Display>(
173 *mComposer.get(), mCapabilities, displayId, displayType);
174 mDisplays.emplace(displayId, std::move(newDisplay));
Dan Stoza651bf312015-10-23 17:03:17 -0700175 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700176 } else if (connection == Connection::Disconnected) {
177 // The display will later be destroyed by a call to
178 // destroyDisplay(). For now we just mark it disconnected.
179 auto display = getDisplayById(displayId);
180 if (display) {
181 display->setConnected(false);
182 } else {
183 ALOGW("Attempted to disconnect unknown display %" PRIu64,
184 displayId);
185 }
Dan Stoza651bf312015-10-23 17:03:17 -0700186 }
187}
188
189// Other Device methods
190
Steven Thomas94e35b92017-07-26 18:48:28 -0700191Display* Device::getDisplayById(hwc2_display_t id) {
192 auto iter = mDisplays.find(id);
193 return iter == mDisplays.end() ? nullptr : iter->second.get();
Dan Stoza651bf312015-10-23 17:03:17 -0700194}
195
196// Device initialization methods
197
198void Device::loadCapabilities()
199{
200 static_assert(sizeof(Capability) == sizeof(int32_t),
201 "Capability size has changed");
Chia-I Wuaab99f52016-10-05 12:59:58 +0800202 auto capabilities = mComposer->getCapabilities();
203 for (auto capability : capabilities) {
204 mCapabilities.emplace(static_cast<Capability>(capability));
205 }
Dan Stoza651bf312015-10-23 17:03:17 -0700206}
207
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700208Error Device::flushCommands()
209{
210 return static_cast<Error>(mComposer->executeCommands());
211}
212
Dan Stoza651bf312015-10-23 17:03:17 -0700213// Display methods
214
Steven Thomas94e35b92017-07-26 18:48:28 -0700215Display::Display(android::Hwc2::Composer& composer,
216 const std::unordered_set<Capability>& capabilities,
217 hwc2_display_t id, DisplayType type)
218 : mComposer(composer),
219 mCapabilities(capabilities),
Dan Stoza651bf312015-10-23 17:03:17 -0700220 mId(id),
221 mIsConnected(false),
Steven Thomas94e35b92017-07-26 18:48:28 -0700222 mType(type)
Dan Stoza651bf312015-10-23 17:03:17 -0700223{
224 ALOGV("Created display %" PRIu64, id);
Steven Thomas94e35b92017-07-26 18:48:28 -0700225 setConnected(true);
Dan Stoza651bf312015-10-23 17:03:17 -0700226}
227
Steven Thomas94e35b92017-07-26 18:48:28 -0700228Display::~Display() {
229 mLayers.clear();
230
Chris Forbesceb67d12017-04-11 12:20:00 -0700231 if (mType == DisplayType::Virtual) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700232 ALOGV("Destroying virtual display");
233 auto intError = mComposer.destroyVirtualDisplay(mId);
234 auto error = static_cast<Error>(intError);
235 ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64
236 ") failed: %s (%d)", mId, to_string(error).c_str(), intError);
237 } else if (mType == DisplayType::Physical) {
238 auto error = setVsyncEnabled(HWC2::Vsync::Disable);
239 if (error != Error::None) {
240 ALOGE("~Display: Failed to disable vsync for display %" PRIu64
241 ": %s (%d)", mId, to_string(error).c_str(),
242 static_cast<int32_t>(error));
243 }
Dan Stoza651bf312015-10-23 17:03:17 -0700244 }
245}
246
247Display::Config::Config(Display& display, hwc2_config_t id)
248 : mDisplay(display),
249 mId(id),
250 mWidth(-1),
251 mHeight(-1),
252 mVsyncPeriod(-1),
253 mDpiX(-1),
254 mDpiY(-1) {}
255
256Display::Config::Builder::Builder(Display& display, hwc2_config_t id)
257 : mConfig(new Config(display, id)) {}
258
259float Display::Config::Builder::getDefaultDensity() {
260 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
261 // resolution displays get TV density. Maybe eventually we'll need to update
262 // it for 4k displays, though hopefully those will just report accurate DPI
263 // information to begin with. This is also used for virtual displays and
264 // older HWC implementations, so be careful about orientation.
265
266 auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight);
267 if (longDimension >= 1080) {
268 return ACONFIGURATION_DENSITY_XHIGH;
269 } else {
270 return ACONFIGURATION_DENSITY_TV;
271 }
272}
273
274// Required by HWC2 display
275
276Error Display::acceptChanges()
277{
Steven Thomas94e35b92017-07-26 18:48:28 -0700278 auto intError = mComposer.acceptDisplayChanges(mId);
Dan Stoza651bf312015-10-23 17:03:17 -0700279 return static_cast<Error>(intError);
280}
281
Steven Thomas94e35b92017-07-26 18:48:28 -0700282Error Display::createLayer(Layer** outLayer)
Dan Stoza651bf312015-10-23 17:03:17 -0700283{
Steven Thomas94e35b92017-07-26 18:48:28 -0700284 if (!outLayer) {
285 return Error::BadParameter;
286 }
Dan Stoza651bf312015-10-23 17:03:17 -0700287 hwc2_layer_t layerId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700288 auto intError = mComposer.createLayer(mId, &layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700289 auto error = static_cast<Error>(intError);
290 if (error != Error::None) {
291 return error;
292 }
293
Steven Thomas94e35b92017-07-26 18:48:28 -0700294 auto layer = std::make_unique<Layer>(
295 mComposer, mCapabilities, mId, layerId);
296 *outLayer = layer.get();
297 mLayers.emplace(layerId, std::move(layer));
298 return Error::None;
299}
300
301Error Display::destroyLayer(Layer* layer)
302{
303 if (!layer) {
304 return Error::BadParameter;
305 }
306 mLayers.erase(layer->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700307 return Error::None;
308}
309
310Error Display::getActiveConfig(
311 std::shared_ptr<const Display::Config>* outConfig) const
312{
313 ALOGV("[%" PRIu64 "] getActiveConfig", mId);
314 hwc2_config_t configId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700315 auto intError = mComposer.getActiveConfig(mId, &configId);
Dan Stoza651bf312015-10-23 17:03:17 -0700316 auto error = static_cast<Error>(intError);
317
318 if (error != Error::None) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800319 ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
320 *outConfig = nullptr;
Dan Stoza651bf312015-10-23 17:03:17 -0700321 return error;
322 }
323
324 if (mConfigs.count(configId) != 0) {
325 *outConfig = mConfigs.at(configId);
326 } else {
327 ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId,
328 configId);
329 // Return no error, but the caller needs to check for a null pointer to
330 // detect this case
331 *outConfig = nullptr;
332 }
333
334 return Error::None;
335}
336
337Error Display::getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700338 std::unordered_map<Layer*, Composition>* outTypes)
Dan Stoza651bf312015-10-23 17:03:17 -0700339{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800340 std::vector<Hwc2::Layer> layerIds;
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800341 std::vector<Hwc2::IComposerClient::Composition> types;
Steven Thomas94e35b92017-07-26 18:48:28 -0700342 auto intError = mComposer.getChangedCompositionTypes(
343 mId, &layerIds, &types);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800344 uint32_t numElements = layerIds.size();
345 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700346 error = static_cast<Error>(intError);
347 if (error != Error::None) {
348 return error;
349 }
350
351 outTypes->clear();
352 outTypes->reserve(numElements);
353 for (uint32_t element = 0; element < numElements; ++element) {
354 auto layer = getLayerById(layerIds[element]);
355 if (layer) {
356 auto type = static_cast<Composition>(types[element]);
357 ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
358 layer->getId(), to_string(type).c_str());
359 outTypes->emplace(layer, type);
360 } else {
361 ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
362 " on display %" PRIu64, layerIds[element], mId);
363 }
364 }
365
366 return Error::None;
367}
368
Michael Wright28f24d02016-07-12 13:30:53 -0700369Error Display::getColorModes(std::vector<android_color_mode_t>* outModes) const
Dan Stoza076ac672016-03-14 10:47:53 -0700370{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800371 std::vector<Hwc2::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700372 auto intError = mComposer.getColorModes(mId, &modes);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800373 uint32_t numModes = modes.size();
374 auto error = static_cast<Error>(intError);
Dan Stoza076ac672016-03-14 10:47:53 -0700375 if (error != Error::None) {
376 return error;
377 }
378
Michael Wright28f24d02016-07-12 13:30:53 -0700379 outModes->resize(numModes);
380 for (size_t i = 0; i < numModes; i++) {
381 (*outModes)[i] = static_cast<android_color_mode_t>(modes[i]);
382 }
Dan Stoza076ac672016-03-14 10:47:53 -0700383 return Error::None;
384}
385
Dan Stoza651bf312015-10-23 17:03:17 -0700386std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const
387{
388 std::vector<std::shared_ptr<const Config>> configs;
389 for (const auto& element : mConfigs) {
390 configs.emplace_back(element.second);
391 }
392 return configs;
393}
394
395Error Display::getName(std::string* outName) const
396{
Steven Thomas94e35b92017-07-26 18:48:28 -0700397 auto intError = mComposer.getDisplayName(mId, outName);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800398 return static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700399}
400
401Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700402 std::unordered_map<Layer*, LayerRequest>* outLayerRequests)
Dan Stoza651bf312015-10-23 17:03:17 -0700403{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800404 uint32_t intDisplayRequests;
405 std::vector<Hwc2::Layer> layerIds;
406 std::vector<uint32_t> layerRequests;
Steven Thomas94e35b92017-07-26 18:48:28 -0700407 auto intError = mComposer.getDisplayRequests(
408 mId, &intDisplayRequests, &layerIds, &layerRequests);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800409 uint32_t numElements = layerIds.size();
410 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700411 if (error != Error::None) {
412 return error;
413 }
414
415 *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
416 outLayerRequests->clear();
417 outLayerRequests->reserve(numElements);
418 for (uint32_t element = 0; element < numElements; ++element) {
419 auto layer = getLayerById(layerIds[element]);
420 if (layer) {
421 auto layerRequest =
422 static_cast<LayerRequest>(layerRequests[element]);
423 outLayerRequests->emplace(layer, layerRequest);
424 } else {
425 ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
426 PRIu64, layerIds[element], mId);
427 }
428 }
429
430 return Error::None;
431}
432
433Error Display::getType(DisplayType* outType) const
434{
Chris Forbes016d73c2017-04-11 10:04:31 -0700435 *outType = mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700436 return Error::None;
437}
438
439Error Display::supportsDoze(bool* outSupport) const
440{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800441 bool intSupport = false;
Steven Thomas94e35b92017-07-26 18:48:28 -0700442 auto intError = mComposer.getDozeSupport(mId, &intSupport);
Dan Stoza651bf312015-10-23 17:03:17 -0700443 auto error = static_cast<Error>(intError);
444 if (error != Error::None) {
445 return error;
446 }
447 *outSupport = static_cast<bool>(intSupport);
448 return Error::None;
449}
450
Dan Stoza7d7ae732016-03-16 12:23:40 -0700451Error Display::getHdrCapabilities(
452 std::unique_ptr<HdrCapabilities>* outCapabilities) const
453{
Dan Stoza7d7ae732016-03-16 12:23:40 -0700454 float maxLuminance = -1.0f;
455 float maxAverageLuminance = -1.0f;
456 float minLuminance = -1.0f;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800457 std::vector<Hwc2::Hdr> intTypes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700458 auto intError = mComposer.getHdrCapabilities(mId, &intTypes,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800459 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800460 auto error = static_cast<HWC2::Error>(intError);
461
462 std::vector<int32_t> types;
463 for (auto type : intTypes) {
464 types.push_back(static_cast<int32_t>(type));
465 }
Dan Stoza7d7ae732016-03-16 12:23:40 -0700466 if (error != Error::None) {
467 return error;
468 }
469
470 *outCapabilities = std::make_unique<HdrCapabilities>(std::move(types),
471 maxLuminance, maxAverageLuminance, minLuminance);
472 return Error::None;
473}
474
Dan Stoza651bf312015-10-23 17:03:17 -0700475Error Display::getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700476 std::unordered_map<Layer*, sp<Fence>>* outFences) const
Dan Stoza651bf312015-10-23 17:03:17 -0700477{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800478 std::vector<Hwc2::Layer> layerIds;
479 std::vector<int> fenceFds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700480 auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800481 auto error = static_cast<Error>(intError);
482 uint32_t numElements = layerIds.size();
Dan Stoza651bf312015-10-23 17:03:17 -0700483 if (error != Error::None) {
484 return error;
485 }
486
Steven Thomas94e35b92017-07-26 18:48:28 -0700487 std::unordered_map<Layer*, sp<Fence>> releaseFences;
Dan Stoza651bf312015-10-23 17:03:17 -0700488 releaseFences.reserve(numElements);
489 for (uint32_t element = 0; element < numElements; ++element) {
490 auto layer = getLayerById(layerIds[element]);
491 if (layer) {
492 sp<Fence> fence(new Fence(fenceFds[element]));
Steven Thomas94e35b92017-07-26 18:48:28 -0700493 releaseFences.emplace(layer, fence);
Dan Stoza651bf312015-10-23 17:03:17 -0700494 } else {
495 ALOGE("getReleaseFences: invalid layer %" PRIu64
496 " found on display %" PRIu64, layerIds[element], mId);
Chia-I Wu5e74c652017-05-17 13:43:16 -0700497 for (; element < numElements; ++element) {
498 close(fenceFds[element]);
499 }
Dan Stoza651bf312015-10-23 17:03:17 -0700500 return Error::BadLayer;
501 }
502 }
503
504 *outFences = std::move(releaseFences);
505 return Error::None;
506}
507
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800508Error Display::present(sp<Fence>* outPresentFence)
Dan Stoza651bf312015-10-23 17:03:17 -0700509{
Naseer Ahmed847650b2016-06-17 11:14:25 -0400510 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700511 auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700512 auto error = static_cast<Error>(intError);
513 if (error != Error::None) {
514 return error;
515 }
516
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800517 *outPresentFence = new Fence(presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700518 return Error::None;
519}
520
521Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
522{
523 if (config->getDisplayId() != mId) {
524 ALOGE("setActiveConfig received config %u for the wrong display %"
525 PRIu64 " (expected %" PRIu64 ")", config->getId(),
526 config->getDisplayId(), mId);
527 return Error::BadConfig;
528 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700529 auto intError = mComposer.setActiveConfig(mId, config->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700530 return static_cast<Error>(intError);
531}
532
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400533Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700534 const sp<Fence>& acquireFence, android_dataspace_t dataspace)
535{
Dan Stoza5cf424b2016-05-20 14:02:39 -0700536 // TODO: Properly encode client target surface damage
Dan Stoza651bf312015-10-23 17:03:17 -0700537 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700538 auto intError = mComposer.setClientTarget(mId, slot, target,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800539 fenceFd, static_cast<Hwc2::Dataspace>(dataspace),
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800540 std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700541 return static_cast<Error>(intError);
542}
543
Michael Wright28f24d02016-07-12 13:30:53 -0700544Error Display::setColorMode(android_color_mode_t mode)
Dan Stoza076ac672016-03-14 10:47:53 -0700545{
Steven Thomas94e35b92017-07-26 18:48:28 -0700546 auto intError = mComposer.setColorMode(
547 mId, static_cast<Hwc2::ColorMode>(mode));
Dan Stoza076ac672016-03-14 10:47:53 -0700548 return static_cast<Error>(intError);
549}
550
Dan Stoza5df2a862016-03-24 16:19:37 -0700551Error Display::setColorTransform(const android::mat4& matrix,
552 android_color_transform_t hint)
553{
Steven Thomas94e35b92017-07-26 18:48:28 -0700554 auto intError = mComposer.setColorTransform(mId,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800555 matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
Dan Stoza5df2a862016-03-24 16:19:37 -0700556 return static_cast<Error>(intError);
557}
558
Dan Stoza651bf312015-10-23 17:03:17 -0700559Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
560 const sp<Fence>& releaseFence)
561{
562 int32_t fenceFd = releaseFence->dup();
563 auto handle = buffer->getNativeBuffer()->handle;
Steven Thomas94e35b92017-07-26 18:48:28 -0700564 auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
Dan Stoza38628982016-07-13 15:48:58 -0700565 close(fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700566 return static_cast<Error>(intError);
567}
568
569Error Display::setPowerMode(PowerMode mode)
570{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800571 auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700572 auto intError = mComposer.setPowerMode(mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700573 return static_cast<Error>(intError);
574}
575
576Error Display::setVsyncEnabled(Vsync enabled)
577{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800578 auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
Steven Thomas94e35b92017-07-26 18:48:28 -0700579 auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
Dan Stoza651bf312015-10-23 17:03:17 -0700580 return static_cast<Error>(intError);
581}
582
583Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
584{
585 uint32_t numTypes = 0;
586 uint32_t numRequests = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700587 auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700588 auto error = static_cast<Error>(intError);
589 if (error != Error::None && error != Error::HasChanges) {
590 return error;
591 }
592
593 *outNumTypes = numTypes;
594 *outNumRequests = numRequests;
595 return error;
596}
597
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700598Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests,
599 sp<android::Fence>* outPresentFence, uint32_t* state) {
600
601 uint32_t numTypes = 0;
602 uint32_t numRequests = 0;
603 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700604 auto intError = mComposer.presentOrValidateDisplay(
605 mId, &numTypes, &numRequests, &presentFenceFd, state);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700606 auto error = static_cast<Error>(intError);
607 if (error != Error::None && error != Error::HasChanges) {
608 return error;
609 }
610
611 if (*state == 1) {
612 *outPresentFence = new Fence(presentFenceFd);
613 }
614
615 if (*state == 0) {
616 *outNumTypes = numTypes;
617 *outNumRequests = numRequests;
618 }
619 return error;
620}
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700621
Dan Stoza651bf312015-10-23 17:03:17 -0700622// For use by Device
623
Steven Thomas94e35b92017-07-26 18:48:28 -0700624void Display::setConnected(bool connected) {
625 if (!mIsConnected && connected && mType == DisplayType::Physical) {
626 mComposer.setClientTargetSlotCount(mId);
627 loadConfigs();
628 }
629 mIsConnected = connected;
630}
631
Dan Stoza651bf312015-10-23 17:03:17 -0700632int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute)
633{
634 int32_t value = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700635 auto intError = mComposer.getDisplayAttribute(mId, configId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800636 static_cast<Hwc2::IComposerClient::Attribute>(attribute),
637 &value);
Dan Stoza651bf312015-10-23 17:03:17 -0700638 auto error = static_cast<Error>(intError);
639 if (error != Error::None) {
640 ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId,
641 configId, to_string(attribute).c_str(),
642 to_string(error).c_str(), intError);
643 return -1;
644 }
645 return value;
646}
647
648void Display::loadConfig(hwc2_config_t configId)
649{
650 ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId);
651
652 auto config = Config::Builder(*this, configId)
653 .setWidth(getAttribute(configId, Attribute::Width))
654 .setHeight(getAttribute(configId, Attribute::Height))
655 .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod))
656 .setDpiX(getAttribute(configId, Attribute::DpiX))
657 .setDpiY(getAttribute(configId, Attribute::DpiY))
658 .build();
659 mConfigs.emplace(configId, std::move(config));
660}
661
662void Display::loadConfigs()
663{
664 ALOGV("[%" PRIu64 "] loadConfigs", mId);
665
Chia-I Wuaab99f52016-10-05 12:59:58 +0800666 std::vector<Hwc2::Config> configIds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700667 auto intError = mComposer.getDisplayConfigs(mId, &configIds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800668 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700669 if (error != Error::None) {
670 ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId,
671 to_string(error).c_str(), intError);
672 return;
673 }
674
675 for (auto configId : configIds) {
676 loadConfig(configId);
677 }
678}
679
Dan Stoza651bf312015-10-23 17:03:17 -0700680// Other Display methods
681
Steven Thomas94e35b92017-07-26 18:48:28 -0700682Layer* Display::getLayerById(hwc2_layer_t id) const
Dan Stoza651bf312015-10-23 17:03:17 -0700683{
684 if (mLayers.count(id) == 0) {
685 return nullptr;
686 }
687
Steven Thomas94e35b92017-07-26 18:48:28 -0700688 return mLayers.at(id).get();
Dan Stoza651bf312015-10-23 17:03:17 -0700689}
690
691// Layer methods
692
Steven Thomas94e35b92017-07-26 18:48:28 -0700693Layer::Layer(android::Hwc2::Composer& composer,
694 const std::unordered_set<Capability>& capabilities,
695 hwc2_display_t displayId, hwc2_layer_t layerId)
696 : mComposer(composer),
697 mCapabilities(capabilities),
698 mDisplayId(displayId),
699 mId(layerId)
Dan Stoza651bf312015-10-23 17:03:17 -0700700{
Steven Thomas94e35b92017-07-26 18:48:28 -0700701 ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700702}
703
704Layer::~Layer()
705{
Steven Thomas94e35b92017-07-26 18:48:28 -0700706 auto intError = mComposer.destroyLayer(mDisplayId, mId);
707 auto error = static_cast<Error>(intError);
708 ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
709 " failed: %s (%d)", mDisplayId, mId, to_string(error).c_str(),
710 intError);
711 if (mLayerDestroyedListener) {
712 mLayerDestroyedListener(this);
Dan Stoza651bf312015-10-23 17:03:17 -0700713 }
714}
715
Steven Thomas94e35b92017-07-26 18:48:28 -0700716void Layer::setLayerDestroyedListener(std::function<void(Layer*)> listener) {
717 LOG_ALWAYS_FATAL_IF(mLayerDestroyedListener && listener,
718 "Attempt to set layer destroyed listener multiple times");
719 mLayerDestroyedListener = listener;
720}
721
Dan Stoza651bf312015-10-23 17:03:17 -0700722Error Layer::setCursorPosition(int32_t x, int32_t y)
723{
Steven Thomas94e35b92017-07-26 18:48:28 -0700724 auto intError = mComposer.setCursorPosition(mDisplayId, mId, x, y);
Dan Stoza651bf312015-10-23 17:03:17 -0700725 return static_cast<Error>(intError);
726}
727
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400728Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700729 const sp<Fence>& acquireFence)
730{
731 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700732 auto intError = mComposer.setLayerBuffer(mDisplayId, mId, slot, buffer,
733 fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700734 return static_cast<Error>(intError);
735}
736
737Error Layer::setSurfaceDamage(const Region& damage)
738{
739 // We encode default full-screen damage as INVALID_RECT upstream, but as 0
740 // rects for HWC
Chia-I Wuaab99f52016-10-05 12:59:58 +0800741 Hwc2::Error intError = Hwc2::Error::NONE;
Dan Stoza651bf312015-10-23 17:03:17 -0700742 if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700743 intError = mComposer.setLayerSurfaceDamage(mDisplayId,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800744 mId, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700745 } else {
746 size_t rectCount = 0;
747 auto rectArray = damage.getArray(&rectCount);
748
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800749 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700750 for (size_t rect = 0; rect < rectCount; ++rect) {
751 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
752 rectArray[rect].right, rectArray[rect].bottom});
753 }
754
Steven Thomas94e35b92017-07-26 18:48:28 -0700755 intError = mComposer.setLayerSurfaceDamage(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700756 }
757
758 return static_cast<Error>(intError);
759}
760
761Error Layer::setBlendMode(BlendMode mode)
762{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800763 auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700764 auto intError = mComposer.setLayerBlendMode(mDisplayId, mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700765 return static_cast<Error>(intError);
766}
767
768Error Layer::setColor(hwc_color_t color)
769{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800770 Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
Steven Thomas94e35b92017-07-26 18:48:28 -0700771 auto intError = mComposer.setLayerColor(mDisplayId, mId, hwcColor);
Dan Stoza651bf312015-10-23 17:03:17 -0700772 return static_cast<Error>(intError);
773}
774
775Error Layer::setCompositionType(Composition type)
776{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800777 auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
Steven Thomas94e35b92017-07-26 18:48:28 -0700778 auto intError = mComposer.setLayerCompositionType(
779 mDisplayId, mId, intType);
Dan Stoza651bf312015-10-23 17:03:17 -0700780 return static_cast<Error>(intError);
781}
782
Dan Stoza5df2a862016-03-24 16:19:37 -0700783Error Layer::setDataspace(android_dataspace_t dataspace)
784{
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600785 if (dataspace == mDataSpace) {
786 return Error::None;
787 }
788 mDataSpace = dataspace;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800789 auto intDataspace = static_cast<Hwc2::Dataspace>(dataspace);
Steven Thomas94e35b92017-07-26 18:48:28 -0700790 auto intError = mComposer.setLayerDataspace(mDisplayId, mId, intDataspace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700791 return static_cast<Error>(intError);
792}
793
Dan Stoza651bf312015-10-23 17:03:17 -0700794Error Layer::setDisplayFrame(const Rect& frame)
795{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800796 Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800797 frame.right, frame.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700798 auto intError = mComposer.setLayerDisplayFrame(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700799 return static_cast<Error>(intError);
800}
801
802Error Layer::setPlaneAlpha(float alpha)
803{
Steven Thomas94e35b92017-07-26 18:48:28 -0700804 auto intError = mComposer.setLayerPlaneAlpha(mDisplayId, mId, alpha);
Dan Stoza651bf312015-10-23 17:03:17 -0700805 return static_cast<Error>(intError);
806}
807
808Error Layer::setSidebandStream(const native_handle_t* stream)
809{
Steven Thomas94e35b92017-07-26 18:48:28 -0700810 if (mCapabilities.count(Capability::SidebandStream) == 0) {
Dan Stoza09e7a272016-04-14 12:31:01 -0700811 ALOGE("Attempted to call setSidebandStream without checking that the "
812 "device supports sideband streams");
813 return Error::Unsupported;
814 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700815 auto intError = mComposer.setLayerSidebandStream(mDisplayId, mId, stream);
Dan Stoza651bf312015-10-23 17:03:17 -0700816 return static_cast<Error>(intError);
817}
818
819Error Layer::setSourceCrop(const FloatRect& crop)
820{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800821 Hwc2::IComposerClient::FRect hwcRect{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800822 crop.left, crop.top, crop.right, crop.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700823 auto intError = mComposer.setLayerSourceCrop(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700824 return static_cast<Error>(intError);
825}
826
827Error Layer::setTransform(Transform transform)
828{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800829 auto intTransform = static_cast<Hwc2::Transform>(transform);
Steven Thomas94e35b92017-07-26 18:48:28 -0700830 auto intError = mComposer.setLayerTransform(mDisplayId, mId, intTransform);
Dan Stoza651bf312015-10-23 17:03:17 -0700831 return static_cast<Error>(intError);
832}
833
834Error Layer::setVisibleRegion(const Region& region)
835{
836 size_t rectCount = 0;
837 auto rectArray = region.getArray(&rectCount);
838
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800839 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700840 for (size_t rect = 0; rect < rectCount; ++rect) {
841 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
842 rectArray[rect].right, rectArray[rect].bottom});
843 }
844
Steven Thomas94e35b92017-07-26 18:48:28 -0700845 auto intError = mComposer.setLayerVisibleRegion(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700846 return static_cast<Error>(intError);
847}
848
849Error Layer::setZOrder(uint32_t z)
850{
Steven Thomas94e35b92017-07-26 18:48:28 -0700851 auto intError = mComposer.setLayerZOrder(mDisplayId, mId, z);
Dan Stoza651bf312015-10-23 17:03:17 -0700852 return static_cast<Error>(intError);
853}
854
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500855Error Layer::setInfo(uint32_t type, uint32_t appId)
856{
Steven Thomas94e35b92017-07-26 18:48:28 -0700857 auto intError = mComposer.setLayerInfo(mDisplayId, mId, type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500858 return static_cast<Error>(intError);
859}
860
Dan Stoza651bf312015-10-23 17:03:17 -0700861} // namespace HWC2