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/RenderSurface.h>
33 #include <compositionengine/RenderSurfaceCreationArgs.h>
34 #include <compositionengine/impl/OutputCompositionState.h>
35 #include <configstore/Utils.h>
36 #include <log/log.h>
37 #include <system/window.h>
38 #include <ui/GraphicTypes.h>
39 
40 #include "DisplayDevice.h"
41 #include "Layer.h"
42 #include "SurfaceFlinger.h"
43 
44 namespace android {
45 
46 namespace hal = hardware::graphics::composer::hal;
47 
48 using android::base::StringAppendF;
49 
50 ui::Transform::RotationFlags DisplayDevice::sPrimaryDisplayRotationFlags = ui::Transform::ROT_0;
51 
DisplayDeviceCreationArgs(const sp<SurfaceFlinger> & flinger,const wp<IBinder> & displayToken,std::shared_ptr<compositionengine::Display> compositionDisplay)52 DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(
53         const sp<SurfaceFlinger>& flinger, const wp<IBinder>& displayToken,
54         std::shared_ptr<compositionengine::Display> compositionDisplay)
55       : flinger(flinger), displayToken(displayToken), compositionDisplay(compositionDisplay) {}
56 
DisplayDevice(DisplayDeviceCreationArgs & args)57 DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
58       : mFlinger(args.flinger),
59         mDisplayToken(args.displayToken),
60         mSequenceId(args.sequenceId),
61         mConnectionType(args.connectionType),
62         mCompositionDisplay{args.compositionDisplay},
63         mPhysicalOrientation(args.physicalOrientation),
64         mIsPrimary(args.isPrimary) {
65     mCompositionDisplay->editState().isSecure = args.isSecure;
66     mCompositionDisplay->createRenderSurface(
67             compositionengine::RenderSurfaceCreationArgs{ANativeWindow_getWidth(
68                                                                  args.nativeWindow.get()),
69                                                          ANativeWindow_getHeight(
70                                                                  args.nativeWindow.get()),
71                                                          args.nativeWindow, args.displaySurface});
72 
73     if (!mFlinger->mDisableClientCompositionCache &&
74         SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
75         mCompositionDisplay->createClientCompositionCache(
76                 static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
77     }
78 
79     mCompositionDisplay->createDisplayColorProfile(
80             compositionengine::DisplayColorProfileCreationArgs{args.hasWideColorGamut,
81                                                                std::move(args.hdrCapabilities),
82                                                                args.supportedPerFrameMetadata,
83                                                                args.hwcColorModes});
84 
85     if (!mCompositionDisplay->isValid()) {
86         ALOGE("Composition Display did not validate!");
87     }
88 
89     mCompositionDisplay->getRenderSurface()->initialize();
90 
91     setPowerMode(args.initialPowerMode);
92 
93     // initialize the display orientation transform.
94     setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
95 }
96 
97 DisplayDevice::~DisplayDevice() = default;
98 
disconnect()99 void DisplayDevice::disconnect() {
100     mCompositionDisplay->disconnect();
101 }
102 
getWidth() const103 int DisplayDevice::getWidth() const {
104     return mCompositionDisplay->getState().bounds.getWidth();
105 }
106 
getHeight() const107 int DisplayDevice::getHeight() const {
108     return mCompositionDisplay->getState().bounds.getHeight();
109 }
110 
setDisplayName(const std::string & displayName)111 void DisplayDevice::setDisplayName(const std::string& displayName) {
112     if (!displayName.empty()) {
113         // never override the name with an empty name
114         mDisplayName = displayName;
115         mCompositionDisplay->setName(displayName);
116     }
117 }
118 
getPageFlipCount() const119 uint32_t DisplayDevice::getPageFlipCount() const {
120     return mCompositionDisplay->getRenderSurface()->getPageFlipCount();
121 }
122 
123 // ----------------------------------------------------------------------------
setPowerMode(hal::PowerMode mode)124 void DisplayDevice::setPowerMode(hal::PowerMode mode) {
125     mPowerMode = mode;
126     getCompositionDisplay()->setCompositionEnabled(mPowerMode != hal::PowerMode::OFF);
127 }
128 
getPowerMode() const129 hal::PowerMode DisplayDevice::getPowerMode() const {
130     return mPowerMode;
131 }
132 
isPoweredOn() const133 bool DisplayDevice::isPoweredOn() const {
134     return mPowerMode != hal::PowerMode::OFF;
135 }
136 
setActiveConfig(HwcConfigIndexType mode)137 void DisplayDevice::setActiveConfig(HwcConfigIndexType mode) {
138     mActiveConfig = mode;
139 }
140 
getActiveConfig() const141 HwcConfigIndexType DisplayDevice::getActiveConfig() const {
142     return mActiveConfig;
143 }
144 
getCompositionDataSpace() const145 ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
146     return mCompositionDisplay->getState().dataspace;
147 }
148 
setLayerStack(ui::LayerStack stack)149 void DisplayDevice::setLayerStack(ui::LayerStack stack) {
150     mCompositionDisplay->setLayerStackFilter(stack, isPrimary());
151 }
152 
setDisplaySize(int width,int height)153 void DisplayDevice::setDisplaySize(int width, int height) {
154     mCompositionDisplay->setBounds(ui::Size(width, height));
155 }
156 
setProjection(ui::Rotation orientation,Rect viewport,Rect frame)157 void DisplayDevice::setProjection(ui::Rotation orientation, Rect viewport, Rect frame) {
158     mOrientation = orientation;
159 
160     const Rect& displayBounds = getCompositionDisplay()->getState().bounds;
161     const int displayWidth = displayBounds.width();
162     const int displayHeight = displayBounds.height();
163 
164     ui::Transform rotation;
165     if (const auto flags = ui::Transform::toRotationFlags(orientation);
166         flags != ui::Transform::ROT_INVALID) {
167         rotation.set(flags, displayWidth, displayHeight);
168     }
169 
170     if (!frame.isValid()) {
171         // the destination frame can be invalid if it has never been set,
172         // in that case we assume the whole display frame.
173         frame = Rect(displayWidth, displayHeight);
174     }
175 
176     if (viewport.isEmpty()) {
177         // viewport can be invalid if it has never been set, in that case
178         // we assume the whole display size.
179         // it's also invalid to have an empty viewport, so we handle that
180         // case in the same way.
181         viewport = Rect(displayWidth, displayHeight);
182         if (rotation.getOrientation() & ui::Transform::ROT_90) {
183             // viewport is always specified in the logical orientation
184             // of the display (ie: post-rotation).
185             std::swap(viewport.right, viewport.bottom);
186         }
187     }
188 
189     ui::Transform logicalTranslation, physicalTranslation, scale;
190     const float sourceWidth = viewport.width();
191     const float sourceHeight = viewport.height();
192     const float destWidth = frame.width();
193     const float destHeight = frame.height();
194     if (sourceWidth != destWidth || sourceHeight != destHeight) {
195         const float scaleX = destWidth / sourceWidth;
196         const float scaleY = destHeight / sourceHeight;
197         scale.set(scaleX, 0, 0, scaleY);
198     }
199 
200     const float sourceX = viewport.left;
201     const float sourceY = viewport.top;
202     const float destX = frame.left;
203     const float destY = frame.top;
204     logicalTranslation.set(-sourceX, -sourceY);
205     physicalTranslation.set(destX, destY);
206 
207     // need to take care of primary display rotation for globalTransform
208     // for case if the panel is not installed aligned with device orientation
209     if (isPrimary()) {
210         if (const auto flags = ui::Transform::toRotationFlags(orientation + mPhysicalOrientation);
211             flags != ui::Transform::ROT_INVALID) {
212             rotation.set(flags, displayWidth, displayHeight);
213         }
214     }
215 
216     // The viewport and frame are both in the logical orientation.
217     // Apply the logical translation, scale to physical size, apply the
218     // physical translation and finally rotate to the physical orientation.
219     ui::Transform globalTransform = rotation * physicalTranslation * scale * logicalTranslation;
220 
221     const uint8_t type = globalTransform.getType();
222     const bool needsFiltering =
223             (!globalTransform.preserveRects() || (type >= ui::Transform::SCALE));
224 
225     const Rect& sourceClip = viewport;
226     Rect destinationClip = globalTransform.transform(viewport);
227     if (destinationClip.isEmpty()) {
228         destinationClip = displayBounds;
229     }
230     // Make sure the destination clip is contained in the display bounds
231     destinationClip.intersect(displayBounds, &destinationClip);
232 
233     uint32_t transformOrientation;
234 
235     if (isPrimary()) {
236         sPrimaryDisplayRotationFlags = ui::Transform::toRotationFlags(orientation);
237         transformOrientation = ui::Transform::toRotationFlags(orientation + mPhysicalOrientation);
238     } else {
239         transformOrientation = ui::Transform::toRotationFlags(orientation);
240     }
241 
242     getCompositionDisplay()->setProjection(globalTransform, transformOrientation, frame, viewport,
243                                            sourceClip, destinationClip, needsFiltering);
244 }
245 
getPrimaryDisplayRotationFlags()246 ui::Transform::RotationFlags DisplayDevice::getPrimaryDisplayRotationFlags() {
247     return sPrimaryDisplayRotationFlags;
248 }
249 
getDebugName() const250 std::string DisplayDevice::getDebugName() const {
251     std::string displayId;
252     if (const auto id = getId()) {
253         displayId = to_string(*id) + ", ";
254     }
255 
256     const char* type = "virtual";
257     if (mConnectionType) {
258         type = *mConnectionType == DisplayConnectionType::Internal ? "internal" : "external";
259     }
260 
261     return base::StringPrintf("DisplayDevice{%s%s%s, \"%s\"}", displayId.c_str(), type,
262                               isPrimary() ? ", primary" : "", mDisplayName.c_str());
263 }
264 
dump(std::string & result) const265 void DisplayDevice::dump(std::string& result) const {
266     StringAppendF(&result, "+ %s\n", getDebugName().c_str());
267 
268     result.append("   ");
269     StringAppendF(&result, "powerMode=%s (%d), ", to_string(mPowerMode).c_str(),
270                   static_cast<int32_t>(mPowerMode));
271     StringAppendF(&result, "activeConfig=%d, ", mActiveConfig.value());
272     getCompositionDisplay()->dump(result);
273 }
274 
hasRenderIntent(ui::RenderIntent intent) const275 bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
276     return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
277 }
278 
279 // ----------------------------------------------------------------------------
280 
getId() const281 const std::optional<DisplayId>& DisplayDevice::getId() const {
282     return mCompositionDisplay->getId();
283 }
284 
isSecure() const285 bool DisplayDevice::isSecure() const {
286     return mCompositionDisplay->isSecure();
287 }
288 
getBounds() const289 const Rect& DisplayDevice::getBounds() const {
290     return mCompositionDisplay->getState().bounds;
291 }
292 
getUndefinedRegion() const293 const Region& DisplayDevice::getUndefinedRegion() const {
294     return mCompositionDisplay->getState().undefinedRegion;
295 }
296 
needsFiltering() const297 bool DisplayDevice::needsFiltering() const {
298     return mCompositionDisplay->getState().needsFiltering;
299 }
300 
getLayerStack() const301 ui::LayerStack DisplayDevice::getLayerStack() const {
302     return mCompositionDisplay->getState().layerStackId;
303 }
304 
getTransform() const305 const ui::Transform& DisplayDevice::getTransform() const {
306     return mCompositionDisplay->getState().transform;
307 }
308 
getViewport() const309 const Rect& DisplayDevice::getViewport() const {
310     return mCompositionDisplay->getState().viewport;
311 }
312 
getFrame() const313 const Rect& DisplayDevice::getFrame() const {
314     return mCompositionDisplay->getState().frame;
315 }
316 
getSourceClip() const317 const Rect& DisplayDevice::getSourceClip() const {
318     return mCompositionDisplay->getState().sourceClip;
319 }
320 
hasWideColorGamut() const321 bool DisplayDevice::hasWideColorGamut() const {
322     return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
323 }
324 
hasHDR10PlusSupport() const325 bool DisplayDevice::hasHDR10PlusSupport() const {
326     return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
327 }
328 
hasHDR10Support() const329 bool DisplayDevice::hasHDR10Support() const {
330     return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
331 }
332 
hasHLGSupport() const333 bool DisplayDevice::hasHLGSupport() const {
334     return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
335 }
336 
hasDolbyVisionSupport() const337 bool DisplayDevice::hasDolbyVisionSupport() const {
338     return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
339 }
340 
getSupportedPerFrameMetadata() const341 int DisplayDevice::getSupportedPerFrameMetadata() const {
342     return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
343 }
344 
getHdrCapabilities() const345 const HdrCapabilities& DisplayDevice::getHdrCapabilities() const {
346     return mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
347 }
348 
349 std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
350 
351 }  // namespace android
352 
353 // TODO(b/129481165): remove the #pragma below and fix conversion issues
354 #pragma clang diagnostic pop // ignored "-Wconversion"
355