blob: ab4a4b23bcbe68799e5223c03922e43dc099ec26 [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)
55 : mCallback(callback), mSequenceId(sequenceId),
56 mHasPrimaryDisplay(false) {}
57
58 Return<void> onHotplug(Hwc2::Display display,
59 IComposerCallback::Connection conn) override
60 {
61 HWC2::Connection connection = static_cast<HWC2::Connection>(conn);
62 if (!mHasPrimaryDisplay) {
63 LOG_ALWAYS_FATAL_IF(connection != HWC2::Connection::Connected,
64 "Initial onHotplug callback should be "
65 "primary display connected");
66 mHasPrimaryDisplay = true;
67 mCallback->onHotplugReceived(mSequenceId, display,
68 connection, true);
69 } else {
70 mCallback->onHotplugReceived(mSequenceId, display,
71 connection, false);
72 }
73 return Void();
74 }
75
76 Return<void> onRefresh(Hwc2::Display display) override
77 {
78 mCallback->onRefreshReceived(mSequenceId, display);
79 return Void();
80 }
81
82 Return<void> onVsync(Hwc2::Display display, int64_t timestamp) override
83 {
84 mCallback->onVsyncReceived(mSequenceId, display, timestamp);
85 return Void();
86 }
87
88 bool HasPrimaryDisplay() { return mHasPrimaryDisplay; }
89
90private:
91 ComposerCallback* mCallback;
92 int32_t mSequenceId;
93 bool mHasPrimaryDisplay;
94};
95
96} // namespace anonymous
97
98
Dan Stoza651bf312015-10-23 17:03:17 -070099// Device methods
100
Kalle Raitaa099a242017-01-11 11:17:29 -0800101Device::Device(const std::string& serviceName)
102 : mComposer(std::make_unique<Hwc2::Composer>(serviceName)),
Dan Stoza651bf312015-10-23 17:03:17 -0700103 mCapabilities(),
104 mDisplays(),
Steven Thomas94e35b92017-07-26 18:48:28 -0700105 mRegisteredCallback(false)
Dan Stoza651bf312015-10-23 17:03:17 -0700106{
107 loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700108}
109
Steven Thomas94e35b92017-07-26 18:48:28 -0700110void Device::registerCallback(ComposerCallback* callback, int32_t sequenceId) {
111 if (mRegisteredCallback) {
112 ALOGW("Callback already registered. Ignored extra registration "
113 "attempt.");
114 return;
Dan Stoza651bf312015-10-23 17:03:17 -0700115 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700116 mRegisteredCallback = true;
117 sp<ComposerCallbackBridge> callbackBridge(
118 new ComposerCallbackBridge(callback, sequenceId));
119 mComposer->registerCallback(callbackBridge);
120 LOG_ALWAYS_FATAL_IF(!callbackBridge->HasPrimaryDisplay(),
121 "Registered composer callback but didn't get primary display");
Dan Stoza651bf312015-10-23 17:03:17 -0700122}
123
124// Required by HWC2 device
125
126std::string Device::dump() const
127{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800128 return mComposer->dumpDebugInfo();
Dan Stoza651bf312015-10-23 17:03:17 -0700129}
130
131uint32_t Device::getMaxVirtualDisplayCount() const
132{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800133 return mComposer->getMaxVirtualDisplayCount();
Dan Stoza651bf312015-10-23 17:03:17 -0700134}
135
136Error Device::createVirtualDisplay(uint32_t width, uint32_t height,
Steven Thomas94e35b92017-07-26 18:48:28 -0700137 android_pixel_format_t* format, Display** outDisplay)
Dan Stoza651bf312015-10-23 17:03:17 -0700138{
139 ALOGI("Creating virtual display");
140
141 hwc2_display_t displayId = 0;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800142 auto intFormat = static_cast<Hwc2::PixelFormat>(*format);
143 auto intError = mComposer->createVirtualDisplay(width, height,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800144 &intFormat, &displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700145 auto error = static_cast<Error>(intError);
146 if (error != Error::None) {
147 return error;
148 }
149
Steven Thomas94e35b92017-07-26 18:48:28 -0700150 auto display = std::make_unique<Display>(
151 *mComposer.get(), mCapabilities, displayId, DisplayType::Virtual);
152 *outDisplay = display.get();
Dan Stoza5cf424b2016-05-20 14:02:39 -0700153 *format = static_cast<android_pixel_format_t>(intFormat);
Steven Thomas94e35b92017-07-26 18:48:28 -0700154 mDisplays.emplace(displayId, std::move(display));
155 ALOGI("Created virtual display");
Dan Stoza651bf312015-10-23 17:03:17 -0700156 return Error::None;
157}
158
Steven Thomas94e35b92017-07-26 18:48:28 -0700159void Device::destroyDisplay(hwc2_display_t displayId)
Dan Stoza651bf312015-10-23 17:03:17 -0700160{
Steven Thomas94e35b92017-07-26 18:48:28 -0700161 ALOGI("Destroying display %" PRIu64, displayId);
162 mDisplays.erase(displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700163}
164
Steven Thomas94e35b92017-07-26 18:48:28 -0700165void Device::onHotplug(hwc2_display_t displayId, Connection connection) {
166 if (connection == Connection::Connected) {
167 auto display = getDisplayById(displayId);
168 if (display) {
169 if (display->isConnected()) {
170 ALOGW("Attempt to hotplug connect display %" PRIu64
171 " , which is already connected.", displayId);
172 } else {
173 display->setConnected(true);
174 }
175 } else {
176 DisplayType displayType;
177 auto intError = mComposer->getDisplayType(displayId,
178 reinterpret_cast<Hwc2::IComposerClient::DisplayType *>(
179 &displayType));
180 auto error = static_cast<Error>(intError);
181 if (error != Error::None) {
182 ALOGE("getDisplayType(%" PRIu64 ") failed: %s (%d). "
183 "Aborting hotplug attempt.",
184 displayId, to_string(error).c_str(), intError);
185 return;
186 }
Dan Stoza651bf312015-10-23 17:03:17 -0700187
Steven Thomas94e35b92017-07-26 18:48:28 -0700188 auto newDisplay = std::make_unique<Display>(
189 *mComposer.get(), mCapabilities, displayId, displayType);
190 mDisplays.emplace(displayId, std::move(newDisplay));
Dan Stoza651bf312015-10-23 17:03:17 -0700191 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700192 } else if (connection == Connection::Disconnected) {
193 // The display will later be destroyed by a call to
194 // destroyDisplay(). For now we just mark it disconnected.
195 auto display = getDisplayById(displayId);
196 if (display) {
197 display->setConnected(false);
198 } else {
199 ALOGW("Attempted to disconnect unknown display %" PRIu64,
200 displayId);
201 }
Dan Stoza651bf312015-10-23 17:03:17 -0700202 }
203}
204
205// Other Device methods
206
Steven Thomas94e35b92017-07-26 18:48:28 -0700207Display* Device::getDisplayById(hwc2_display_t id) {
208 auto iter = mDisplays.find(id);
209 return iter == mDisplays.end() ? nullptr : iter->second.get();
Dan Stoza651bf312015-10-23 17:03:17 -0700210}
211
212// Device initialization methods
213
214void Device::loadCapabilities()
215{
216 static_assert(sizeof(Capability) == sizeof(int32_t),
217 "Capability size has changed");
Chia-I Wuaab99f52016-10-05 12:59:58 +0800218 auto capabilities = mComposer->getCapabilities();
219 for (auto capability : capabilities) {
220 mCapabilities.emplace(static_cast<Capability>(capability));
221 }
Dan Stoza651bf312015-10-23 17:03:17 -0700222}
223
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700224Error Device::flushCommands()
225{
226 return static_cast<Error>(mComposer->executeCommands());
227}
228
Dan Stoza651bf312015-10-23 17:03:17 -0700229// Display methods
230
Steven Thomas94e35b92017-07-26 18:48:28 -0700231Display::Display(android::Hwc2::Composer& composer,
232 const std::unordered_set<Capability>& capabilities,
233 hwc2_display_t id, DisplayType type)
234 : mComposer(composer),
235 mCapabilities(capabilities),
Dan Stoza651bf312015-10-23 17:03:17 -0700236 mId(id),
237 mIsConnected(false),
Steven Thomas94e35b92017-07-26 18:48:28 -0700238 mType(type)
Dan Stoza651bf312015-10-23 17:03:17 -0700239{
240 ALOGV("Created display %" PRIu64, id);
Steven Thomas94e35b92017-07-26 18:48:28 -0700241 setConnected(true);
Dan Stoza651bf312015-10-23 17:03:17 -0700242}
243
Steven Thomas94e35b92017-07-26 18:48:28 -0700244Display::~Display() {
245 mLayers.clear();
246
Chris Forbesceb67d12017-04-11 12:20:00 -0700247 if (mType == DisplayType::Virtual) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700248 ALOGV("Destroying virtual display");
249 auto intError = mComposer.destroyVirtualDisplay(mId);
250 auto error = static_cast<Error>(intError);
251 ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64
252 ") failed: %s (%d)", mId, to_string(error).c_str(), intError);
253 } else if (mType == DisplayType::Physical) {
254 auto error = setVsyncEnabled(HWC2::Vsync::Disable);
255 if (error != Error::None) {
256 ALOGE("~Display: Failed to disable vsync for display %" PRIu64
257 ": %s (%d)", mId, to_string(error).c_str(),
258 static_cast<int32_t>(error));
259 }
Dan Stoza651bf312015-10-23 17:03:17 -0700260 }
261}
262
263Display::Config::Config(Display& display, hwc2_config_t id)
264 : mDisplay(display),
265 mId(id),
266 mWidth(-1),
267 mHeight(-1),
268 mVsyncPeriod(-1),
269 mDpiX(-1),
270 mDpiY(-1) {}
271
272Display::Config::Builder::Builder(Display& display, hwc2_config_t id)
273 : mConfig(new Config(display, id)) {}
274
275float Display::Config::Builder::getDefaultDensity() {
276 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
277 // resolution displays get TV density. Maybe eventually we'll need to update
278 // it for 4k displays, though hopefully those will just report accurate DPI
279 // information to begin with. This is also used for virtual displays and
280 // older HWC implementations, so be careful about orientation.
281
282 auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight);
283 if (longDimension >= 1080) {
284 return ACONFIGURATION_DENSITY_XHIGH;
285 } else {
286 return ACONFIGURATION_DENSITY_TV;
287 }
288}
289
290// Required by HWC2 display
291
292Error Display::acceptChanges()
293{
Steven Thomas94e35b92017-07-26 18:48:28 -0700294 auto intError = mComposer.acceptDisplayChanges(mId);
Dan Stoza651bf312015-10-23 17:03:17 -0700295 return static_cast<Error>(intError);
296}
297
Steven Thomas94e35b92017-07-26 18:48:28 -0700298Error Display::createLayer(Layer** outLayer)
Dan Stoza651bf312015-10-23 17:03:17 -0700299{
Steven Thomas94e35b92017-07-26 18:48:28 -0700300 if (!outLayer) {
301 return Error::BadParameter;
302 }
Dan Stoza651bf312015-10-23 17:03:17 -0700303 hwc2_layer_t layerId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700304 auto intError = mComposer.createLayer(mId, &layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700305 auto error = static_cast<Error>(intError);
306 if (error != Error::None) {
307 return error;
308 }
309
Steven Thomas94e35b92017-07-26 18:48:28 -0700310 auto layer = std::make_unique<Layer>(
311 mComposer, mCapabilities, mId, layerId);
312 *outLayer = layer.get();
313 mLayers.emplace(layerId, std::move(layer));
314 return Error::None;
315}
316
317Error Display::destroyLayer(Layer* layer)
318{
319 if (!layer) {
320 return Error::BadParameter;
321 }
322 mLayers.erase(layer->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700323 return Error::None;
324}
325
326Error Display::getActiveConfig(
327 std::shared_ptr<const Display::Config>* outConfig) const
328{
329 ALOGV("[%" PRIu64 "] getActiveConfig", mId);
330 hwc2_config_t configId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700331 auto intError = mComposer.getActiveConfig(mId, &configId);
Dan Stoza651bf312015-10-23 17:03:17 -0700332 auto error = static_cast<Error>(intError);
333
334 if (error != Error::None) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800335 ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
336 *outConfig = nullptr;
Dan Stoza651bf312015-10-23 17:03:17 -0700337 return error;
338 }
339
340 if (mConfigs.count(configId) != 0) {
341 *outConfig = mConfigs.at(configId);
342 } else {
343 ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId,
344 configId);
345 // Return no error, but the caller needs to check for a null pointer to
346 // detect this case
347 *outConfig = nullptr;
348 }
349
350 return Error::None;
351}
352
353Error Display::getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700354 std::unordered_map<Layer*, Composition>* outTypes)
Dan Stoza651bf312015-10-23 17:03:17 -0700355{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800356 std::vector<Hwc2::Layer> layerIds;
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800357 std::vector<Hwc2::IComposerClient::Composition> types;
Steven Thomas94e35b92017-07-26 18:48:28 -0700358 auto intError = mComposer.getChangedCompositionTypes(
359 mId, &layerIds, &types);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800360 uint32_t numElements = layerIds.size();
361 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700362 error = static_cast<Error>(intError);
363 if (error != Error::None) {
364 return error;
365 }
366
367 outTypes->clear();
368 outTypes->reserve(numElements);
369 for (uint32_t element = 0; element < numElements; ++element) {
370 auto layer = getLayerById(layerIds[element]);
371 if (layer) {
372 auto type = static_cast<Composition>(types[element]);
373 ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
374 layer->getId(), to_string(type).c_str());
375 outTypes->emplace(layer, type);
376 } else {
377 ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
378 " on display %" PRIu64, layerIds[element], mId);
379 }
380 }
381
382 return Error::None;
383}
384
Michael Wright28f24d02016-07-12 13:30:53 -0700385Error Display::getColorModes(std::vector<android_color_mode_t>* outModes) const
Dan Stoza076ac672016-03-14 10:47:53 -0700386{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800387 std::vector<Hwc2::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700388 auto intError = mComposer.getColorModes(mId, &modes);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800389 uint32_t numModes = modes.size();
390 auto error = static_cast<Error>(intError);
Dan Stoza076ac672016-03-14 10:47:53 -0700391 if (error != Error::None) {
392 return error;
393 }
394
Michael Wright28f24d02016-07-12 13:30:53 -0700395 outModes->resize(numModes);
396 for (size_t i = 0; i < numModes; i++) {
397 (*outModes)[i] = static_cast<android_color_mode_t>(modes[i]);
398 }
Dan Stoza076ac672016-03-14 10:47:53 -0700399 return Error::None;
400}
401
Dan Stoza651bf312015-10-23 17:03:17 -0700402std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const
403{
404 std::vector<std::shared_ptr<const Config>> configs;
405 for (const auto& element : mConfigs) {
406 configs.emplace_back(element.second);
407 }
408 return configs;
409}
410
411Error Display::getName(std::string* outName) const
412{
Steven Thomas94e35b92017-07-26 18:48:28 -0700413 auto intError = mComposer.getDisplayName(mId, outName);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800414 return static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700415}
416
417Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700418 std::unordered_map<Layer*, LayerRequest>* outLayerRequests)
Dan Stoza651bf312015-10-23 17:03:17 -0700419{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800420 uint32_t intDisplayRequests;
421 std::vector<Hwc2::Layer> layerIds;
422 std::vector<uint32_t> layerRequests;
Steven Thomas94e35b92017-07-26 18:48:28 -0700423 auto intError = mComposer.getDisplayRequests(
424 mId, &intDisplayRequests, &layerIds, &layerRequests);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800425 uint32_t numElements = layerIds.size();
426 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700427 if (error != Error::None) {
428 return error;
429 }
430
431 *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
432 outLayerRequests->clear();
433 outLayerRequests->reserve(numElements);
434 for (uint32_t element = 0; element < numElements; ++element) {
435 auto layer = getLayerById(layerIds[element]);
436 if (layer) {
437 auto layerRequest =
438 static_cast<LayerRequest>(layerRequests[element]);
439 outLayerRequests->emplace(layer, layerRequest);
440 } else {
441 ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
442 PRIu64, layerIds[element], mId);
443 }
444 }
445
446 return Error::None;
447}
448
449Error Display::getType(DisplayType* outType) const
450{
Chris Forbes016d73c2017-04-11 10:04:31 -0700451 *outType = mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700452 return Error::None;
453}
454
455Error Display::supportsDoze(bool* outSupport) const
456{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800457 bool intSupport = false;
Steven Thomas94e35b92017-07-26 18:48:28 -0700458 auto intError = mComposer.getDozeSupport(mId, &intSupport);
Dan Stoza651bf312015-10-23 17:03:17 -0700459 auto error = static_cast<Error>(intError);
460 if (error != Error::None) {
461 return error;
462 }
463 *outSupport = static_cast<bool>(intSupport);
464 return Error::None;
465}
466
Dan Stoza7d7ae732016-03-16 12:23:40 -0700467Error Display::getHdrCapabilities(
468 std::unique_ptr<HdrCapabilities>* outCapabilities) const
469{
470 uint32_t numTypes = 0;
471 float maxLuminance = -1.0f;
472 float maxAverageLuminance = -1.0f;
473 float minLuminance = -1.0f;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800474 std::vector<Hwc2::Hdr> intTypes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700475 auto intError = mComposer.getHdrCapabilities(mId, &intTypes,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800476 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800477 auto error = static_cast<HWC2::Error>(intError);
478
479 std::vector<int32_t> types;
480 for (auto type : intTypes) {
481 types.push_back(static_cast<int32_t>(type));
482 }
483 numTypes = types.size();
Dan Stoza7d7ae732016-03-16 12:23:40 -0700484 if (error != Error::None) {
485 return error;
486 }
487
488 *outCapabilities = std::make_unique<HdrCapabilities>(std::move(types),
489 maxLuminance, maxAverageLuminance, minLuminance);
490 return Error::None;
491}
492
Dan Stoza651bf312015-10-23 17:03:17 -0700493Error Display::getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700494 std::unordered_map<Layer*, sp<Fence>>* outFences) const
Dan Stoza651bf312015-10-23 17:03:17 -0700495{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800496 std::vector<Hwc2::Layer> layerIds;
497 std::vector<int> fenceFds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700498 auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800499 auto error = static_cast<Error>(intError);
500 uint32_t numElements = layerIds.size();
Dan Stoza651bf312015-10-23 17:03:17 -0700501 if (error != Error::None) {
502 return error;
503 }
504
Steven Thomas94e35b92017-07-26 18:48:28 -0700505 std::unordered_map<Layer*, sp<Fence>> releaseFences;
Dan Stoza651bf312015-10-23 17:03:17 -0700506 releaseFences.reserve(numElements);
507 for (uint32_t element = 0; element < numElements; ++element) {
508 auto layer = getLayerById(layerIds[element]);
509 if (layer) {
510 sp<Fence> fence(new Fence(fenceFds[element]));
Steven Thomas94e35b92017-07-26 18:48:28 -0700511 releaseFences.emplace(layer, fence);
Dan Stoza651bf312015-10-23 17:03:17 -0700512 } else {
513 ALOGE("getReleaseFences: invalid layer %" PRIu64
514 " found on display %" PRIu64, layerIds[element], mId);
Chia-I Wu5e74c652017-05-17 13:43:16 -0700515 for (; element < numElements; ++element) {
516 close(fenceFds[element]);
517 }
Dan Stoza651bf312015-10-23 17:03:17 -0700518 return Error::BadLayer;
519 }
520 }
521
522 *outFences = std::move(releaseFences);
523 return Error::None;
524}
525
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800526Error Display::present(sp<Fence>* outPresentFence)
Dan Stoza651bf312015-10-23 17:03:17 -0700527{
Naseer Ahmed847650b2016-06-17 11:14:25 -0400528 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700529 auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700530 auto error = static_cast<Error>(intError);
531 if (error != Error::None) {
532 return error;
533 }
534
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800535 *outPresentFence = new Fence(presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700536 return Error::None;
537}
538
539Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
540{
541 if (config->getDisplayId() != mId) {
542 ALOGE("setActiveConfig received config %u for the wrong display %"
543 PRIu64 " (expected %" PRIu64 ")", config->getId(),
544 config->getDisplayId(), mId);
545 return Error::BadConfig;
546 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700547 auto intError = mComposer.setActiveConfig(mId, config->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700548 return static_cast<Error>(intError);
549}
550
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400551Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700552 const sp<Fence>& acquireFence, android_dataspace_t dataspace)
553{
Dan Stoza5cf424b2016-05-20 14:02:39 -0700554 // TODO: Properly encode client target surface damage
Dan Stoza651bf312015-10-23 17:03:17 -0700555 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700556 auto intError = mComposer.setClientTarget(mId, slot, target,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800557 fenceFd, static_cast<Hwc2::Dataspace>(dataspace),
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800558 std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700559 return static_cast<Error>(intError);
560}
561
Michael Wright28f24d02016-07-12 13:30:53 -0700562Error Display::setColorMode(android_color_mode_t mode)
Dan Stoza076ac672016-03-14 10:47:53 -0700563{
Steven Thomas94e35b92017-07-26 18:48:28 -0700564 auto intError = mComposer.setColorMode(
565 mId, static_cast<Hwc2::ColorMode>(mode));
Dan Stoza076ac672016-03-14 10:47:53 -0700566 return static_cast<Error>(intError);
567}
568
Dan Stoza5df2a862016-03-24 16:19:37 -0700569Error Display::setColorTransform(const android::mat4& matrix,
570 android_color_transform_t hint)
571{
Steven Thomas94e35b92017-07-26 18:48:28 -0700572 auto intError = mComposer.setColorTransform(mId,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800573 matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
Dan Stoza5df2a862016-03-24 16:19:37 -0700574 return static_cast<Error>(intError);
575}
576
Dan Stoza651bf312015-10-23 17:03:17 -0700577Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
578 const sp<Fence>& releaseFence)
579{
580 int32_t fenceFd = releaseFence->dup();
581 auto handle = buffer->getNativeBuffer()->handle;
Steven Thomas94e35b92017-07-26 18:48:28 -0700582 auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
Dan Stoza38628982016-07-13 15:48:58 -0700583 close(fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700584 return static_cast<Error>(intError);
585}
586
587Error Display::setPowerMode(PowerMode mode)
588{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800589 auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700590 auto intError = mComposer.setPowerMode(mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700591 return static_cast<Error>(intError);
592}
593
594Error Display::setVsyncEnabled(Vsync enabled)
595{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800596 auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
Steven Thomas94e35b92017-07-26 18:48:28 -0700597 auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
Dan Stoza651bf312015-10-23 17:03:17 -0700598 return static_cast<Error>(intError);
599}
600
601Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
602{
603 uint32_t numTypes = 0;
604 uint32_t numRequests = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700605 auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700606 auto error = static_cast<Error>(intError);
607 if (error != Error::None && error != Error::HasChanges) {
608 return error;
609 }
610
611 *outNumTypes = numTypes;
612 *outNumRequests = numRequests;
613 return error;
614}
615
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700616Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests,
617 sp<android::Fence>* outPresentFence, uint32_t* state) {
618
619 uint32_t numTypes = 0;
620 uint32_t numRequests = 0;
621 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700622 auto intError = mComposer.presentOrValidateDisplay(
623 mId, &numTypes, &numRequests, &presentFenceFd, state);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700624 auto error = static_cast<Error>(intError);
625 if (error != Error::None && error != Error::HasChanges) {
626 return error;
627 }
628
629 if (*state == 1) {
630 *outPresentFence = new Fence(presentFenceFd);
631 }
632
633 if (*state == 0) {
634 *outNumTypes = numTypes;
635 *outNumRequests = numRequests;
636 }
637 return error;
638}
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700639
Dan Stoza651bf312015-10-23 17:03:17 -0700640// For use by Device
641
Steven Thomas94e35b92017-07-26 18:48:28 -0700642void Display::setConnected(bool connected) {
643 if (!mIsConnected && connected && mType == DisplayType::Physical) {
644 mComposer.setClientTargetSlotCount(mId);
645 loadConfigs();
646 }
647 mIsConnected = connected;
648}
649
Dan Stoza651bf312015-10-23 17:03:17 -0700650int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute)
651{
652 int32_t value = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700653 auto intError = mComposer.getDisplayAttribute(mId, configId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800654 static_cast<Hwc2::IComposerClient::Attribute>(attribute),
655 &value);
Dan Stoza651bf312015-10-23 17:03:17 -0700656 auto error = static_cast<Error>(intError);
657 if (error != Error::None) {
658 ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId,
659 configId, to_string(attribute).c_str(),
660 to_string(error).c_str(), intError);
661 return -1;
662 }
663 return value;
664}
665
666void Display::loadConfig(hwc2_config_t configId)
667{
668 ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId);
669
670 auto config = Config::Builder(*this, configId)
671 .setWidth(getAttribute(configId, Attribute::Width))
672 .setHeight(getAttribute(configId, Attribute::Height))
673 .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod))
674 .setDpiX(getAttribute(configId, Attribute::DpiX))
675 .setDpiY(getAttribute(configId, Attribute::DpiY))
676 .build();
677 mConfigs.emplace(configId, std::move(config));
678}
679
680void Display::loadConfigs()
681{
682 ALOGV("[%" PRIu64 "] loadConfigs", mId);
683
Chia-I Wuaab99f52016-10-05 12:59:58 +0800684 std::vector<Hwc2::Config> configIds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700685 auto intError = mComposer.getDisplayConfigs(mId, &configIds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800686 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700687 if (error != Error::None) {
688 ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId,
689 to_string(error).c_str(), intError);
690 return;
691 }
692
693 for (auto configId : configIds) {
694 loadConfig(configId);
695 }
696}
697
Dan Stoza651bf312015-10-23 17:03:17 -0700698// Other Display methods
699
Steven Thomas94e35b92017-07-26 18:48:28 -0700700Layer* Display::getLayerById(hwc2_layer_t id) const
Dan Stoza651bf312015-10-23 17:03:17 -0700701{
702 if (mLayers.count(id) == 0) {
703 return nullptr;
704 }
705
Steven Thomas94e35b92017-07-26 18:48:28 -0700706 return mLayers.at(id).get();
Dan Stoza651bf312015-10-23 17:03:17 -0700707}
708
709// Layer methods
710
Steven Thomas94e35b92017-07-26 18:48:28 -0700711Layer::Layer(android::Hwc2::Composer& composer,
712 const std::unordered_set<Capability>& capabilities,
713 hwc2_display_t displayId, hwc2_layer_t layerId)
714 : mComposer(composer),
715 mCapabilities(capabilities),
716 mDisplayId(displayId),
717 mId(layerId)
Dan Stoza651bf312015-10-23 17:03:17 -0700718{
Steven Thomas94e35b92017-07-26 18:48:28 -0700719 ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700720}
721
722Layer::~Layer()
723{
Steven Thomas94e35b92017-07-26 18:48:28 -0700724 auto intError = mComposer.destroyLayer(mDisplayId, mId);
725 auto error = static_cast<Error>(intError);
726 ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
727 " failed: %s (%d)", mDisplayId, mId, to_string(error).c_str(),
728 intError);
729 if (mLayerDestroyedListener) {
730 mLayerDestroyedListener(this);
Dan Stoza651bf312015-10-23 17:03:17 -0700731 }
732}
733
Steven Thomas94e35b92017-07-26 18:48:28 -0700734void Layer::setLayerDestroyedListener(std::function<void(Layer*)> listener) {
735 LOG_ALWAYS_FATAL_IF(mLayerDestroyedListener && listener,
736 "Attempt to set layer destroyed listener multiple times");
737 mLayerDestroyedListener = listener;
738}
739
Dan Stoza651bf312015-10-23 17:03:17 -0700740Error Layer::setCursorPosition(int32_t x, int32_t y)
741{
Steven Thomas94e35b92017-07-26 18:48:28 -0700742 auto intError = mComposer.setCursorPosition(mDisplayId, mId, x, y);
Dan Stoza651bf312015-10-23 17:03:17 -0700743 return static_cast<Error>(intError);
744}
745
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400746Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700747 const sp<Fence>& acquireFence)
748{
749 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700750 auto intError = mComposer.setLayerBuffer(mDisplayId, mId, slot, buffer,
751 fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700752 return static_cast<Error>(intError);
753}
754
755Error Layer::setSurfaceDamage(const Region& damage)
756{
757 // We encode default full-screen damage as INVALID_RECT upstream, but as 0
758 // rects for HWC
Chia-I Wuaab99f52016-10-05 12:59:58 +0800759 Hwc2::Error intError = Hwc2::Error::NONE;
Dan Stoza651bf312015-10-23 17:03:17 -0700760 if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700761 intError = mComposer.setLayerSurfaceDamage(mDisplayId,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800762 mId, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700763 } else {
764 size_t rectCount = 0;
765 auto rectArray = damage.getArray(&rectCount);
766
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800767 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700768 for (size_t rect = 0; rect < rectCount; ++rect) {
769 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
770 rectArray[rect].right, rectArray[rect].bottom});
771 }
772
Steven Thomas94e35b92017-07-26 18:48:28 -0700773 intError = mComposer.setLayerSurfaceDamage(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700774 }
775
776 return static_cast<Error>(intError);
777}
778
779Error Layer::setBlendMode(BlendMode mode)
780{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800781 auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700782 auto intError = mComposer.setLayerBlendMode(mDisplayId, mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700783 return static_cast<Error>(intError);
784}
785
786Error Layer::setColor(hwc_color_t color)
787{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800788 Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
Steven Thomas94e35b92017-07-26 18:48:28 -0700789 auto intError = mComposer.setLayerColor(mDisplayId, mId, hwcColor);
Dan Stoza651bf312015-10-23 17:03:17 -0700790 return static_cast<Error>(intError);
791}
792
793Error Layer::setCompositionType(Composition type)
794{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800795 auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
Steven Thomas94e35b92017-07-26 18:48:28 -0700796 auto intError = mComposer.setLayerCompositionType(
797 mDisplayId, mId, intType);
Dan Stoza651bf312015-10-23 17:03:17 -0700798 return static_cast<Error>(intError);
799}
800
Dan Stoza5df2a862016-03-24 16:19:37 -0700801Error Layer::setDataspace(android_dataspace_t dataspace)
802{
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600803 if (dataspace == mDataSpace) {
804 return Error::None;
805 }
806 mDataSpace = dataspace;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800807 auto intDataspace = static_cast<Hwc2::Dataspace>(dataspace);
Steven Thomas94e35b92017-07-26 18:48:28 -0700808 auto intError = mComposer.setLayerDataspace(mDisplayId, mId, intDataspace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700809 return static_cast<Error>(intError);
810}
811
Dan Stoza651bf312015-10-23 17:03:17 -0700812Error Layer::setDisplayFrame(const Rect& frame)
813{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800814 Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800815 frame.right, frame.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700816 auto intError = mComposer.setLayerDisplayFrame(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700817 return static_cast<Error>(intError);
818}
819
820Error Layer::setPlaneAlpha(float alpha)
821{
Steven Thomas94e35b92017-07-26 18:48:28 -0700822 auto intError = mComposer.setLayerPlaneAlpha(mDisplayId, mId, alpha);
Dan Stoza651bf312015-10-23 17:03:17 -0700823 return static_cast<Error>(intError);
824}
825
826Error Layer::setSidebandStream(const native_handle_t* stream)
827{
Steven Thomas94e35b92017-07-26 18:48:28 -0700828 if (mCapabilities.count(Capability::SidebandStream) == 0) {
Dan Stoza09e7a272016-04-14 12:31:01 -0700829 ALOGE("Attempted to call setSidebandStream without checking that the "
830 "device supports sideband streams");
831 return Error::Unsupported;
832 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700833 auto intError = mComposer.setLayerSidebandStream(mDisplayId, mId, stream);
Dan Stoza651bf312015-10-23 17:03:17 -0700834 return static_cast<Error>(intError);
835}
836
837Error Layer::setSourceCrop(const FloatRect& crop)
838{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800839 Hwc2::IComposerClient::FRect hwcRect{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800840 crop.left, crop.top, crop.right, crop.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700841 auto intError = mComposer.setLayerSourceCrop(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700842 return static_cast<Error>(intError);
843}
844
845Error Layer::setTransform(Transform transform)
846{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800847 auto intTransform = static_cast<Hwc2::Transform>(transform);
Steven Thomas94e35b92017-07-26 18:48:28 -0700848 auto intError = mComposer.setLayerTransform(mDisplayId, mId, intTransform);
Dan Stoza651bf312015-10-23 17:03:17 -0700849 return static_cast<Error>(intError);
850}
851
852Error Layer::setVisibleRegion(const Region& region)
853{
854 size_t rectCount = 0;
855 auto rectArray = region.getArray(&rectCount);
856
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800857 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700858 for (size_t rect = 0; rect < rectCount; ++rect) {
859 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
860 rectArray[rect].right, rectArray[rect].bottom});
861 }
862
Steven Thomas94e35b92017-07-26 18:48:28 -0700863 auto intError = mComposer.setLayerVisibleRegion(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700864 return static_cast<Error>(intError);
865}
866
867Error Layer::setZOrder(uint32_t z)
868{
Steven Thomas94e35b92017-07-26 18:48:28 -0700869 auto intError = mComposer.setLayerZOrder(mDisplayId, mId, z);
Dan Stoza651bf312015-10-23 17:03:17 -0700870 return static_cast<Error>(intError);
871}
872
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500873Error Layer::setInfo(uint32_t type, uint32_t appId)
874{
Steven Thomas94e35b92017-07-26 18:48:28 -0700875 auto intError = mComposer.setLayerInfo(mDisplayId, mId, type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500876 return static_cast<Error>(intError);
877}
878
Dan Stoza651bf312015-10-23 17:03:17 -0700879} // namespace HWC2