blob: 814b55e19a3deffe2bfad0587e558041eef04033 [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)
Lloyd Pique715a2c12017-12-14 17:18:08 -080055 : mCallback(callback), mSequenceId(sequenceId) {}
Steven Thomas94e35b92017-07-26 18:48:28 -070056
57 Return<void> onHotplug(Hwc2::Display display,
58 IComposerCallback::Connection conn) override
59 {
60 HWC2::Connection connection = static_cast<HWC2::Connection>(conn);
Lloyd Pique715a2c12017-12-14 17:18:08 -080061 mCallback->onHotplugReceived(mSequenceId, display, connection);
Steven Thomas94e35b92017-07-26 18:48:28 -070062 return Void();
63 }
64
65 Return<void> onRefresh(Hwc2::Display display) override
66 {
67 mCallback->onRefreshReceived(mSequenceId, display);
68 return Void();
69 }
70
71 Return<void> onVsync(Hwc2::Display display, int64_t timestamp) override
72 {
73 mCallback->onVsyncReceived(mSequenceId, display, timestamp);
74 return Void();
75 }
76
Steven Thomas94e35b92017-07-26 18:48:28 -070077private:
78 ComposerCallback* mCallback;
79 int32_t mSequenceId;
Steven Thomas94e35b92017-07-26 18:48:28 -070080};
81
82} // namespace anonymous
83
84
Dan Stoza651bf312015-10-23 17:03:17 -070085// Device methods
86
Lloyd Piquea822d522017-12-20 16:42:57 -080087Device::Device(std::unique_ptr<android::Hwc2::Composer> composer) : mComposer(std::move(composer)) {
Dan Stoza651bf312015-10-23 17:03:17 -070088 loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -070089}
90
Steven Thomas94e35b92017-07-26 18:48:28 -070091void Device::registerCallback(ComposerCallback* callback, int32_t sequenceId) {
92 if (mRegisteredCallback) {
93 ALOGW("Callback already registered. Ignored extra registration "
94 "attempt.");
95 return;
Dan Stoza651bf312015-10-23 17:03:17 -070096 }
Steven Thomas94e35b92017-07-26 18:48:28 -070097 mRegisteredCallback = true;
98 sp<ComposerCallbackBridge> callbackBridge(
99 new ComposerCallbackBridge(callback, sequenceId));
100 mComposer->registerCallback(callbackBridge);
Dan Stoza651bf312015-10-23 17:03:17 -0700101}
102
103// Required by HWC2 device
104
105std::string Device::dump() const
106{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800107 return mComposer->dumpDebugInfo();
Dan Stoza651bf312015-10-23 17:03:17 -0700108}
109
110uint32_t Device::getMaxVirtualDisplayCount() const
111{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800112 return mComposer->getMaxVirtualDisplayCount();
Dan Stoza651bf312015-10-23 17:03:17 -0700113}
114
115Error Device::createVirtualDisplay(uint32_t width, uint32_t height,
Steven Thomas94e35b92017-07-26 18:48:28 -0700116 android_pixel_format_t* format, Display** outDisplay)
Dan Stoza651bf312015-10-23 17:03:17 -0700117{
118 ALOGI("Creating virtual display");
119
120 hwc2_display_t displayId = 0;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800121 auto intFormat = static_cast<Hwc2::PixelFormat>(*format);
122 auto intError = mComposer->createVirtualDisplay(width, height,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800123 &intFormat, &displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700124 auto error = static_cast<Error>(intError);
125 if (error != Error::None) {
126 return error;
127 }
128
Steven Thomas94e35b92017-07-26 18:48:28 -0700129 auto display = std::make_unique<Display>(
130 *mComposer.get(), mCapabilities, displayId, DisplayType::Virtual);
131 *outDisplay = display.get();
Dan Stoza5cf424b2016-05-20 14:02:39 -0700132 *format = static_cast<android_pixel_format_t>(intFormat);
Steven Thomas94e35b92017-07-26 18:48:28 -0700133 mDisplays.emplace(displayId, std::move(display));
134 ALOGI("Created virtual display");
Dan Stoza651bf312015-10-23 17:03:17 -0700135 return Error::None;
136}
137
Steven Thomas94e35b92017-07-26 18:48:28 -0700138void Device::destroyDisplay(hwc2_display_t displayId)
Dan Stoza651bf312015-10-23 17:03:17 -0700139{
Steven Thomas94e35b92017-07-26 18:48:28 -0700140 ALOGI("Destroying display %" PRIu64, displayId);
141 mDisplays.erase(displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700142}
143
Steven Thomas94e35b92017-07-26 18:48:28 -0700144void Device::onHotplug(hwc2_display_t displayId, Connection connection) {
145 if (connection == Connection::Connected) {
146 auto display = getDisplayById(displayId);
147 if (display) {
148 if (display->isConnected()) {
149 ALOGW("Attempt to hotplug connect display %" PRIu64
150 " , which is already connected.", displayId);
151 } else {
152 display->setConnected(true);
153 }
154 } else {
155 DisplayType displayType;
156 auto intError = mComposer->getDisplayType(displayId,
157 reinterpret_cast<Hwc2::IComposerClient::DisplayType *>(
158 &displayType));
159 auto error = static_cast<Error>(intError);
160 if (error != Error::None) {
161 ALOGE("getDisplayType(%" PRIu64 ") failed: %s (%d). "
162 "Aborting hotplug attempt.",
163 displayId, to_string(error).c_str(), intError);
164 return;
165 }
Dan Stoza651bf312015-10-23 17:03:17 -0700166
Steven Thomas94e35b92017-07-26 18:48:28 -0700167 auto newDisplay = std::make_unique<Display>(
168 *mComposer.get(), mCapabilities, displayId, displayType);
169 mDisplays.emplace(displayId, std::move(newDisplay));
Dan Stoza651bf312015-10-23 17:03:17 -0700170 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700171 } else if (connection == Connection::Disconnected) {
172 // The display will later be destroyed by a call to
173 // destroyDisplay(). For now we just mark it disconnected.
174 auto display = getDisplayById(displayId);
175 if (display) {
176 display->setConnected(false);
177 } else {
178 ALOGW("Attempted to disconnect unknown display %" PRIu64,
179 displayId);
180 }
Dan Stoza651bf312015-10-23 17:03:17 -0700181 }
182}
183
184// Other Device methods
185
Steven Thomas94e35b92017-07-26 18:48:28 -0700186Display* Device::getDisplayById(hwc2_display_t id) {
187 auto iter = mDisplays.find(id);
188 return iter == mDisplays.end() ? nullptr : iter->second.get();
Dan Stoza651bf312015-10-23 17:03:17 -0700189}
190
191// Device initialization methods
192
193void Device::loadCapabilities()
194{
195 static_assert(sizeof(Capability) == sizeof(int32_t),
196 "Capability size has changed");
Chia-I Wuaab99f52016-10-05 12:59:58 +0800197 auto capabilities = mComposer->getCapabilities();
198 for (auto capability : capabilities) {
199 mCapabilities.emplace(static_cast<Capability>(capability));
200 }
Dan Stoza651bf312015-10-23 17:03:17 -0700201}
202
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700203Error Device::flushCommands()
204{
205 return static_cast<Error>(mComposer->executeCommands());
206}
207
Dan Stoza651bf312015-10-23 17:03:17 -0700208// Display methods
209
Steven Thomas94e35b92017-07-26 18:48:28 -0700210Display::Display(android::Hwc2::Composer& composer,
211 const std::unordered_set<Capability>& capabilities,
212 hwc2_display_t id, DisplayType type)
213 : mComposer(composer),
214 mCapabilities(capabilities),
Dan Stoza651bf312015-10-23 17:03:17 -0700215 mId(id),
216 mIsConnected(false),
Steven Thomas94e35b92017-07-26 18:48:28 -0700217 mType(type)
Dan Stoza651bf312015-10-23 17:03:17 -0700218{
219 ALOGV("Created display %" PRIu64, id);
Steven Thomas94e35b92017-07-26 18:48:28 -0700220 setConnected(true);
Dan Stoza651bf312015-10-23 17:03:17 -0700221}
222
Steven Thomas94e35b92017-07-26 18:48:28 -0700223Display::~Display() {
224 mLayers.clear();
225
Chris Forbesceb67d12017-04-11 12:20:00 -0700226 if (mType == DisplayType::Virtual) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700227 ALOGV("Destroying virtual display");
228 auto intError = mComposer.destroyVirtualDisplay(mId);
229 auto error = static_cast<Error>(intError);
230 ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64
231 ") failed: %s (%d)", mId, to_string(error).c_str(), intError);
232 } else if (mType == DisplayType::Physical) {
233 auto error = setVsyncEnabled(HWC2::Vsync::Disable);
234 if (error != Error::None) {
235 ALOGE("~Display: Failed to disable vsync for display %" PRIu64
236 ": %s (%d)", mId, to_string(error).c_str(),
237 static_cast<int32_t>(error));
238 }
Dan Stoza651bf312015-10-23 17:03:17 -0700239 }
240}
241
242Display::Config::Config(Display& display, hwc2_config_t id)
243 : mDisplay(display),
244 mId(id),
245 mWidth(-1),
246 mHeight(-1),
247 mVsyncPeriod(-1),
248 mDpiX(-1),
249 mDpiY(-1) {}
250
251Display::Config::Builder::Builder(Display& display, hwc2_config_t id)
252 : mConfig(new Config(display, id)) {}
253
254float Display::Config::Builder::getDefaultDensity() {
255 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
256 // resolution displays get TV density. Maybe eventually we'll need to update
257 // it for 4k displays, though hopefully those will just report accurate DPI
258 // information to begin with. This is also used for virtual displays and
259 // older HWC implementations, so be careful about orientation.
260
261 auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight);
262 if (longDimension >= 1080) {
263 return ACONFIGURATION_DENSITY_XHIGH;
264 } else {
265 return ACONFIGURATION_DENSITY_TV;
266 }
267}
268
269// Required by HWC2 display
270
271Error Display::acceptChanges()
272{
Steven Thomas94e35b92017-07-26 18:48:28 -0700273 auto intError = mComposer.acceptDisplayChanges(mId);
Dan Stoza651bf312015-10-23 17:03:17 -0700274 return static_cast<Error>(intError);
275}
276
Steven Thomas94e35b92017-07-26 18:48:28 -0700277Error Display::createLayer(Layer** outLayer)
Dan Stoza651bf312015-10-23 17:03:17 -0700278{
Steven Thomas94e35b92017-07-26 18:48:28 -0700279 if (!outLayer) {
280 return Error::BadParameter;
281 }
Dan Stoza651bf312015-10-23 17:03:17 -0700282 hwc2_layer_t layerId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700283 auto intError = mComposer.createLayer(mId, &layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700284 auto error = static_cast<Error>(intError);
285 if (error != Error::None) {
286 return error;
287 }
288
Steven Thomas94e35b92017-07-26 18:48:28 -0700289 auto layer = std::make_unique<Layer>(
290 mComposer, mCapabilities, mId, layerId);
291 *outLayer = layer.get();
292 mLayers.emplace(layerId, std::move(layer));
293 return Error::None;
294}
295
296Error Display::destroyLayer(Layer* layer)
297{
298 if (!layer) {
299 return Error::BadParameter;
300 }
301 mLayers.erase(layer->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700302 return Error::None;
303}
304
305Error Display::getActiveConfig(
306 std::shared_ptr<const Display::Config>* outConfig) const
307{
308 ALOGV("[%" PRIu64 "] getActiveConfig", mId);
309 hwc2_config_t configId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700310 auto intError = mComposer.getActiveConfig(mId, &configId);
Dan Stoza651bf312015-10-23 17:03:17 -0700311 auto error = static_cast<Error>(intError);
312
313 if (error != Error::None) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800314 ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
315 *outConfig = nullptr;
Dan Stoza651bf312015-10-23 17:03:17 -0700316 return error;
317 }
318
319 if (mConfigs.count(configId) != 0) {
320 *outConfig = mConfigs.at(configId);
321 } else {
322 ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId,
323 configId);
324 // Return no error, but the caller needs to check for a null pointer to
325 // detect this case
326 *outConfig = nullptr;
327 }
328
329 return Error::None;
330}
331
332Error Display::getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700333 std::unordered_map<Layer*, Composition>* outTypes)
Dan Stoza651bf312015-10-23 17:03:17 -0700334{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800335 std::vector<Hwc2::Layer> layerIds;
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800336 std::vector<Hwc2::IComposerClient::Composition> types;
Steven Thomas94e35b92017-07-26 18:48:28 -0700337 auto intError = mComposer.getChangedCompositionTypes(
338 mId, &layerIds, &types);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800339 uint32_t numElements = layerIds.size();
340 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700341 error = static_cast<Error>(intError);
342 if (error != Error::None) {
343 return error;
344 }
345
346 outTypes->clear();
347 outTypes->reserve(numElements);
348 for (uint32_t element = 0; element < numElements; ++element) {
349 auto layer = getLayerById(layerIds[element]);
350 if (layer) {
351 auto type = static_cast<Composition>(types[element]);
352 ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
353 layer->getId(), to_string(type).c_str());
354 outTypes->emplace(layer, type);
355 } else {
356 ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
357 " on display %" PRIu64, layerIds[element], mId);
358 }
359 }
360
361 return Error::None;
362}
363
Michael Wright28f24d02016-07-12 13:30:53 -0700364Error Display::getColorModes(std::vector<android_color_mode_t>* outModes) const
Dan Stoza076ac672016-03-14 10:47:53 -0700365{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800366 std::vector<Hwc2::ColorMode> modes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700367 auto intError = mComposer.getColorModes(mId, &modes);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800368 uint32_t numModes = modes.size();
369 auto error = static_cast<Error>(intError);
Dan Stoza076ac672016-03-14 10:47:53 -0700370 if (error != Error::None) {
371 return error;
372 }
373
Michael Wright28f24d02016-07-12 13:30:53 -0700374 outModes->resize(numModes);
375 for (size_t i = 0; i < numModes; i++) {
376 (*outModes)[i] = static_cast<android_color_mode_t>(modes[i]);
377 }
Dan Stoza076ac672016-03-14 10:47:53 -0700378 return Error::None;
379}
380
Dan Stoza651bf312015-10-23 17:03:17 -0700381std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const
382{
383 std::vector<std::shared_ptr<const Config>> configs;
384 for (const auto& element : mConfigs) {
385 configs.emplace_back(element.second);
386 }
387 return configs;
388}
389
390Error Display::getName(std::string* outName) const
391{
Steven Thomas94e35b92017-07-26 18:48:28 -0700392 auto intError = mComposer.getDisplayName(mId, outName);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800393 return static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700394}
395
396Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700397 std::unordered_map<Layer*, LayerRequest>* outLayerRequests)
Dan Stoza651bf312015-10-23 17:03:17 -0700398{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800399 uint32_t intDisplayRequests;
400 std::vector<Hwc2::Layer> layerIds;
401 std::vector<uint32_t> layerRequests;
Steven Thomas94e35b92017-07-26 18:48:28 -0700402 auto intError = mComposer.getDisplayRequests(
403 mId, &intDisplayRequests, &layerIds, &layerRequests);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800404 uint32_t numElements = layerIds.size();
405 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700406 if (error != Error::None) {
407 return error;
408 }
409
410 *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
411 outLayerRequests->clear();
412 outLayerRequests->reserve(numElements);
413 for (uint32_t element = 0; element < numElements; ++element) {
414 auto layer = getLayerById(layerIds[element]);
415 if (layer) {
416 auto layerRequest =
417 static_cast<LayerRequest>(layerRequests[element]);
418 outLayerRequests->emplace(layer, layerRequest);
419 } else {
420 ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
421 PRIu64, layerIds[element], mId);
422 }
423 }
424
425 return Error::None;
426}
427
428Error Display::getType(DisplayType* outType) const
429{
Chris Forbes016d73c2017-04-11 10:04:31 -0700430 *outType = mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700431 return Error::None;
432}
433
434Error Display::supportsDoze(bool* outSupport) const
435{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800436 bool intSupport = false;
Steven Thomas94e35b92017-07-26 18:48:28 -0700437 auto intError = mComposer.getDozeSupport(mId, &intSupport);
Dan Stoza651bf312015-10-23 17:03:17 -0700438 auto error = static_cast<Error>(intError);
439 if (error != Error::None) {
440 return error;
441 }
442 *outSupport = static_cast<bool>(intSupport);
443 return Error::None;
444}
445
Dan Stoza7d7ae732016-03-16 12:23:40 -0700446Error Display::getHdrCapabilities(
447 std::unique_ptr<HdrCapabilities>* outCapabilities) const
448{
Dan Stoza7d7ae732016-03-16 12:23:40 -0700449 float maxLuminance = -1.0f;
450 float maxAverageLuminance = -1.0f;
451 float minLuminance = -1.0f;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800452 std::vector<Hwc2::Hdr> intTypes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700453 auto intError = mComposer.getHdrCapabilities(mId, &intTypes,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800454 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800455 auto error = static_cast<HWC2::Error>(intError);
456
457 std::vector<int32_t> types;
458 for (auto type : intTypes) {
459 types.push_back(static_cast<int32_t>(type));
460 }
Dan Stoza7d7ae732016-03-16 12:23:40 -0700461 if (error != Error::None) {
462 return error;
463 }
464
465 *outCapabilities = std::make_unique<HdrCapabilities>(std::move(types),
466 maxLuminance, maxAverageLuminance, minLuminance);
467 return Error::None;
468}
469
Dan Stoza651bf312015-10-23 17:03:17 -0700470Error Display::getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700471 std::unordered_map<Layer*, sp<Fence>>* outFences) const
Dan Stoza651bf312015-10-23 17:03:17 -0700472{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800473 std::vector<Hwc2::Layer> layerIds;
474 std::vector<int> fenceFds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700475 auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800476 auto error = static_cast<Error>(intError);
477 uint32_t numElements = layerIds.size();
Dan Stoza651bf312015-10-23 17:03:17 -0700478 if (error != Error::None) {
479 return error;
480 }
481
Steven Thomas94e35b92017-07-26 18:48:28 -0700482 std::unordered_map<Layer*, sp<Fence>> releaseFences;
Dan Stoza651bf312015-10-23 17:03:17 -0700483 releaseFences.reserve(numElements);
484 for (uint32_t element = 0; element < numElements; ++element) {
485 auto layer = getLayerById(layerIds[element]);
486 if (layer) {
487 sp<Fence> fence(new Fence(fenceFds[element]));
Steven Thomas94e35b92017-07-26 18:48:28 -0700488 releaseFences.emplace(layer, fence);
Dan Stoza651bf312015-10-23 17:03:17 -0700489 } else {
490 ALOGE("getReleaseFences: invalid layer %" PRIu64
491 " found on display %" PRIu64, layerIds[element], mId);
Chia-I Wu5e74c652017-05-17 13:43:16 -0700492 for (; element < numElements; ++element) {
493 close(fenceFds[element]);
494 }
Dan Stoza651bf312015-10-23 17:03:17 -0700495 return Error::BadLayer;
496 }
497 }
498
499 *outFences = std::move(releaseFences);
500 return Error::None;
501}
502
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800503Error Display::present(sp<Fence>* outPresentFence)
Dan Stoza651bf312015-10-23 17:03:17 -0700504{
Naseer Ahmed847650b2016-06-17 11:14:25 -0400505 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700506 auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700507 auto error = static_cast<Error>(intError);
508 if (error != Error::None) {
509 return error;
510 }
511
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800512 *outPresentFence = new Fence(presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700513 return Error::None;
514}
515
516Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
517{
518 if (config->getDisplayId() != mId) {
519 ALOGE("setActiveConfig received config %u for the wrong display %"
520 PRIu64 " (expected %" PRIu64 ")", config->getId(),
521 config->getDisplayId(), mId);
522 return Error::BadConfig;
523 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700524 auto intError = mComposer.setActiveConfig(mId, config->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700525 return static_cast<Error>(intError);
526}
527
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400528Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
Dan Stoza651bf312015-10-23 17:03:17 -0700529 const sp<Fence>& acquireFence, android_dataspace_t dataspace)
530{
Dan Stoza5cf424b2016-05-20 14:02:39 -0700531 // TODO: Properly encode client target surface damage
Dan Stoza651bf312015-10-23 17:03:17 -0700532 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700533 auto intError = mComposer.setClientTarget(mId, slot, target,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800534 fenceFd, static_cast<Hwc2::Dataspace>(dataspace),
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800535 std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700536 return static_cast<Error>(intError);
537}
538
Michael Wright28f24d02016-07-12 13:30:53 -0700539Error Display::setColorMode(android_color_mode_t mode)
Dan Stoza076ac672016-03-14 10:47:53 -0700540{
Steven Thomas94e35b92017-07-26 18:48:28 -0700541 auto intError = mComposer.setColorMode(
542 mId, static_cast<Hwc2::ColorMode>(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
Steven Thomas94e35b92017-07-26 18:48:28 -0700690Layer::Layer(android::Hwc2::Composer& composer,
691 const std::unordered_set<Capability>& capabilities,
692 hwc2_display_t displayId, hwc2_layer_t layerId)
693 : mComposer(composer),
694 mCapabilities(capabilities),
695 mDisplayId(displayId),
696 mId(layerId)
Dan Stoza651bf312015-10-23 17:03:17 -0700697{
Steven Thomas94e35b92017-07-26 18:48:28 -0700698 ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700699}
700
701Layer::~Layer()
702{
Steven Thomas94e35b92017-07-26 18:48:28 -0700703 auto intError = mComposer.destroyLayer(mDisplayId, mId);
704 auto error = static_cast<Error>(intError);
705 ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
706 " failed: %s (%d)", mDisplayId, mId, to_string(error).c_str(),
707 intError);
708 if (mLayerDestroyedListener) {
709 mLayerDestroyedListener(this);
Dan Stoza651bf312015-10-23 17:03:17 -0700710 }
711}
712
Steven Thomas94e35b92017-07-26 18:48:28 -0700713void Layer::setLayerDestroyedListener(std::function<void(Layer*)> listener) {
714 LOG_ALWAYS_FATAL_IF(mLayerDestroyedListener && listener,
715 "Attempt to set layer destroyed listener multiple times");
716 mLayerDestroyedListener = listener;
717}
718
Dan Stoza651bf312015-10-23 17:03:17 -0700719Error Layer::setCursorPosition(int32_t x, int32_t y)
720{
Steven Thomas94e35b92017-07-26 18:48:28 -0700721 auto intError = mComposer.setCursorPosition(mDisplayId, mId, x, y);
Dan Stoza651bf312015-10-23 17:03:17 -0700722 return static_cast<Error>(intError);
723}
724
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400725Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700726 const sp<Fence>& acquireFence)
727{
728 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700729 auto intError = mComposer.setLayerBuffer(mDisplayId, mId, slot, buffer,
730 fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700731 return static_cast<Error>(intError);
732}
733
734Error Layer::setSurfaceDamage(const Region& damage)
735{
736 // We encode default full-screen damage as INVALID_RECT upstream, but as 0
737 // rects for HWC
Chia-I Wuaab99f52016-10-05 12:59:58 +0800738 Hwc2::Error intError = Hwc2::Error::NONE;
Dan Stoza651bf312015-10-23 17:03:17 -0700739 if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700740 intError = mComposer.setLayerSurfaceDamage(mDisplayId,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800741 mId, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700742 } else {
743 size_t rectCount = 0;
744 auto rectArray = damage.getArray(&rectCount);
745
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800746 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700747 for (size_t rect = 0; rect < rectCount; ++rect) {
748 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
749 rectArray[rect].right, rectArray[rect].bottom});
750 }
751
Steven Thomas94e35b92017-07-26 18:48:28 -0700752 intError = mComposer.setLayerSurfaceDamage(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700753 }
754
755 return static_cast<Error>(intError);
756}
757
758Error Layer::setBlendMode(BlendMode mode)
759{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800760 auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700761 auto intError = mComposer.setLayerBlendMode(mDisplayId, mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700762 return static_cast<Error>(intError);
763}
764
765Error Layer::setColor(hwc_color_t color)
766{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800767 Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
Steven Thomas94e35b92017-07-26 18:48:28 -0700768 auto intError = mComposer.setLayerColor(mDisplayId, mId, hwcColor);
Dan Stoza651bf312015-10-23 17:03:17 -0700769 return static_cast<Error>(intError);
770}
771
772Error Layer::setCompositionType(Composition type)
773{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800774 auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
Steven Thomas94e35b92017-07-26 18:48:28 -0700775 auto intError = mComposer.setLayerCompositionType(
776 mDisplayId, mId, intType);
Dan Stoza651bf312015-10-23 17:03:17 -0700777 return static_cast<Error>(intError);
778}
779
Dan Stoza5df2a862016-03-24 16:19:37 -0700780Error Layer::setDataspace(android_dataspace_t dataspace)
781{
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600782 if (dataspace == mDataSpace) {
783 return Error::None;
784 }
785 mDataSpace = dataspace;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800786 auto intDataspace = static_cast<Hwc2::Dataspace>(dataspace);
Steven Thomas94e35b92017-07-26 18:48:28 -0700787 auto intError = mComposer.setLayerDataspace(mDisplayId, mId, intDataspace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700788 return static_cast<Error>(intError);
789}
790
Dan Stoza651bf312015-10-23 17:03:17 -0700791Error Layer::setDisplayFrame(const Rect& frame)
792{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800793 Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800794 frame.right, frame.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700795 auto intError = mComposer.setLayerDisplayFrame(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700796 return static_cast<Error>(intError);
797}
798
799Error Layer::setPlaneAlpha(float alpha)
800{
Steven Thomas94e35b92017-07-26 18:48:28 -0700801 auto intError = mComposer.setLayerPlaneAlpha(mDisplayId, mId, alpha);
Dan Stoza651bf312015-10-23 17:03:17 -0700802 return static_cast<Error>(intError);
803}
804
805Error Layer::setSidebandStream(const native_handle_t* stream)
806{
Steven Thomas94e35b92017-07-26 18:48:28 -0700807 if (mCapabilities.count(Capability::SidebandStream) == 0) {
Dan Stoza09e7a272016-04-14 12:31:01 -0700808 ALOGE("Attempted to call setSidebandStream without checking that the "
809 "device supports sideband streams");
810 return Error::Unsupported;
811 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700812 auto intError = mComposer.setLayerSidebandStream(mDisplayId, mId, stream);
Dan Stoza651bf312015-10-23 17:03:17 -0700813 return static_cast<Error>(intError);
814}
815
816Error Layer::setSourceCrop(const FloatRect& crop)
817{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800818 Hwc2::IComposerClient::FRect hwcRect{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800819 crop.left, crop.top, crop.right, crop.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700820 auto intError = mComposer.setLayerSourceCrop(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700821 return static_cast<Error>(intError);
822}
823
824Error Layer::setTransform(Transform transform)
825{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800826 auto intTransform = static_cast<Hwc2::Transform>(transform);
Steven Thomas94e35b92017-07-26 18:48:28 -0700827 auto intError = mComposer.setLayerTransform(mDisplayId, mId, intTransform);
Dan Stoza651bf312015-10-23 17:03:17 -0700828 return static_cast<Error>(intError);
829}
830
831Error Layer::setVisibleRegion(const Region& region)
832{
833 size_t rectCount = 0;
834 auto rectArray = region.getArray(&rectCount);
835
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800836 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700837 for (size_t rect = 0; rect < rectCount; ++rect) {
838 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
839 rectArray[rect].right, rectArray[rect].bottom});
840 }
841
Steven Thomas94e35b92017-07-26 18:48:28 -0700842 auto intError = mComposer.setLayerVisibleRegion(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700843 return static_cast<Error>(intError);
844}
845
846Error Layer::setZOrder(uint32_t z)
847{
Steven Thomas94e35b92017-07-26 18:48:28 -0700848 auto intError = mComposer.setLayerZOrder(mDisplayId, mId, z);
Dan Stoza651bf312015-10-23 17:03:17 -0700849 return static_cast<Error>(intError);
850}
851
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500852Error Layer::setInfo(uint32_t type, uint32_t appId)
853{
Steven Thomas94e35b92017-07-26 18:48:28 -0700854 auto intError = mComposer.setLayerInfo(mDisplayId, mId, type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500855 return static_cast<Error>(intError);
856}
857
Dan Stoza651bf312015-10-23 17:03:17 -0700858} // namespace HWC2