blob: b7bf964f294b127d13a8c004723e6257429fddde [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;
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -070040using android::HdrMetadata;
Dan Stoza651bf312015-10-23 17:03:17 -070041using android::Rect;
42using android::Region;
43using android::sp;
Chia-I Wuaab99f52016-10-05 12:59:58 +080044using android::hardware::Return;
45using android::hardware::Void;
Dan Stoza651bf312015-10-23 17:03:17 -070046
47namespace HWC2 {
48
Chia-I Wuaab99f52016-10-05 12:59:58 +080049namespace Hwc2 = android::Hwc2;
50
Steven Thomas94e35b92017-07-26 18:48:28 -070051namespace {
52
53class ComposerCallbackBridge : public Hwc2::IComposerCallback {
54public:
55 ComposerCallbackBridge(ComposerCallback* callback, int32_t sequenceId)
Lloyd Pique715a2c12017-12-14 17:18:08 -080056 : mCallback(callback), mSequenceId(sequenceId) {}
Steven Thomas94e35b92017-07-26 18:48:28 -070057
58 Return<void> onHotplug(Hwc2::Display display,
59 IComposerCallback::Connection conn) override
60 {
61 HWC2::Connection connection = static_cast<HWC2::Connection>(conn);
Lloyd Pique715a2c12017-12-14 17:18:08 -080062 mCallback->onHotplugReceived(mSequenceId, display, connection);
Steven Thomas94e35b92017-07-26 18:48:28 -070063 return Void();
64 }
65
66 Return<void> onRefresh(Hwc2::Display display) override
67 {
68 mCallback->onRefreshReceived(mSequenceId, display);
69 return Void();
70 }
71
72 Return<void> onVsync(Hwc2::Display display, int64_t timestamp) override
73 {
74 mCallback->onVsyncReceived(mSequenceId, display, timestamp);
75 return Void();
76 }
77
Steven Thomas94e35b92017-07-26 18:48:28 -070078private:
79 ComposerCallback* mCallback;
80 int32_t mSequenceId;
Steven Thomas94e35b92017-07-26 18:48:28 -070081};
82
83} // namespace anonymous
84
85
Dan Stoza651bf312015-10-23 17:03:17 -070086// Device methods
87
Lloyd Piquea822d522017-12-20 16:42:57 -080088Device::Device(std::unique_ptr<android::Hwc2::Composer> composer) : mComposer(std::move(composer)) {
Dan Stoza651bf312015-10-23 17:03:17 -070089 loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -070090}
91
Steven Thomas94e35b92017-07-26 18:48:28 -070092void Device::registerCallback(ComposerCallback* callback, int32_t sequenceId) {
93 if (mRegisteredCallback) {
94 ALOGW("Callback already registered. Ignored extra registration "
95 "attempt.");
96 return;
Dan Stoza651bf312015-10-23 17:03:17 -070097 }
Steven Thomas94e35b92017-07-26 18:48:28 -070098 mRegisteredCallback = true;
99 sp<ComposerCallbackBridge> callbackBridge(
100 new ComposerCallbackBridge(callback, sequenceId));
101 mComposer->registerCallback(callbackBridge);
Dan Stoza651bf312015-10-23 17:03:17 -0700102}
103
104// Required by HWC2 device
105
106std::string Device::dump() const
107{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800108 return mComposer->dumpDebugInfo();
Dan Stoza651bf312015-10-23 17:03:17 -0700109}
110
111uint32_t Device::getMaxVirtualDisplayCount() const
112{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800113 return mComposer->getMaxVirtualDisplayCount();
Dan Stoza651bf312015-10-23 17:03:17 -0700114}
115
116Error Device::createVirtualDisplay(uint32_t width, uint32_t height,
Steven Thomas94e35b92017-07-26 18:48:28 -0700117 android_pixel_format_t* format, Display** outDisplay)
Dan Stoza651bf312015-10-23 17:03:17 -0700118{
119 ALOGI("Creating virtual display");
120
121 hwc2_display_t displayId = 0;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800122 auto intFormat = static_cast<Hwc2::PixelFormat>(*format);
123 auto intError = mComposer->createVirtualDisplay(width, height,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800124 &intFormat, &displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700125 auto error = static_cast<Error>(intError);
126 if (error != Error::None) {
127 return error;
128 }
129
Steven Thomas94e35b92017-07-26 18:48:28 -0700130 auto display = std::make_unique<Display>(
131 *mComposer.get(), mCapabilities, displayId, DisplayType::Virtual);
132 *outDisplay = display.get();
Dan Stoza5cf424b2016-05-20 14:02:39 -0700133 *format = static_cast<android_pixel_format_t>(intFormat);
Steven Thomas94e35b92017-07-26 18:48:28 -0700134 mDisplays.emplace(displayId, std::move(display));
135 ALOGI("Created virtual display");
Dan Stoza651bf312015-10-23 17:03:17 -0700136 return Error::None;
137}
138
Steven Thomas94e35b92017-07-26 18:48:28 -0700139void Device::destroyDisplay(hwc2_display_t displayId)
Dan Stoza651bf312015-10-23 17:03:17 -0700140{
Steven Thomas94e35b92017-07-26 18:48:28 -0700141 ALOGI("Destroying display %" PRIu64, displayId);
142 mDisplays.erase(displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700143}
144
Steven Thomas94e35b92017-07-26 18:48:28 -0700145void Device::onHotplug(hwc2_display_t displayId, Connection connection) {
146 if (connection == Connection::Connected) {
147 auto display = getDisplayById(displayId);
148 if (display) {
149 if (display->isConnected()) {
150 ALOGW("Attempt to hotplug connect display %" PRIu64
151 " , which is already connected.", displayId);
152 } else {
153 display->setConnected(true);
154 }
155 } else {
156 DisplayType displayType;
157 auto intError = mComposer->getDisplayType(displayId,
158 reinterpret_cast<Hwc2::IComposerClient::DisplayType *>(
159 &displayType));
160 auto error = static_cast<Error>(intError);
161 if (error != Error::None) {
162 ALOGE("getDisplayType(%" PRIu64 ") failed: %s (%d). "
163 "Aborting hotplug attempt.",
164 displayId, to_string(error).c_str(), intError);
165 return;
166 }
Dan Stoza651bf312015-10-23 17:03:17 -0700167
Steven Thomas94e35b92017-07-26 18:48:28 -0700168 auto newDisplay = std::make_unique<Display>(
169 *mComposer.get(), mCapabilities, displayId, displayType);
170 mDisplays.emplace(displayId, std::move(newDisplay));
Dan Stoza651bf312015-10-23 17:03:17 -0700171 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700172 } else if (connection == Connection::Disconnected) {
173 // The display will later be destroyed by a call to
174 // destroyDisplay(). For now we just mark it disconnected.
175 auto display = getDisplayById(displayId);
176 if (display) {
177 display->setConnected(false);
178 } else {
179 ALOGW("Attempted to disconnect unknown display %" PRIu64,
180 displayId);
181 }
Dan Stoza651bf312015-10-23 17:03:17 -0700182 }
183}
184
185// Other Device methods
186
Steven Thomas94e35b92017-07-26 18:48:28 -0700187Display* Device::getDisplayById(hwc2_display_t id) {
188 auto iter = mDisplays.find(id);
189 return iter == mDisplays.end() ? nullptr : iter->second.get();
Dan Stoza651bf312015-10-23 17:03:17 -0700190}
191
192// Device initialization methods
193
194void Device::loadCapabilities()
195{
196 static_assert(sizeof(Capability) == sizeof(int32_t),
197 "Capability size has changed");
Chia-I Wuaab99f52016-10-05 12:59:58 +0800198 auto capabilities = mComposer->getCapabilities();
199 for (auto capability : capabilities) {
200 mCapabilities.emplace(static_cast<Capability>(capability));
201 }
Dan Stoza651bf312015-10-23 17:03:17 -0700202}
203
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700204Error Device::flushCommands()
205{
206 return static_cast<Error>(mComposer->executeCommands());
207}
208
Dan Stoza651bf312015-10-23 17:03:17 -0700209// Display methods
210
Steven Thomas94e35b92017-07-26 18:48:28 -0700211Display::Display(android::Hwc2::Composer& composer,
212 const std::unordered_set<Capability>& capabilities,
213 hwc2_display_t id, DisplayType type)
214 : mComposer(composer),
215 mCapabilities(capabilities),
Dan Stoza651bf312015-10-23 17:03:17 -0700216 mId(id),
217 mIsConnected(false),
Steven Thomas94e35b92017-07-26 18:48:28 -0700218 mType(type)
Dan Stoza651bf312015-10-23 17:03:17 -0700219{
220 ALOGV("Created display %" PRIu64, id);
Steven Thomas94e35b92017-07-26 18:48:28 -0700221 setConnected(true);
Dan Stoza651bf312015-10-23 17:03:17 -0700222}
223
Steven Thomas94e35b92017-07-26 18:48:28 -0700224Display::~Display() {
225 mLayers.clear();
226
Chris Forbesceb67d12017-04-11 12:20:00 -0700227 if (mType == DisplayType::Virtual) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700228 ALOGV("Destroying virtual display");
229 auto intError = mComposer.destroyVirtualDisplay(mId);
230 auto error = static_cast<Error>(intError);
231 ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64
232 ") failed: %s (%d)", mId, to_string(error).c_str(), intError);
233 } else if (mType == DisplayType::Physical) {
234 auto error = setVsyncEnabled(HWC2::Vsync::Disable);
235 if (error != Error::None) {
236 ALOGE("~Display: Failed to disable vsync for display %" PRIu64
237 ": %s (%d)", mId, to_string(error).c_str(),
238 static_cast<int32_t>(error));
239 }
Dan Stoza651bf312015-10-23 17:03:17 -0700240 }
241}
242
243Display::Config::Config(Display& display, hwc2_config_t id)
244 : mDisplay(display),
245 mId(id),
246 mWidth(-1),
247 mHeight(-1),
248 mVsyncPeriod(-1),
249 mDpiX(-1),
250 mDpiY(-1) {}
251
252Display::Config::Builder::Builder(Display& display, hwc2_config_t id)
253 : mConfig(new Config(display, id)) {}
254
255float Display::Config::Builder::getDefaultDensity() {
256 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
257 // resolution displays get TV density. Maybe eventually we'll need to update
258 // it for 4k displays, though hopefully those will just report accurate DPI
259 // information to begin with. This is also used for virtual displays and
260 // older HWC implementations, so be careful about orientation.
261
262 auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight);
263 if (longDimension >= 1080) {
264 return ACONFIGURATION_DENSITY_XHIGH;
265 } else {
266 return ACONFIGURATION_DENSITY_TV;
267 }
268}
269
270// Required by HWC2 display
271
272Error Display::acceptChanges()
273{
Steven Thomas94e35b92017-07-26 18:48:28 -0700274 auto intError = mComposer.acceptDisplayChanges(mId);
Dan Stoza651bf312015-10-23 17:03:17 -0700275 return static_cast<Error>(intError);
276}
277
Steven Thomas94e35b92017-07-26 18:48:28 -0700278Error Display::createLayer(Layer** outLayer)
Dan Stoza651bf312015-10-23 17:03:17 -0700279{
Steven Thomas94e35b92017-07-26 18:48:28 -0700280 if (!outLayer) {
281 return Error::BadParameter;
282 }
Dan Stoza651bf312015-10-23 17:03:17 -0700283 hwc2_layer_t layerId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700284 auto intError = mComposer.createLayer(mId, &layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700285 auto error = static_cast<Error>(intError);
286 if (error != Error::None) {
287 return error;
288 }
289
Steven Thomas94e35b92017-07-26 18:48:28 -0700290 auto layer = std::make_unique<Layer>(
291 mComposer, mCapabilities, mId, layerId);
292 *outLayer = layer.get();
293 mLayers.emplace(layerId, std::move(layer));
294 return Error::None;
295}
296
297Error Display::destroyLayer(Layer* layer)
298{
299 if (!layer) {
300 return Error::BadParameter;
301 }
302 mLayers.erase(layer->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700303 return Error::None;
304}
305
306Error Display::getActiveConfig(
307 std::shared_ptr<const Display::Config>* outConfig) const
308{
309 ALOGV("[%" PRIu64 "] getActiveConfig", mId);
310 hwc2_config_t configId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700311 auto intError = mComposer.getActiveConfig(mId, &configId);
Dan Stoza651bf312015-10-23 17:03:17 -0700312 auto error = static_cast<Error>(intError);
313
314 if (error != Error::None) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800315 ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
316 *outConfig = nullptr;
Dan Stoza651bf312015-10-23 17:03:17 -0700317 return error;
318 }
319
320 if (mConfigs.count(configId) != 0) {
321 *outConfig = mConfigs.at(configId);
322 } else {
323 ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId,
324 configId);
325 // Return no error, but the caller needs to check for a null pointer to
326 // detect this case
327 *outConfig = nullptr;
328 }
329
330 return Error::None;
331}
332
333Error Display::getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700334 std::unordered_map<Layer*, Composition>* outTypes)
Dan Stoza651bf312015-10-23 17:03:17 -0700335{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800336 std::vector<Hwc2::Layer> layerIds;
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800337 std::vector<Hwc2::IComposerClient::Composition> types;
Steven Thomas94e35b92017-07-26 18:48:28 -0700338 auto intError = mComposer.getChangedCompositionTypes(
339 mId, &layerIds, &types);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800340 uint32_t numElements = layerIds.size();
341 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700342 error = static_cast<Error>(intError);
343 if (error != Error::None) {
344 return error;
345 }
346
347 outTypes->clear();
348 outTypes->reserve(numElements);
349 for (uint32_t element = 0; element < numElements; ++element) {
350 auto layer = getLayerById(layerIds[element]);
351 if (layer) {
352 auto type = static_cast<Composition>(types[element]);
353 ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
354 layer->getId(), to_string(type).c_str());
355 outTypes->emplace(layer, type);
356 } else {
357 ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
358 " on display %" PRIu64, layerIds[element], mId);
359 }
360 }
361
362 return Error::None;
363}
364
Peiyong Lina52f0292018-03-14 17:26:31 -0700365Error Display::getColorModes(std::vector<android::ColorMode>* outModes) const
Dan Stoza076ac672016-03-14 10:47:53 -0700366{
Peiyong Lina52f0292018-03-14 17:26:31 -0700367 std::vector<android::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700368 auto intError = mComposer.getColorModes(mId, &modes);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800369 uint32_t numModes = modes.size();
370 auto error = static_cast<Error>(intError);
Dan Stoza076ac672016-03-14 10:47:53 -0700371 if (error != Error::None) {
372 return error;
373 }
374
Michael Wright28f24d02016-07-12 13:30:53 -0700375 outModes->resize(numModes);
376 for (size_t i = 0; i < numModes; i++) {
Peiyong Lina52f0292018-03-14 17:26:31 -0700377 (*outModes)[i] = modes[i];
Michael Wright28f24d02016-07-12 13:30:53 -0700378 }
Dan Stoza076ac672016-03-14 10:47:53 -0700379 return Error::None;
380}
381
Dan Stoza651bf312015-10-23 17:03:17 -0700382std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const
383{
384 std::vector<std::shared_ptr<const Config>> configs;
385 for (const auto& element : mConfigs) {
386 configs.emplace_back(element.second);
387 }
388 return configs;
389}
390
391Error Display::getName(std::string* outName) const
392{
Steven Thomas94e35b92017-07-26 18:48:28 -0700393 auto intError = mComposer.getDisplayName(mId, outName);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800394 return static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700395}
396
397Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700398 std::unordered_map<Layer*, LayerRequest>* outLayerRequests)
Dan Stoza651bf312015-10-23 17:03:17 -0700399{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800400 uint32_t intDisplayRequests;
401 std::vector<Hwc2::Layer> layerIds;
402 std::vector<uint32_t> layerRequests;
Steven Thomas94e35b92017-07-26 18:48:28 -0700403 auto intError = mComposer.getDisplayRequests(
404 mId, &intDisplayRequests, &layerIds, &layerRequests);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800405 uint32_t numElements = layerIds.size();
406 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700407 if (error != Error::None) {
408 return error;
409 }
410
411 *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
412 outLayerRequests->clear();
413 outLayerRequests->reserve(numElements);
414 for (uint32_t element = 0; element < numElements; ++element) {
415 auto layer = getLayerById(layerIds[element]);
416 if (layer) {
417 auto layerRequest =
418 static_cast<LayerRequest>(layerRequests[element]);
419 outLayerRequests->emplace(layer, layerRequest);
420 } else {
421 ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
422 PRIu64, layerIds[element], mId);
423 }
424 }
425
426 return Error::None;
427}
428
429Error Display::getType(DisplayType* outType) const
430{
Chris Forbes016d73c2017-04-11 10:04:31 -0700431 *outType = mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700432 return Error::None;
433}
434
435Error Display::supportsDoze(bool* outSupport) const
436{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800437 bool intSupport = false;
Steven Thomas94e35b92017-07-26 18:48:28 -0700438 auto intError = mComposer.getDozeSupport(mId, &intSupport);
Dan Stoza651bf312015-10-23 17:03:17 -0700439 auto error = static_cast<Error>(intError);
440 if (error != Error::None) {
441 return error;
442 }
443 *outSupport = static_cast<bool>(intSupport);
444 return Error::None;
445}
446
Dan Stoza7d7ae732016-03-16 12:23:40 -0700447Error Display::getHdrCapabilities(
448 std::unique_ptr<HdrCapabilities>* outCapabilities) const
449{
Dan Stoza7d7ae732016-03-16 12:23:40 -0700450 float maxLuminance = -1.0f;
451 float maxAverageLuminance = -1.0f;
452 float minLuminance = -1.0f;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800453 std::vector<Hwc2::Hdr> intTypes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700454 auto intError = mComposer.getHdrCapabilities(mId, &intTypes,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800455 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800456 auto error = static_cast<HWC2::Error>(intError);
457
458 std::vector<int32_t> types;
459 for (auto type : intTypes) {
460 types.push_back(static_cast<int32_t>(type));
461 }
Dan Stoza7d7ae732016-03-16 12:23:40 -0700462 if (error != Error::None) {
463 return error;
464 }
465
466 *outCapabilities = std::make_unique<HdrCapabilities>(std::move(types),
467 maxLuminance, maxAverageLuminance, minLuminance);
468 return Error::None;
469}
470
Dan Stoza651bf312015-10-23 17:03:17 -0700471Error Display::getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700472 std::unordered_map<Layer*, sp<Fence>>* outFences) const
Dan Stoza651bf312015-10-23 17:03:17 -0700473{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800474 std::vector<Hwc2::Layer> layerIds;
475 std::vector<int> fenceFds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700476 auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800477 auto error = static_cast<Error>(intError);
478 uint32_t numElements = layerIds.size();
Dan Stoza651bf312015-10-23 17:03:17 -0700479 if (error != Error::None) {
480 return error;
481 }
482
Steven Thomas94e35b92017-07-26 18:48:28 -0700483 std::unordered_map<Layer*, sp<Fence>> releaseFences;
Dan Stoza651bf312015-10-23 17:03:17 -0700484 releaseFences.reserve(numElements);
485 for (uint32_t element = 0; element < numElements; ++element) {
486 auto layer = getLayerById(layerIds[element]);
487 if (layer) {
488 sp<Fence> fence(new Fence(fenceFds[element]));
Steven Thomas94e35b92017-07-26 18:48:28 -0700489 releaseFences.emplace(layer, fence);
Dan Stoza651bf312015-10-23 17:03:17 -0700490 } else {
491 ALOGE("getReleaseFences: invalid layer %" PRIu64
492 " found on display %" PRIu64, layerIds[element], mId);
Chia-I Wu5e74c652017-05-17 13:43:16 -0700493 for (; element < numElements; ++element) {
494 close(fenceFds[element]);
495 }
Dan Stoza651bf312015-10-23 17:03:17 -0700496 return Error::BadLayer;
497 }
498 }
499
500 *outFences = std::move(releaseFences);
501 return Error::None;
502}
503
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800504Error Display::present(sp<Fence>* outPresentFence)
Dan Stoza651bf312015-10-23 17:03:17 -0700505{
Naseer Ahmed847650b2016-06-17 11:14:25 -0400506 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700507 auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700508 auto error = static_cast<Error>(intError);
509 if (error != Error::None) {
510 return error;
511 }
512
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800513 *outPresentFence = new Fence(presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700514 return Error::None;
515}
516
517Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
518{
519 if (config->getDisplayId() != mId) {
520 ALOGE("setActiveConfig received config %u for the wrong display %"
521 PRIu64 " (expected %" PRIu64 ")", config->getId(),
522 config->getDisplayId(), mId);
523 return Error::BadConfig;
524 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700525 auto intError = mComposer.setActiveConfig(mId, config->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700526 return static_cast<Error>(intError);
527}
528
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400529Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700530 const sp<Fence>& acquireFence, android_dataspace_t dataspace)
531{
Dan Stoza5cf424b2016-05-20 14:02:39 -0700532 // TODO: Properly encode client target surface damage
Dan Stoza651bf312015-10-23 17:03:17 -0700533 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700534 auto intError = mComposer.setClientTarget(mId, slot, target,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800535 fenceFd, static_cast<Hwc2::Dataspace>(dataspace),
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800536 std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700537 return static_cast<Error>(intError);
538}
539
Peiyong Lina52f0292018-03-14 17:26:31 -0700540Error Display::setColorMode(android::ColorMode mode)
Dan Stoza076ac672016-03-14 10:47:53 -0700541{
Peiyong Lina52f0292018-03-14 17:26:31 -0700542 auto intError = mComposer.setColorMode(mId, mode);
Dan Stoza076ac672016-03-14 10:47:53 -0700543 return static_cast<Error>(intError);
544}
545
Dan Stoza5df2a862016-03-24 16:19:37 -0700546Error Display::setColorTransform(const android::mat4& matrix,
547 android_color_transform_t hint)
548{
Steven Thomas94e35b92017-07-26 18:48:28 -0700549 auto intError = mComposer.setColorTransform(mId,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800550 matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
Dan Stoza5df2a862016-03-24 16:19:37 -0700551 return static_cast<Error>(intError);
552}
553
Dan Stoza651bf312015-10-23 17:03:17 -0700554Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
555 const sp<Fence>& releaseFence)
556{
557 int32_t fenceFd = releaseFence->dup();
558 auto handle = buffer->getNativeBuffer()->handle;
Steven Thomas94e35b92017-07-26 18:48:28 -0700559 auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
Dan Stoza38628982016-07-13 15:48:58 -0700560 close(fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700561 return static_cast<Error>(intError);
562}
563
564Error Display::setPowerMode(PowerMode mode)
565{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800566 auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700567 auto intError = mComposer.setPowerMode(mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700568 return static_cast<Error>(intError);
569}
570
571Error Display::setVsyncEnabled(Vsync enabled)
572{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800573 auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
Steven Thomas94e35b92017-07-26 18:48:28 -0700574 auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
Dan Stoza651bf312015-10-23 17:03:17 -0700575 return static_cast<Error>(intError);
576}
577
578Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
579{
580 uint32_t numTypes = 0;
581 uint32_t numRequests = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700582 auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700583 auto error = static_cast<Error>(intError);
584 if (error != Error::None && error != Error::HasChanges) {
585 return error;
586 }
587
588 *outNumTypes = numTypes;
589 *outNumRequests = numRequests;
590 return error;
591}
592
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700593Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests,
594 sp<android::Fence>* outPresentFence, uint32_t* state) {
595
596 uint32_t numTypes = 0;
597 uint32_t numRequests = 0;
598 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700599 auto intError = mComposer.presentOrValidateDisplay(
600 mId, &numTypes, &numRequests, &presentFenceFd, state);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700601 auto error = static_cast<Error>(intError);
602 if (error != Error::None && error != Error::HasChanges) {
603 return error;
604 }
605
606 if (*state == 1) {
607 *outPresentFence = new Fence(presentFenceFd);
608 }
609
610 if (*state == 0) {
611 *outNumTypes = numTypes;
612 *outNumRequests = numRequests;
613 }
614 return error;
615}
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700616
Dan Stoza651bf312015-10-23 17:03:17 -0700617// For use by Device
618
Steven Thomas94e35b92017-07-26 18:48:28 -0700619void Display::setConnected(bool connected) {
Steven Thomasb6c6ad42018-01-29 12:22:00 -0800620 if (!mIsConnected && connected) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700621 mComposer.setClientTargetSlotCount(mId);
Steven Thomasb6c6ad42018-01-29 12:22:00 -0800622 if (mType == DisplayType::Physical) {
623 loadConfigs();
624 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700625 }
626 mIsConnected = connected;
627}
628
Dan Stoza651bf312015-10-23 17:03:17 -0700629int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute)
630{
631 int32_t value = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700632 auto intError = mComposer.getDisplayAttribute(mId, configId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800633 static_cast<Hwc2::IComposerClient::Attribute>(attribute),
634 &value);
Dan Stoza651bf312015-10-23 17:03:17 -0700635 auto error = static_cast<Error>(intError);
636 if (error != Error::None) {
637 ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId,
638 configId, to_string(attribute).c_str(),
639 to_string(error).c_str(), intError);
640 return -1;
641 }
642 return value;
643}
644
645void Display::loadConfig(hwc2_config_t configId)
646{
647 ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId);
648
649 auto config = Config::Builder(*this, configId)
650 .setWidth(getAttribute(configId, Attribute::Width))
651 .setHeight(getAttribute(configId, Attribute::Height))
652 .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod))
653 .setDpiX(getAttribute(configId, Attribute::DpiX))
654 .setDpiY(getAttribute(configId, Attribute::DpiY))
655 .build();
656 mConfigs.emplace(configId, std::move(config));
657}
658
659void Display::loadConfigs()
660{
661 ALOGV("[%" PRIu64 "] loadConfigs", mId);
662
Chia-I Wuaab99f52016-10-05 12:59:58 +0800663 std::vector<Hwc2::Config> configIds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700664 auto intError = mComposer.getDisplayConfigs(mId, &configIds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800665 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700666 if (error != Error::None) {
667 ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId,
668 to_string(error).c_str(), intError);
669 return;
670 }
671
672 for (auto configId : configIds) {
673 loadConfig(configId);
674 }
675}
676
Dan Stoza651bf312015-10-23 17:03:17 -0700677// Other Display methods
678
Steven Thomas94e35b92017-07-26 18:48:28 -0700679Layer* Display::getLayerById(hwc2_layer_t id) const
Dan Stoza651bf312015-10-23 17:03:17 -0700680{
681 if (mLayers.count(id) == 0) {
682 return nullptr;
683 }
684
Steven Thomas94e35b92017-07-26 18:48:28 -0700685 return mLayers.at(id).get();
Dan Stoza651bf312015-10-23 17:03:17 -0700686}
687
688// Layer methods
689
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700690Layer::Layer(android::Hwc2::Composer& composer, const std::unordered_set<Capability>& capabilities,
Steven Thomas94e35b92017-07-26 18:48:28 -0700691 hwc2_display_t displayId, hwc2_layer_t layerId)
692 : mComposer(composer),
693 mCapabilities(capabilities),
694 mDisplayId(displayId),
695 mId(layerId)
Dan Stoza651bf312015-10-23 17:03:17 -0700696{
Steven Thomas94e35b92017-07-26 18:48:28 -0700697 ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700698}
699
700Layer::~Layer()
701{
Steven Thomas94e35b92017-07-26 18:48:28 -0700702 auto intError = mComposer.destroyLayer(mDisplayId, mId);
703 auto error = static_cast<Error>(intError);
704 ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
705 " failed: %s (%d)", mDisplayId, mId, to_string(error).c_str(),
706 intError);
707 if (mLayerDestroyedListener) {
708 mLayerDestroyedListener(this);
Dan Stoza651bf312015-10-23 17:03:17 -0700709 }
710}
711
Steven Thomas94e35b92017-07-26 18:48:28 -0700712void Layer::setLayerDestroyedListener(std::function<void(Layer*)> listener) {
713 LOG_ALWAYS_FATAL_IF(mLayerDestroyedListener && listener,
714 "Attempt to set layer destroyed listener multiple times");
715 mLayerDestroyedListener = listener;
716}
717
Dan Stoza651bf312015-10-23 17:03:17 -0700718Error Layer::setCursorPosition(int32_t x, int32_t y)
719{
Steven Thomas94e35b92017-07-26 18:48:28 -0700720 auto intError = mComposer.setCursorPosition(mDisplayId, mId, x, y);
Dan Stoza651bf312015-10-23 17:03:17 -0700721 return static_cast<Error>(intError);
722}
723
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400724Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700725 const sp<Fence>& acquireFence)
726{
727 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700728 auto intError = mComposer.setLayerBuffer(mDisplayId, mId, slot, buffer,
729 fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700730 return static_cast<Error>(intError);
731}
732
733Error Layer::setSurfaceDamage(const Region& damage)
734{
735 // We encode default full-screen damage as INVALID_RECT upstream, but as 0
736 // rects for HWC
Chia-I Wuaab99f52016-10-05 12:59:58 +0800737 Hwc2::Error intError = Hwc2::Error::NONE;
Dan Stoza651bf312015-10-23 17:03:17 -0700738 if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700739 intError = mComposer.setLayerSurfaceDamage(mDisplayId,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800740 mId, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700741 } else {
742 size_t rectCount = 0;
743 auto rectArray = damage.getArray(&rectCount);
744
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800745 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700746 for (size_t rect = 0; rect < rectCount; ++rect) {
747 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
748 rectArray[rect].right, rectArray[rect].bottom});
749 }
750
Steven Thomas94e35b92017-07-26 18:48:28 -0700751 intError = mComposer.setLayerSurfaceDamage(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700752 }
753
754 return static_cast<Error>(intError);
755}
756
757Error Layer::setBlendMode(BlendMode mode)
758{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800759 auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700760 auto intError = mComposer.setLayerBlendMode(mDisplayId, mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700761 return static_cast<Error>(intError);
762}
763
764Error Layer::setColor(hwc_color_t color)
765{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800766 Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
Steven Thomas94e35b92017-07-26 18:48:28 -0700767 auto intError = mComposer.setLayerColor(mDisplayId, mId, hwcColor);
Dan Stoza651bf312015-10-23 17:03:17 -0700768 return static_cast<Error>(intError);
769}
770
771Error Layer::setCompositionType(Composition type)
772{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800773 auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
Steven Thomas94e35b92017-07-26 18:48:28 -0700774 auto intError = mComposer.setLayerCompositionType(
775 mDisplayId, mId, intType);
Dan Stoza651bf312015-10-23 17:03:17 -0700776 return static_cast<Error>(intError);
777}
778
Dan Stoza5df2a862016-03-24 16:19:37 -0700779Error Layer::setDataspace(android_dataspace_t dataspace)
780{
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600781 if (dataspace == mDataSpace) {
782 return Error::None;
783 }
784 mDataSpace = dataspace;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800785 auto intDataspace = static_cast<Hwc2::Dataspace>(dataspace);
Steven Thomas94e35b92017-07-26 18:48:28 -0700786 auto intError = mComposer.setLayerDataspace(mDisplayId, mId, intDataspace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700787 return static_cast<Error>(intError);
788}
789
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700790Error Layer::setHdrMetadata(const android::HdrMetadata& metadata) {
791 if (metadata == mHdrMetadata) {
792 return Error::None;
793 }
794
795 mHdrMetadata = metadata;
796 auto intError = mComposer.setLayerHdrMetadata(mDisplayId, mId, metadata);
797 return static_cast<Error>(intError);
798}
799
Dan Stoza651bf312015-10-23 17:03:17 -0700800Error Layer::setDisplayFrame(const Rect& frame)
801{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800802 Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800803 frame.right, frame.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700804 auto intError = mComposer.setLayerDisplayFrame(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700805 return static_cast<Error>(intError);
806}
807
808Error Layer::setPlaneAlpha(float alpha)
809{
Steven Thomas94e35b92017-07-26 18:48:28 -0700810 auto intError = mComposer.setLayerPlaneAlpha(mDisplayId, mId, alpha);
Dan Stoza651bf312015-10-23 17:03:17 -0700811 return static_cast<Error>(intError);
812}
813
814Error Layer::setSidebandStream(const native_handle_t* stream)
815{
Steven Thomas94e35b92017-07-26 18:48:28 -0700816 if (mCapabilities.count(Capability::SidebandStream) == 0) {
Dan Stoza09e7a272016-04-14 12:31:01 -0700817 ALOGE("Attempted to call setSidebandStream without checking that the "
818 "device supports sideband streams");
819 return Error::Unsupported;
820 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700821 auto intError = mComposer.setLayerSidebandStream(mDisplayId, mId, stream);
Dan Stoza651bf312015-10-23 17:03:17 -0700822 return static_cast<Error>(intError);
823}
824
825Error Layer::setSourceCrop(const FloatRect& crop)
826{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800827 Hwc2::IComposerClient::FRect hwcRect{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800828 crop.left, crop.top, crop.right, crop.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700829 auto intError = mComposer.setLayerSourceCrop(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700830 return static_cast<Error>(intError);
831}
832
833Error Layer::setTransform(Transform transform)
834{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800835 auto intTransform = static_cast<Hwc2::Transform>(transform);
Steven Thomas94e35b92017-07-26 18:48:28 -0700836 auto intError = mComposer.setLayerTransform(mDisplayId, mId, intTransform);
Dan Stoza651bf312015-10-23 17:03:17 -0700837 return static_cast<Error>(intError);
838}
839
840Error Layer::setVisibleRegion(const Region& region)
841{
842 size_t rectCount = 0;
843 auto rectArray = region.getArray(&rectCount);
844
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800845 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700846 for (size_t rect = 0; rect < rectCount; ++rect) {
847 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
848 rectArray[rect].right, rectArray[rect].bottom});
849 }
850
Steven Thomas94e35b92017-07-26 18:48:28 -0700851 auto intError = mComposer.setLayerVisibleRegion(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700852 return static_cast<Error>(intError);
853}
854
855Error Layer::setZOrder(uint32_t z)
856{
Steven Thomas94e35b92017-07-26 18:48:28 -0700857 auto intError = mComposer.setLayerZOrder(mDisplayId, mId, z);
Dan Stoza651bf312015-10-23 17:03:17 -0700858 return static_cast<Error>(intError);
859}
860
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500861Error Layer::setInfo(uint32_t type, uint32_t appId)
862{
Steven Thomas94e35b92017-07-26 18:48:28 -0700863 auto intError = mComposer.setLayerInfo(mDisplayId, mId, type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500864 return static_cast<Error>(intError);
865}
866
Dan Stoza651bf312015-10-23 17:03:17 -0700867} // namespace HWC2