blob: d50a768474a4eabe6fdefb09f569ddd4e48fdc51 [file] [log] [blame]
Lloyd Pique45a165a2018-10-19 11:54:47 -07001/*
2 * Copyright 2019 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
Lloyd Pique32cbe282018-10-19 13:09:22 -070017#include <android-base/stringprintf.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070018#include <compositionengine/CompositionEngine.h>
Lloyd Piqued3d69882019-02-28 16:03:46 -080019#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070020#include <compositionengine/DisplayCreationArgs.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070021#include <compositionengine/DisplaySurface.h>
Lloyd Piquedf336d92019-03-07 21:38:42 -080022#include <compositionengine/LayerFE.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070023#include <compositionengine/impl/Display.h>
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070024#include <compositionengine/impl/DisplayColorProfile.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070025#include <compositionengine/impl/DumpHelpers.h>
Lloyd Pique66d68602019-02-13 14:23:31 -080026#include <compositionengine/impl/OutputLayer.h>
Lloyd Pique31cb2942018-10-19 17:23:03 -070027#include <compositionengine/impl/RenderSurface.h>
Leon Scroggins III5a655b82022-09-07 13:17:09 -040028#include <gui/TraceUtils.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080029
Lloyd Pique66d68602019-02-13 14:23:31 -080030#include <utils/Trace.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070031
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080032// TODO(b/129481165): remove the #pragma below and fix conversion issues
33#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wconversion"
35
Lloyd Pique45a165a2018-10-19 11:54:47 -070036#include "DisplayHardware/HWComposer.h"
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080037
38// TODO(b/129481165): remove the #pragma below and fix conversion issues
39#pragma clang diagnostic pop // ignored "-Wconversion"
40
Lloyd Pique688abd42019-02-15 15:42:24 -080041#include "DisplayHardware/PowerAdvisor.h"
Lloyd Pique45a165a2018-10-19 11:54:47 -070042
Ady Abrahamde549d42022-01-26 19:19:17 -080043using aidl::android::hardware::graphics::composer3::Capability;
Leon Scroggins III5967aec2021-12-29 11:14:22 -050044using aidl::android::hardware::graphics::composer3::DisplayCapability;
45
Lloyd Pique45a165a2018-10-19 11:54:47 -070046namespace android::compositionengine::impl {
47
Lloyd Pique35fca9d2019-02-13 14:24:11 -080048std::shared_ptr<Display> createDisplay(
Lloyd Pique45a165a2018-10-19 11:54:47 -070049 const compositionengine::CompositionEngine& compositionEngine,
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070050 const compositionengine::DisplayCreationArgs& args) {
51 return createDisplayTemplated<Display>(compositionEngine, args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070052}
53
Lloyd Pique45a165a2018-10-19 11:54:47 -070054Display::~Display() = default;
55
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070056void Display::setConfiguration(const compositionengine::DisplayCreationArgs& args) {
Dominik Laskowski13948602021-03-08 20:48:28 -080057 mId = args.id;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070058 mPowerAdvisor = args.powerAdvisor;
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070059 editState().isSecure = args.isSecure;
Angel Aguayob084e0c2021-08-04 23:27:28 +000060 editState().displaySpace.setBounds(args.pixels);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070061 setName(args.name);
Lloyd Piqueaad4ebf2019-10-03 17:58:30 -070062}
63
64bool Display::isValid() const {
65 return Output::isValid() && mPowerAdvisor;
66}
67
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020068DisplayId Display::getId() const {
Lloyd Pique45a165a2018-10-19 11:54:47 -070069 return mId;
70}
71
72bool Display::isSecure() const {
Lloyd Pique32cbe282018-10-19 13:09:22 -070073 return getState().isSecure;
Lloyd Pique45a165a2018-10-19 11:54:47 -070074}
75
76bool Display::isVirtual() const {
Dominik Laskowski29fa1462021-04-27 15:51:50 -070077 return VirtualDisplayId::tryCast(mId).has_value();
Lloyd Pique45a165a2018-10-19 11:54:47 -070078}
79
Lloyd Pique6c564cf2019-05-17 17:31:36 -070080std::optional<DisplayId> Display::getDisplayId() const {
81 return mId;
82}
83
Lloyd Pique45a165a2018-10-19 11:54:47 -070084void Display::disconnect() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020085 if (mIsDisconnected) {
Lloyd Pique45a165a2018-10-19 11:54:47 -070086 return;
87 }
88
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020089 mIsDisconnected = true;
Dominik Laskowski13948602021-03-08 20:48:28 -080090
91 if (const auto id = HalDisplayId::tryCast(mId)) {
92 getCompositionEngine().getHwComposer().disconnectDisplay(*id);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020093 }
Lloyd Pique45a165a2018-10-19 11:54:47 -070094}
95
Lloyd Pique3eb1b212019-03-07 21:15:40 -080096void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
97 Output::setColorTransform(args);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +020098 const auto halDisplayId = HalDisplayId::tryCast(mId);
99 if (mIsDisconnected || !halDisplayId || CC_LIKELY(!args.colorTransformMatrix)) {
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800100 return;
101 }
Lloyd Pique32cbe282018-10-19 13:09:22 -0700102
103 auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200104 status_t result = hwc.setColorTransform(*halDisplayId, *args.colorTransformMatrix);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700105 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200106 to_string(mId).c_str(), result);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700107}
108
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800109void Display::setColorProfile(const ColorProfile& colorProfile) {
110 const ui::Dataspace targetDataspace =
111 getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace,
112 colorProfile.colorSpaceAgnosticDataspace);
Lloyd Piquef5275482019-01-29 18:42:42 -0800113
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800114 if (colorProfile.mode == getState().colorMode &&
115 colorProfile.dataspace == getState().dataspace &&
116 colorProfile.renderIntent == getState().renderIntent &&
117 targetDataspace == getState().targetDataspace) {
Lloyd Pique32cbe282018-10-19 13:09:22 -0700118 return;
119 }
120
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700121 if (isVirtual()) {
122 ALOGW("%s: Invalid operation on virtual display", __func__);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700123 return;
124 }
125
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800126 Output::setColorProfile(colorProfile);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700127
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200128 const auto physicalId = PhysicalDisplayId::tryCast(mId);
129 LOG_FATAL_IF(!physicalId);
130 getCompositionEngine().getHwComposer().setActiveColorMode(*physicalId, colorProfile.mode,
131 colorProfile.renderIntent);
Lloyd Pique32cbe282018-10-19 13:09:22 -0700132}
133
134void Display::dump(std::string& out) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700135 const char* const type = isVirtual() ? "virtual" : "physical";
136 base::StringAppendF(&out, "Display %s (%s, \"%s\")", to_string(mId).c_str(), type,
137 getName().c_str());
Lloyd Pique32cbe282018-10-19 13:09:22 -0700138
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700139 out.append("\n Composition Display State:\n");
Lloyd Pique32cbe282018-10-19 13:09:22 -0700140 Output::dumpBase(out);
141}
142
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700143void Display::createDisplayColorProfile(const DisplayColorProfileCreationArgs& args) {
144 setDisplayColorProfile(compositionengine::impl::createDisplayColorProfile(args));
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700145}
146
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700147void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
148 setRenderSurface(
149 compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
Lloyd Pique31cb2942018-10-19 17:23:03 -0700150}
151
Vishnu Nair9b079a22020-01-21 14:36:08 -0800152void Display::createClientCompositionCache(uint32_t cacheSize) {
153 cacheClientCompositionRequests(cacheSize);
154}
155
Lloyd Piquedf336d92019-03-07 21:38:42 -0800156std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -0800157 const sp<compositionengine::LayerFE>& layerFE) const {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200158 auto outputLayer = impl::createOutputLayer(*this, layerFE);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800159
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200160 if (const auto halDisplayId = HalDisplayId::tryCast(mId);
161 outputLayer && !mIsDisconnected && halDisplayId) {
Lloyd Piquedf336d92019-03-07 21:38:42 -0800162 auto& hwc = getCompositionEngine().getHwComposer();
Lloyd Pique1b33fc32021-05-07 14:36:58 -0700163 auto hwcLayer = hwc.createLayer(*halDisplayId);
Lloyd Piquedf336d92019-03-07 21:38:42 -0800164 ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
165 getName().c_str());
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200166 outputLayer->setHwcLayer(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -0800167 }
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200168 return outputLayer;
Lloyd Piquedf336d92019-03-07 21:38:42 -0800169}
170
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800171void Display::setReleasedLayers(const compositionengine::CompositionRefreshArgs& refreshArgs) {
172 Output::setReleasedLayers(refreshArgs);
173
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200174 if (mIsDisconnected || GpuVirtualDisplayId::tryCast(mId) ||
175 refreshArgs.layersWithQueuedFrames.empty()) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800176 return;
177 }
178
179 // For layers that are being removed from a HWC display, and that have
180 // queued frames, add them to a a list of released layers so we can properly
181 // set a fence.
182 compositionengine::Output::ReleasedLayers releasedLayers;
183
184 // Any non-null entries in the current list of layers are layers that are no
185 // longer going to be visible
Lloyd Piquede196652020-01-22 17:29:58 -0800186 for (auto* outputLayer : getOutputLayersOrderedByZ()) {
187 if (!outputLayer) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800188 continue;
189 }
190
Lloyd Piquede196652020-01-22 17:29:58 -0800191 compositionengine::LayerFE* layerFE = &outputLayer->getLayerFE();
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800192 const bool hasQueuedFrames =
Lloyd Piquede196652020-01-22 17:29:58 -0800193 std::any_of(refreshArgs.layersWithQueuedFrames.cbegin(),
194 refreshArgs.layersWithQueuedFrames.cend(),
195 [layerFE](sp<compositionengine::LayerFE> layerWithQueuedFrames) {
196 return layerFE == layerWithQueuedFrames.get();
197 });
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800198
199 if (hasQueuedFrames) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700200 releasedLayers.emplace_back(wp<LayerFE>::fromExisting(layerFE));
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800201 }
202 }
203
204 setReleasedLayers(std::move(releasedLayers));
205}
206
joenchene72ba5e2022-08-24 13:08:58 +0000207void Display::applyDisplayBrightness(const bool applyImmediately) {
208 auto& hwc = getCompositionEngine().getHwComposer();
209 const auto halDisplayId = HalDisplayId::tryCast(*getDisplayId());
210 if (const auto physicalDisplayId = PhysicalDisplayId::tryCast(*halDisplayId);
211 physicalDisplayId && getState().displayBrightness) {
212 const status_t result =
213 hwc.setDisplayBrightness(*physicalDisplayId, *getState().displayBrightness,
214 getState().displayBrightnessNits,
215 Hwc2::Composer::DisplayBrightnessOptions{
216 .applyImmediately = applyImmediately})
217 .get();
218 ALOGE_IF(result != NO_ERROR, "setDisplayBrightness failed for %s: %d, (%s)",
219 getName().c_str(), result, strerror(-result));
220 }
221 // Clear out the display brightness now that it's been communicated to composer.
222 editState().displayBrightness.reset();
223}
224
Vishnu Naira3140382022-02-24 14:07:11 -0800225void Display::beginFrame() {
226 Output::beginFrame();
Lloyd Pique66d68602019-02-13 14:23:31 -0800227
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200228 // If we don't have a HWC display, then we are done.
229 const auto halDisplayId = HalDisplayId::tryCast(mId);
230 if (!halDisplayId) {
Robert Carr6fe2bef2022-03-09 13:49:41 -0800231 return;
Lloyd Pique66d68602019-02-13 14:23:31 -0800232 }
233
joenchene72ba5e2022-08-24 13:08:58 +0000234 applyDisplayBrightness(false);
Vishnu Naira3140382022-02-24 14:07:11 -0800235}
Alec Mouricdf16792021-12-10 13:16:06 -0800236
Vishnu Naira3140382022-02-24 14:07:11 -0800237bool Display::chooseCompositionStrategy(
238 std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
Leon Scroggins III5a655b82022-09-07 13:17:09 -0400239 ATRACE_FORMAT("%s for %s", __func__, getNamePlusId().c_str());
Vishnu Naira3140382022-02-24 14:07:11 -0800240 ALOGV(__FUNCTION__);
241
242 if (mIsDisconnected) {
243 return false;
244 }
245
246 // If we don't have a HWC display, then we are done.
247 const auto halDisplayId = HalDisplayId::tryCast(mId);
248 if (!halDisplayId) {
249 return false;
250 }
251
252 // Get any composition changes requested by the HWC device, and apply them.
253 std::optional<android::HWComposer::DeviceRequestedChanges> changes;
254 auto& hwc = getCompositionEngine().getHwComposer();
Matt Buckley50c44062022-01-17 20:48:10 +0000255 const bool requiresClientComposition = anyLayersRequireClientComposition();
Matt Buckley3d398a02022-10-28 09:03:37 +0000256
257 if (isPowerHintSessionEnabled()) {
258 mPowerAdvisor->setRequiresClientComposition(mId, requiresClientComposition);
259 }
260
261 const TimePoint hwcValidateStartTime = TimePoint::now();
262
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200263 if (status_t result =
Matt Buckley50c44062022-01-17 20:48:10 +0000264 hwc.getDeviceCompositionChanges(*halDisplayId, requiresClientComposition,
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700265 getState().earliestPresentTime,
Ady Abraham43065bd2021-12-10 17:22:15 -0800266 getState().previousPresentFence,
Vishnu Naira3140382022-02-24 14:07:11 -0800267 getState().expectedPresentTime, outChanges);
Lloyd Pique66d68602019-02-13 14:23:31 -0800268 result != NO_ERROR) {
269 ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
270 strerror(-result));
Vishnu Naira3140382022-02-24 14:07:11 -0800271 return false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800272 }
Vishnu Naira3140382022-02-24 14:07:11 -0800273
Matt Buckley50c44062022-01-17 20:48:10 +0000274 if (isPowerHintSessionEnabled()) {
Matt Buckley3d398a02022-10-28 09:03:37 +0000275 mPowerAdvisor->setHwcValidateTiming(mId, hwcValidateStartTime, TimePoint::now());
276 if (auto halDisplayId = HalDisplayId::tryCast(mId)) {
277 mPowerAdvisor->setSkippedValidate(mId, hwc.getValidateSkipped(*halDisplayId));
278 }
Matt Buckley50c44062022-01-17 20:48:10 +0000279 }
280
Vishnu Naira3140382022-02-24 14:07:11 -0800281 return true;
282}
283
284void Display::applyCompositionStrategy(const std::optional<DeviceRequestedChanges>& changes) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800285 if (changes) {
286 applyChangedTypesToLayers(changes->changedTypes);
287 applyDisplayRequests(changes->displayRequests);
288 applyLayerRequestsToLayers(changes->layerRequests);
Alec Mouri85065692022-03-18 00:58:26 +0000289 applyClientTargetRequests(changes->clientTargetProperty);
Lloyd Pique66d68602019-02-13 14:23:31 -0800290 }
291
292 // Determine what type of composition we are doing from the final state
293 auto& state = editState();
294 state.usesClientComposition = anyLayersRequireClientComposition();
295 state.usesDeviceComposition = !allLayersRequireClientComposition();
296}
297
Lloyd Pique688abd42019-02-15 15:42:24 -0800298bool Display::getSkipColorTransform() const {
Dominik Laskowski1162e472020-04-02 19:02:47 -0700299 const auto& hwc = getCompositionEngine().getHwComposer();
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200300 if (const auto halDisplayId = HalDisplayId::tryCast(mId)) {
301 return hwc.hasDisplayCapability(*halDisplayId,
Leon Scroggins III5967aec2021-12-29 11:14:22 -0500302 DisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM);
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200303 }
304
Ady Abrahamde549d42022-01-26 19:19:17 -0800305 return hwc.hasCapability(Capability::SKIP_CLIENT_COLOR_TRANSFORM);
Lloyd Pique688abd42019-02-15 15:42:24 -0800306}
307
Lloyd Pique66d68602019-02-13 14:23:31 -0800308bool Display::allLayersRequireClientComposition() const {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700309 const auto layers = getOutputLayersOrderedByZ();
310 return std::all_of(layers.begin(), layers.end(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800311 [](const auto& layer) { return layer->requiresClientComposition(); });
312}
313
314void Display::applyChangedTypesToLayers(const ChangedTypes& changedTypes) {
315 if (changedTypes.empty()) {
316 return;
317 }
318
Lloyd Pique01c77c12019-04-17 12:48:32 -0700319 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800320 auto hwcLayer = layer->getHwcLayer();
321 if (!hwcLayer) {
322 continue;
323 }
324
325 if (auto it = changedTypes.find(hwcLayer); it != changedTypes.end()) {
326 layer->applyDeviceCompositionTypeChange(
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500327 static_cast<aidl::android::hardware::graphics::composer3::Composition>(
328 it->second));
Lloyd Pique66d68602019-02-13 14:23:31 -0800329 }
330 }
331}
332
333void Display::applyDisplayRequests(const DisplayRequests& displayRequests) {
334 auto& state = editState();
335 state.flipClientTarget = (static_cast<uint32_t>(displayRequests) &
Peiyong Line9d809e2020-04-14 13:10:48 -0700336 static_cast<uint32_t>(hal::DisplayRequest::FLIP_CLIENT_TARGET)) != 0;
Lloyd Pique66d68602019-02-13 14:23:31 -0800337 // Note: HWC2::DisplayRequest::WriteClientTargetToOutput is currently ignored.
338}
339
340void Display::applyLayerRequestsToLayers(const LayerRequests& layerRequests) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700341 for (auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique66d68602019-02-13 14:23:31 -0800342 layer->prepareForDeviceLayerRequests();
343
344 auto hwcLayer = layer->getHwcLayer();
345 if (!hwcLayer) {
346 continue;
347 }
348
349 if (auto it = layerRequests.find(hwcLayer); it != layerRequests.end()) {
350 layer->applyDeviceLayerRequest(
351 static_cast<Hwc2::IComposerClient::LayerRequest>(it->second));
352 }
353 }
354}
355
Alec Mouri85065692022-03-18 00:58:26 +0000356void Display::applyClientTargetRequests(const ClientTargetProperty& clientTargetProperty) {
357 if (static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace) ==
358 ui::Dataspace::UNKNOWN) {
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700359 return;
360 }
Ady Abraham0094dc62021-06-03 10:08:33 -0700361
Alec Mouri85065692022-03-18 00:58:26 +0000362 editState().dataspace =
363 static_cast<ui::Dataspace>(clientTargetProperty.clientTargetProperty.dataspace);
364 editState().clientTargetBrightness = clientTargetProperty.brightness;
365 editState().clientTargetDimmingStage = clientTargetProperty.dimmingStage;
366 getRenderSurface()->setBufferDataspace(editState().dataspace);
367 getRenderSurface()->setBufferPixelFormat(
368 static_cast<ui::PixelFormat>(clientTargetProperty.clientTargetProperty.pixelFormat));
Peiyong Lindfc3f7c2020-05-07 20:15:50 -0700369}
370
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800371compositionengine::Output::FrameFences Display::presentAndGetFrameFences() {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200372 auto fences = impl::Output::presentAndGetFrameFences();
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800373
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200374 const auto halDisplayIdOpt = HalDisplayId::tryCast(mId);
375 if (mIsDisconnected || !halDisplayIdOpt) {
376 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800377 }
378
379 auto& hwc = getCompositionEngine().getHwComposer();
Matt Buckley50c44062022-01-17 20:48:10 +0000380
Matt Buckley2fa85012022-08-30 22:38:45 +0000381 const TimePoint startTime = TimePoint::now();
Matt Buckley50c44062022-01-17 20:48:10 +0000382
383 if (isPowerHintSessionEnabled()) {
384 if (!getCompositionEngine().getHwComposer().getComposer()->isSupported(
385 Hwc2::Composer::OptionalFeature::ExpectedPresentTime) &&
386 getState().previousPresentFence->getSignalTime() != Fence::SIGNAL_TIME_PENDING) {
Matt Buckley16dec1f2022-06-07 21:46:20 +0000387 mPowerAdvisor->setHwcPresentDelayedTime(mId, getState().earliestPresentTime);
Matt Buckley50c44062022-01-17 20:48:10 +0000388 }
389 }
390
Ady Abrahamec7aa8a2021-06-28 12:37:09 -0700391 hwc.presentAndGetReleaseFences(*halDisplayIdOpt, getState().earliestPresentTime,
392 getState().previousPresentFence);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800393
Matt Buckley50c44062022-01-17 20:48:10 +0000394 if (isPowerHintSessionEnabled()) {
Matt Buckley2fa85012022-08-30 22:38:45 +0000395 mPowerAdvisor->setHwcPresentTiming(mId, startTime, TimePoint::now());
Matt Buckley50c44062022-01-17 20:48:10 +0000396 }
397
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200398 fences.presentFence = hwc.getPresentFence(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800399
400 // TODO(b/121291683): Change HWComposer call to return entire map
Lloyd Pique01c77c12019-04-17 12:48:32 -0700401 for (const auto* layer : getOutputLayersOrderedByZ()) {
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800402 auto hwcLayer = layer->getHwcLayer();
403 if (!hwcLayer) {
404 continue;
405 }
406
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200407 fences.layerFences.emplace(hwcLayer, hwc.getLayerReleaseFence(*halDisplayIdOpt, hwcLayer));
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800408 }
409
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200410 hwc.clearReleaseFences(*halDisplayIdOpt);
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800411
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200412 return fences;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800413}
414
Lloyd Pique688abd42019-02-15 15:42:24 -0800415void Display::setExpensiveRenderingExpected(bool enabled) {
416 Output::setExpensiveRenderingExpected(enabled);
417
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200418 if (mPowerAdvisor && !GpuVirtualDisplayId::tryCast(mId)) {
419 mPowerAdvisor->setExpensiveRenderingExpected(mId, enabled);
Lloyd Pique688abd42019-02-15 15:42:24 -0800420 }
421}
422
Matt Buckley50c44062022-01-17 20:48:10 +0000423bool Display::isPowerHintSessionEnabled() {
424 return mPowerAdvisor != nullptr && mPowerAdvisor->usePowerHintSession();
425}
426
427void Display::setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) {
428 mPowerAdvisor->setGpuFenceTime(mId, std::move(gpuFence));
429}
430
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000431void Display::finishFrame(GpuCompositionResult&& result) {
Lloyd Piqued3d69882019-02-28 16:03:46 -0800432 // We only need to actually compose the display if:
433 // 1) It is being handled by hardware composer, which may need this to
434 // keep its virtual display state machine in sync, or
435 // 2) There is work to be done (the dirty region isn't empty)
Chavi Weingarten09fa1d62022-08-17 21:57:04 +0000436 if (GpuVirtualDisplayId::tryCast(mId) && !mustRecompose()) {
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200437 ALOGV("Skipping display composition");
438 return;
Lloyd Piqued3d69882019-02-28 16:03:46 -0800439 }
440
Carlos Martinez Romeroe5d57ea2022-11-15 19:14:36 +0000441 impl::Output::finishFrame(std::move(result));
Lloyd Piqued3d69882019-02-28 16:03:46 -0800442}
443
Lloyd Pique45a165a2018-10-19 11:54:47 -0700444} // namespace android::compositionengine::impl