DisplayDevice.h 13 KB

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