blob: 0366630d946346f6808bf9f9a75797e09a957447 [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
36extern "C" {
37 static void hotplug_hook(hwc2_callback_data_t callbackData,
38 hwc2_display_t displayId, int32_t intConnected) {
39 auto device = static_cast<HWC2::Device*>(callbackData);
40 auto display = device->getDisplayById(displayId);
41 if (display) {
42 auto connected = static_cast<HWC2::Connection>(intConnected);
43 device->callHotplug(std::move(display), connected);
44 } else {
45 ALOGE("Hotplug callback called with unknown display %" PRIu64,
46 displayId);
47 }
48 }
49
50 static void refresh_hook(hwc2_callback_data_t callbackData,
51 hwc2_display_t displayId) {
52 auto device = static_cast<HWC2::Device*>(callbackData);
53 auto display = device->getDisplayById(displayId);
54 if (display) {
55 device->callRefresh(std::move(display));
56 } else {
57 ALOGE("Refresh callback called with unknown display %" PRIu64,
58 displayId);
59 }
60 }
61
62 static void vsync_hook(hwc2_callback_data_t callbackData,
63 hwc2_display_t displayId, int64_t timestamp) {
64 auto device = static_cast<HWC2::Device*>(callbackData);
65 auto display = device->getDisplayById(displayId);
66 if (display) {
67 device->callVsync(std::move(display), timestamp);
68 } else {
69 ALOGE("Vsync callback called with unknown display %" PRIu64,
70 displayId);
71 }
72 }
73}
74
75using android::Fence;
Dan Stoza5a423ea2017-02-16 14:10:39 -080076using android::FloatRect;
Dan Stoza651bf312015-10-23 17:03:17 -070077using android::GraphicBuffer;
Dan Stoza7d7ae732016-03-16 12:23:40 -070078using android::HdrCapabilities;
Dan Stoza651bf312015-10-23 17:03:17 -070079using android::Rect;
80using android::Region;
81using android::sp;
Chia-I Wuaab99f52016-10-05 12:59:58 +080082using android::hardware::Return;
83using android::hardware::Void;
Dan Stoza651bf312015-10-23 17:03:17 -070084
85namespace HWC2 {
86
Chia-I Wuaab99f52016-10-05 12:59:58 +080087namespace Hwc2 = android::Hwc2;
88
Dan Stoza651bf312015-10-23 17:03:17 -070089// Device methods
90
Hendrik Wagenaar87670ff2017-02-01 12:10:46 -080091Device::Device(bool useVrComposer)
92 : mComposer(std::make_unique<Hwc2::Composer>(useVrComposer)),
Dan Stoza651bf312015-10-23 17:03:17 -070093 mCapabilities(),
94 mDisplays(),
95 mHotplug(),
96 mPendingHotplugs(),
97 mRefresh(),
98 mPendingRefreshes(),
99 mVsync(),
100 mPendingVsyncs()
101{
102 loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -0700103 registerCallbacks();
104}
105
106Device::~Device()
107{
Dan Stoza651bf312015-10-23 17:03:17 -0700108 for (auto element : mDisplays) {
Dan Stoza38628982016-07-13 15:48:58 -0700109 auto display = element.second.lock();
110 if (!display) {
111 ALOGE("~Device: Found a display (%" PRId64 " that has already been"
112 " destroyed", element.first);
113 continue;
114 }
Dan Stoza651bf312015-10-23 17:03:17 -0700115
116 DisplayType displayType = HWC2::DisplayType::Invalid;
117 auto error = display->getType(&displayType);
118 if (error != Error::None) {
119 ALOGE("~Device: Failed to determine type of display %" PRIu64
120 ": %s (%d)", display->getId(), to_string(error).c_str(),
121 static_cast<int32_t>(error));
122 continue;
123 }
124
125 if (displayType == HWC2::DisplayType::Physical) {
126 error = display->setVsyncEnabled(HWC2::Vsync::Disable);
127 if (error != Error::None) {
128 ALOGE("~Device: Failed to disable vsync for display %" PRIu64
129 ": %s (%d)", display->getId(), to_string(error).c_str(),
130 static_cast<int32_t>(error));
131 }
132 }
133 }
Dan Stoza651bf312015-10-23 17:03:17 -0700134}
135
136// Required by HWC2 device
137
138std::string Device::dump() const
139{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800140 return mComposer->dumpDebugInfo();
Dan Stoza651bf312015-10-23 17:03:17 -0700141}
142
143uint32_t Device::getMaxVirtualDisplayCount() const
144{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800145 return mComposer->getMaxVirtualDisplayCount();
Dan Stoza651bf312015-10-23 17:03:17 -0700146}
147
148Error Device::createVirtualDisplay(uint32_t width, uint32_t height,
Dan Stoza5cf424b2016-05-20 14:02:39 -0700149 android_pixel_format_t* format, std::shared_ptr<Display>* outDisplay)
Dan Stoza651bf312015-10-23 17:03:17 -0700150{
151 ALOGI("Creating virtual display");
152
153 hwc2_display_t displayId = 0;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800154 auto intFormat = static_cast<Hwc2::PixelFormat>(*format);
155 auto intError = mComposer->createVirtualDisplay(width, height,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800156 &intFormat, &displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700157 auto error = static_cast<Error>(intError);
158 if (error != Error::None) {
159 return error;
160 }
161
162 ALOGI("Created virtual display");
Dan Stoza5cf424b2016-05-20 14:02:39 -0700163 *format = static_cast<android_pixel_format_t>(intFormat);
Dan Stoza651bf312015-10-23 17:03:17 -0700164 *outDisplay = getDisplayById(displayId);
Dan Stoza38628982016-07-13 15:48:58 -0700165 if (!*outDisplay) {
166 ALOGE("Failed to get display by id");
167 return Error::BadDisplay;
168 }
Chris Forbesceb67d12017-04-11 12:20:00 -0700169 (*outDisplay)->setConnected(true);
Dan Stoza651bf312015-10-23 17:03:17 -0700170 return Error::None;
171}
172
173void Device::registerHotplugCallback(HotplugCallback hotplug)
174{
175 ALOGV("registerHotplugCallback");
176 mHotplug = hotplug;
177 for (auto& pending : mPendingHotplugs) {
178 auto& display = pending.first;
179 auto connected = pending.second;
180 ALOGV("Sending pending hotplug(%" PRIu64 ", %s)", display->getId(),
181 to_string(connected).c_str());
182 mHotplug(std::move(display), connected);
183 }
184}
185
186void Device::registerRefreshCallback(RefreshCallback refresh)
187{
188 mRefresh = refresh;
189 for (auto& pending : mPendingRefreshes) {
190 mRefresh(std::move(pending));
191 }
192}
193
194void Device::registerVsyncCallback(VsyncCallback vsync)
195{
196 mVsync = vsync;
197 for (auto& pending : mPendingVsyncs) {
198 auto& display = pending.first;
199 auto timestamp = pending.second;
200 mVsync(std::move(display), timestamp);
201 }
202}
203
204// For use by Device callbacks
205
206void Device::callHotplug(std::shared_ptr<Display> display, Connection connected)
207{
208 if (connected == Connection::Connected) {
209 if (!display->isConnected()) {
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800210 mComposer->setClientTargetSlotCount(display->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700211 display->loadConfigs();
212 display->setConnected(true);
213 }
214 } else {
215 display->setConnected(false);
216 mDisplays.erase(display->getId());
217 }
218
219 if (mHotplug) {
220 mHotplug(std::move(display), connected);
221 } else {
222 ALOGV("callHotplug called, but no valid callback registered, storing");
223 mPendingHotplugs.emplace_back(std::move(display), connected);
224 }
225}
226
227void Device::callRefresh(std::shared_ptr<Display> display)
228{
229 if (mRefresh) {
230 mRefresh(std::move(display));
231 } else {
232 ALOGV("callRefresh called, but no valid callback registered, storing");
233 mPendingRefreshes.emplace_back(std::move(display));
234 }
235}
236
237void Device::callVsync(std::shared_ptr<Display> display, nsecs_t timestamp)
238{
239 if (mVsync) {
240 mVsync(std::move(display), timestamp);
241 } else {
242 ALOGV("callVsync called, but no valid callback registered, storing");
243 mPendingVsyncs.emplace_back(std::move(display), timestamp);
244 }
245}
246
247// Other Device methods
248
249std::shared_ptr<Display> Device::getDisplayById(hwc2_display_t id) {
250 if (mDisplays.count(id) != 0) {
Dan Stoza38628982016-07-13 15:48:58 -0700251 auto strongDisplay = mDisplays[id].lock();
252 ALOGE_IF(!strongDisplay, "Display %" PRId64 " is in mDisplays but is no"
253 " longer alive", id);
254 return strongDisplay;
Dan Stoza651bf312015-10-23 17:03:17 -0700255 }
256
257 auto display = std::make_shared<Display>(*this, id);
258 mDisplays.emplace(id, display);
259 return display;
260}
261
262// Device initialization methods
263
264void Device::loadCapabilities()
265{
266 static_assert(sizeof(Capability) == sizeof(int32_t),
267 "Capability size has changed");
Chia-I Wuaab99f52016-10-05 12:59:58 +0800268 auto capabilities = mComposer->getCapabilities();
269 for (auto capability : capabilities) {
270 mCapabilities.emplace(static_cast<Capability>(capability));
271 }
Dan Stoza651bf312015-10-23 17:03:17 -0700272}
273
Dan Stoza09e7a272016-04-14 12:31:01 -0700274bool Device::hasCapability(HWC2::Capability capability) const
275{
276 return std::find(mCapabilities.cbegin(), mCapabilities.cend(),
277 capability) != mCapabilities.cend();
278}
279
Chia-I Wuaab99f52016-10-05 12:59:58 +0800280namespace {
281class ComposerCallback : public Hwc2::IComposerCallback {
282public:
283 ComposerCallback(Device* device) : mDevice(device) {}
284
285 Return<void> onHotplug(Hwc2::Display display,
286 Connection connected) override
287 {
288 hotplug_hook(mDevice, display, static_cast<int32_t>(connected));
289 return Void();
290 }
291
292 Return<void> onRefresh(Hwc2::Display display) override
293 {
294 refresh_hook(mDevice, display);
295 return Void();
296 }
297
298 Return<void> onVsync(Hwc2::Display display, int64_t timestamp) override
299 {
300 vsync_hook(mDevice, display, timestamp);
301 return Void();
302 }
303
304private:
305 Device* mDevice;
306};
307} // namespace anonymous
308
Dan Stoza651bf312015-10-23 17:03:17 -0700309void Device::registerCallbacks()
310{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800311 sp<ComposerCallback> callback = new ComposerCallback(this);
312 mComposer->registerCallback(callback);
Dan Stoza651bf312015-10-23 17:03:17 -0700313}
314
315
316// For use by Display
317
318void Device::destroyVirtualDisplay(hwc2_display_t display)
319{
320 ALOGI("Destroying virtual display");
Chia-I Wuaab99f52016-10-05 12:59:58 +0800321 auto intError = mComposer->destroyVirtualDisplay(display);
Dan Stoza651bf312015-10-23 17:03:17 -0700322 auto error = static_cast<Error>(intError);
323 ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64 ") failed:"
324 " %s (%d)", display, to_string(error).c_str(), intError);
Dan Stoza38628982016-07-13 15:48:58 -0700325 mDisplays.erase(display);
Dan Stoza651bf312015-10-23 17:03:17 -0700326}
327
328// Display methods
329
330Display::Display(Device& device, hwc2_display_t id)
331 : mDevice(device),
332 mId(id),
333 mIsConnected(false),
Chris Forbes016d73c2017-04-11 10:04:31 -0700334 mType(DisplayType::Invalid)
Dan Stoza651bf312015-10-23 17:03:17 -0700335{
336 ALOGV("Created display %" PRIu64, id);
Chris Forbes016d73c2017-04-11 10:04:31 -0700337
Chris Forbes016d73c2017-04-11 10:04:31 -0700338 auto intError = mDevice.mComposer->getDisplayType(mId,
339 reinterpret_cast<Hwc2::IComposerClient::DisplayType *>(&mType));
Chris Forbes016d73c2017-04-11 10:04:31 -0700340 auto error = static_cast<Error>(intError);
341 if (error != Error::None) {
342 ALOGE("getDisplayType(%" PRIu64 ") failed: %s (%d)",
343 id, to_string(error).c_str(), intError);
344 }
Dan Stoza651bf312015-10-23 17:03:17 -0700345}
346
347Display::~Display()
348{
349 ALOGV("Destroyed display %" PRIu64, mId);
Chris Forbesceb67d12017-04-11 12:20:00 -0700350 if (mType == DisplayType::Virtual) {
Dan Stoza651bf312015-10-23 17:03:17 -0700351 mDevice.destroyVirtualDisplay(mId);
352 }
353}
354
355Display::Config::Config(Display& display, hwc2_config_t id)
356 : mDisplay(display),
357 mId(id),
358 mWidth(-1),
359 mHeight(-1),
360 mVsyncPeriod(-1),
361 mDpiX(-1),
362 mDpiY(-1) {}
363
364Display::Config::Builder::Builder(Display& display, hwc2_config_t id)
365 : mConfig(new Config(display, id)) {}
366
367float Display::Config::Builder::getDefaultDensity() {
368 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
369 // resolution displays get TV density. Maybe eventually we'll need to update
370 // it for 4k displays, though hopefully those will just report accurate DPI
371 // information to begin with. This is also used for virtual displays and
372 // older HWC implementations, so be careful about orientation.
373
374 auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight);
375 if (longDimension >= 1080) {
376 return ACONFIGURATION_DENSITY_XHIGH;
377 } else {
378 return ACONFIGURATION_DENSITY_TV;
379 }
380}
381
382// Required by HWC2 display
383
384Error Display::acceptChanges()
385{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800386 auto intError = mDevice.mComposer->acceptDisplayChanges(mId);
Dan Stoza651bf312015-10-23 17:03:17 -0700387 return static_cast<Error>(intError);
388}
389
390Error Display::createLayer(std::shared_ptr<Layer>* outLayer)
391{
392 hwc2_layer_t layerId = 0;
Chia-I Wu67e376d2016-12-19 11:36:22 +0800393 auto intError = mDevice.mComposer->createLayer(mId, &layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700394 auto error = static_cast<Error>(intError);
395 if (error != Error::None) {
396 return error;
397 }
398
399 auto layer = std::make_shared<Layer>(shared_from_this(), layerId);
400 mLayers.emplace(layerId, layer);
401 *outLayer = std::move(layer);
402 return Error::None;
403}
404
405Error Display::getActiveConfig(
406 std::shared_ptr<const Display::Config>* outConfig) const
407{
408 ALOGV("[%" PRIu64 "] getActiveConfig", mId);
409 hwc2_config_t configId = 0;
Chia-I Wu67e376d2016-12-19 11:36:22 +0800410 auto intError = mDevice.mComposer->getActiveConfig(mId, &configId);
Dan Stoza651bf312015-10-23 17:03:17 -0700411 auto error = static_cast<Error>(intError);
412
413 if (error != Error::None) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800414 ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
415 *outConfig = nullptr;
Dan Stoza651bf312015-10-23 17:03:17 -0700416 return error;
417 }
418
419 if (mConfigs.count(configId) != 0) {
420 *outConfig = mConfigs.at(configId);
421 } else {
422 ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId,
423 configId);
424 // Return no error, but the caller needs to check for a null pointer to
425 // detect this case
426 *outConfig = nullptr;
427 }
428
429 return Error::None;
430}
431
432Error Display::getChangedCompositionTypes(
433 std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes)
434{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800435 std::vector<Hwc2::Layer> layerIds;
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800436 std::vector<Hwc2::IComposerClient::Composition> types;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800437 auto intError = mDevice.mComposer->getChangedCompositionTypes(mId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800438 &layerIds, &types);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800439 uint32_t numElements = layerIds.size();
440 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700441 error = static_cast<Error>(intError);
442 if (error != Error::None) {
443 return error;
444 }
445
446 outTypes->clear();
447 outTypes->reserve(numElements);
448 for (uint32_t element = 0; element < numElements; ++element) {
449 auto layer = getLayerById(layerIds[element]);
450 if (layer) {
451 auto type = static_cast<Composition>(types[element]);
452 ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
453 layer->getId(), to_string(type).c_str());
454 outTypes->emplace(layer, type);
455 } else {
456 ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
457 " on display %" PRIu64, layerIds[element], mId);
458 }
459 }
460
461 return Error::None;
462}
463
Michael Wright28f24d02016-07-12 13:30:53 -0700464Error Display::getColorModes(std::vector<android_color_mode_t>* outModes) const
Dan Stoza076ac672016-03-14 10:47:53 -0700465{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800466 std::vector<Hwc2::ColorMode> modes;
Chia-I Wu67e376d2016-12-19 11:36:22 +0800467 auto intError = mDevice.mComposer->getColorModes(mId, &modes);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800468 uint32_t numModes = modes.size();
469 auto error = static_cast<Error>(intError);
Dan Stoza076ac672016-03-14 10:47:53 -0700470 if (error != Error::None) {
471 return error;
472 }
473
Michael Wright28f24d02016-07-12 13:30:53 -0700474 outModes->resize(numModes);
475 for (size_t i = 0; i < numModes; i++) {
476 (*outModes)[i] = static_cast<android_color_mode_t>(modes[i]);
477 }
Dan Stoza076ac672016-03-14 10:47:53 -0700478 return Error::None;
479}
480
Dan Stoza651bf312015-10-23 17:03:17 -0700481std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const
482{
483 std::vector<std::shared_ptr<const Config>> configs;
484 for (const auto& element : mConfigs) {
485 configs.emplace_back(element.second);
486 }
487 return configs;
488}
489
490Error Display::getName(std::string* outName) const
491{
Chia-I Wu67e376d2016-12-19 11:36:22 +0800492 auto intError = mDevice.mComposer->getDisplayName(mId, outName);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800493 return static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700494}
495
496Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
497 std::unordered_map<std::shared_ptr<Layer>, LayerRequest>*
498 outLayerRequests)
499{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800500 uint32_t intDisplayRequests;
501 std::vector<Hwc2::Layer> layerIds;
502 std::vector<uint32_t> layerRequests;
503 auto intError = mDevice.mComposer->getDisplayRequests(mId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800504 &intDisplayRequests, &layerIds, &layerRequests);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800505 uint32_t numElements = layerIds.size();
506 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700507 if (error != Error::None) {
508 return error;
509 }
510
511 *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
512 outLayerRequests->clear();
513 outLayerRequests->reserve(numElements);
514 for (uint32_t element = 0; element < numElements; ++element) {
515 auto layer = getLayerById(layerIds[element]);
516 if (layer) {
517 auto layerRequest =
518 static_cast<LayerRequest>(layerRequests[element]);
519 outLayerRequests->emplace(layer, layerRequest);
520 } else {
521 ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
522 PRIu64, layerIds[element], mId);
523 }
524 }
525
526 return Error::None;
527}
528
529Error Display::getType(DisplayType* outType) const
530{
Chris Forbes016d73c2017-04-11 10:04:31 -0700531 *outType = mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700532 return Error::None;
533}
534
535Error Display::supportsDoze(bool* outSupport) const
536{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800537 bool intSupport = false;
Chia-I Wu67e376d2016-12-19 11:36:22 +0800538 auto intError = mDevice.mComposer->getDozeSupport(mId, &intSupport);
Dan Stoza651bf312015-10-23 17:03:17 -0700539 auto error = static_cast<Error>(intError);
540 if (error != Error::None) {
541 return error;
542 }
543 *outSupport = static_cast<bool>(intSupport);
544 return Error::None;
545}
546
Dan Stoza7d7ae732016-03-16 12:23:40 -0700547Error Display::getHdrCapabilities(
548 std::unique_ptr<HdrCapabilities>* outCapabilities) const
549{
550 uint32_t numTypes = 0;
551 float maxLuminance = -1.0f;
552 float maxAverageLuminance = -1.0f;
553 float minLuminance = -1.0f;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800554 std::vector<Hwc2::Hdr> intTypes;
Chia-I Wu67e376d2016-12-19 11:36:22 +0800555 auto intError = mDevice.mComposer->getHdrCapabilities(mId, &intTypes,
556 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800557 auto error = static_cast<HWC2::Error>(intError);
558
559 std::vector<int32_t> types;
560 for (auto type : intTypes) {
561 types.push_back(static_cast<int32_t>(type));
562 }
563 numTypes = types.size();
Dan Stoza7d7ae732016-03-16 12:23:40 -0700564 if (error != Error::None) {
565 return error;
566 }
567
568 *outCapabilities = std::make_unique<HdrCapabilities>(std::move(types),
569 maxLuminance, maxAverageLuminance, minLuminance);
570 return Error::None;
571}
572
Dan Stoza651bf312015-10-23 17:03:17 -0700573Error Display::getReleaseFences(
574 std::unordered_map<std::shared_ptr<Layer>, sp<Fence>>* outFences) const
575{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800576 std::vector<Hwc2::Layer> layerIds;
577 std::vector<int> fenceFds;
578 auto intError = mDevice.mComposer->getReleaseFences(mId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800579 &layerIds, &fenceFds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800580 auto error = static_cast<Error>(intError);
581 uint32_t numElements = layerIds.size();
Dan Stoza651bf312015-10-23 17:03:17 -0700582 if (error != Error::None) {
583 return error;
584 }
585
586 std::unordered_map<std::shared_ptr<Layer>, sp<Fence>> releaseFences;
587 releaseFences.reserve(numElements);
588 for (uint32_t element = 0; element < numElements; ++element) {
589 auto layer = getLayerById(layerIds[element]);
590 if (layer) {
591 sp<Fence> fence(new Fence(fenceFds[element]));
592 releaseFences.emplace(std::move(layer), fence);
593 } else {
594 ALOGE("getReleaseFences: invalid layer %" PRIu64
595 " found on display %" PRIu64, layerIds[element], mId);
Chia-I Wu5e74c652017-05-17 13:43:16 -0700596 for (; element < numElements; ++element) {
597 close(fenceFds[element]);
598 }
Dan Stoza651bf312015-10-23 17:03:17 -0700599 return Error::BadLayer;
600 }
601 }
602
603 *outFences = std::move(releaseFences);
604 return Error::None;
605}
606
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800607Error Display::present(sp<Fence>* outPresentFence)
Dan Stoza651bf312015-10-23 17:03:17 -0700608{
Naseer Ahmed847650b2016-06-17 11:14:25 -0400609 int32_t presentFenceFd = -1;
Chia-I Wu67e376d2016-12-19 11:36:22 +0800610 auto intError = mDevice.mComposer->presentDisplay(mId, &presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700611 auto error = static_cast<Error>(intError);
612 if (error != Error::None) {
613 return error;
614 }
615
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800616 *outPresentFence = new Fence(presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700617 return Error::None;
618}
619
620Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
621{
622 if (config->getDisplayId() != mId) {
623 ALOGE("setActiveConfig received config %u for the wrong display %"
624 PRIu64 " (expected %" PRIu64 ")", config->getId(),
625 config->getDisplayId(), mId);
626 return Error::BadConfig;
627 }
Chia-I Wuaab99f52016-10-05 12:59:58 +0800628 auto intError = mDevice.mComposer->setActiveConfig(mId, config->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700629 return static_cast<Error>(intError);
630}
631
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400632Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700633 const sp<Fence>& acquireFence, android_dataspace_t dataspace)
634{
Dan Stoza5cf424b2016-05-20 14:02:39 -0700635 // TODO: Properly encode client target surface damage
Dan Stoza651bf312015-10-23 17:03:17 -0700636 int32_t fenceFd = acquireFence->dup();
Chia-I Wu06d63de2017-01-04 14:58:51 +0800637 auto intError = mDevice.mComposer->setClientTarget(mId, slot, target,
638 fenceFd, static_cast<Hwc2::Dataspace>(dataspace),
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800639 std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700640 return static_cast<Error>(intError);
641}
642
Michael Wright28f24d02016-07-12 13:30:53 -0700643Error Display::setColorMode(android_color_mode_t mode)
Dan Stoza076ac672016-03-14 10:47:53 -0700644{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800645 auto intError = mDevice.mComposer->setColorMode(mId,
646 static_cast<Hwc2::ColorMode>(mode));
Dan Stoza076ac672016-03-14 10:47:53 -0700647 return static_cast<Error>(intError);
648}
649
Dan Stoza5df2a862016-03-24 16:19:37 -0700650Error Display::setColorTransform(const android::mat4& matrix,
651 android_color_transform_t hint)
652{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800653 auto intError = mDevice.mComposer->setColorTransform(mId,
654 matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
Dan Stoza5df2a862016-03-24 16:19:37 -0700655 return static_cast<Error>(intError);
656}
657
Dan Stoza651bf312015-10-23 17:03:17 -0700658Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
659 const sp<Fence>& releaseFence)
660{
661 int32_t fenceFd = releaseFence->dup();
662 auto handle = buffer->getNativeBuffer()->handle;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800663 auto intError = mDevice.mComposer->setOutputBuffer(mId, handle, fenceFd);
Dan Stoza38628982016-07-13 15:48:58 -0700664 close(fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700665 return static_cast<Error>(intError);
666}
667
668Error Display::setPowerMode(PowerMode mode)
669{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800670 auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800671 auto intError = mDevice.mComposer->setPowerMode(mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700672 return static_cast<Error>(intError);
673}
674
675Error Display::setVsyncEnabled(Vsync enabled)
676{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800677 auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800678 auto intError = mDevice.mComposer->setVsyncEnabled(mId, intEnabled);
Dan Stoza651bf312015-10-23 17:03:17 -0700679 return static_cast<Error>(intError);
680}
681
682Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
683{
684 uint32_t numTypes = 0;
685 uint32_t numRequests = 0;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800686 auto intError = mDevice.mComposer->validateDisplay(mId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800687 &numTypes, &numRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700688 auto error = static_cast<Error>(intError);
689 if (error != Error::None && error != Error::HasChanges) {
690 return error;
691 }
692
693 *outNumTypes = numTypes;
694 *outNumRequests = numRequests;
695 return error;
696}
697
698// For use by Device
699
700int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute)
701{
702 int32_t value = 0;
Chia-I Wu67e376d2016-12-19 11:36:22 +0800703 auto intError = mDevice.mComposer->getDisplayAttribute(mId, configId,
704 static_cast<Hwc2::IComposerClient::Attribute>(attribute),
705 &value);
Dan Stoza651bf312015-10-23 17:03:17 -0700706 auto error = static_cast<Error>(intError);
707 if (error != Error::None) {
708 ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId,
709 configId, to_string(attribute).c_str(),
710 to_string(error).c_str(), intError);
711 return -1;
712 }
713 return value;
714}
715
716void Display::loadConfig(hwc2_config_t configId)
717{
718 ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId);
719
720 auto config = Config::Builder(*this, configId)
721 .setWidth(getAttribute(configId, Attribute::Width))
722 .setHeight(getAttribute(configId, Attribute::Height))
723 .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod))
724 .setDpiX(getAttribute(configId, Attribute::DpiX))
725 .setDpiY(getAttribute(configId, Attribute::DpiY))
726 .build();
727 mConfigs.emplace(configId, std::move(config));
728}
729
730void Display::loadConfigs()
731{
732 ALOGV("[%" PRIu64 "] loadConfigs", mId);
733
Chia-I Wuaab99f52016-10-05 12:59:58 +0800734 std::vector<Hwc2::Config> configIds;
Chia-I Wu67e376d2016-12-19 11:36:22 +0800735 auto intError = mDevice.mComposer->getDisplayConfigs(mId, &configIds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800736 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700737 if (error != Error::None) {
738 ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId,
739 to_string(error).c_str(), intError);
740 return;
741 }
742
743 for (auto configId : configIds) {
744 loadConfig(configId);
745 }
746}
747
748// For use by Layer
749
750void Display::destroyLayer(hwc2_layer_t layerId)
751{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800752 auto intError =mDevice.mComposer->destroyLayer(mId, layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700753 auto error = static_cast<Error>(intError);
754 ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
755 " failed: %s (%d)", mId, layerId, to_string(error).c_str(),
756 intError);
757 mLayers.erase(layerId);
758}
759
760// Other Display methods
761
762std::shared_ptr<Layer> Display::getLayerById(hwc2_layer_t id) const
763{
764 if (mLayers.count(id) == 0) {
765 return nullptr;
766 }
767
768 auto layer = mLayers.at(id).lock();
769 return layer;
770}
771
772// Layer methods
773
774Layer::Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id)
775 : mDisplay(display),
776 mDisplayId(display->getId()),
777 mDevice(display->getDevice()),
778 mId(id)
779{
780 ALOGV("Created layer %" PRIu64 " on display %" PRIu64, id,
781 display->getId());
782}
783
784Layer::~Layer()
785{
786 auto display = mDisplay.lock();
787 if (display) {
788 display->destroyLayer(mId);
789 }
790}
791
792Error Layer::setCursorPosition(int32_t x, int32_t y)
793{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800794 auto intError = mDevice.mComposer->setCursorPosition(mDisplayId,
795 mId, x, y);
Dan Stoza651bf312015-10-23 17:03:17 -0700796 return static_cast<Error>(intError);
797}
798
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400799Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700800 const sp<Fence>& acquireFence)
801{
802 int32_t fenceFd = acquireFence->dup();
Chia-I Wuaab99f52016-10-05 12:59:58 +0800803 auto intError = mDevice.mComposer->setLayerBuffer(mDisplayId,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800804 mId, slot, buffer, fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700805 return static_cast<Error>(intError);
806}
807
808Error Layer::setSurfaceDamage(const Region& damage)
809{
810 // We encode default full-screen damage as INVALID_RECT upstream, but as 0
811 // rects for HWC
Chia-I Wuaab99f52016-10-05 12:59:58 +0800812 Hwc2::Error intError = Hwc2::Error::NONE;
Dan Stoza651bf312015-10-23 17:03:17 -0700813 if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
Chia-I Wuaab99f52016-10-05 12:59:58 +0800814 intError = mDevice.mComposer->setLayerSurfaceDamage(mDisplayId,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800815 mId, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700816 } else {
817 size_t rectCount = 0;
818 auto rectArray = damage.getArray(&rectCount);
819
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800820 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700821 for (size_t rect = 0; rect < rectCount; ++rect) {
822 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
823 rectArray[rect].right, rectArray[rect].bottom});
824 }
825
Chia-I Wuaab99f52016-10-05 12:59:58 +0800826 intError = mDevice.mComposer->setLayerSurfaceDamage(mDisplayId,
827 mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700828 }
829
830 return static_cast<Error>(intError);
831}
832
833Error Layer::setBlendMode(BlendMode mode)
834{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800835 auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800836 auto intError = mDevice.mComposer->setLayerBlendMode(mDisplayId,
837 mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700838 return static_cast<Error>(intError);
839}
840
841Error Layer::setColor(hwc_color_t color)
842{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800843 Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
Chia-I Wuaab99f52016-10-05 12:59:58 +0800844 auto intError = mDevice.mComposer->setLayerColor(mDisplayId,
845 mId, hwcColor);
Dan Stoza651bf312015-10-23 17:03:17 -0700846 return static_cast<Error>(intError);
847}
848
849Error Layer::setCompositionType(Composition type)
850{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800851 auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800852 auto intError = mDevice.mComposer->setLayerCompositionType(mDisplayId,
853 mId, intType);
Dan Stoza651bf312015-10-23 17:03:17 -0700854 return static_cast<Error>(intError);
855}
856
Dan Stoza5df2a862016-03-24 16:19:37 -0700857Error Layer::setDataspace(android_dataspace_t dataspace)
858{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800859 auto intDataspace = static_cast<Hwc2::Dataspace>(dataspace);
860 auto intError = mDevice.mComposer->setLayerDataspace(mDisplayId,
861 mId, intDataspace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700862 return static_cast<Error>(intError);
863}
864
Dan Stoza651bf312015-10-23 17:03:17 -0700865Error Layer::setDisplayFrame(const Rect& frame)
866{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800867 Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800868 frame.right, frame.bottom};
869 auto intError = mDevice.mComposer->setLayerDisplayFrame(mDisplayId,
870 mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700871 return static_cast<Error>(intError);
872}
873
874Error Layer::setPlaneAlpha(float alpha)
875{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800876 auto intError = mDevice.mComposer->setLayerPlaneAlpha(mDisplayId,
877 mId, alpha);
Dan Stoza651bf312015-10-23 17:03:17 -0700878 return static_cast<Error>(intError);
879}
880
881Error Layer::setSidebandStream(const native_handle_t* stream)
882{
Dan Stoza09e7a272016-04-14 12:31:01 -0700883 if (!mDevice.hasCapability(Capability::SidebandStream)) {
884 ALOGE("Attempted to call setSidebandStream without checking that the "
885 "device supports sideband streams");
886 return Error::Unsupported;
887 }
Chia-I Wuaab99f52016-10-05 12:59:58 +0800888 auto intError = mDevice.mComposer->setLayerSidebandStream(mDisplayId,
889 mId, stream);
Dan Stoza651bf312015-10-23 17:03:17 -0700890 return static_cast<Error>(intError);
891}
892
893Error Layer::setSourceCrop(const FloatRect& crop)
894{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800895 Hwc2::IComposerClient::FRect hwcRect{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800896 crop.left, crop.top, crop.right, crop.bottom};
897 auto intError = mDevice.mComposer->setLayerSourceCrop(mDisplayId,
898 mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700899 return static_cast<Error>(intError);
900}
901
902Error Layer::setTransform(Transform transform)
903{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800904 auto intTransform = static_cast<Hwc2::Transform>(transform);
905 auto intError = mDevice.mComposer->setLayerTransform(mDisplayId,
906 mId, intTransform);
Dan Stoza651bf312015-10-23 17:03:17 -0700907 return static_cast<Error>(intError);
908}
909
910Error Layer::setVisibleRegion(const Region& region)
911{
912 size_t rectCount = 0;
913 auto rectArray = region.getArray(&rectCount);
914
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800915 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700916 for (size_t rect = 0; rect < rectCount; ++rect) {
917 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
918 rectArray[rect].right, rectArray[rect].bottom});
919 }
920
Chia-I Wuaab99f52016-10-05 12:59:58 +0800921 auto intError = mDevice.mComposer->setLayerVisibleRegion(mDisplayId,
922 mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700923 return static_cast<Error>(intError);
924}
925
926Error Layer::setZOrder(uint32_t z)
927{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800928 auto intError = mDevice.mComposer->setLayerZOrder(mDisplayId, mId, z);
Dan Stoza651bf312015-10-23 17:03:17 -0700929 return static_cast<Error>(intError);
930}
931
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500932Error Layer::setInfo(uint32_t type, uint32_t appId)
933{
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500934 auto intError = mDevice.mComposer->setLayerInfo(mDisplayId, mId, type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500935 return static_cast<Error>(intError);
936}
937
Dan Stoza651bf312015-10-23 17:03:17 -0700938} // namespace HWC2