DisplayDevice.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. // #define LOG_NDEBUG 0
  17. #undef LOG_TAG
  18. #define LOG_TAG "DisplayDevice"
  19. #include <android-base/stringprintf.h>
  20. #include <compositionengine/CompositionEngine.h>
  21. #include <compositionengine/Display.h>
  22. #include <compositionengine/DisplayColorProfile.h>
  23. #include <compositionengine/DisplayColorProfileCreationArgs.h>
  24. #include <compositionengine/DisplayCreationArgs.h>
  25. #include <compositionengine/DisplaySurface.h>
  26. #include <compositionengine/RenderSurface.h>
  27. #include <compositionengine/RenderSurfaceCreationArgs.h>
  28. #include <compositionengine/impl/OutputCompositionState.h>
  29. #include <configstore/Utils.h>
  30. #include <log/log.h>
  31. #include <system/window.h>
  32. #include <ui/GraphicTypes.h>
  33. #include <cutils/properties.h>
  34. #include "DisplayDevice.h"
  35. #include "Layer.h"
  36. #include "SurfaceFlinger.h"
  37. namespace android {
  38. using android::base::StringAppendF;
  39. /*
  40. * Initialize the display to the specified values.
  41. *
  42. */
  43. uint32_t DisplayDevice::sPrimaryDisplayOrientation = 0;
  44. DisplayDeviceCreationArgs::DisplayDeviceCreationArgs(const sp<SurfaceFlinger>& flinger,
  45. const wp<IBinder>& displayToken,
  46. const std::optional<DisplayId>& displayId)
  47. : flinger(flinger), displayToken(displayToken), displayId(displayId) {}
  48. DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs&& args)
  49. : mFlinger(args.flinger),
  50. mDisplayToken(args.displayToken),
  51. mSequenceId(args.sequenceId),
  52. mDisplayInstallOrientation(args.displayInstallOrientation),
  53. mCompositionDisplay{mFlinger->getCompositionEngine().createDisplay(
  54. compositionengine::DisplayCreationArgs{args.isSecure, args.isVirtual,
  55. args.displayId})},
  56. mIsVirtual(args.isVirtual),
  57. mOrientation(),
  58. mActiveConfig(0),
  59. mIsPrimary(args.isPrimary) {
  60. mCompositionDisplay->createRenderSurface(
  61. compositionengine::RenderSurfaceCreationArgs{ANativeWindow_getWidth(
  62. args.nativeWindow.get()),
  63. ANativeWindow_getHeight(
  64. args.nativeWindow.get()),
  65. args.nativeWindow, args.displaySurface});
  66. mCompositionDisplay->createDisplayColorProfile(
  67. compositionengine::DisplayColorProfileCreationArgs{args.hasWideColorGamut,
  68. std::move(args.hdrCapabilities),
  69. args.supportedPerFrameMetadata,
  70. args.hwcColorModes});
  71. if (!mCompositionDisplay->isValid()) {
  72. ALOGE("Composition Display did not validate!");
  73. }
  74. mCompositionDisplay->getRenderSurface()->initialize();
  75. setPowerMode(args.initialPowerMode);
  76. // initialize the display orientation transform.
  77. setProjection(DisplayState::eOrientationDefault, Rect::INVALID_RECT, Rect::INVALID_RECT);
  78. }
  79. DisplayDevice::~DisplayDevice() = default;
  80. void DisplayDevice::disconnect() {
  81. mCompositionDisplay->disconnect();
  82. }
  83. int DisplayDevice::getWidth() const {
  84. return mCompositionDisplay->getState().bounds.getWidth();
  85. }
  86. int DisplayDevice::getHeight() const {
  87. return mCompositionDisplay->getState().bounds.getHeight();
  88. }
  89. void DisplayDevice::setDisplayName(const std::string& displayName) {
  90. if (!displayName.empty()) {
  91. // never override the name with an empty name
  92. mDisplayName = displayName;
  93. mCompositionDisplay->setName(displayName);
  94. }
  95. }
  96. uint32_t DisplayDevice::getPageFlipCount() const {
  97. return mCompositionDisplay->getRenderSurface()->getPageFlipCount();
  98. }
  99. // ----------------------------------------------------------------------------
  100. void DisplayDevice::setVisibleLayersSortedByZ(const Vector< sp<Layer> >& layers) {
  101. mVisibleLayersSortedByZ = layers;
  102. }
  103. const Vector< sp<Layer> >& DisplayDevice::getVisibleLayersSortedByZ() const {
  104. return mVisibleLayersSortedByZ;
  105. }
  106. void DisplayDevice::setLayersNeedingFences(const Vector< sp<Layer> >& layers) {
  107. mLayersNeedingFences = layers;
  108. }
  109. const Vector< sp<Layer> >& DisplayDevice::getLayersNeedingFences() const {
  110. return mLayersNeedingFences;
  111. }
  112. // ----------------------------------------------------------------------------
  113. void DisplayDevice::setPowerMode(int mode) {
  114. mPowerMode = mode;
  115. getCompositionDisplay()->setCompositionEnabled(mPowerMode != HWC_POWER_MODE_OFF);
  116. }
  117. int DisplayDevice::getPowerMode() const {
  118. return mPowerMode;
  119. }
  120. bool DisplayDevice::isPoweredOn() const {
  121. return mPowerMode != HWC_POWER_MODE_OFF;
  122. }
  123. // ----------------------------------------------------------------------------
  124. void DisplayDevice::setActiveConfig(int mode) {
  125. mActiveConfig = mode;
  126. }
  127. int DisplayDevice::getActiveConfig() const {
  128. return mActiveConfig;
  129. }
  130. // ----------------------------------------------------------------------------
  131. ui::Dataspace DisplayDevice::getCompositionDataSpace() const {
  132. return mCompositionDisplay->getState().dataspace;
  133. }
  134. // ----------------------------------------------------------------------------
  135. void DisplayDevice::setLayerStack(uint32_t stack) {
  136. mCompositionDisplay->setLayerStackFilter(stack, isPrimary());
  137. }
  138. // ----------------------------------------------------------------------------
  139. uint32_t DisplayDevice::displayStateOrientationToTransformOrientation(int orientation) {
  140. switch (orientation) {
  141. case DisplayState::eOrientationDefault:
  142. return ui::Transform::ROT_0;
  143. case DisplayState::eOrientation90:
  144. return ui::Transform::ROT_90;
  145. case DisplayState::eOrientation180:
  146. return ui::Transform::ROT_180;
  147. case DisplayState::eOrientation270:
  148. return ui::Transform::ROT_270;
  149. default:
  150. return ui::Transform::ROT_INVALID;
  151. }
  152. }
  153. status_t DisplayDevice::orientationToTransfrom(int orientation, int w, int h, ui::Transform* tr) {
  154. uint32_t flags = displayStateOrientationToTransformOrientation(orientation);
  155. if (flags == ui::Transform::ROT_INVALID) {
  156. return BAD_VALUE;
  157. }
  158. tr->set(flags, w, h);
  159. return NO_ERROR;
  160. }
  161. void DisplayDevice::setDisplaySize(const int newWidth, const int newHeight) {
  162. mCompositionDisplay->setBounds(ui::Size(newWidth, newHeight));
  163. }
  164. void DisplayDevice::setProjection(int orientation,
  165. const Rect& newViewport, const Rect& newFrame) {
  166. Rect viewport(newViewport);
  167. Rect frame(newFrame);
  168. mOrientation = orientation;
  169. const Rect& displayBounds = getCompositionDisplay()->getState().bounds;
  170. const int w = displayBounds.width();
  171. const int h = displayBounds.height();
  172. ui::Transform R;
  173. DisplayDevice::orientationToTransfrom(orientation, w, h, &R);
  174. if (!frame.isValid()) {
  175. // the destination frame can be invalid if it has never been set,
  176. // in that case we assume the whole display frame.
  177. frame = Rect(w, h);
  178. }
  179. if (viewport.isEmpty()) {
  180. // viewport can be invalid if it has never been set, in that case
  181. // we assume the whole display size.
  182. // it's also invalid to have an empty viewport, so we handle that
  183. // case in the same way.
  184. viewport = Rect(w, h);
  185. if (R.getOrientation() & ui::Transform::ROT_90) {
  186. // viewport is always specified in the logical orientation
  187. // of the display (ie: post-rotation).
  188. std::swap(viewport.right, viewport.bottom);
  189. }
  190. }
  191. ui::Transform TL, TP, S;
  192. float src_width = viewport.width();
  193. float src_height = viewport.height();
  194. float dst_width = frame.width();
  195. float dst_height = frame.height();
  196. if (src_width != dst_width || src_height != dst_height) {
  197. float sx = dst_width / src_width;
  198. float sy = dst_height / src_height;
  199. S.set(sx, 0, 0, sy);
  200. }
  201. float src_x = viewport.left;
  202. float src_y = viewport.top;
  203. float dst_x = frame.left;
  204. float dst_y = frame.top;
  205. TL.set(-src_x, -src_y);
  206. TP.set(dst_x, dst_y);
  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. DisplayDevice::orientationToTransfrom(
  211. (orientation + mDisplayInstallOrientation) % (DisplayState::eOrientation270 + 1),
  212. w, h, &R);
  213. }
  214. // The viewport and frame are both in the logical orientation.
  215. // Apply the logical translation, scale to physical size, apply the
  216. // physical translation and finally rotate to the physical orientation.
  217. ui::Transform globalTransform = R * TP * S * TL;
  218. const uint8_t type = globalTransform.getType();
  219. const bool needsFiltering =
  220. (!globalTransform.preserveRects() || (type >= ui::Transform::SCALE));
  221. Rect scissor = globalTransform.transform(viewport);
  222. if (scissor.isEmpty()) {
  223. scissor = displayBounds;
  224. }
  225. if (isPrimary()) {
  226. sPrimaryDisplayOrientation = displayStateOrientationToTransformOrientation(orientation);
  227. }
  228. getCompositionDisplay()->setProjection(globalTransform,
  229. displayStateOrientationToTransformOrientation(
  230. orientation),
  231. frame, viewport, scissor, needsFiltering);
  232. }
  233. uint32_t DisplayDevice::getPrimaryDisplayOrientationTransform() {
  234. return sPrimaryDisplayOrientation;
  235. }
  236. std::string DisplayDevice::getDebugName() const {
  237. const auto id = getId() ? to_string(*getId()) + ", " : std::string();
  238. return base::StringPrintf("DisplayDevice{%s%s%s\"%s\"}", id.c_str(),
  239. isPrimary() ? "primary, " : "", isVirtual() ? "virtual, " : "",
  240. mDisplayName.c_str());
  241. }
  242. void DisplayDevice::dump(std::string& result) const {
  243. StringAppendF(&result, "+ %s\n", getDebugName().c_str());
  244. result.append(" ");
  245. StringAppendF(&result, "activeSystemName=%s, ", getActiveSystemName().c_str());
  246. StringAppendF(&result, "powerMode=%d, ", mPowerMode);
  247. StringAppendF(&result, "activeConfig=%d, ", mActiveConfig);
  248. StringAppendF(&result, "numLayers=%zu\n", mVisibleLayersSortedByZ.size());
  249. getCompositionDisplay()->dump(result);
  250. }
  251. bool DisplayDevice::hasRenderIntent(ui::RenderIntent intent) const {
  252. return mCompositionDisplay->getDisplayColorProfile()->hasRenderIntent(intent);
  253. }
  254. // ----------------------------------------------------------------------------
  255. const std::optional<DisplayId>& DisplayDevice::getId() const {
  256. return mCompositionDisplay->getId();
  257. }
  258. bool DisplayDevice::isSecure() const {
  259. return mCompositionDisplay->isSecure();
  260. }
  261. const Rect& DisplayDevice::getBounds() const {
  262. return mCompositionDisplay->getState().bounds;
  263. }
  264. const Region& DisplayDevice::getUndefinedRegion() const {
  265. return mCompositionDisplay->getState().undefinedRegion;
  266. }
  267. bool DisplayDevice::needsFiltering() const {
  268. return mCompositionDisplay->getState().needsFiltering;
  269. }
  270. uint32_t DisplayDevice::getLayerStack() const {
  271. return mCompositionDisplay->getState().layerStackId;
  272. }
  273. const ui::Transform& DisplayDevice::getTransform() const {
  274. return mCompositionDisplay->getState().transform;
  275. }
  276. const Rect& DisplayDevice::getViewport() const {
  277. return mCompositionDisplay->getState().viewport;
  278. }
  279. const Rect& DisplayDevice::getFrame() const {
  280. return mCompositionDisplay->getState().frame;
  281. }
  282. const Rect& DisplayDevice::getScissor() const {
  283. return mCompositionDisplay->getState().scissor;
  284. }
  285. bool DisplayDevice::hasWideColorGamut() const {
  286. return mCompositionDisplay->getDisplayColorProfile()->hasWideColorGamut();
  287. }
  288. bool DisplayDevice::hasHDR10PlusSupport() const {
  289. return mCompositionDisplay->getDisplayColorProfile()->hasHDR10PlusSupport();
  290. }
  291. bool DisplayDevice::hasHDR10Support() const {
  292. return mCompositionDisplay->getDisplayColorProfile()->hasHDR10Support();
  293. }
  294. bool DisplayDevice::hasHLGSupport() const {
  295. return mCompositionDisplay->getDisplayColorProfile()->hasHLGSupport();
  296. }
  297. bool DisplayDevice::hasDolbyVisionSupport() const {
  298. return mCompositionDisplay->getDisplayColorProfile()->hasDolbyVisionSupport();
  299. }
  300. int DisplayDevice::getSupportedPerFrameMetadata() const {
  301. return mCompositionDisplay->getDisplayColorProfile()->getSupportedPerFrameMetadata();
  302. }
  303. const HdrCapabilities& DisplayDevice::getHdrCapabilities() const {
  304. return mCompositionDisplay->getDisplayColorProfile()->getHdrCapabilities();
  305. }
  306. String8 DisplayDevice::getActiveSystemName() const{
  307. char value[PROPERTY_VALUE_MAX];
  308. property_get("persist.sys.active", value, "");
  309. return String8(value);
  310. }
  311. std::atomic<int32_t> DisplayDeviceState::sNextSequenceId(1);
  312. } // namespace android