• 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 #ifndef ANDROID_DISPLAY_DEVICE_H
18 #define ANDROID_DISPLAY_DEVICE_H
19 
20 #include <stdlib.h>
21 
22 #include <memory>
23 #include <optional>
24 #include <string>
25 #include <unordered_map>
26 
27 #include <android/native_window.h>
28 #include <binder/IBinder.h>
29 #include <gui/LayerState.h>
30 #include <hardware/hwcomposer_defs.h>
31 #include <math/mat4.h>
32 #include <renderengine/RenderEngine.h>
33 #include <system/window.h>
34 #include <ui/GraphicTypes.h>
35 #include <ui/HdrCapabilities.h>
36 #include <ui/Region.h>
37 #include <ui/Transform.h>
38 #include <utils/Mutex.h>
39 #include <utils/RefBase.h>
40 #include <utils/Timers.h>
41 
42 #include "DisplayHardware/DisplayIdentification.h"
43 #include "RenderArea.h"
44 
45 namespace android {
46 
47 class Fence;
48 class HWComposer;
49 class IGraphicBufferProducer;
50 class Layer;
51 class SurfaceFlinger;
52 
53 struct CompositionInfo;
54 struct DisplayDeviceCreationArgs;
55 struct DisplayInfo;
56 
57 namespace compositionengine {
58 class Display;
59 class DisplaySurface;
60 } // namespace compositionengine
61 
62 class DisplayDevice : public LightRefBase<DisplayDevice> {
63 public:
64     constexpr static float sDefaultMinLumiance = 0.0;
65     constexpr static float sDefaultMaxLumiance = 500.0;
66 
67     enum {
68         NO_LAYER_STACK = 0xFFFFFFFF,
69     };
70 
71     explicit DisplayDevice(DisplayDeviceCreationArgs&& args);
72     virtual ~DisplayDevice();
73 
getCompositionDisplay()74     std::shared_ptr<compositionengine::Display> getCompositionDisplay() const {
75         return mCompositionDisplay;
76     }
77 
isVirtual()78     bool isVirtual() const { return mIsVirtual; }
isPrimary()79     bool isPrimary() const { return mIsPrimary; }
80 
81     // isSecure indicates whether this display can be trusted to display
82     // secure surfaces.
83     bool isSecure() const;
84 
85     int         getWidth() const;
86     int         getHeight() const;
getInstallOrientation()87     int         getInstallOrientation() const { return mDisplayInstallOrientation; }
88 
89     void                    setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers);
90     const Vector< sp<Layer> >& getVisibleLayersSortedByZ() const;
91     void                    setLayersNeedingFences(const Vector< sp<Layer> >& layers);
92     const Vector< sp<Layer> >& getLayersNeedingFences() const;
93 
94     void                    setLayerStack(uint32_t stack);
95     void                    setDisplaySize(const int newWidth, const int newHeight);
96     void                    setProjection(int orientation, const Rect& viewport, const Rect& frame);
97 
getOrientation()98     int                     getOrientation() const { return mOrientation; }
99     static uint32_t         getPrimaryDisplayOrientationTransform();
100     const ui::Transform& getTransform() const;
101     const Rect& getViewport() const;
102     const Rect& getFrame() const;
103     const Rect& getScissor() const;
104     bool needsFiltering() const;
105     uint32_t getLayerStack() const;
106 
107     const std::optional<DisplayId>& getId() const;
getDisplayToken()108     const wp<IBinder>& getDisplayToken() const { return mDisplayToken; }
getSequenceId()109     int32_t getSequenceId() const { return mSequenceId; }
110 
111     const Region& getUndefinedRegion() const;
112 
113     int32_t getSupportedPerFrameMetadata() const;
114 
115     bool hasWideColorGamut() const;
116     // Whether h/w composer has native support for specific HDR type.
117     bool hasHDR10PlusSupport() const;
118     bool hasHDR10Support() const;
119     bool hasHLGSupport() const;
120     bool hasDolbyVisionSupport() const;
121 
122     // The returned HdrCapabilities is the combination of HDR capabilities from
123     // hardware composer and RenderEngine. When the DisplayDevice supports wide
124     // color gamut, RenderEngine is able to simulate HDR support in Display P3
125     // color space for both PQ and HLG HDR contents. The minimum and maximum
126     // luminance will be set to sDefaultMinLumiance and sDefaultMaxLumiance
127     // respectively if hardware composer doesn't return meaningful values.
128     const HdrCapabilities& getHdrCapabilities() const;
129 
130     // Return true if intent is supported by the display.
131     bool hasRenderIntent(ui::RenderIntent intent) const;
132 
133     const Rect& getBounds() const;
bounds()134     const Rect& bounds() const { return getBounds(); }
135 
136     void setDisplayName(const std::string& displayName);
getDisplayName()137     const std::string& getDisplayName() const { return mDisplayName; }
138 
139     /* ------------------------------------------------------------------------
140      * Display power mode management.
141      */
142     int getPowerMode() const;
143     void setPowerMode(int mode);
144     bool isPoweredOn() const;
145 
146     ui::Dataspace getCompositionDataSpace() const;
147 
148     /* ------------------------------------------------------------------------
149      * Display active config management.
150      */
151     int getActiveConfig() const;
152     void setActiveConfig(int mode);
153 
154     // release HWC resources (if any) for removable displays
155     void disconnect();
156 
157     /* ------------------------------------------------------------------------
158      * Debugging
159      */
160     uint32_t getPageFlipCount() const;
161     std::string getDebugName() const;
162     void dump(std::string& result) const;
163 
164 private:
165     /*
166      *  Constants, set during initialization
167      */
168     const sp<SurfaceFlinger> mFlinger;
169     const wp<IBinder> mDisplayToken;
170     const int32_t mSequenceId;
171 
172     const int mDisplayInstallOrientation;
173     const std::shared_ptr<compositionengine::Display> mCompositionDisplay;
174 
175     std::string mDisplayName;
176     const bool mIsVirtual;
177 
178     /*
179      * Can only accessed from the main thread, these members
180      * don't need synchronization.
181      */
182 
183     // list of visible layers on that display
184     Vector< sp<Layer> > mVisibleLayersSortedByZ;
185     // list of layers needing fences
186     Vector< sp<Layer> > mLayersNeedingFences;
187 
188     /*
189      * Transaction state
190      */
191     static uint32_t displayStateOrientationToTransformOrientation(int orientation);
192     static status_t orientationToTransfrom(int orientation,
193                                            int w, int h, ui::Transform* tr);
194 
195     int mOrientation;
196     static uint32_t sPrimaryDisplayOrientation;
197 
198     // Current power mode
199     int mPowerMode;
200     // Current active config
201     int mActiveConfig;
202 
203     // TODO(b/74619554): Remove special cases for primary display.
204     const bool mIsPrimary;
205 };
206 
207 struct DisplayDeviceState {
isVirtualDisplayDeviceState208     bool isVirtual() const { return !displayId.has_value(); }
209 
210     int32_t sequenceId = sNextSequenceId++;
211     std::optional<DisplayId> displayId;
212     sp<IGraphicBufferProducer> surface;
213     uint32_t layerStack = DisplayDevice::NO_LAYER_STACK;
214     Rect viewport;
215     Rect frame;
216     uint8_t orientation = 0;
217     uint32_t width = 0;
218     uint32_t height = 0;
219     std::string displayName;
220     bool isSecure = false;
221 
222 private:
223     static std::atomic<int32_t> sNextSequenceId;
224 };
225 
226 struct DisplayDeviceCreationArgs {
227     // We use a constructor to ensure some of the values are set, without
228     // assuming a default value.
229     DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger, const wp<IBinder>& displayToken,
230                               const std::optional<DisplayId>& displayId);
231 
232     const sp<SurfaceFlinger> flinger;
233     const wp<IBinder> displayToken;
234     const std::optional<DisplayId> displayId;
235 
236     int32_t sequenceId{0};
237     bool isVirtual{false};
238     bool isSecure{false};
239     sp<ANativeWindow> nativeWindow;
240     sp<compositionengine::DisplaySurface> displaySurface;
241     int displayInstallOrientation{DisplayState::eOrientationDefault};
242     bool hasWideColorGamut{false};
243     HdrCapabilities hdrCapabilities;
244     int32_t supportedPerFrameMetadata{0};
245     std::unordered_map<ui::ColorMode, std::vector<ui::RenderIntent>> hwcColorModes;
246     int initialPowerMode{HWC_POWER_MODE_NORMAL};
247     bool isPrimary{false};
248 };
249 
250 class DisplayRenderArea : public RenderArea {
251 public:
252     DisplayRenderArea(const sp<const DisplayDevice> device,
253                       ui::Transform::orientation_flags rotation = ui::Transform::ROT_0)
254           : DisplayRenderArea(device, device->getBounds(), device->getWidth(), device->getHeight(),
255                               device->getCompositionDataSpace(), rotation) {}
256     DisplayRenderArea(const sp<const DisplayDevice> device, Rect sourceCrop, uint32_t reqWidth,
257                       uint32_t reqHeight, ui::Dataspace reqDataSpace,
258                       ui::Transform::orientation_flags rotation, bool allowSecureLayers = true)
259           : RenderArea(reqWidth, reqHeight, CaptureFill::OPAQUE, reqDataSpace,
260                        getDisplayRotation(rotation, device->getInstallOrientation())),
261             mDevice(device),
262             mSourceCrop(sourceCrop),
263             mAllowSecureLayers(allowSecureLayers) {}
264 
getTransform()265     const ui::Transform& getTransform() const override { return mDevice->getTransform(); }
getBounds()266     Rect getBounds() const override { return mDevice->getBounds(); }
getHeight()267     int getHeight() const override { return mDevice->getHeight(); }
getWidth()268     int getWidth() const override { return mDevice->getWidth(); }
isSecure()269     bool isSecure() const override { return mAllowSecureLayers && mDevice->isSecure(); }
getDisplayDevice()270     const sp<const DisplayDevice> getDisplayDevice() const override { return mDevice; }
271 
needsFiltering()272     bool needsFiltering() const override {
273         // check if the projection from the logical display to the physical
274         // display needs filtering
275         if (mDevice->needsFiltering()) {
276             return true;
277         }
278 
279         // check if the projection from the logical render area (i.e., the
280         // physical display) to the physical render area requires filtering
281         const Rect sourceCrop = getSourceCrop();
282         int width = sourceCrop.width();
283         int height = sourceCrop.height();
284         if (getRotationFlags() & ui::Transform::ROT_90) {
285             std::swap(width, height);
286         }
287         return width != getReqWidth() || height != getReqHeight();
288     }
289 
getSourceCrop()290     Rect getSourceCrop() const override {
291         // use the projected display viewport by default.
292         if (mSourceCrop.isEmpty()) {
293             return mDevice->getScissor();
294         }
295 
296         // Recompute the device transformation for the source crop.
297         ui::Transform rotation;
298         ui::Transform translatePhysical;
299         ui::Transform translateLogical;
300         ui::Transform scale;
301         const Rect& viewport = mDevice->getViewport();
302         const Rect& scissor = mDevice->getScissor();
303         const Rect& frame = mDevice->getFrame();
304 
305         const int orientation = mDevice->getInstallOrientation();
306         // Install orientation is transparent to the callers.  Apply it now.
307         uint32_t flags = 0x00;
308         switch (orientation) {
309             case DisplayState::eOrientation90:
310                 flags = ui::Transform::ROT_90;
311                 break;
312             case DisplayState::eOrientation180:
313                 flags = ui::Transform::ROT_180;
314                 break;
315             case DisplayState::eOrientation270:
316                 flags = ui::Transform::ROT_270;
317                 break;
318             default:
319                 break;
320         }
321         rotation.set(flags, getWidth(), getHeight());
322         translateLogical.set(-viewport.left, -viewport.top);
323         translatePhysical.set(scissor.left, scissor.top);
324         scale.set(frame.getWidth() / float(viewport.getWidth()), 0, 0,
325                   frame.getHeight() / float(viewport.getHeight()));
326         const ui::Transform finalTransform =
327                 rotation * translatePhysical * scale * translateLogical;
328         return finalTransform.transform(mSourceCrop);
329     }
330 
331 private:
332     // Install orientation is transparent to the callers.  We need to cancel
333     // it out by modifying rotation flags.
getDisplayRotation(ui::Transform::orientation_flags rotation,int orientation)334     static ui::Transform::orientation_flags getDisplayRotation(
335             ui::Transform::orientation_flags rotation, int orientation) {
336         if (orientation == DisplayState::eOrientationDefault) {
337             return rotation;
338         }
339 
340         // convert hw orientation into flag presentation
341         // here inverse transform needed
342         uint8_t hw_rot_90 = 0x00;
343         uint8_t hw_flip_hv = 0x00;
344         switch (orientation) {
345             case DisplayState::eOrientation90:
346                 hw_rot_90 = ui::Transform::ROT_90;
347                 hw_flip_hv = ui::Transform::ROT_180;
348                 break;
349             case DisplayState::eOrientation180:
350                 hw_flip_hv = ui::Transform::ROT_180;
351                 break;
352             case DisplayState::eOrientation270:
353                 hw_rot_90 = ui::Transform::ROT_90;
354                 break;
355         }
356 
357         // transform flags operation
358         // 1) flip H V if both have ROT_90 flag
359         // 2) XOR these flags
360         uint8_t rotation_rot_90 = rotation & ui::Transform::ROT_90;
361         uint8_t rotation_flip_hv = rotation & ui::Transform::ROT_180;
362         if (rotation_rot_90 & hw_rot_90) {
363             rotation_flip_hv = (~rotation_flip_hv) & ui::Transform::ROT_180;
364         }
365 
366         return static_cast<ui::Transform::orientation_flags>(
367                 (rotation_rot_90 ^ hw_rot_90) | (rotation_flip_hv ^ hw_flip_hv));
368     }
369 
370     const sp<const DisplayDevice> mDevice;
371     const Rect mSourceCrop;
372     const bool mAllowSecureLayers;
373 };
374 
375 }; // namespace android
376 
377 #endif // ANDROID_DISPLAY_DEVICE_H
378