blob: 78c0c8567a459cfb0893935eb021035a8ab4eb6f [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
Dan Stoza651bf312015-10-23 17:03:17 -0700224// Display methods
225
Steven Thomas94e35b92017-07-26 18:48:28 -0700226Display::Display(android::Hwc2::Composer& composer,
227 const std::unordered_set<Capability>& capabilities,
228 hwc2_display_t id, DisplayType type)
229 : mComposer(composer),
230 mCapabilities(capabilities),
Dan Stoza651bf312015-10-23 17:03:17 -0700231 mId(id),
232 mIsConnected(false),
Steven Thomas94e35b92017-07-26 18:48:28 -0700233 mType(type)
Dan Stoza651bf312015-10-23 17:03:17 -0700234{
235 ALOGV("Created display %" PRIu64, id);
Steven Thomas94e35b92017-07-26 18:48:28 -0700236 setConnected(true);
Dan Stoza651bf312015-10-23 17:03:17 -0700237}
238
Steven Thomas94e35b92017-07-26 18:48:28 -0700239Display::~Display() {
240 mLayers.clear();
241
Chris Forbesceb67d12017-04-11 12:20:00 -0700242 if (mType == DisplayType::Virtual) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700243 ALOGV("Destroying virtual display");
244 auto intError = mComposer.destroyVirtualDisplay(mId);
245 auto error = static_cast<Error>(intError);
246 ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64
247 ") failed: %s (%d)", mId, to_string(error).c_str(), intError);
248 } else if (mType == DisplayType::Physical) {
249 auto error = setVsyncEnabled(HWC2::Vsync::Disable);
250 if (error != Error::None) {
251 ALOGE("~Display: Failed to disable vsync for display %" PRIu64
252 ": %s (%d)", mId, to_string(error).c_str(),
253 static_cast<int32_t>(error));
254 }
Dan Stoza651bf312015-10-23 17:03:17 -0700255 }
256}
257
258Display::Config::Config(Display& display, hwc2_config_t id)
259 : mDisplay(display),
260 mId(id),
261 mWidth(-1),
262 mHeight(-1),
263 mVsyncPeriod(-1),
264 mDpiX(-1),
265 mDpiY(-1) {}
266
267Display::Config::Builder::Builder(Display& display, hwc2_config_t id)
268 : mConfig(new Config(display, id)) {}
269
270float Display::Config::Builder::getDefaultDensity() {
271 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
272 // resolution displays get TV density. Maybe eventually we'll need to update
273 // it for 4k displays, though hopefully those will just report accurate DPI
274 // information to begin with. This is also used for virtual displays and
275 // older HWC implementations, so be careful about orientation.
276
277 auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight);
278 if (longDimension >= 1080) {
279 return ACONFIGURATION_DENSITY_XHIGH;
280 } else {
281 return ACONFIGURATION_DENSITY_TV;
282 }
283}
284
285// Required by HWC2 display
286
287Error Display::acceptChanges()
288{
Steven Thomas94e35b92017-07-26 18:48:28 -0700289 auto intError = mComposer.acceptDisplayChanges(mId);
Dan Stoza651bf312015-10-23 17:03:17 -0700290 return static_cast<Error>(intError);
291}
292
Steven Thomas94e35b92017-07-26 18:48:28 -0700293Error Display::createLayer(Layer** outLayer)
Dan Stoza651bf312015-10-23 17:03:17 -0700294{
Steven Thomas94e35b92017-07-26 18:48:28 -0700295 if (!outLayer) {
296 return Error::BadParameter;
297 }
Dan Stoza651bf312015-10-23 17:03:17 -0700298 hwc2_layer_t layerId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700299 auto intError = mComposer.createLayer(mId, &layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700300 auto error = static_cast<Error>(intError);
301 if (error != Error::None) {
302 return error;
303 }
304
Steven Thomas94e35b92017-07-26 18:48:28 -0700305 auto layer = std::make_unique<Layer>(
306 mComposer, mCapabilities, mId, layerId);
307 *outLayer = layer.get();
308 mLayers.emplace(layerId, std::move(layer));
309 return Error::None;
310}
311
312Error Display::destroyLayer(Layer* layer)
313{
314 if (!layer) {
315 return Error::BadParameter;
316 }
317 mLayers.erase(layer->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700318 return Error::None;
319}
320
321Error Display::getActiveConfig(
322 std::shared_ptr<const Display::Config>* outConfig) const
323{
324 ALOGV("[%" PRIu64 "] getActiveConfig", mId);
325 hwc2_config_t configId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700326 auto intError = mComposer.getActiveConfig(mId, &configId);
Dan Stoza651bf312015-10-23 17:03:17 -0700327 auto error = static_cast<Error>(intError);
328
329 if (error != Error::None) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800330 ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
331 *outConfig = nullptr;
Dan Stoza651bf312015-10-23 17:03:17 -0700332 return error;
333 }
334
335 if (mConfigs.count(configId) != 0) {
336 *outConfig = mConfigs.at(configId);
337 } else {
338 ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId,
339 configId);
340 // Return no error, but the caller needs to check for a null pointer to
341 // detect this case
342 *outConfig = nullptr;
343 }
344
345 return Error::None;
346}
347
348Error Display::getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700349 std::unordered_map<Layer*, Composition>* outTypes)
Dan Stoza651bf312015-10-23 17:03:17 -0700350{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800351 std::vector<Hwc2::Layer> layerIds;
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800352 std::vector<Hwc2::IComposerClient::Composition> types;
Steven Thomas94e35b92017-07-26 18:48:28 -0700353 auto intError = mComposer.getChangedCompositionTypes(
354 mId, &layerIds, &types);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800355 uint32_t numElements = layerIds.size();
356 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700357 error = static_cast<Error>(intError);
358 if (error != Error::None) {
359 return error;
360 }
361
362 outTypes->clear();
363 outTypes->reserve(numElements);
364 for (uint32_t element = 0; element < numElements; ++element) {
365 auto layer = getLayerById(layerIds[element]);
366 if (layer) {
367 auto type = static_cast<Composition>(types[element]);
368 ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
369 layer->getId(), to_string(type).c_str());
370 outTypes->emplace(layer, type);
371 } else {
372 ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
373 " on display %" PRIu64, layerIds[element], mId);
374 }
375 }
376
377 return Error::None;
378}
379
Michael Wright28f24d02016-07-12 13:30:53 -0700380Error Display::getColorModes(std::vector<android_color_mode_t>* outModes) const
Dan Stoza076ac672016-03-14 10:47:53 -0700381{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800382 std::vector<Hwc2::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700383 auto intError = mComposer.getColorModes(mId, &modes);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800384 uint32_t numModes = modes.size();
385 auto error = static_cast<Error>(intError);
Dan Stoza076ac672016-03-14 10:47:53 -0700386 if (error != Error::None) {
387 return error;
388 }
389
Michael Wright28f24d02016-07-12 13:30:53 -0700390 outModes->resize(numModes);
391 for (size_t i = 0; i < numModes; i++) {
392 (*outModes)[i] = static_cast<android_color_mode_t>(modes[i]);
393 }
Dan Stoza076ac672016-03-14 10:47:53 -0700394 return Error::None;
395}
396
Dan Stoza651bf312015-10-23 17:03:17 -0700397std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const
398{
399 std::vector<std::shared_ptr<const Config>> configs;
400 for (const auto& element : mConfigs) {
401 configs.emplace_back(element.second);
402 }
403 return configs;
404}
405
406Error Display::getName(std::string* outName) const
407{
Steven Thomas94e35b92017-07-26 18:48:28 -0700408 auto intError = mComposer.getDisplayName(mId, outName);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800409 return static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700410}
411
412Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700413 std::unordered_map<Layer*, LayerRequest>* outLayerRequests)
Dan Stoza651bf312015-10-23 17:03:17 -0700414{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800415 uint32_t intDisplayRequests;
416 std::vector<Hwc2::Layer> layerIds;
417 std::vector<uint32_t> layerRequests;
Steven Thomas94e35b92017-07-26 18:48:28 -0700418 auto intError = mComposer.getDisplayRequests(
419 mId, &intDisplayRequests, &layerIds, &layerRequests);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800420 uint32_t numElements = layerIds.size();
421 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700422 if (error != Error::None) {
423 return error;
424 }
425
426 *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
427 outLayerRequests->clear();
428 outLayerRequests->reserve(numElements);
429 for (uint32_t element = 0; element < numElements; ++element) {
430 auto layer = getLayerById(layerIds[element]);
431 if (layer) {
432 auto layerRequest =
433 static_cast<LayerRequest>(layerRequests[element]);
434 outLayerRequests->emplace(layer, layerRequest);
435 } else {
436 ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
437 PRIu64, layerIds[element], mId);
438 }
439 }
440
441 return Error::None;
442}
443
444Error Display::getType(DisplayType* outType) const
445{
Chris Forbes016d73c2017-04-11 10:04:31 -0700446 *outType = mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700447 return Error::None;
448}
449
450Error Display::supportsDoze(bool* outSupport) const
451{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800452 bool intSupport = false;
Steven Thomas94e35b92017-07-26 18:48:28 -0700453 auto intError = mComposer.getDozeSupport(mId, &intSupport);
Dan Stoza651bf312015-10-23 17:03:17 -0700454 auto error = static_cast<Error>(intError);
455 if (error != Error::None) {
456 return error;
457 }
458 *outSupport = static_cast<bool>(intSupport);
459 return Error::None;
460}
461
Dan Stoza7d7ae732016-03-16 12:23:40 -0700462Error Display::getHdrCapabilities(
463 std::unique_ptr<HdrCapabilities>* outCapabilities) const
464{
465 uint32_t numTypes = 0;
466 float maxLuminance = -1.0f;
467 float maxAverageLuminance = -1.0f;
468 float minLuminance = -1.0f;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800469 std::vector<Hwc2::Hdr> intTypes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700470 auto intError = mComposer.getHdrCapabilities(mId, &intTypes,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800471 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800472 auto error = static_cast<HWC2::Error>(intError);
473
474 std::vector<int32_t> types;
475 for (auto type : intTypes) {
476 types.push_back(static_cast<int32_t>(type));
477 }
478 numTypes = types.size();
Dan Stoza7d7ae732016-03-16 12:23:40 -0700479 if (error != Error::None) {
480 return error;
481 }
482
483 *outCapabilities = std::make_unique<HdrCapabilities>(std::move(types),
484 maxLuminance, maxAverageLuminance, minLuminance);
485 return Error::None;
486}
487
Dan Stoza651bf312015-10-23 17:03:17 -0700488Error Display::getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700489 std::unordered_map<Layer*, sp<Fence>>* outFences) const
Dan Stoza651bf312015-10-23 17:03:17 -0700490{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800491 std::vector<Hwc2::Layer> layerIds;
492 std::vector<int> fenceFds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700493 auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800494 auto error = static_cast<Error>(intError);
495 uint32_t numElements = layerIds.size();
Dan Stoza651bf312015-10-23 17:03:17 -0700496 if (error != Error::None) {
497 return error;
498 }
499
Steven Thomas94e35b92017-07-26 18:48:28 -0700500 std::unordered_map<Layer*, sp<Fence>> releaseFences;
Dan Stoza651bf312015-10-23 17:03:17 -0700501 releaseFences.reserve(numElements);
502 for (uint32_t element = 0; element < numElements; ++element) {
503 auto layer = getLayerById(layerIds[element]);
504 if (layer) {
505 sp<Fence> fence(new Fence(fenceFds[element]));
Steven Thomas94e35b92017-07-26 18:48:28 -0700506 releaseFences.emplace(layer, fence);
Dan Stoza651bf312015-10-23 17:03:17 -0700507 } else {
508 ALOGE("getReleaseFences: invalid layer %" PRIu64
509 " found on display %" PRIu64, layerIds[element], mId);
Chia-I Wu5e74c652017-05-17 13:43:16 -0700510 for (; element < numElements; ++element) {
511 close(fenceFds[element]);
512 }
Dan Stoza651bf312015-10-23 17:03:17 -0700513 return Error::BadLayer;
514 }
515 }
516
517 *outFences = std::move(releaseFences);
518 return Error::None;
519}
520
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800521Error Display::present(sp<Fence>* outPresentFence)
Dan Stoza651bf312015-10-23 17:03:17 -0700522{
Naseer Ahmed847650b2016-06-17 11:14:25 -0400523 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700524 auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700525 auto error = static_cast<Error>(intError);
526 if (error != Error::None) {
527 return error;
528 }
529
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800530 *outPresentFence = new Fence(presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700531 return Error::None;
532}
533
534Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
535{
536 if (config->getDisplayId() != mId) {
537 ALOGE("setActiveConfig received config %u for the wrong display %"
538 PRIu64 " (expected %" PRIu64 ")", config->getId(),
539 config->getDisplayId(), mId);
540 return Error::BadConfig;
541 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700542 auto intError = mComposer.setActiveConfig(mId, config->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700543 return static_cast<Error>(intError);
544}
545
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400546Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700547 const sp<Fence>& acquireFence, android_dataspace_t dataspace)
548{
Dan Stoza5cf424b2016-05-20 14:02:39 -0700549 // TODO: Properly encode client target surface damage
Dan Stoza651bf312015-10-23 17:03:17 -0700550 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700551 auto intError = mComposer.setClientTarget(mId, slot, target,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800552 fenceFd, static_cast<Hwc2::Dataspace>(dataspace),
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800553 std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700554 return static_cast<Error>(intError);
555}
556
Michael Wright28f24d02016-07-12 13:30:53 -0700557Error Display::setColorMode(android_color_mode_t mode)
Dan Stoza076ac672016-03-14 10:47:53 -0700558{
Steven Thomas94e35b92017-07-26 18:48:28 -0700559 auto intError = mComposer.setColorMode(
560 mId, static_cast<Hwc2::ColorMode>(mode));
Dan Stoza076ac672016-03-14 10:47:53 -0700561 return static_cast<Error>(intError);
562}
563
Dan Stoza5df2a862016-03-24 16:19:37 -0700564Error Display::setColorTransform(const android::mat4& matrix,
565 android_color_transform_t hint)
566{
Steven Thomas94e35b92017-07-26 18:48:28 -0700567 auto intError = mComposer.setColorTransform(mId,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800568 matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
Dan Stoza5df2a862016-03-24 16:19:37 -0700569 return static_cast<Error>(intError);
570}
571
Dan Stoza651bf312015-10-23 17:03:17 -0700572Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
573 const sp<Fence>& releaseFence)
574{
575 int32_t fenceFd = releaseFence->dup();
576 auto handle = buffer->getNativeBuffer()->handle;
Steven Thomas94e35b92017-07-26 18:48:28 -0700577 auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
Dan Stoza38628982016-07-13 15:48:58 -0700578 close(fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700579 return static_cast<Error>(intError);
580}
581
582Error Display::setPowerMode(PowerMode mode)
583{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800584 auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700585 auto intError = mComposer.setPowerMode(mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700586 return static_cast<Error>(intError);
587}
588
589Error Display::setVsyncEnabled(Vsync enabled)
590{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800591 auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
Steven Thomas94e35b92017-07-26 18:48:28 -0700592 auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
Dan Stoza651bf312015-10-23 17:03:17 -0700593 return static_cast<Error>(intError);
594}
595
596Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
597{
598 uint32_t numTypes = 0;
599 uint32_t numRequests = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700600 auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700601 auto error = static_cast<Error>(intError);
602 if (error != Error::None && error != Error::HasChanges) {
603 return error;
604 }
605
606 *outNumTypes = numTypes;
607 *outNumRequests = numRequests;
608 return error;
609}
610
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700611Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests,
612 sp<android::Fence>* outPresentFence, uint32_t* state) {
613
614 uint32_t numTypes = 0;
615 uint32_t numRequests = 0;
616 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700617 auto intError = mComposer.presentOrValidateDisplay(
618 mId, &numTypes, &numRequests, &presentFenceFd, state);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700619 auto error = static_cast<Error>(intError);
620 if (error != Error::None && error != Error::HasChanges) {
621 return error;
622 }
623
624 if (*state == 1) {
625 *outPresentFence = new Fence(presentFenceFd);
626 }
627
628 if (*state == 0) {
629 *outNumTypes = numTypes;
630 *outNumRequests = numRequests;
631 }
632 return error;
633}
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700634
635void Display::discardCommands()
636{
Steven Thomas94e35b92017-07-26 18:48:28 -0700637 mComposer.resetCommands();
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700638}
639
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