• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 // #define LOG_NDEBUG 0
22 #undef LOG_TAG
23 #define LOG_TAG "DisplayDevice"
24 
25 #include <android-base/stringprintf.h>
26 #include <compositionengine/CompositionEngine.h>
27 #include <compositionengine/Display.h>
28 #include <compositionengine/DisplayColorProfile.h>
29 #include <compositionengine/DisplayColorProfileCreationArgs.h>
30 #include <compositionengine/DisplayCreationArgs.h>
31 #include <compositionengine/DisplaySurface.h>
32 #include <compositionengine/ProjectionSpace.h>
33 #include <compositionengine/RenderSurface.h>
34 #include <compositionengine/RenderSurfaceCreationArgs.h>
35 #include <compositionengine/impl/OutputCompositionState.h>
36 #include <configstore/Utils.h>
37 #include <log/log.h>
38 #include <system/window.h>
39 #include <ui/GraphicTypes.h>
40 
41 #include "DisplayDevice.h"
42 #include "Layer.h"
43 #include "SurfaceFlinger.h"
44 
45 namespace android {
46 
47 namespace hal = hardware::graphics::composer::hal;
48 
49 using android::base::StringAppendF;
50 
51 ui::Transform::RotationFlags DisplayDevice::sPrimaryDisplayRotationFlags = ui::Transform::ROT_0;
52 
DisplayDeviceCreationArgs(const sp<SurfaceFlinger> & flinger,HWComposer & hwComposer,const wp<IBinder> & displayToken,std::shared_ptr<compositionengine::Display> compositionDisplay)53 DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
54         const sp<SurfaceFlinger>& flinger, HWComposer& hwComposer, const wp<IBinder>& displayToken,
55         std::shared_ptr<compositionengine::Display> compositionDisplay)
56       : flinger(flinger),
57         hwComposer(hwComposer),
58         displayToken(displayToken),
59         compositionDisplay(compositionDisplay) {}
60 
DisplayDevice(DisplayDeviceCreationArgs & args)61 DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
62       : mFlinger(args.flinger),
63         mHwComposer(args.hwComposer),
64         mDisplayToken(args.displayToken),
65         mSequenceId(args.sequenceId),
66         mConnectionType(args.connectionType),
67         mCompositionDisplay{args.compositionDisplay},
68         mPhysicalOrientation(args.physicalOrientation),
69         mSupportedModes(std::move(args.supportedModes)),
70         mIsPrimary(args.isPrimary) {
71     mCompositionDisplay->editState().isSecure = args.isSecure;
72     mCompositionDisplay->createRenderSurface(
73             compositionengine::RenderSurfaceCreationArgsBuilder()
74                     .setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
75                     .setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
76                     .setNativeWindow(std::move(args.nativeWindow))
77                     .setDisplaySurface(std::move(args.displaySurface))
78                     .setMaxTextureCacheSize(
79                             static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
80                     .build());
81 
82     if (!mFlinger->mDisableClientCompositionCache &&
83         SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
84         mCompositionDisplay->createClientCompositionCache(
85                 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
86     }
87 
88     mCompositionDisplay->createDisplayColorProfile(
89             compositionengine::DisplayColorProfileCreationArgs{args.hasWideColorGamut,
90                                                                std::move(args.hdrCapabilities),
91                                                                args.supportedPerFrameMetadata,
92                                                                args.hwcColorModes});
93 
94     if (!mCompositionDisplay->isValid()) {
95         ALOGE("Composition Display did not validate!");
96     }
97 
98     mCompositionDisplay->getRenderSurface()->initialize();
99 
100     setPowerMode(args.initialPowerMode);
101 
102     // initialize the display orientation transform.
103     setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
104 }
105 
106 DisplayDevice::~DisplayDevice() = default;
107 
disconnect()108 void DisplayDevice::disconnect() {
109     mCompositionDisplay->disconnect();
110 }
111 
getWidth() const112 int DisplayDevice::getWidth() const {
113     return mCompositionDisplay->getState().displaySpace.bounds.getWidth();
114 }
115 
getHeight() const116 int DisplayDevice::getHeight() const {
117     return mCompositionDisplay->getState().displaySpace.bounds.getHeight();
118 }
119 
setDisplayName(const std::string & displayName)120 void DisplayDevice::setDisplayName(const std::string& displayName) {
121     if (!displayName.empty()) {
122         // never override the name with an empty name
123         mDisplayName = displayName;
124         mCompositionDisplay->setName(displayName);
125     }
126 }
127 
setDeviceProductInfo(std::optional<DeviceProductInfo> info)128 void DisplayDevice::setDeviceProductInfo(std::optional<DeviceProductInfo> info) {
129     mDeviceProductInfo = std::move(info);
130 }
131 
getPageFlipCount() const132 uint32_t DisplayDevice::getPageFlipCount() const {
133     return mCompositionDisplay->getRenderSurface()->getPageFlipCount();
134 }
135 
136 // ----------------------------------------------------------------------------
setPowerMode(hal::PowerMode mode)137 void DisplayDevice::setPowerMode(hal::PowerMode mode) {
138     mPowerMode = mode;
139     getCompositionDisplay()->setCompositionEnabled(mPowerMode != hal::PowerMode::OFF);
140 }
141 
enableLayerCaching(bool enable)142 void DisplayDevice::enableLayerCaching(bool enable) {
143     getCompositionDisplay()->setLayerCachingEnabled(enable);
144 }
145 
getPowerMode() const146 hal::PowerMode DisplayDevice::getPowerMode() const {
147     return mPowerMode;
148 }
149 
isPoweredOn() const150 bool DisplayDevice::isPoweredOn() const {
151     return mPowerMode != hal::PowerMode::OFF;
152 }
153 
setActiveMode(DisplayModeId id)154 void DisplayDevice::setActiveMode(DisplayModeId id) {
155     const auto mode = getMode(id);
156     LOG_FATAL_IF(!mode, "Cannot set active mode which is not supported.");
157     mActiveMode = mode;
158 }
159 
initiateModeChange(DisplayModeId modeId,const hal::VsyncPeriodChangeConstraints & constraints,hal::VsyncPeriodChangeTimeline * outTimeline) const160 status_t DisplayDevice::initiateModeChange(DisplayModeId modeId,
161                                            const hal::VsyncPeriodChangeConstraints& constraints,
162                                            hal::VsyncPeriodChangeTimeline* outTimeline) const {
163     const auto mode = getMode(modeId);
164     if (!mode) {
165         ALOGE("Trying to initiate a mode change to invalid mode %s on display %s",
166               std::to_string(modeId.value()).c_str(), to_string(getId()).c_str());
167         return BAD_VALUE;
168     }
169     return mHwComposer.setActiveModeWithConstraints(getPhysicalId(), mode->getHwcId(), constraints,
170                                                     outTimeline);
171 }
172 
getActiveMode() const173 const DisplayModePtr& DisplayDevice::getActiveMode() const {
174     return mActiveMode;
175 }
176 
getSupportedModes() const177 const DisplayModes& DisplayDevice::getSupportedModes() const {
178     return mSupportedModes;
179 }
180 
getMode(DisplayModeId modeId) const181 DisplayModePtr DisplayDevice::getMode(DisplayModeId modeId) const {
182     const auto it = std::find_if(mSupportedModes.begin(), mSupportedModes.end(),
183                                  [&](DisplayModePtr mode) { return mode->getId() == modeId; });
184     if (it != mSupportedModes.end()) {
185         return *it;
186     }
187     return nullptr;
188 }
189 
getVsyncPeriodFromHWC() const190 nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
191     const auto physicalId = getPhysicalId();
192     if (!mHwComposer.isConnected(physicalId)) {
193         return 0;
194     }
195 
196     nsecs_t vsyncPeriod;
197     const auto status = mHwComposer.getDisplayVsyncPeriod(physicalId, &vsyncPeriod);
198     if (status == NO_ERROR) {
199         return vsyncPeriod;
200     }
201 
202     return getActiveMode()->getFps().getPeriodNsecs();
203 }
204 
getRefreshTimestamp() const205 nsecs_t DisplayDevice::getRefreshTimestamp() const {
206     const nsecs_t now = systemTime(CLOCK_MONOTONIC);
207     const auto vsyncPeriodNanos = getVsyncPeriodFromHWC();
208     return now - ((now - mLastHwVsync) % vsyncPeriodNanos);
209 }
210 
onVsync(nsecs_t timestamp)211 void DisplayDevice::onVsync(nsecs_t timestamp) {
212     mLastHwVsync = timestamp;
213 }
214 
getCompositionDataSpace() const215 ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
216     return mCompositionDisplay->getState().dataspace;
217 }
218 
setLayerStack(ui::LayerStack stack)219 void DisplayDevice::setLayerStack(ui::LayerStack stack) {
220     mCompositionDisplay->setLayerStackFilter(stack, isPrimary());
221 }
222 
setDisplaySize(int width,int height)223 void DisplayDevice::setDisplaySize(int width, int height) {
224     LOG_FATAL_IF(!isVirtual(), "Changing the display size is supported only for virtual displays.");
225     mCompositionDisplay->setDisplaySize(ui::Size(width, height));
226 }
227 
setProjection(ui::Rotation orientation,Rect layerStackSpaceRect,Rect orientedDisplaySpaceRect)228 void DisplayDevice::setProjection(ui::Rotation orientation, Rect layerStackSpaceRect,
229                                   Rect orientedDisplaySpaceRect) {
230     mOrientation = orientation;
231 
232     if (isPrimary()) {
233         sPrimaryDisplayRotationFlags = ui::Transform::toRotationFlags(orientation);
234     }
235 
236     if (!orientedDisplaySpaceRect.isValid()) {
237         // The destination frame can be invalid if it has never been set,
238         // in that case we assume the whole display size.
239         orientedDisplaySpaceRect = getCompositionDisplay()->getState().displaySpace.bounds;
240     }
241 
242     if (layerStackSpaceRect.isEmpty()) {
243         // The layerStackSpaceRect can be invalid if it has never been set, in that case
244         // we assume the whole framebuffer size.
245         layerStackSpaceRect = getCompositionDisplay()->getState().framebufferSpace.bounds;
246         if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
247             std::swap(layerStackSpaceRect.right, layerStackSpaceRect.bottom);
248         }
249     }
250 
251     // We need to take care of display rotation for globalTransform for case if the panel is not
252     // installed aligned with device orientation.
253     const auto transformOrientation = orientation + mPhysicalOrientation;
254     getCompositionDisplay()->setProjection(transformOrientation, layerStackSpaceRect,
255                                            orientedDisplaySpaceRect);
256 }
257 
getPrimaryDisplayRotationFlags()258 ui::Transform::RotationFlags DisplayDevice::getPrimaryDisplayRotationFlags() {
259     return sPrimaryDisplayRotationFlags;
260 }
261 
getDebugName() const262 std::string DisplayDevice::getDebugName() const {
263     const char* type = "virtual";
264     if (mConnectionType) {
265         type = *mConnectionType == ui::DisplayConnectionType::Internal ? "internal" : "external";
266     }
267 
268     return base::StringPrintf("DisplayDevice{%s, %s%s, \"%s\"}", to_string(getId()).c_str(), type,
269                               isPrimary() ? ", primary" : "", mDisplayName.c_str());
270 }
271 
dump(std::string & result) const272 void DisplayDevice::dump(std::string& result) const {
273     StringAppendF(&result, "+ %s\n", getDebugName().c_str());
274     StringAppendF(&result, "   powerMode=%s (%d)\n", to_string(mPowerMode).c_str(),
275                   static_cast<int32_t>(mPowerMode));
276     const auto activeMode = getActiveMode();
277     StringAppendF(&result, "   activeMode=%s\n",
278                   activeMode ? to_string(*activeMode).c_str() : "none");
279 
280     result.append("   supportedModes=\n");
281 
282     for (const auto& mode : mSupportedModes) {
283         result.append("     ");
284         result.append(to_string(*mode));
285         result.append("\n");
286     }
287     StringAppendF(&result, "   deviceProductInfo=");
288     if (mDeviceProductInfo) {
289         mDeviceProductInfo->dump(result);
290     } else {
291         result.append("{}");
292     }
293     result.append("\n");
294     getCompositionDisplay()->dump(result);
295 }
296 
hasRenderIntent(ui::RenderIntent intent) const297 bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
298     return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
299 }
300 
getId() const301 DisplayId DisplayDevice::getId() const {
302     return mCompositionDisplay->getId();
303 }
304 
isSecure() const305 bool DisplayDevice::isSecure() const {
306     return mCompositionDisplay->isSecure();
307 }
308 
getBounds() const309 const Rect& DisplayDevice::getBounds() const {
310     return mCompositionDisplay->getState().displaySpace.bounds;
311 }
312 
getUndefinedRegion() const313 const Region& DisplayDevice::getUndefinedRegion() const {
314     return mCompositionDisplay->getState().undefinedRegion;
315 }
316 
needsFiltering() const317 bool DisplayDevice::needsFiltering() const {
318     return mCompositionDisplay->getState().needsFiltering;
319 }
320 
getLayerStack() const321 ui::LayerStack DisplayDevice::getLayerStack() const {
322     return mCompositionDisplay->getState().layerStackId;
323 }
324 
getTransformHint() const325 ui::Transform::RotationFlags DisplayDevice::getTransformHint() const {
326     return mCompositionDisplay->getTransformHint();
327 }
328 
getTransform() const329 const ui::Transform& DisplayDevice::getTransform() const {
330     return mCompositionDisplay->getState().transform;
331 }
332 
getLayerStackSpaceRect() const333 const Rect& DisplayDevice::getLayerStackSpaceRect() const {
334     return mCompositionDisplay->getState().layerStackSpace.content;
335 }
336 
getOrientedDisplaySpaceRect() const337 const Rect& DisplayDevice::getOrientedDisplaySpaceRect() const {
338     return mCompositionDisplay->getState().orientedDisplaySpace.content;
339 }
340 
hasWideColorGamut() const341 bool DisplayDevice::hasWideColorGamut() const {
342     return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
343 }
344 
hasHDR10PlusSupport() const345 bool DisplayDevice::hasHDR10PlusSupport() const {
346     return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
347 }
348 
hasHDR10Support() const349 bool DisplayDevice::hasHDR10Support() const {
350     return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
351 }
352 
hasHLGSupport() const353 bool DisplayDevice::hasHLGSupport() const {
354     return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
355 }
356 
hasDolbyVisionSupport() const357 bool DisplayDevice::hasDolbyVisionSupport() const {
358     return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
359 }
360 
getSupportedPerFrameMetadata() const361 int DisplayDevice::getSupportedPerFrameMetadata() const {
362     return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
363 }
364 
overrideHdrTypes(const std::vector<ui::Hdr> & hdrTypes)365 void DisplayDevice::overrideHdrTypes(const std::vector<ui::Hdr>& hdrTypes) {
366     mOverrideHdrTypes = hdrTypes;
367 }
368 
getHdrCapabilities() const369 HdrCapabilities DisplayDevice::getHdrCapabilities() const {
370     const HdrCapabilities& capabilities =
371             mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
372     std::vector<ui::Hdr> hdrTypes = capabilities.getSupportedHdrTypes();
373     if (!mOverrideHdrTypes.empty()) {
374         hdrTypes = mOverrideHdrTypes;
375     }
376     return HdrCapabilities(hdrTypes, capabilities.getDesiredMaxLuminance(),
377                            capabilities.getDesiredMaxAverageLuminance(),
378                            capabilities.getDesiredMinLuminance());
379 }
380 
381 std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
382 
383 }  // namespace android
384 
385 // TODO(b/129481165): remove the #pragma below and fix conversion issues
386 #pragma clang diagnostic pop // ignored "-Wconversion"
387