blob: 0667f8d5b69342c6218f7b55c0747ba8e523b3b6 [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
Peiyong Lin62665892018-04-16 11:07:44 -0700451Error Display::getHdrCapabilities(HdrCapabilities* outCapabilities) const
Dan Stoza7d7ae732016-03-16 12:23:40 -0700452{
Dan Stoza7d7ae732016-03-16 12:23:40 -0700453 float maxLuminance = -1.0f;
454 float maxAverageLuminance = -1.0f;
455 float minLuminance = -1.0f;
Peiyong Lin62665892018-04-16 11:07:44 -0700456 std::vector<Hwc2::Hdr> types;
457 auto intError = mComposer.getHdrCapabilities(mId, &types,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800458 &maxLuminance, &maxAverageLuminance, &minLuminance);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800459 auto error = static_cast<HWC2::Error>(intError);
460
Dan Stoza7d7ae732016-03-16 12:23:40 -0700461 if (error != Error::None) {
462 return error;
463 }
464
Peiyong Lin62665892018-04-16 11:07:44 -0700465 *outCapabilities = HdrCapabilities(std::move(types),
Dan Stoza7d7ae732016-03-16 12:23:40 -0700466 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,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700529 const sp<Fence>& acquireFence, Dataspace dataspace)
Dan Stoza651bf312015-10-23 17:03:17 -0700530{
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,
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700534 fenceFd, dataspace, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700535 return static_cast<Error>(intError);
536}
537
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700538Error Display::setColorMode(ColorMode mode, RenderIntent renderIntent)
Dan Stoza076ac672016-03-14 10:47:53 -0700539{
Peiyong Lin0e7a7912018-04-05 14:36:36 -0700540 auto intError = mComposer.setColorMode(mId, mode, renderIntent);
Dan Stoza076ac672016-03-14 10:47:53 -0700541 return static_cast<Error>(intError);
542}
543
Dan Stoza5df2a862016-03-24 16:19:37 -0700544Error Display::setColorTransform(const android::mat4& matrix,
545 android_color_transform_t hint)
546{
Steven Thomas94e35b92017-07-26 18:48:28 -0700547 auto intError = mComposer.setColorTransform(mId,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800548 matrix.asArray(), static_cast<Hwc2::ColorTransform>(hint));
Dan Stoza5df2a862016-03-24 16:19:37 -0700549 return static_cast<Error>(intError);
550}
551
Dan Stoza651bf312015-10-23 17:03:17 -0700552Error Display::setOutputBuffer(const sp<GraphicBuffer>& buffer,
553 const sp<Fence>& releaseFence)
554{
555 int32_t fenceFd = releaseFence->dup();
556 auto handle = buffer->getNativeBuffer()->handle;
Steven Thomas94e35b92017-07-26 18:48:28 -0700557 auto intError = mComposer.setOutputBuffer(mId, handle, fenceFd);
Dan Stoza38628982016-07-13 15:48:58 -0700558 close(fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700559 return static_cast<Error>(intError);
560}
561
562Error Display::setPowerMode(PowerMode mode)
563{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800564 auto intMode = static_cast<Hwc2::IComposerClient::PowerMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700565 auto intError = mComposer.setPowerMode(mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700566 return static_cast<Error>(intError);
567}
568
569Error Display::setVsyncEnabled(Vsync enabled)
570{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800571 auto intEnabled = static_cast<Hwc2::IComposerClient::Vsync>(enabled);
Steven Thomas94e35b92017-07-26 18:48:28 -0700572 auto intError = mComposer.setVsyncEnabled(mId, intEnabled);
Dan Stoza651bf312015-10-23 17:03:17 -0700573 return static_cast<Error>(intError);
574}
575
576Error Display::validate(uint32_t* outNumTypes, uint32_t* outNumRequests)
577{
578 uint32_t numTypes = 0;
579 uint32_t numRequests = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700580 auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests);
Dan Stoza651bf312015-10-23 17:03:17 -0700581 auto error = static_cast<Error>(intError);
582 if (error != Error::None && error != Error::HasChanges) {
583 return error;
584 }
585
586 *outNumTypes = numTypes;
587 *outNumRequests = numRequests;
588 return error;
589}
590
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700591Error Display::presentOrValidate(uint32_t* outNumTypes, uint32_t* outNumRequests,
592 sp<android::Fence>* outPresentFence, uint32_t* state) {
593
594 uint32_t numTypes = 0;
595 uint32_t numRequests = 0;
596 int32_t presentFenceFd = -1;
Steven Thomas94e35b92017-07-26 18:48:28 -0700597 auto intError = mComposer.presentOrValidateDisplay(
598 mId, &numTypes, &numRequests, &presentFenceFd, state);
Fabien Sanglard249c0ae2017-06-19 19:22:36 -0700599 auto error = static_cast<Error>(intError);
600 if (error != Error::None && error != Error::HasChanges) {
601 return error;
602 }
603
604 if (*state == 1) {
605 *outPresentFence = new Fence(presentFenceFd);
606 }
607
608 if (*state == 0) {
609 *outNumTypes = numTypes;
610 *outNumRequests = numRequests;
611 }
612 return error;
613}
Chia-I Wu0c6ce462017-06-22 10:48:28 -0700614
Dan Stoza651bf312015-10-23 17:03:17 -0700615// For use by Device
616
Steven Thomas94e35b92017-07-26 18:48:28 -0700617void Display::setConnected(bool connected) {
Steven Thomasb6c6ad42018-01-29 12:22:00 -0800618 if (!mIsConnected && connected) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700619 mComposer.setClientTargetSlotCount(mId);
Steven Thomasb6c6ad42018-01-29 12:22:00 -0800620 if (mType == DisplayType::Physical) {
621 loadConfigs();
622 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700623 }
624 mIsConnected = connected;
625}
626
Dan Stoza651bf312015-10-23 17:03:17 -0700627int32_t Display::getAttribute(hwc2_config_t configId, Attribute attribute)
628{
629 int32_t value = 0;
Steven Thomas94e35b92017-07-26 18:48:28 -0700630 auto intError = mComposer.getDisplayAttribute(mId, configId,
Chia-I Wu67e376d2016-12-19 11:36:22 +0800631 static_cast<Hwc2::IComposerClient::Attribute>(attribute),
632 &value);
Dan Stoza651bf312015-10-23 17:03:17 -0700633 auto error = static_cast<Error>(intError);
634 if (error != Error::None) {
635 ALOGE("getDisplayAttribute(%" PRIu64 ", %u, %s) failed: %s (%d)", mId,
636 configId, to_string(attribute).c_str(),
637 to_string(error).c_str(), intError);
638 return -1;
639 }
640 return value;
641}
642
643void Display::loadConfig(hwc2_config_t configId)
644{
645 ALOGV("[%" PRIu64 "] loadConfig(%u)", mId, configId);
646
647 auto config = Config::Builder(*this, configId)
648 .setWidth(getAttribute(configId, Attribute::Width))
649 .setHeight(getAttribute(configId, Attribute::Height))
650 .setVsyncPeriod(getAttribute(configId, Attribute::VsyncPeriod))
651 .setDpiX(getAttribute(configId, Attribute::DpiX))
652 .setDpiY(getAttribute(configId, Attribute::DpiY))
653 .build();
654 mConfigs.emplace(configId, std::move(config));
655}
656
657void Display::loadConfigs()
658{
659 ALOGV("[%" PRIu64 "] loadConfigs", mId);
660
Chia-I Wuaab99f52016-10-05 12:59:58 +0800661 std::vector<Hwc2::Config> configIds;
Steven Thomas94e35b92017-07-26 18:48:28 -0700662 auto intError = mComposer.getDisplayConfigs(mId, &configIds);
Chia-I Wuaab99f52016-10-05 12:59:58 +0800663 auto error = static_cast<Error>(intError);
Dan Stoza651bf312015-10-23 17:03:17 -0700664 if (error != Error::None) {
665 ALOGE("[%" PRIu64 "] getDisplayConfigs [2] failed: %s (%d)", mId,
666 to_string(error).c_str(), intError);
667 return;
668 }
669
670 for (auto configId : configIds) {
671 loadConfig(configId);
672 }
673}
674
Dan Stoza651bf312015-10-23 17:03:17 -0700675// Other Display methods
676
Steven Thomas94e35b92017-07-26 18:48:28 -0700677Layer* Display::getLayerById(hwc2_layer_t id) const
Dan Stoza651bf312015-10-23 17:03:17 -0700678{
679 if (mLayers.count(id) == 0) {
680 return nullptr;
681 }
682
Steven Thomas94e35b92017-07-26 18:48:28 -0700683 return mLayers.at(id).get();
Dan Stoza651bf312015-10-23 17:03:17 -0700684}
685
686// Layer methods
687
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700688Layer::Layer(android::Hwc2::Composer& composer, const std::unordered_set<Capability>& capabilities,
Steven Thomas94e35b92017-07-26 18:48:28 -0700689 hwc2_display_t displayId, hwc2_layer_t layerId)
690 : mComposer(composer),
691 mCapabilities(capabilities),
692 mDisplayId(displayId),
693 mId(layerId)
Dan Stoza651bf312015-10-23 17:03:17 -0700694{
Steven Thomas94e35b92017-07-26 18:48:28 -0700695 ALOGV("Created layer %" PRIu64 " on display %" PRIu64, layerId, displayId);
Dan Stoza651bf312015-10-23 17:03:17 -0700696}
697
698Layer::~Layer()
699{
Steven Thomas94e35b92017-07-26 18:48:28 -0700700 auto intError = mComposer.destroyLayer(mDisplayId, mId);
701 auto error = static_cast<Error>(intError);
702 ALOGE_IF(error != Error::None, "destroyLayer(%" PRIu64 ", %" PRIu64 ")"
703 " failed: %s (%d)", mDisplayId, mId, to_string(error).c_str(),
704 intError);
705 if (mLayerDestroyedListener) {
706 mLayerDestroyedListener(this);
Dan Stoza651bf312015-10-23 17:03:17 -0700707 }
708}
709
Steven Thomas94e35b92017-07-26 18:48:28 -0700710void Layer::setLayerDestroyedListener(std::function<void(Layer*)> listener) {
711 LOG_ALWAYS_FATAL_IF(mLayerDestroyedListener && listener,
712 "Attempt to set layer destroyed listener multiple times");
713 mLayerDestroyedListener = listener;
714}
715
Dan Stoza651bf312015-10-23 17:03:17 -0700716Error Layer::setCursorPosition(int32_t x, int32_t y)
717{
Steven Thomas94e35b92017-07-26 18:48:28 -0700718 auto intError = mComposer.setCursorPosition(mDisplayId, mId, x, y);
Dan Stoza651bf312015-10-23 17:03:17 -0700719 return static_cast<Error>(intError);
720}
721
Daniel Nicoara1f42e3a2017-04-10 13:27:32 -0400722Error Layer::setBuffer(uint32_t slot, const sp<GraphicBuffer>& buffer,
Dan Stoza651bf312015-10-23 17:03:17 -0700723 const sp<Fence>& acquireFence)
724{
725 int32_t fenceFd = acquireFence->dup();
Steven Thomas94e35b92017-07-26 18:48:28 -0700726 auto intError = mComposer.setLayerBuffer(mDisplayId, mId, slot, buffer,
727 fenceFd);
Dan Stoza651bf312015-10-23 17:03:17 -0700728 return static_cast<Error>(intError);
729}
730
731Error Layer::setSurfaceDamage(const Region& damage)
732{
733 // We encode default full-screen damage as INVALID_RECT upstream, but as 0
734 // rects for HWC
Chia-I Wuaab99f52016-10-05 12:59:58 +0800735 Hwc2::Error intError = Hwc2::Error::NONE;
Dan Stoza651bf312015-10-23 17:03:17 -0700736 if (damage.isRect() && damage.getBounds() == Rect::INVALID_RECT) {
Steven Thomas94e35b92017-07-26 18:48:28 -0700737 intError = mComposer.setLayerSurfaceDamage(mDisplayId,
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800738 mId, std::vector<Hwc2::IComposerClient::Rect>());
Dan Stoza651bf312015-10-23 17:03:17 -0700739 } else {
740 size_t rectCount = 0;
741 auto rectArray = damage.getArray(&rectCount);
742
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800743 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700744 for (size_t rect = 0; rect < rectCount; ++rect) {
745 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
746 rectArray[rect].right, rectArray[rect].bottom});
747 }
748
Steven Thomas94e35b92017-07-26 18:48:28 -0700749 intError = mComposer.setLayerSurfaceDamage(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700750 }
751
752 return static_cast<Error>(intError);
753}
754
755Error Layer::setBlendMode(BlendMode mode)
756{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800757 auto intMode = static_cast<Hwc2::IComposerClient::BlendMode>(mode);
Steven Thomas94e35b92017-07-26 18:48:28 -0700758 auto intError = mComposer.setLayerBlendMode(mDisplayId, mId, intMode);
Dan Stoza651bf312015-10-23 17:03:17 -0700759 return static_cast<Error>(intError);
760}
761
762Error Layer::setColor(hwc_color_t color)
763{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800764 Hwc2::IComposerClient::Color hwcColor{color.r, color.g, color.b, color.a};
Steven Thomas94e35b92017-07-26 18:48:28 -0700765 auto intError = mComposer.setLayerColor(mDisplayId, mId, hwcColor);
Dan Stoza651bf312015-10-23 17:03:17 -0700766 return static_cast<Error>(intError);
767}
768
769Error Layer::setCompositionType(Composition type)
770{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800771 auto intType = static_cast<Hwc2::IComposerClient::Composition>(type);
Steven Thomas94e35b92017-07-26 18:48:28 -0700772 auto intError = mComposer.setLayerCompositionType(
773 mDisplayId, mId, intType);
Dan Stoza651bf312015-10-23 17:03:17 -0700774 return static_cast<Error>(intError);
775}
776
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700777Error Layer::setDataspace(Dataspace dataspace)
Dan Stoza5df2a862016-03-24 16:19:37 -0700778{
Courtney Goeltzenleuchterc988ee42017-05-31 17:56:46 -0600779 if (dataspace == mDataSpace) {
780 return Error::None;
781 }
782 mDataSpace = dataspace;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700783 auto intError = mComposer.setLayerDataspace(mDisplayId, mId, mDataSpace);
Dan Stoza5df2a862016-03-24 16:19:37 -0700784 return static_cast<Error>(intError);
785}
786
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700787Error Layer::setHdrMetadata(const android::HdrMetadata& metadata) {
788 if (metadata == mHdrMetadata) {
789 return Error::None;
790 }
791
792 mHdrMetadata = metadata;
793 auto intError = mComposer.setLayerHdrMetadata(mDisplayId, mId, metadata);
794 return static_cast<Error>(intError);
795}
796
Dan Stoza651bf312015-10-23 17:03:17 -0700797Error Layer::setDisplayFrame(const Rect& frame)
798{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800799 Hwc2::IComposerClient::Rect hwcRect{frame.left, frame.top,
Chia-I Wuaab99f52016-10-05 12:59:58 +0800800 frame.right, frame.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700801 auto intError = mComposer.setLayerDisplayFrame(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700802 return static_cast<Error>(intError);
803}
804
805Error Layer::setPlaneAlpha(float alpha)
806{
Steven Thomas94e35b92017-07-26 18:48:28 -0700807 auto intError = mComposer.setLayerPlaneAlpha(mDisplayId, mId, alpha);
Dan Stoza651bf312015-10-23 17:03:17 -0700808 return static_cast<Error>(intError);
809}
810
811Error Layer::setSidebandStream(const native_handle_t* stream)
812{
Steven Thomas94e35b92017-07-26 18:48:28 -0700813 if (mCapabilities.count(Capability::SidebandStream) == 0) {
Dan Stoza09e7a272016-04-14 12:31:01 -0700814 ALOGE("Attempted to call setSidebandStream without checking that the "
815 "device supports sideband streams");
816 return Error::Unsupported;
817 }
Steven Thomas94e35b92017-07-26 18:48:28 -0700818 auto intError = mComposer.setLayerSidebandStream(mDisplayId, mId, stream);
Dan Stoza651bf312015-10-23 17:03:17 -0700819 return static_cast<Error>(intError);
820}
821
822Error Layer::setSourceCrop(const FloatRect& crop)
823{
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800824 Hwc2::IComposerClient::FRect hwcRect{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800825 crop.left, crop.top, crop.right, crop.bottom};
Steven Thomas94e35b92017-07-26 18:48:28 -0700826 auto intError = mComposer.setLayerSourceCrop(mDisplayId, mId, hwcRect);
Dan Stoza651bf312015-10-23 17:03:17 -0700827 return static_cast<Error>(intError);
828}
829
830Error Layer::setTransform(Transform transform)
831{
Chia-I Wuaab99f52016-10-05 12:59:58 +0800832 auto intTransform = static_cast<Hwc2::Transform>(transform);
Steven Thomas94e35b92017-07-26 18:48:28 -0700833 auto intError = mComposer.setLayerTransform(mDisplayId, mId, intTransform);
Dan Stoza651bf312015-10-23 17:03:17 -0700834 return static_cast<Error>(intError);
835}
836
837Error Layer::setVisibleRegion(const Region& region)
838{
839 size_t rectCount = 0;
840 auto rectArray = region.getArray(&rectCount);
841
Chia-I Wucd8d7f02016-11-16 11:02:31 +0800842 std::vector<Hwc2::IComposerClient::Rect> hwcRects;
Dan Stoza651bf312015-10-23 17:03:17 -0700843 for (size_t rect = 0; rect < rectCount; ++rect) {
844 hwcRects.push_back({rectArray[rect].left, rectArray[rect].top,
845 rectArray[rect].right, rectArray[rect].bottom});
846 }
847
Steven Thomas94e35b92017-07-26 18:48:28 -0700848 auto intError = mComposer.setLayerVisibleRegion(mDisplayId, mId, hwcRects);
Dan Stoza651bf312015-10-23 17:03:17 -0700849 return static_cast<Error>(intError);
850}
851
852Error Layer::setZOrder(uint32_t z)
853{
Steven Thomas94e35b92017-07-26 18:48:28 -0700854 auto intError = mComposer.setLayerZOrder(mDisplayId, mId, z);
Dan Stoza651bf312015-10-23 17:03:17 -0700855 return static_cast<Error>(intError);
856}
857
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500858Error Layer::setInfo(uint32_t type, uint32_t appId)
859{
Steven Thomas94e35b92017-07-26 18:48:28 -0700860 auto intError = mComposer.setLayerInfo(mDisplayId, mId, type, appId);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500861 return static_cast<Error>(intError);
862}
863
Dan Stoza651bf312015-10-23 17:03:17 -0700864} // namespace HWC2