blob: 5f94bb420231d4eeb71ab5f1fb776ed6d7b24eaa [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;
Peiyong Lin34beb7a2018-03-28 11:57:12 -070050using android::ui::ColorMode;
51using android::ui::Dataspace;
52using android::ui::PixelFormat;
Peiyong Lin0e7a7912018-04-05 14:36:36 -070053using android::ui::RenderIntent;
Chia-I Wuaab99f52016-10-05 12:59:58 +080054
Steven Thomas94e35b92017-07-26 18:48:28 -070055namespace {
56
57class ComposerCallbackBridge : public Hwc2::IComposerCallback {
58public:
59 ComposerCallbackBridge(ComposerCallback* callback, int32_t sequenceId)
Lloyd Pique715a2c12017-12-14 17:18:08 -080060 : mCallback(callback), mSequenceId(sequenceId) {}
Steven Thomas94e35b92017-07-26 18:48:28 -070061
62 Return<void> onHotplug(Hwc2::Display display,
63 IComposerCallback::Connection conn) override
64 {
65 HWC2::Connection connection = static_cast<HWC2::Connection>(conn);
Lloyd Pique715a2c12017-12-14 17:18:08 -080066 mCallback->onHotplugReceived(mSequenceId, display, connection);
Steven Thomas94e35b92017-07-26 18:48:28 -070067 return Void();
68 }
69
70 Return<void> onRefresh(Hwc2::Display display) override
71 {
72 mCallback->onRefreshReceived(mSequenceId, display);
73 return Void();
74 }
75
76 Return<void> onVsync(Hwc2::Display display, int64_t timestamp) override
77 {
78 mCallback->onVsyncReceived(mSequenceId, display, timestamp);
79 return Void();
80 }
81
Steven Thomas94e35b92017-07-26 18:48:28 -070082private:
83 ComposerCallback* mCallback;
84 int32_t mSequenceId;
Steven Thomas94e35b92017-07-26 18:48:28 -070085};
86
87} // namespace anonymous
88
89
Dan Stoza651bf312015-10-23 17:03:17 -070090// Device methods
91
Lloyd Piquea822d522017-12-20 16:42:57 -080092Device::Device(std::unique_ptr<android::Hwc2::Composer> composer) : mComposer(std::move(composer)) {
Dan Stoza651bf312015-10-23 17:03:17 -070093 loadCapabilities();
Dan Stoza651bf312015-10-23 17:03:17 -070094}
95
Steven Thomas94e35b92017-07-26 18:48:28 -070096void Device::registerCallback(ComposerCallback* callback, int32_t sequenceId) {
97 if (mRegisteredCallback) {
98 ALOGW("Callback already registered. Ignored extra registration "
99 "attempt.");
100 return;
Dan Stoza651bf312015-10-23 17:03:17 -0700101 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700102 mRegisteredCallback = true;
103 sp<ComposerCallbackBridge> callbackBridge(
104 new ComposerCallbackBridge(callback, sequenceId));
105 mComposer->registerCallback(callbackBridge);
Dan Stoza651bf312015-10-23 17:03:17 -0700106}
107
108// Required by HWC2 device
109
110std::string Device::dump() const
111{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800112 return mComposer->dumpDebugInfo();
Dan Stoza651bf312015-10-23 17:03:17 -0700113}
114
115uint32_t Device::getMaxVirtualDisplayCount() const
116{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800117 return mComposer->getMaxVirtualDisplayCount();
Dan Stoza651bf312015-10-23 17:03:17 -0700118}
119
120Error Device::createVirtualDisplay(uint32_t width, uint32_t height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700121 PixelFormat* format, Display** outDisplay)
Dan Stoza651bf312015-10-23 17:03:17 -0700122{
123 ALOGI("Creating virtual display");
124
125 hwc2_display_t displayId = 0;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800126 auto intError = mComposer->createVirtualDisplay(width, height,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700127 format, &displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700128 auto error = static_cast<Error>(intError);
129 if (error != Error::None) {
130 return error;
131 }
132
Steven Thomas94e35b92017-07-26 18:48:28 -0700133 auto display = std::make_unique<Display>(
134 *mComposer.get(), mCapabilities, displayId, DisplayType::Virtual);
Lloyd Piquebc792092018-01-17 11:52:30 -0800135 display->setConnected(true);
Steven Thomas94e35b92017-07-26 18:48:28 -0700136 *outDisplay = display.get();
Steven Thomas94e35b92017-07-26 18:48:28 -0700137 mDisplays.emplace(displayId, std::move(display));
138 ALOGI("Created virtual display");
Dan Stoza651bf312015-10-23 17:03:17 -0700139 return Error::None;
140}
141
Steven Thomas94e35b92017-07-26 18:48:28 -0700142void Device::destroyDisplay(hwc2_display_t displayId)
Dan Stoza651bf312015-10-23 17:03:17 -0700143{
Steven Thomas94e35b92017-07-26 18:48:28 -0700144 ALOGI("Destroying display %" PRIu64, displayId);
145 mDisplays.erase(displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700146}
147
Steven Thomas94e35b92017-07-26 18:48:28 -0700148void Device::onHotplug(hwc2_display_t displayId, Connection connection) {
149 if (connection == Connection::Connected) {
Steven Thomas3bed0522018-03-20 15:40:48 -0700150 // If we get a hotplug connected event for a display we already have,
151 // destroy the display and recreate it. This will force us to requery
152 // the display params and recreate all layers on that display.
153 auto oldDisplay = getDisplayById(displayId);
154 if (oldDisplay != nullptr && oldDisplay->isConnected()) {
155 ALOGI("Hotplug connecting an already connected display."
156 " Clearing old display state.");
Dan Stoza651bf312015-10-23 17:03:17 -0700157 }
Steven Thomas3bed0522018-03-20 15:40:48 -0700158 mDisplays.erase(displayId);
159
160 DisplayType displayType;
161 auto intError = mComposer->getDisplayType(displayId,
162 reinterpret_cast<Hwc2::IComposerClient::DisplayType *>(
163 &displayType));
164 auto error = static_cast<Error>(intError);
165 if (error != Error::None) {
166 ALOGE("getDisplayType(%" PRIu64 ") failed: %s (%d). "
167 "Aborting hotplug attempt.",
168 displayId, to_string(error).c_str(), intError);
169 return;
170 }
171
172 auto newDisplay = std::make_unique<Display>(
173 *mComposer.get(), mCapabilities, displayId, displayType);
Lloyd Piquebc792092018-01-17 11:52:30 -0800174 newDisplay->setConnected(true);
Steven Thomas3bed0522018-03-20 15:40:48 -0700175 mDisplays.emplace(displayId, std::move(newDisplay));
Steven Thomas94e35b92017-07-26 18:48:28 -0700176 } else if (connection == Connection::Disconnected) {
177 // The display will later be destroyed by a call to
178 // destroyDisplay(). For now we just mark it disconnected.
179 auto display = getDisplayById(displayId);
180 if (display) {
181 display->setConnected(false);
182 } else {
183 ALOGW("Attempted to disconnect unknown display %" PRIu64,
184 displayId);
185 }
Dan Stoza651bf312015-10-23 17:03:17 -0700186 }
187}
188
189// Other Device methods
190
Steven Thomas94e35b92017-07-26 18:48:28 -0700191Display* Device::getDisplayById(hwc2_display_t id) {
192 auto iter = mDisplays.find(id);
193 return iter == mDisplays.end() ? nullptr : iter->second.get();
Dan Stoza651bf312015-10-23 17:03:17 -0700194}
195
196// Device initialization methods
197
198void Device::loadCapabilities()
199{
200 static_assert(sizeof(Capability) == sizeof(int32_t),
201 "Capability size has changed");
Chia-I Wuaab99f52016-10-05 12:59:58 +0800202 auto capabilities = mComposer->getCapabilities();
203 for (auto capability : capabilities) {
204 mCapabilities.emplace(static_cast<Capability>(capability));
205 }
Dan Stoza651bf312015-10-23 17:03:17 -0700206}
207
Chia-I Wuae5a6b82017-10-10 09:09:22 -0700208Error Device::flushCommands()
209{
210 return static_cast<Error>(mComposer->executeCommands());
211}
212
Dan Stoza651bf312015-10-23 17:03:17 -0700213// Display methods
214
Steven Thomas94e35b92017-07-26 18:48:28 -0700215Display::Display(android::Hwc2::Composer& composer,
Lloyd Piquebc792092018-01-17 11:52:30 -0800216 const std::unordered_set<Capability>& capabilities, hwc2_display_t id,
217 DisplayType type)
218 : mComposer(composer),
219 mCapabilities(capabilities),
220 mId(id),
221 mIsConnected(false),
222 mType(type) {
Dan Stoza651bf312015-10-23 17:03:17 -0700223 ALOGV("Created display %" PRIu64, id);
224}
225
Steven Thomas94e35b92017-07-26 18:48:28 -0700226Display::~Display() {
227 mLayers.clear();
228
Chris Forbesceb67d12017-04-11 12:20:00 -0700229 if (mType == DisplayType::Virtual) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700230 ALOGV("Destroying virtual display");
231 auto intError = mComposer.destroyVirtualDisplay(mId);
232 auto error = static_cast<Error>(intError);
233 ALOGE_IF(error != Error::None, "destroyVirtualDisplay(%" PRIu64
234 ") failed: %s (%d)", mId, to_string(error).c_str(), intError);
235 } else if (mType == DisplayType::Physical) {
236 auto error = setVsyncEnabled(HWC2::Vsync::Disable);
237 if (error != Error::None) {
238 ALOGE("~Display: Failed to disable vsync for display %" PRIu64
239 ": %s (%d)", mId, to_string(error).c_str(),
240 static_cast<int32_t>(error));
241 }
Dan Stoza651bf312015-10-23 17:03:17 -0700242 }
243}
244
245Display::Config::Config(Display& display, hwc2_config_t id)
246 : mDisplay(display),
247 mId(id),
248 mWidth(-1),
249 mHeight(-1),
250 mVsyncPeriod(-1),
251 mDpiX(-1),
252 mDpiY(-1) {}
253
254Display::Config::Builder::Builder(Display& display, hwc2_config_t id)
255 : mConfig(new Config(display, id)) {}
256
257float Display::Config::Builder::getDefaultDensity() {
258 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
259 // resolution displays get TV density. Maybe eventually we'll need to update
260 // it for 4k displays, though hopefully those will just report accurate DPI
261 // information to begin with. This is also used for virtual displays and
262 // older HWC implementations, so be careful about orientation.
263
264 auto longDimension = std::max(mConfig->mWidth, mConfig->mHeight);
265 if (longDimension >= 1080) {
266 return ACONFIGURATION_DENSITY_XHIGH;
267 } else {
268 return ACONFIGURATION_DENSITY_TV;
269 }
270}
271
272// Required by HWC2 display
273
274Error Display::acceptChanges()
275{
Steven Thomas94e35b92017-07-26 18:48:28 -0700276 auto intError = mComposer.acceptDisplayChanges(mId);
Dan Stoza651bf312015-10-23 17:03:17 -0700277 return static_cast<Error>(intError);
278}
279
Steven Thomas94e35b92017-07-26 18:48:28 -0700280Error Display::createLayer(Layer** outLayer)
Dan Stoza651bf312015-10-23 17:03:17 -0700281{
Steven Thomas94e35b92017-07-26 18:48:28 -0700282 if (!outLayer) {
283 return Error::BadParameter;
284 }
Dan Stoza651bf312015-10-23 17:03:17 -0700285 hwc2_layer_t layerId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700286 auto intError = mComposer.createLayer(mId, &layerId);
Dan Stoza651bf312015-10-23 17:03:17 -0700287 auto error = static_cast<Error>(intError);
288 if (error != Error::None) {
289 return error;
290 }
291
Steven Thomas94e35b92017-07-26 18:48:28 -0700292 auto layer = std::make_unique<Layer>(
293 mComposer, mCapabilities, mId, layerId);
294 *outLayer = layer.get();
295 mLayers.emplace(layerId, std::move(layer));
296 return Error::None;
297}
298
299Error Display::destroyLayer(Layer* layer)
300{
301 if (!layer) {
302 return Error::BadParameter;
303 }
304 mLayers.erase(layer->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700305 return Error::None;
306}
307
308Error Display::getActiveConfig(
309 std::shared_ptr<const Display::Config>* outConfig) const
310{
311 ALOGV("[%" PRIu64 "] getActiveConfig", mId);
312 hwc2_config_t configId = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700313 auto intError = mComposer.getActiveConfig(mId, &configId);
Dan Stoza651bf312015-10-23 17:03:17 -0700314 auto error = static_cast<Error>(intError);
315
316 if (error != Error::None) {
Fabien Sanglardb7432cc2016-11-11 09:40:27 -0800317 ALOGE("Unable to get active config for mId:[%" PRIu64 "]", mId);
318 *outConfig = nullptr;
Dan Stoza651bf312015-10-23 17:03:17 -0700319 return error;
320 }
321
322 if (mConfigs.count(configId) != 0) {
323 *outConfig = mConfigs.at(configId);
324 } else {
325 ALOGE("[%" PRIu64 "] getActiveConfig returned unknown config %u", mId,
326 configId);
327 // Return no error, but the caller needs to check for a null pointer to
328 // detect this case
329 *outConfig = nullptr;
330 }
331
332 return Error::None;
333}
334
335Error Display::getChangedCompositionTypes(
Steven Thomas94e35b92017-07-26 18:48:28 -0700336 std::unordered_map<Layer*, Composition>* outTypes)
Dan Stoza651bf312015-10-23 17:03:17 -0700337{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800338 std::vector<Hwc2::Layer> layerIds;
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800339 std::vector<Hwc2::IComposerClient::Composition> types;
Steven Thomas94e35b92017-07-26 18:48:28 -0700340 auto intError = mComposer.getChangedCompositionTypes(
341 mId, &layerIds, &types);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800342 uint32_t numElements = layerIds.size();
343 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700344 error = static_cast<Error>(intError);
345 if (error != Error::None) {
346 return error;
347 }
348
349 outTypes->clear();
350 outTypes->reserve(numElements);
351 for (uint32_t element = 0; element < numElements; ++element) {
352 auto layer = getLayerById(layerIds[element]);
353 if (layer) {
354 auto type = static_cast<Composition>(types[element]);
355 ALOGV("getChangedCompositionTypes: adding %" PRIu64 " %s",
356 layer->getId(), to_string(type).c_str());
357 outTypes->emplace(layer, type);
358 } else {
359 ALOGE("getChangedCompositionTypes: invalid layer %" PRIu64 " found"
360 " on display %" PRIu64, layerIds[element], mId);
361 }
362 }
363
364 return Error::None;
365}
366
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700367Error Display::getColorModes(std::vector<ColorMode>* outModes) const
Dan Stoza076ac672016-03-14 10:47:53 -0700368{
Peiyong Linfd997e02018-03-28 15:29:00 -0700369 auto intError = mComposer.getColorModes(mId, outModes);
370 return static_cast<Error>(intError);
Dan Stoza076ac672016-03-14 10:47:53 -0700371}
372
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700373Error Display::getRenderIntents(ColorMode colorMode,
374 std::vector<RenderIntent>* outRenderIntents) const
375{
376 auto intError = mComposer.getRenderIntents(mId, colorMode, outRenderIntents);
377 return static_cast<Error>(intError);
378}
379
380Error Display::getDataspaceSaturationMatrix(Dataspace dataspace, android::mat4* outMatrix)
381{
382 auto intError = mComposer.getDataspaceSaturationMatrix(dataspace, outMatrix);
383 return static_cast<Error>(intError);
384}
385
Dan Stoza651bf312015-10-23 17:03:17 -0700386std::vector<std::shared_ptr<const Display::Config>> Display::getConfigs() const
387{
388 std::vector<std::shared_ptr<const Config>> configs;
389 for (const auto& element : mConfigs) {
390 configs.emplace_back(element.second);
391 }
392 return configs;
393}
394
395Error Display::getName(std::string* outName) const
396{
Steven Thomas94e35b92017-07-26 18:48:28 -0700397 auto intError = mComposer.getDisplayName(mId, outName);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800398 return static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700399}
400
401Error Display::getRequests(HWC2::DisplayRequest* outDisplayRequests,
Steven Thomas94e35b92017-07-26 18:48:28 -0700402 std::unordered_map<Layer*, LayerRequest>* outLayerRequests)
Dan Stoza651bf312015-10-23 17:03:17 -0700403{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800404 uint32_t intDisplayRequests;
405 std::vector<Hwc2::Layer> layerIds;
406 std::vector<uint32_t> layerRequests;
Steven Thomas94e35b92017-07-26 18:48:28 -0700407 auto intError = mComposer.getDisplayRequests(
408 mId, &intDisplayRequests, &layerIds, &layerRequests);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800409 uint32_t numElements = layerIds.size();
410 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700411 if (error != Error::None) {
412 return error;
413 }
414
415 *outDisplayRequests = static_cast<DisplayRequest>(intDisplayRequests);
416 outLayerRequests->clear();
417 outLayerRequests->reserve(numElements);
418 for (uint32_t element = 0; element < numElements; ++element) {
419 auto layer = getLayerById(layerIds[element]);
420 if (layer) {
421 auto layerRequest =
422 static_cast<LayerRequest>(layerRequests[element]);
423 outLayerRequests->emplace(layer, layerRequest);
424 } else {
425 ALOGE("getRequests: invalid layer %" PRIu64 " found on display %"
426 PRIu64, layerIds[element], mId);
427 }
428 }
429
430 return Error::None;
431}
432
433Error Display::getType(DisplayType* outType) const
434{
Chris Forbes016d73c2017-04-11 10:04:31 -0700435 *outType = mType;
Dan Stoza651bf312015-10-23 17:03:17 -0700436 return Error::None;
437}
438
439Error Display::supportsDoze(bool* outSupport) const
440{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800441 bool intSupport = false;
Steven Thomas94e35b92017-07-26 18:48:28 -0700442 auto intError = mComposer.getDozeSupport(mId, &intSupport);
Dan Stoza651bf312015-10-23 17:03:17 -0700443 auto error = static_cast<Error>(intError);
444 if (error != Error::None) {
445 return error;
446 }
447 *outSupport = static_cast<bool>(intSupport);
448 return Error::None;
449}
450
Dan Stoza7d7ae732016-03-16 12:23:40 -0700451Error Display::getHdrCapabilities(
452 std::unique_ptr<HdrCapabilities>* outCapabilities) const
453{
Dan Stoza7d7ae732016-03-16 12:23:40 -0700454 float maxLuminance = -1.0f;
455 float maxAverageLuminance = -1.0f;
456 float minLuminance = -1.0f;
Chia-I Wuaab99f52016-10-05 12:59:58 +0800457 std::vector<Hwc2::Hdr> intTypes;
Steven Thomas94e35b92017-07-26 18:48:28 -0700458 auto intError = mComposer.getHdrCapabilities(mId, &intTypes,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800459 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800460 auto error = static_cast<HWC2::Error>(intError);
461
462 std::vector<int32_t> types;
463 for (auto type : intTypes) {
464 types.push_back(static_cast<int32_t>(type));
465 }
Dan Stoza7d7ae732016-03-16 12:23:40 -0700466 if (error != Error::None) {
467 return error;
468 }
469
470 *outCapabilities = std::make_unique<HdrCapabilities>(std::move(types),
471 maxLuminance, maxAverageLuminance, minLuminance);
472 return Error::None;
473}
474
Dan Stoza651bf312015-10-23 17:03:17 -0700475Error Display::getReleaseFences(
Steven Thomas94e35b92017-07-26 18:48:28 -0700476 std::unordered_map<Layer*, sp<Fence>>* outFences) const
Dan Stoza651bf312015-10-23 17:03:17 -0700477{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800478 std::vector<Hwc2::Layer> layerIds;
479 std::vector<int> fenceFds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700480 auto intError = mComposer.getReleaseFences(mId, &layerIds, &fenceFds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800481 auto error = static_cast<Error>(intError);
482 uint32_t numElements = layerIds.size();
Dan Stoza651bf312015-10-23 17:03:17 -0700483 if (error != Error::None) {
484 return error;
485 }
486
Steven Thomas94e35b92017-07-26 18:48:28 -0700487 std::unordered_map<Layer*, sp<Fence>> releaseFences;
Dan Stoza651bf312015-10-23 17:03:17 -0700488 releaseFences.reserve(numElements);
489 for (uint32_t element = 0; element < numElements; ++element) {
490 auto layer = getLayerById(layerIds[element]);
491 if (layer) {
492 sp<Fence> fence(new Fence(fenceFds[element]));
Steven Thomas94e35b92017-07-26 18:48:28 -0700493 releaseFences.emplace(layer, fence);
Dan Stoza651bf312015-10-23 17:03:17 -0700494 } else {
495 ALOGE("getReleaseFences: invalid layer %" PRIu64
496 " found on display %" PRIu64, layerIds[element], mId);
Chia-I Wu5e74c652017-05-17 13:43:16 -0700497 for (; element < numElements; ++element) {
498 close(fenceFds[element]);
499 }
Dan Stoza651bf312015-10-23 17:03:17 -0700500 return Error::BadLayer;
501 }
502 }
503
504 *outFences = std::move(releaseFences);
505 return Error::None;
506}
507
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800508Error Display::present(sp<Fence>* outPresentFence)
Dan Stoza651bf312015-10-23 17:03:17 -0700509{
Naseer Ahmed847650b2016-06-17 11:14:25 -0400510 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700511 auto intError = mComposer.presentDisplay(mId, &presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700512 auto error = static_cast<Error>(intError);
513 if (error != Error::None) {
514 return error;
515 }
516
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800517 *outPresentFence = new Fence(presentFenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700518 return Error::None;
519}
520
521Error Display::setActiveConfig(const std::shared_ptr<const Config>& config)
522{
523 if (config->getDisplayId() != mId) {
524 ALOGE("setActiveConfig received config %u for the wrong display %"
525 PRIu64 " (expected %" PRIu64 ")", config->getId(),
526 config->getDisplayId(), mId);
527 return Error::BadConfig;
528 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700529 auto intError = mComposer.setActiveConfig(mId, config->getId());
Dan Stoza651bf312015-10-23 17:03:17 -0700530 return static_cast<Error>(intError);
531}
532
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400533Error Display::setClientTarget(uint32_t slot, const sp<GraphicBuffer>& target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700534 const sp<Fence>& acquireFence, Dataspace dataspace)
Dan Stoza651bf312015-10-23 17:03:17 -0700535{
Dan Stoza5cf424b2016-05-20 14:02:39 -0700536 // TODO: Properly encode client target surface damage
Dan Stoza651bf312015-10-23 17:03:17 -0700537 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700538 auto intError = mComposer.setClientTarget(mId, slot, target,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700539 fenceFd, dataspace, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700540 return static_cast<Error>(intError);
541}
542
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700543Error Display::setColorMode(ColorMode mode, RenderIntent renderIntent)
Dan Stoza076ac672016-03-14 10:47:53 -0700544{
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700545 auto intError = mComposer.setColorMode(mId, mode, renderIntent);
Dan Stoza076ac672016-03-14 10:47:53 -0700546 return static_cast<Error>(intError);
547}
548
Dan Stoza5df2a862016-03-24 16:19:37 -0700549Error Display::setColorTransform(const android::mat4& matrix,
550 android_color_transform_t hint)
551{
Steven Thomas94e35b92017-07-26 18:48:28 -0700552 auto intError = mComposer.setColorTransform(mId,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800553 matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
Dan Stoza5df2a862016-03-24 16:19:37 -0700554 return static_cast<Error>(intError);
555}
556
Dan Stoza651bf312015-10-23 17:03:17 -0700557Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
558 const sp<Fence>& releaseFence)
559{
560 int32_t fenceFd = releaseFence->dup();
561 auto handle = buffer->getNativeBuffer()->handle;
Steven Thomas94e35b92017-07-26 18:48:28 -0700562 auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
Dan Stoza38628982016-07-13 15:48:58 -0700563 close(fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700564 return static_cast<Error>(intError);
565}
566
567Error Display::setPowerMode(PowerMode mode)
568{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800569 auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700570 auto intError = mComposer.setPowerMode(mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700571 return static_cast<Error>(intError);
572}
573
574Error Display::setVsyncEnabled(Vsync enabled)
575{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800576 auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
Steven Thomas94e35b92017-07-26 18:48:28 -0700577 auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
Dan Stoza651bf312015-10-23 17:03:17 -0700578 return static_cast<Error>(intError);
579}
580
581Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
582{
583 uint32_t numTypes = 0;
584 uint32_t numRequests = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700585 auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700586 auto error = static_cast<Error>(intError);
587 if (error != Error::None && error != Error::HasChanges) {
588 return error;
589 }
590
591 *outNumTypes = numTypes;
592 *outNumRequests = numRequests;
593 return error;
594}
595
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700596Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests,
597 sp<android::Fence>* outPresentFence, uint32_t* state) {
598
599 uint32_t numTypes = 0;
600 uint32_t numRequests = 0;
601 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700602 auto intError = mComposer.presentOrValidateDisplay(
603 mId, &numTypes, &numRequests, &presentFenceFd, state);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700604 auto error = static_cast<Error>(intError);
605 if (error != Error::None && error != Error::HasChanges) {
606 return error;
607 }
608
609 if (*state == 1) {
610 *outPresentFence = new Fence(presentFenceFd);
611 }
612
613 if (*state == 0) {
614 *outNumTypes = numTypes;
615 *outNumRequests = numRequests;
616 }
617 return error;
618}
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700619
Dan Stoza651bf312015-10-23 17:03:17 -0700620// For use by Device
621
Steven Thomas94e35b92017-07-26 18:48:28 -0700622void Display::setConnected(bool connected) {
Steven Thomasb6c6ad42018-01-29 12:22:00 -0800623 if (!mIsConnected && connected) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700624 mComposer.setClientTargetSlotCount(mId);
Steven Thomasb6c6ad42018-01-29 12:22:00 -0800625 if (mType == DisplayType::Physical) {
626 loadConfigs();
627 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700628 }
629 mIsConnected = connected;
630}
631
Dan Stoza651bf312015-10-23 17:03:17 -0700632int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute)
633{
634 int32_t value = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700635 auto intError = mComposer.getDisplayAttribute(mId, configId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800636 static_cast<Hwc2::IComposerClient::Attribute>(attribute),
637 &value);
Dan Stoza651bf312015-10-23 17:03:17 -0700638 auto error = static_cast<Error>(intError);
639 if (error != Error::None) {
640 ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId,
641 configId, to_string(attribute).c_str(),
642 to_string(error).c_str(), intError);
643 return -1;
644 }
645 return value;
646}
647
648void Display::loadConfig(hwc2_config_t configId)
649{
650 ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId);
651
652 auto config = Config::Builder(*this, configId)
653 .setWidth(getAttribute(configId, Attribute::Width))
654 .setHeight(getAttribute(configId, Attribute::Height))
655 .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod))
656 .setDpiX(getAttribute(configId, Attribute::DpiX))
657 .setDpiY(getAttribute(configId, Attribute::DpiY))
658 .build();
659 mConfigs.emplace(configId, std::move(config));
660}
661
662void Display::loadConfigs()
663{
664 ALOGV("[%" PRIu64 "] loadConfigs", mId);
665
Chia-I Wuaab99f52016-10-05 12:59:58 +0800666 std::vector<Hwc2::Config> configIds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700667 auto intError = mComposer.getDisplayConfigs(mId, &configIds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800668 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700669 if (error != Error::None) {
670 ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId,
671 to_string(error).c_str(), intError);
672 return;
673 }
674
675 for (auto configId : configIds) {
676 loadConfig(configId);
677 }
678}
679
Dan Stoza651bf312015-10-23 17:03:17 -0700680// Other Display methods
681
Steven Thomas94e35b92017-07-26 18:48:28 -0700682Layer* Display::getLayerById(hwc2_layer_t id) const
Dan Stoza651bf312015-10-23 17:03:17 -0700683{
684 if (mLayers.count(id) == 0) {
685 return nullptr;
686 }
687
Steven Thomas94e35b92017-07-26 18:48:28 -0700688 return mLayers.at(id).get();
Dan Stoza651bf312015-10-23 17:03:17 -0700689}
690
691// Layer methods
692
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700693Layer::Layer(android::Hwc2::Composer& composer, const std::unordered_set<Capability>& capabilities,
Steven Thomas94e35b92017-07-26 18:48:28 -0700694 hwc2_display_t displayId, hwc2_layer_t layerId)
695 : mComposer(composer),
696 mCapabilities(capabilities),
697 mDisplayId(displayId),
698 mId(layerId)
Dan Stoza651bf312015-10-23 17:03:17 -0700699{
Steven Thomas94e35b92017-07-26 18:48:28 -0700700 ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700701}
702
703Layer::~Layer()
704{
Steven Thomas94e35b92017-07-26 18:48:28 -0700705 auto intError = mComposer.destroyLayer(mDisplayId, mId);
706 auto error = static_cast<Error>(intError);
707 ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
708 " failed: %s (%d)", mDisplayId, mId, to_string(error).c_str(),
709 intError);
710 if (mLayerDestroyedListener) {
711 mLayerDestroyedListener(this);
Dan Stoza651bf312015-10-23 17:03:17 -0700712 }
713}
714
Steven Thomas94e35b92017-07-26 18:48:28 -0700715void Layer::setLayerDestroyedListener(std::function<void(Layer*)> listener) {
716 LOG_ALWAYS_FATAL_IF(mLayerDestroyedListener && listener,
717 "Attempt to set layer destroyed listener multiple times");
718 mLayerDestroyedListener = listener;
719}
720
Dan Stoza651bf312015-10-23 17:03:17 -0700721Error Layer::setCursorPosition(int32_t x, int32_t y)
722{
Steven Thomas94e35b92017-07-26 18:48:28 -0700723 auto intError = mComposer.setCursorPosition(mDisplayId, mId, x, y);
Dan Stoza651bf312015-10-23 17:03:17 -0700724 return static_cast<Error>(intError);
725}
726
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400727Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700728 const sp<Fence>& acquireFence)
729{
730 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700731 auto intError = mComposer.setLayerBuffer(mDisplayId, mId, slot, buffer,
732 fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700733 return static_cast<Error>(intError);
734}
735
736Error Layer::setSurfaceDamage(const Region& damage)
737{
738 // We encode default full-screen damage as INVALID_RECT upstream, but as 0
739 // rects for HWC
Chia-I Wuaab99f52016-10-05 12:59:58 +0800740 Hwc2::Error intError = Hwc2::Error::NONE;
Dan Stoza651bf312015-10-23 17:03:17 -0700741 if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700742 intError = mComposer.setLayerSurfaceDamage(mDisplayId,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800743 mId, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700744 } else {
745 size_t rectCount = 0;
746 auto rectArray = damage.getArray(&rectCount);
747
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800748 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700749 for (size_t rect = 0; rect < rectCount; ++rect) {
750 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
751 rectArray[rect].right, rectArray[rect].bottom});
752 }
753
Steven Thomas94e35b92017-07-26 18:48:28 -0700754 intError = mComposer.setLayerSurfaceDamage(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700755 }
756
757 return static_cast<Error>(intError);
758}
759
760Error Layer::setBlendMode(BlendMode mode)
761{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800762 auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700763 auto intError = mComposer.setLayerBlendMode(mDisplayId, mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700764 return static_cast<Error>(intError);
765}
766
767Error Layer::setColor(hwc_color_t color)
768{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800769 Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
Steven Thomas94e35b92017-07-26 18:48:28 -0700770 auto intError = mComposer.setLayerColor(mDisplayId, mId, hwcColor);
Dan Stoza651bf312015-10-23 17:03:17 -0700771 return static_cast<Error>(intError);
772}
773
774Error Layer::setCompositionType(Composition type)
775{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800776 auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
Steven Thomas94e35b92017-07-26 18:48:28 -0700777 auto intError = mComposer.setLayerCompositionType(
778 mDisplayId, mId, intType);
Dan Stoza651bf312015-10-23 17:03:17 -0700779 return static_cast<Error>(intError);
780}
781
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700782Error Layer::setDataspace(Dataspace dataspace)
Dan Stoza5df2a862016-03-24 16:19:37 -0700783{
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600784 if (dataspace == mDataSpace) {
785 return Error::None;
786 }
787 mDataSpace = dataspace;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700788 auto intError = mComposer.setLayerDataspace(mDisplayId, mId, mDataSpace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700789 return static_cast<Error>(intError);
790}
791
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700792Error Layer::setHdrMetadata(const android::HdrMetadata& metadata) {
793 if (metadata == mHdrMetadata) {
794 return Error::None;
795 }
796
797 mHdrMetadata = metadata;
798 auto intError = mComposer.setLayerHdrMetadata(mDisplayId, mId, metadata);
799 return static_cast<Error>(intError);
800}
801
Dan Stoza651bf312015-10-23 17:03:17 -0700802Error Layer::setDisplayFrame(const Rect& frame)
803{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800804 Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800805 frame.right, frame.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700806 auto intError = mComposer.setLayerDisplayFrame(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700807 return static_cast<Error>(intError);
808}
809
810Error Layer::setPlaneAlpha(float alpha)
811{
Steven Thomas94e35b92017-07-26 18:48:28 -0700812 auto intError = mComposer.setLayerPlaneAlpha(mDisplayId, mId, alpha);
Dan Stoza651bf312015-10-23 17:03:17 -0700813 return static_cast<Error>(intError);
814}
815
816Error Layer::setSidebandStream(const native_handle_t* stream)
817{
Steven Thomas94e35b92017-07-26 18:48:28 -0700818 if (mCapabilities.count(Capability::SidebandStream) == 0) {
Dan Stoza09e7a272016-04-14 12:31:01 -0700819 ALOGE("Attempted to call setSidebandStream without checking that the "
820 "device supports sideband streams");
821 return Error::Unsupported;
822 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700823 auto intError = mComposer.setLayerSidebandStream(mDisplayId, mId, stream);
Dan Stoza651bf312015-10-23 17:03:17 -0700824 return static_cast<Error>(intError);
825}
826
827Error Layer::setSourceCrop(const FloatRect& crop)
828{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800829 Hwc2::IComposerClient::FRect hwcRect{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800830 crop.left, crop.top, crop.right, crop.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700831 auto intError = mComposer.setLayerSourceCrop(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700832 return static_cast<Error>(intError);
833}
834
835Error Layer::setTransform(Transform transform)
836{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800837 auto intTransform = static_cast<Hwc2::Transform>(transform);
Steven Thomas94e35b92017-07-26 18:48:28 -0700838 auto intError = mComposer.setLayerTransform(mDisplayId, mId, intTransform);
Dan Stoza651bf312015-10-23 17:03:17 -0700839 return static_cast<Error>(intError);
840}
841
842Error Layer::setVisibleRegion(const Region& region)
843{
844 size_t rectCount = 0;
845 auto rectArray = region.getArray(&rectCount);
846
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800847 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700848 for (size_t rect = 0; rect < rectCount; ++rect) {
849 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
850 rectArray[rect].right, rectArray[rect].bottom});
851 }
852
Steven Thomas94e35b92017-07-26 18:48:28 -0700853 auto intError = mComposer.setLayerVisibleRegion(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700854 return static_cast<Error>(intError);
855}
856
857Error Layer::setZOrder(uint32_t z)
858{
Steven Thomas94e35b92017-07-26 18:48:28 -0700859 auto intError = mComposer.setLayerZOrder(mDisplayId, mId, z);
Dan Stoza651bf312015-10-23 17:03:17 -0700860 return static_cast<Error>(intError);
861}
862
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500863Error Layer::setInfo(uint32_t type, uint32_t appId)
864{
Steven Thomas94e35b92017-07-26 18:48:28 -0700865 auto intError = mComposer.setLayerInfo(mDisplayId, mId, type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500866 return static_cast<Error>(intError);
867}
868
Dan Stoza651bf312015-10-23 17:03:17 -0700869} // namespace HWC2