blob: affe505f272c4b6a2e01f2841049a8e6c50ed9eb [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{
Dan Stoza7d7ae732016-03-16 12:23:40 -0700470 float maxLuminance = -1.0f;
471 float maxAverageLuminance = -1.0f;
472 float minLuminance = -1.0f;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800473 std::vector<Hwc2::Hdr> intTypes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700474 auto intError = mComposer.getHdrCapabilities(mId, &intTypes,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800475 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800476 auto error = static_cast<HWC2::Error>(intError);
477
478 std::vector<int32_t> types;
479 for (auto type : intTypes) {
480 types.push_back(static_cast<int32_t>(type));
481 }
Dan Stoza7d7ae732016-03-16 12:23:40 -0700482 if (error != Error::None) {
483 return error;
484 }
485
486 *outCapabilities = std::make_unique<HdrCapabilities>(std::move(types),
487 maxLuminance, maxAverageLuminance, minLuminance);
488 return Error::None;
489}
490
Dan Stoza651bf312015-10-23 17:03:17 -0700491Error Display::getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700492 std::unordered_map<Layer*, sp<Fence>>* outFences) const
Dan Stoza651bf312015-10-23 17:03:17 -0700493{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800494 std::vector<Hwc2::Layer> layerIds;
495 std::vector<int> fenceFds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700496 auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800497 auto error = static_cast<Error>(intError);
498 uint32_t numElements = layerIds.size();
Dan Stoza651bf312015-10-23 17:03:17 -0700499 if (error != Error::None) {
500 return error;
501 }
502
Steven Thomas94e35b92017-07-26 18:48:28 -0700503 std::unordered_map<Layer*, sp<Fence>> releaseFences;
Dan Stoza651bf312015-10-23 17:03:17 -0700504 releaseFences.reserve(numElements);
505 for (uint32_t element = 0; element < numElements; ++element) {
506 auto layer = getLayerById(layerIds[element]);
507 if (layer) {
508 sp<Fence> fence(new Fence(fenceFds[element]));
Steven Thomas94e35b92017-07-26 18:48:28 -0700509 releaseFences.emplace(layer, fence);
Dan Stoza651bf312015-10-23 17:03:17 -0700510 } else {
511 ALOGE("getReleaseFences: invalid layer %" PRIu64
512 " found on display %" PRIu64, layerIds[element], mId);
Chia-I Wu5e74c652017-05-17 13:43:16 -0700513 for (; element < numElements; ++element) {
514 close(fenceFds[element]);
515 }
Dan Stoza651bf312015-10-23 17:03:17 -0700516 return Error::BadLayer;
517 }
518 }
519
520 *outFences = std::move(releaseFences);
521 return Error::None;
522}
523
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800524Error Display::present(sp<Fence>* outPresentFence)
Dan Stoza651bf312015-10-23 17:03:17 -0700525{
Naseer Ahmed847650b2016-06-17 11:14:25 -0400526 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700527 auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700528 auto error = static_cast<Error>(intError);
529 if (error != Error::None) {
530 return error;
531 }
532
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800533 *outPresentFence = new Fence(presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700534 return Error::None;
535}
536
537Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
538{
539 if (config->getDisplayId() != mId) {
540 ALOGE("setActiveConfig received config %u for the wrong display %"
541 PRIu64 " (expected %" PRIu64 ")", config->getId(),
542 config->getDisplayId(), mId);
543 return Error::BadConfig;
544 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700545 auto intError = mComposer.setActiveConfig(mId, config->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700546 return static_cast<Error>(intError);
547}
548
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400549Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700550 const sp<Fence>& acquireFence, android_dataspace_t dataspace)
551{
Dan Stoza5cf424b2016-05-20 14:02:39 -0700552 // TODO: Properly encode client target surface damage
Dan Stoza651bf312015-10-23 17:03:17 -0700553 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700554 auto intError = mComposer.setClientTarget(mId, slot, target,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800555 fenceFd, static_cast<Hwc2::Dataspace>(dataspace),
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800556 std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700557 return static_cast<Error>(intError);
558}
559
Michael Wright28f24d02016-07-12 13:30:53 -0700560Error Display::setColorMode(android_color_mode_t mode)
Dan Stoza076ac672016-03-14 10:47:53 -0700561{
Steven Thomas94e35b92017-07-26 18:48:28 -0700562 auto intError = mComposer.setColorMode(
563 mId, static_cast<Hwc2::ColorMode>(mode));
Dan Stoza076ac672016-03-14 10:47:53 -0700564 return static_cast<Error>(intError);
565}
566
Dan Stoza5df2a862016-03-24 16:19:37 -0700567Error Display::setColorTransform(const android::mat4& matrix,
568 android_color_transform_t hint)
569{
Steven Thomas94e35b92017-07-26 18:48:28 -0700570 auto intError = mComposer.setColorTransform(mId,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800571 matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
Dan Stoza5df2a862016-03-24 16:19:37 -0700572 return static_cast<Error>(intError);
573}
574
Dan Stoza651bf312015-10-23 17:03:17 -0700575Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
576 const sp<Fence>& releaseFence)
577{
578 int32_t fenceFd = releaseFence->dup();
579 auto handle = buffer->getNativeBuffer()->handle;
Steven Thomas94e35b92017-07-26 18:48:28 -0700580 auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
Dan Stoza38628982016-07-13 15:48:58 -0700581 close(fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700582 return static_cast<Error>(intError);
583}
584
585Error Display::setPowerMode(PowerMode mode)
586{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800587 auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700588 auto intError = mComposer.setPowerMode(mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700589 return static_cast<Error>(intError);
590}
591
592Error Display::setVsyncEnabled(Vsync enabled)
593{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800594 auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
Steven Thomas94e35b92017-07-26 18:48:28 -0700595 auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
Dan Stoza651bf312015-10-23 17:03:17 -0700596 return static_cast<Error>(intError);
597}
598
599Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
600{
601 uint32_t numTypes = 0;
602 uint32_t numRequests = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700603 auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700604 auto error = static_cast<Error>(intError);
605 if (error != Error::None && error != Error::HasChanges) {
606 return error;
607 }
608
609 *outNumTypes = numTypes;
610 *outNumRequests = numRequests;
611 return error;
612}
613
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700614Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests,
615 sp<android::Fence>* outPresentFence, uint32_t* state) {
616
617 uint32_t numTypes = 0;
618 uint32_t numRequests = 0;
619 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700620 auto intError = mComposer.presentOrValidateDisplay(
621 mId, &numTypes, &numRequests, &presentFenceFd, state);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700622 auto error = static_cast<Error>(intError);
623 if (error != Error::None && error != Error::HasChanges) {
624 return error;
625 }
626
627 if (*state == 1) {
628 *outPresentFence = new Fence(presentFenceFd);
629 }
630
631 if (*state == 0) {
632 *outNumTypes = numTypes;
633 *outNumRequests = numRequests;
634 }
635 return error;
636}
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700637
Dan Stoza651bf312015-10-23 17:03:17 -0700638// For use by Device
639
Steven Thomas94e35b92017-07-26 18:48:28 -0700640void Display::setConnected(bool connected) {
641 if (!mIsConnected && connected && mType == DisplayType::Physical) {
642 mComposer.setClientTargetSlotCount(mId);
643 loadConfigs();
644 }
645 mIsConnected = connected;
646}
647
Dan Stoza651bf312015-10-23 17:03:17 -0700648int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute)
649{
650 int32_t value = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700651 auto intError = mComposer.getDisplayAttribute(mId, configId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800652 static_cast<Hwc2::IComposerClient::Attribute>(attribute),
653 &value);
Dan Stoza651bf312015-10-23 17:03:17 -0700654 auto error = static_cast<Error>(intError);
655 if (error != Error::None) {
656 ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId,
657 configId, to_string(attribute).c_str(),
658 to_string(error).c_str(), intError);
659 return -1;
660 }
661 return value;
662}
663
664void Display::loadConfig(hwc2_config_t configId)
665{
666 ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId);
667
668 auto config = Config::Builder(*this, configId)
669 .setWidth(getAttribute(configId, Attribute::Width))
670 .setHeight(getAttribute(configId, Attribute::Height))
671 .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod))
672 .setDpiX(getAttribute(configId, Attribute::DpiX))
673 .setDpiY(getAttribute(configId, Attribute::DpiY))
674 .build();
675 mConfigs.emplace(configId, std::move(config));
676}
677
678void Display::loadConfigs()
679{
680 ALOGV("[%" PRIu64 "] loadConfigs", mId);
681
Chia-I Wuaab99f52016-10-05 12:59:58 +0800682 std::vector<Hwc2::Config> configIds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700683 auto intError = mComposer.getDisplayConfigs(mId, &configIds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800684 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700685 if (error != Error::None) {
686 ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId,
687 to_string(error).c_str(), intError);
688 return;
689 }
690
691 for (auto configId : configIds) {
692 loadConfig(configId);
693 }
694}
695
Dan Stoza651bf312015-10-23 17:03:17 -0700696// Other Display methods
697
Steven Thomas94e35b92017-07-26 18:48:28 -0700698Layer* Display::getLayerById(hwc2_layer_t id) const
Dan Stoza651bf312015-10-23 17:03:17 -0700699{
700 if (mLayers.count(id) == 0) {
701 return nullptr;
702 }
703
Steven Thomas94e35b92017-07-26 18:48:28 -0700704 return mLayers.at(id).get();
Dan Stoza651bf312015-10-23 17:03:17 -0700705}
706
707// Layer methods
708
Steven Thomas94e35b92017-07-26 18:48:28 -0700709Layer::Layer(android::Hwc2::Composer& composer,
710 const std::unordered_set<Capability>& capabilities,
711 hwc2_display_t displayId, hwc2_layer_t layerId)
712 : mComposer(composer),
713 mCapabilities(capabilities),
714 mDisplayId(displayId),
715 mId(layerId)
Dan Stoza651bf312015-10-23 17:03:17 -0700716{
Steven Thomas94e35b92017-07-26 18:48:28 -0700717 ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700718}
719
720Layer::~Layer()
721{
Steven Thomas94e35b92017-07-26 18:48:28 -0700722 auto intError = mComposer.destroyLayer(mDisplayId, mId);
723 auto error = static_cast<Error>(intError);
724 ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
725 " failed: %s (%d)", mDisplayId, mId, to_string(error).c_str(),
726 intError);
727 if (mLayerDestroyedListener) {
728 mLayerDestroyedListener(this);
Dan Stoza651bf312015-10-23 17:03:17 -0700729 }
730}
731
Steven Thomas94e35b92017-07-26 18:48:28 -0700732void Layer::setLayerDestroyedListener(std::function<void(Layer*)> listener) {
733 LOG_ALWAYS_FATAL_IF(mLayerDestroyedListener && listener,
734 "Attempt to set layer destroyed listener multiple times");
735 mLayerDestroyedListener = listener;
736}
737
Dan Stoza651bf312015-10-23 17:03:17 -0700738Error Layer::setCursorPosition(int32_t x, int32_t y)
739{
Steven Thomas94e35b92017-07-26 18:48:28 -0700740 auto intError = mComposer.setCursorPosition(mDisplayId, mId, x, y);
Dan Stoza651bf312015-10-23 17:03:17 -0700741 return static_cast<Error>(intError);
742}
743
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400744Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700745 const sp<Fence>& acquireFence)
746{
747 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700748 auto intError = mComposer.setLayerBuffer(mDisplayId, mId, slot, buffer,
749 fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700750 return static_cast<Error>(intError);
751}
752
753Error Layer::setSurfaceDamage(const Region& damage)
754{
755 // We encode default full-screen damage as INVALID_RECT upstream, but as 0
756 // rects for HWC
Chia-I Wuaab99f52016-10-05 12:59:58 +0800757 Hwc2::Error intError = Hwc2::Error::NONE;
Dan Stoza651bf312015-10-23 17:03:17 -0700758 if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700759 intError = mComposer.setLayerSurfaceDamage(mDisplayId,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800760 mId, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700761 } else {
762 size_t rectCount = 0;
763 auto rectArray = damage.getArray(&rectCount);
764
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800765 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700766 for (size_t rect = 0; rect < rectCount; ++rect) {
767 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
768 rectArray[rect].right, rectArray[rect].bottom});
769 }
770
Steven Thomas94e35b92017-07-26 18:48:28 -0700771 intError = mComposer.setLayerSurfaceDamage(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700772 }
773
774 return static_cast<Error>(intError);
775}
776
777Error Layer::setBlendMode(BlendMode mode)
778{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800779 auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700780 auto intError = mComposer.setLayerBlendMode(mDisplayId, mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700781 return static_cast<Error>(intError);
782}
783
784Error Layer::setColor(hwc_color_t color)
785{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800786 Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
Steven Thomas94e35b92017-07-26 18:48:28 -0700787 auto intError = mComposer.setLayerColor(mDisplayId, mId, hwcColor);
Dan Stoza651bf312015-10-23 17:03:17 -0700788 return static_cast<Error>(intError);
789}
790
791Error Layer::setCompositionType(Composition type)
792{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800793 auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
Steven Thomas94e35b92017-07-26 18:48:28 -0700794 auto intError = mComposer.setLayerCompositionType(
795 mDisplayId, mId, intType);
Dan Stoza651bf312015-10-23 17:03:17 -0700796 return static_cast<Error>(intError);
797}
798
Dan Stoza5df2a862016-03-24 16:19:37 -0700799Error Layer::setDataspace(android_dataspace_t dataspace)
800{
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600801 if (dataspace == mDataSpace) {
802 return Error::None;
803 }
804 mDataSpace = dataspace;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800805 auto intDataspace = static_cast<Hwc2::Dataspace>(dataspace);
Steven Thomas94e35b92017-07-26 18:48:28 -0700806 auto intError = mComposer.setLayerDataspace(mDisplayId, mId, intDataspace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700807 return static_cast<Error>(intError);
808}
809
Dan Stoza651bf312015-10-23 17:03:17 -0700810Error Layer::setDisplayFrame(const Rect& frame)
811{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800812 Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800813 frame.right, frame.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700814 auto intError = mComposer.setLayerDisplayFrame(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700815 return static_cast<Error>(intError);
816}
817
818Error Layer::setPlaneAlpha(float alpha)
819{
Steven Thomas94e35b92017-07-26 18:48:28 -0700820 auto intError = mComposer.setLayerPlaneAlpha(mDisplayId, mId, alpha);
Dan Stoza651bf312015-10-23 17:03:17 -0700821 return static_cast<Error>(intError);
822}
823
824Error Layer::setSidebandStream(const native_handle_t* stream)
825{
Steven Thomas94e35b92017-07-26 18:48:28 -0700826 if (mCapabilities.count(Capability::SidebandStream) == 0) {
Dan Stoza09e7a272016-04-14 12:31:01 -0700827 ALOGE("Attempted to call setSidebandStream without checking that the "
828 "device supports sideband streams");
829 return Error::Unsupported;
830 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700831 auto intError = mComposer.setLayerSidebandStream(mDisplayId, mId, stream);
Dan Stoza651bf312015-10-23 17:03:17 -0700832 return static_cast<Error>(intError);
833}
834
835Error Layer::setSourceCrop(const FloatRect& crop)
836{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800837 Hwc2::IComposerClient::FRect hwcRect{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800838 crop.left, crop.top, crop.right, crop.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700839 auto intError = mComposer.setLayerSourceCrop(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700840 return static_cast<Error>(intError);
841}
842
843Error Layer::setTransform(Transform transform)
844{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800845 auto intTransform = static_cast<Hwc2::Transform>(transform);
Steven Thomas94e35b92017-07-26 18:48:28 -0700846 auto intError = mComposer.setLayerTransform(mDisplayId, mId, intTransform);
Dan Stoza651bf312015-10-23 17:03:17 -0700847 return static_cast<Error>(intError);
848}
849
850Error Layer::setVisibleRegion(const Region& region)
851{
852 size_t rectCount = 0;
853 auto rectArray = region.getArray(&rectCount);
854
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800855 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700856 for (size_t rect = 0; rect < rectCount; ++rect) {
857 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
858 rectArray[rect].right, rectArray[rect].bottom});
859 }
860
Steven Thomas94e35b92017-07-26 18:48:28 -0700861 auto intError = mComposer.setLayerVisibleRegion(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700862 return static_cast<Error>(intError);
863}
864
865Error Layer::setZOrder(uint32_t z)
866{
Steven Thomas94e35b92017-07-26 18:48:28 -0700867 auto intError = mComposer.setLayerZOrder(mDisplayId, mId, z);
Dan Stoza651bf312015-10-23 17:03:17 -0700868 return static_cast<Error>(intError);
869}
870
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500871Error Layer::setInfo(uint32_t type, uint32_t appId)
872{
Steven Thomas94e35b92017-07-26 18:48:28 -0700873 auto intError = mComposer.setLayerInfo(mDisplayId, mId, type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500874 return static_cast<Error>(intError);
875}
876
Dan Stoza651bf312015-10-23 17:03:17 -0700877} // namespace HWC2