Layer.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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_LAYER_H
  17. #define ANDROID_LAYER_H
  18. #include <compositionengine/LayerFE.h>
  19. #include <gui/BufferQueue.h>
  20. #include <gui/ISurfaceComposerClient.h>
  21. #include <gui/LayerState.h>
  22. #include <input/InputWindow.h>
  23. #include <layerproto/LayerProtoHeader.h>
  24. #include <math/vec4.h>
  25. #include <renderengine/Mesh.h>
  26. #include <renderengine/Texture.h>
  27. #include <sys/types.h>
  28. #include <ui/FloatRect.h>
  29. #include <ui/FrameStats.h>
  30. #include <ui/GraphicBuffer.h>
  31. #include <ui/PixelFormat.h>
  32. #include <ui/Region.h>
  33. #include <ui/Transform.h>
  34. #include <utils/RefBase.h>
  35. #include <utils/String8.h>
  36. #include <utils/Timers.h>
  37. #include <cstdint>
  38. #include <list>
  39. #include <optional>
  40. #include <vector>
  41. #include "Client.h"
  42. #include "ClientCache.h"
  43. #include "DisplayHardware/ComposerHal.h"
  44. #include "DisplayHardware/HWComposer.h"
  45. #include "FrameTracker.h"
  46. #include "LayerVector.h"
  47. #include "MonitoredProducer.h"
  48. #include "RenderArea.h"
  49. #include "SurfaceFlinger.h"
  50. #include "TransactionCompletedThread.h"
  51. using namespace android::surfaceflinger;
  52. namespace android {
  53. // ---------------------------------------------------------------------------
  54. class Client;
  55. class Colorizer;
  56. class DisplayDevice;
  57. class GraphicBuffer;
  58. class SurfaceFlinger;
  59. class LayerDebugInfo;
  60. namespace compositionengine {
  61. class Layer;
  62. class OutputLayer;
  63. struct LayerFECompositionState;
  64. }
  65. namespace impl {
  66. class SurfaceInterceptor;
  67. }
  68. // ---------------------------------------------------------------------------
  69. struct LayerCreationArgs {
  70. LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, const String8& systemname,
  71. uint32_t w, uint32_t h, uint32_t flags, LayerMetadata metadata)
  72. : flinger(flinger), client(client), name(name), systemname(systemname), w(w), h(h), flags(flags),
  73. metadata(std::move(metadata)) {}
  74. SurfaceFlinger* flinger;
  75. const sp<Client>& client;
  76. const String8& name;
  77. const String8& systemname;
  78. uint32_t w;
  79. uint32_t h;
  80. uint32_t flags;
  81. LayerMetadata metadata;
  82. };
  83. class Layer : public virtual compositionengine::LayerFE {
  84. static std::atomic<int32_t> sSequence;
  85. public:
  86. mutable bool contentDirty{false};
  87. // regions below are in window-manager space
  88. Region visibleRegion;
  89. Region coveredRegion;
  90. Region visibleNonTransparentRegion;
  91. Region surfaceDamageRegion;
  92. // Layer serial number. This gives layers an explicit ordering, so we
  93. // have a stable sort order when their layer stack and Z-order are
  94. // the same.
  95. int32_t sequence{sSequence++};
  96. enum { // flags for doTransaction()
  97. eDontUpdateGeometryState = 0x00000001,
  98. eVisibleRegion = 0x00000002,
  99. eInputInfoChanged = 0x00000004
  100. };
  101. struct Geometry {
  102. uint32_t w;
  103. uint32_t h;
  104. ui::Transform transform;
  105. inline bool operator==(const Geometry& rhs) const {
  106. return (w == rhs.w && h == rhs.h) && (transform.tx() == rhs.transform.tx()) &&
  107. (transform.ty() == rhs.transform.ty());
  108. }
  109. inline bool operator!=(const Geometry& rhs) const { return !operator==(rhs); }
  110. };
  111. struct RoundedCornerState {
  112. RoundedCornerState() = default;
  113. RoundedCornerState(FloatRect cropRect, float radius)
  114. : cropRect(cropRect), radius(radius) {}
  115. // Rounded rectangle in local layer coordinate space.
  116. FloatRect cropRect = FloatRect();
  117. // Radius of the rounded rectangle.
  118. float radius = 0.0f;
  119. };
  120. struct State {
  121. Geometry active_legacy;
  122. Geometry requested_legacy;
  123. int32_t z;
  124. // The identifier of the layer stack this layer belongs to. A layer can
  125. // only be associated to a single layer stack. A layer stack is a
  126. // z-ordered group of layers which can be associated to one or more
  127. // displays. Using the same layer stack on different displays is a way
  128. // to achieve mirroring.
  129. uint32_t layerStack;
  130. uint8_t flags;
  131. uint8_t reserved[2];
  132. int32_t sequence; // changes when visible regions can change
  133. bool modified;
  134. // Crop is expressed in layer space coordinate.
  135. Rect crop_legacy;
  136. Rect requestedCrop_legacy;
  137. // If set, defers this state update until the identified Layer
  138. // receives a frame with the given frameNumber
  139. wp<Layer> barrierLayer_legacy;
  140. uint64_t frameNumber_legacy;
  141. // the transparentRegion hint is a bit special, it's latched only
  142. // when we receive a buffer -- this is because it's "content"
  143. // dependent.
  144. Region activeTransparentRegion_legacy;
  145. Region requestedTransparentRegion_legacy;
  146. LayerMetadata metadata;
  147. // If non-null, a Surface this Surface's Z-order is interpreted relative to.
  148. wp<Layer> zOrderRelativeOf;
  149. // A list of surfaces whose Z-order is interpreted relative to ours.
  150. SortedVector<wp<Layer>> zOrderRelatives;
  151. half4 color;
  152. float cornerRadius;
  153. bool inputInfoChanged;
  154. InputWindowInfo inputInfo;
  155. wp<Layer> touchableRegionCrop;
  156. // dataspace is only used by BufferStateLayer and ColorLayer
  157. ui::Dataspace dataspace;
  158. // The fields below this point are only used by BufferStateLayer
  159. Geometry active;
  160. uint32_t transform;
  161. bool transformToDisplayInverse;
  162. Rect crop;
  163. Region transparentRegionHint;
  164. sp<GraphicBuffer> buffer;
  165. client_cache_t clientCacheId;
  166. sp<Fence> acquireFence;
  167. HdrMetadata hdrMetadata;
  168. Region surfaceDamageRegion;
  169. int32_t api;
  170. sp<NativeHandle> sidebandStream;
  171. mat4 colorTransform;
  172. bool hasColorTransform;
  173. // pointer to background color layer that, if set, appears below the buffer state layer
  174. // and the buffer state layer's children. Z order will be set to
  175. // INT_MIN
  176. sp<Layer> bgColorLayer;
  177. // The deque of callback handles for this frame. The back of the deque contains the most
  178. // recent callback handle.
  179. std::deque<sp<CallbackHandle>> callbackHandles;
  180. bool colorSpaceAgnostic;
  181. };
  182. explicit Layer(const LayerCreationArgs& args);
  183. virtual ~Layer();
  184. void setPrimaryDisplayOnly() { mPrimaryDisplayOnly = true; }
  185. bool getPrimaryDisplayOnly() const { return mPrimaryDisplayOnly; }
  186. // ------------------------------------------------------------------------
  187. // Geometry setting functions.
  188. //
  189. // The following group of functions are used to specify the layers
  190. // bounds, and the mapping of the texture on to those bounds. According
  191. // to various settings changes to them may apply immediately, or be delayed until
  192. // a pending resize is completed by the producer submitting a buffer. For example
  193. // if we were to change the buffer size, and update the matrix ahead of the
  194. // new buffer arriving, then we would be stretching the buffer to a different
  195. // aspect before and after the buffer arriving, which probably isn't what we wanted.
  196. //
  197. // The first set of geometry functions are controlled by the scaling mode, described
  198. // in window.h. The scaling mode may be set by the client, as it submits buffers.
  199. // This value may be overriden through SurfaceControl, with setOverrideScalingMode.
  200. //
  201. // Put simply, if our scaling mode is SCALING_MODE_FREEZE, then
  202. // matrix updates will not be applied while a resize is pending
  203. // and the size and transform will remain in their previous state
  204. // until a new buffer is submitted. If the scaling mode is another value
  205. // then the old-buffer will immediately be scaled to the pending size
  206. // and the new matrix will be immediately applied following this scaling
  207. // transformation.
  208. // Set the default buffer size for the assosciated Producer, in pixels. This is
  209. // also the rendered size of the layer prior to any transformations. Parent
  210. // or local matrix transformations will not affect the size of the buffer,
  211. // but may affect it's on-screen size or clipping.
  212. virtual bool setSize(uint32_t w, uint32_t h);
  213. // Set a 2x2 transformation matrix on the layer. This transform
  214. // will be applied after parent transforms, but before any final
  215. // producer specified transform.
  216. virtual bool setMatrix(const layer_state_t::matrix22_t& matrix,
  217. bool allowNonRectPreservingTransforms);
  218. // This second set of geometry attributes are controlled by
  219. // setGeometryAppliesWithResize, and their default mode is to be
  220. // immediate. If setGeometryAppliesWithResize is specified
  221. // while a resize is pending, then update of these attributes will
  222. // be delayed until the resize completes.
  223. // setPosition operates in parent buffer space (pre parent-transform) or display
  224. // space for top-level layers.
  225. virtual bool setPosition(float x, float y, bool immediate);
  226. // Buffer space
  227. virtual bool setCrop_legacy(const Rect& crop, bool immediate);
  228. // TODO(b/38182121): Could we eliminate the various latching modes by
  229. // using the layer hierarchy?
  230. // -----------------------------------------------------------------------
  231. virtual bool setLayer(int32_t z);
  232. virtual bool setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ);
  233. virtual bool setAlpha(float alpha);
  234. virtual bool setColor(const half3& /*color*/) { return false; };
  235. // Set rounded corner radius for this layer and its children.
  236. //
  237. // We only support 1 radius per layer in the hierarchy, where parent layers have precedence.
  238. // The shape of the rounded corner rectangle is specified by the crop rectangle of the layer
  239. // from which we inferred the rounded corner radius.
  240. virtual bool setCornerRadius(float cornerRadius);
  241. virtual bool setTransparentRegionHint(const Region& transparent);
  242. virtual bool setFlags(uint8_t flags, uint8_t mask);
  243. virtual bool setLayerStack(uint32_t layerStack);
  244. virtual uint32_t getLayerStack() const;
  245. virtual void deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle,
  246. uint64_t frameNumber);
  247. virtual void deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber);
  248. virtual bool setOverrideScalingMode(int32_t overrideScalingMode);
  249. virtual bool setMetadata(const LayerMetadata& data);
  250. virtual bool reparentChildren(const sp<IBinder>& layer);
  251. virtual void setChildrenDrawingParent(const sp<Layer>& layer);
  252. virtual bool reparent(const sp<IBinder>& newParentHandle);
  253. virtual bool detachChildren();
  254. bool attachChildren();
  255. bool isLayerDetached() const { return mLayerDetached; }
  256. virtual bool setColorTransform(const mat4& matrix);
  257. virtual mat4 getColorTransform() const;
  258. virtual bool hasColorTransform() const;
  259. virtual bool isColorSpaceAgnostic() const { return mDrawingState.colorSpaceAgnostic; }
  260. // Used only to set BufferStateLayer state
  261. virtual bool setTransform(uint32_t /*transform*/) { return false; };
  262. virtual bool setTransformToDisplayInverse(bool /*transformToDisplayInverse*/) { return false; };
  263. virtual bool setCrop(const Rect& /*crop*/) { return false; };
  264. virtual bool setFrame(const Rect& /*frame*/) { return false; };
  265. virtual bool setBuffer(const sp<GraphicBuffer>& /*buffer*/, nsecs_t /*postTime*/,
  266. nsecs_t /*desiredPresentTime*/,
  267. const client_cache_t& /*clientCacheId*/) {
  268. return false;
  269. };
  270. virtual bool setAcquireFence(const sp<Fence>& /*fence*/) { return false; };
  271. virtual bool setDataspace(ui::Dataspace /*dataspace*/) { return false; };
  272. virtual bool setHdrMetadata(const HdrMetadata& /*hdrMetadata*/) { return false; };
  273. virtual bool setSurfaceDamageRegion(const Region& /*surfaceDamage*/) { return false; };
  274. virtual bool setApi(int32_t /*api*/) { return false; };
  275. virtual bool setSidebandStream(const sp<NativeHandle>& /*sidebandStream*/) { return false; };
  276. virtual bool setTransactionCompletedListeners(
  277. const std::vector<sp<CallbackHandle>>& /*handles*/) {
  278. return false;
  279. };
  280. virtual bool setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace);
  281. virtual bool setColorSpaceAgnostic(const bool agnostic);
  282. ui::Dataspace getDataSpace() const { return mCurrentDataSpace; }
  283. // Before color management is introduced, contents on Android have to be
  284. // desaturated in order to match what they appears like visually.
  285. // With color management, these contents will appear desaturated, thus
  286. // needed to be saturated so that they match what they are designed for
  287. // visually.
  288. bool isLegacyDataSpace() const;
  289. virtual std::shared_ptr<compositionengine::Layer> getCompositionLayer() const;
  290. // If we have received a new buffer this frame, we will pass its surface
  291. // damage down to hardware composer. Otherwise, we must send a region with
  292. // one empty rect.
  293. virtual void useSurfaceDamage() {}
  294. virtual void useEmptyDamage() {}
  295. uint32_t getTransactionFlags() const { return mTransactionFlags; }
  296. uint32_t getTransactionFlags(uint32_t flags);
  297. uint32_t setTransactionFlags(uint32_t flags);
  298. // Deprecated, please use compositionengine::Output::belongsInOutput()
  299. // instead.
  300. // TODO(lpique): Move the remaining callers (screencap) to the new function.
  301. bool belongsToDisplay(uint32_t layerStack, const String8& activesystemname, bool isPrimaryDisplay) const {
  302. return getLayerStack() == layerStack && getSystemName() == activesystemname && (!mPrimaryDisplayOnly || isPrimaryDisplay);
  303. }
  304. void computeGeometry(const RenderArea& renderArea, renderengine::Mesh& mesh,
  305. bool useIdentityTransform) const;
  306. FloatRect getBounds(const Region& activeTransparentRegion) const;
  307. FloatRect getBounds() const;
  308. // Compute bounds for the layer and cache the results.
  309. void computeBounds(FloatRect parentBounds, ui::Transform parentTransform);
  310. // Returns the buffer scale transform if a scaling mode is set.
  311. ui::Transform getBufferScaleTransform() const;
  312. // Get effective layer transform, taking into account all its parent transform with any
  313. // scaling if the parent scaling more is not NATIVE_WINDOW_SCALING_MODE_FREEZE.
  314. ui::Transform getTransformWithScale(const ui::Transform& bufferScaleTransform) const;
  315. // Returns the bounds of the layer without any buffer scaling.
  316. FloatRect getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const;
  317. int32_t getSequence() const { return sequence; }
  318. // -----------------------------------------------------------------------
  319. // Virtuals
  320. virtual const char* getTypeId() const = 0;
  321. /*
  322. * isOpaque - true if this surface is opaque
  323. *
  324. * This takes into account the buffer format (i.e. whether or not the
  325. * pixel format includes an alpha channel) and the "opaque" flag set
  326. * on the layer. It does not examine the current plane alpha value.
  327. */
  328. virtual bool isOpaque(const Layer::State&) const { return false; }
  329. /*
  330. * isSecure - true if this surface is secure, that is if it prevents
  331. * screenshots or VNC servers.
  332. */
  333. bool isSecure() const;
  334. /*
  335. * isVisible - true if this layer is visible, false otherwise
  336. */
  337. virtual bool isVisible() const = 0;
  338. /*
  339. * isHiddenByPolicy - true if this layer has been forced invisible.
  340. * just because this is false, doesn't mean isVisible() is true.
  341. * For example if this layer has no active buffer, it may not be hidden by
  342. * policy, but it still can not be visible.
  343. */
  344. bool isHiddenByPolicy() const;
  345. /*
  346. * Returns whether this layer can receive input.
  347. */
  348. virtual bool canReceiveInput() const;
  349. /*
  350. * isProtected - true if the layer may contain protected content in the
  351. * GRALLOC_USAGE_PROTECTED sense.
  352. */
  353. virtual bool isProtected() const { return false; }
  354. /*
  355. * isFixedSize - true if content has a fixed size
  356. */
  357. virtual bool isFixedSize() const { return true; }
  358. /*
  359. * usesSourceCrop - true if content should use a source crop
  360. */
  361. virtual bool usesSourceCrop() const { return false; }
  362. // Most layers aren't created from the main thread, and therefore need to
  363. // grab the SF state lock to access HWC, but ContainerLayer does, so we need
  364. // to avoid grabbing the lock again to avoid deadlock
  365. virtual bool isCreatedFromMainThread() const { return false; }
  366. bool isRemovedFromCurrentState() const;
  367. void writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
  368. uint32_t traceFlags = SurfaceTracing::TRACE_ALL);
  369. void writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
  370. uint32_t traceFlags = SurfaceTracing::TRACE_ALL);
  371. virtual Geometry getActiveGeometry(const Layer::State& s) const { return s.active_legacy; }
  372. virtual uint32_t getActiveWidth(const Layer::State& s) const { return s.active_legacy.w; }
  373. virtual uint32_t getActiveHeight(const Layer::State& s) const { return s.active_legacy.h; }
  374. virtual ui::Transform getActiveTransform(const Layer::State& s) const {
  375. return s.active_legacy.transform;
  376. }
  377. virtual Region getActiveTransparentRegion(const Layer::State& s) const {
  378. return s.activeTransparentRegion_legacy;
  379. }
  380. virtual Rect getCrop(const Layer::State& s) const { return s.crop_legacy; }
  381. protected:
  382. virtual bool prepareClientLayer(const RenderArea& renderArea, const Region& clip,
  383. bool useIdentityTransform, Region& clearRegion,
  384. const bool supportProtectedContent,
  385. renderengine::LayerSettings& layer);
  386. public:
  387. /*
  388. * compositionengine::LayerFE overrides
  389. */
  390. void latchCompositionState(compositionengine::LayerFECompositionState&,
  391. bool includeGeometry) const override;
  392. void onLayerDisplayed(const sp<Fence>& releaseFence) override;
  393. const char* getDebugName() const override;
  394. protected:
  395. void latchGeometry(compositionengine::LayerFECompositionState& outState) const;
  396. public:
  397. virtual void setDefaultBufferSize(uint32_t /*w*/, uint32_t /*h*/) {}
  398. virtual bool isHdrY410() const { return false; }
  399. void forceClientComposition(const sp<DisplayDevice>& display);
  400. bool getForceClientComposition(const sp<DisplayDevice>& display);
  401. virtual void setPerFrameData(const sp<const DisplayDevice>& display,
  402. const ui::Transform& transform, const Rect& viewport,
  403. int32_t supportedPerFrameMetadata,
  404. const ui::Dataspace targetDataspace) = 0;
  405. // callIntoHwc exists so we can update our local state and call
  406. // acceptDisplayChanges without unnecessarily updating the device's state
  407. void setCompositionType(const sp<const DisplayDevice>& display,
  408. Hwc2::IComposerClient::Composition type);
  409. Hwc2::IComposerClient::Composition getCompositionType(
  410. const sp<const DisplayDevice>& display) const;
  411. bool getClearClientTarget(const sp<const DisplayDevice>& display) const;
  412. void updateCursorPosition(const sp<const DisplayDevice>& display);
  413. virtual bool shouldPresentNow(nsecs_t /*expectedPresentTime*/) const { return false; }
  414. virtual void setTransformHint(uint32_t /*orientation*/) const { }
  415. /*
  416. * called before composition.
  417. * returns true if the layer has pending updates.
  418. */
  419. virtual bool onPreComposition(nsecs_t refreshStartTime) = 0;
  420. /*
  421. * called after composition.
  422. * returns true if the layer latched a new buffer this frame.
  423. */
  424. virtual bool onPostComposition(const std::optional<DisplayId>& /*displayId*/,
  425. const std::shared_ptr<FenceTime>& /*glDoneFence*/,
  426. const std::shared_ptr<FenceTime>& /*presentFence*/,
  427. const CompositorTiming& /*compositorTiming*/) {
  428. return false;
  429. }
  430. // If a buffer was replaced this frame, release the former buffer
  431. virtual void releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) { }
  432. /*
  433. * prepareClientLayer - populates a renderengine::LayerSettings to passed to
  434. * RenderEngine::drawLayers. Returns true if the layer can be used, and
  435. * false otherwise.
  436. */
  437. bool prepareClientLayer(const RenderArea& renderArea, const Region& clip, Region& clearRegion,
  438. const bool supportProtectedContent, renderengine::LayerSettings& layer);
  439. bool prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
  440. Region& clearRegion, const bool supportProtectedContent,
  441. renderengine::LayerSettings& layer);
  442. /*
  443. * doTransaction - process the transaction. This is a good place to figure
  444. * out which attributes of the surface have changed.
  445. */
  446. uint32_t doTransaction(uint32_t transactionFlags);
  447. /*
  448. * setVisibleRegion - called to set the new visible region. This gives
  449. * a chance to update the new visible region or record the fact it changed.
  450. */
  451. void setVisibleRegion(const Region& visibleRegion);
  452. /*
  453. * setCoveredRegion - called when the covered region changes. The covered
  454. * region corresponds to any area of the surface that is covered
  455. * (transparently or not) by another surface.
  456. */
  457. void setCoveredRegion(const Region& coveredRegion);
  458. /*
  459. * setVisibleNonTransparentRegion - called when the visible and
  460. * non-transparent region changes.
  461. */
  462. void setVisibleNonTransparentRegion(const Region& visibleNonTransparentRegion);
  463. /*
  464. * Clear the visible, covered, and non-transparent regions.
  465. */
  466. void clearVisibilityRegions();
  467. /*
  468. * latchBuffer - called each time the screen is redrawn and returns whether
  469. * the visible regions need to be recomputed (this is a fairly heavy
  470. * operation, so this should be set only if needed). Typically this is used
  471. * to figure out if the content or size of a surface has changed.
  472. */
  473. virtual bool latchBuffer(bool& /*recomputeVisibleRegions*/, nsecs_t /*latchTime*/) {
  474. return {};
  475. }
  476. virtual bool isBufferLatched() const { return false; }
  477. /*
  478. * Remove relative z for the layer if its relative parent is not part of the
  479. * provided layer tree.
  480. */
  481. void removeRelativeZ(const std::vector<Layer*>& layersInTree);
  482. /*
  483. * Remove from current state and mark for removal.
  484. */
  485. void removeFromCurrentState();
  486. /*
  487. * called with the state lock from a binder thread when the layer is
  488. * removed from the current list to the pending removal list
  489. */
  490. void onRemovedFromCurrentState();
  491. /*
  492. * Called when the layer is added back to the current state list.
  493. */
  494. void addToCurrentState();
  495. // Updates the transform hint in our SurfaceFlingerConsumer to match
  496. // the current orientation of the display device.
  497. void updateTransformHint(const sp<const DisplayDevice>& display) const;
  498. /*
  499. * returns the rectangle that crops the content of the layer and scales it
  500. * to the layer's size.
  501. */
  502. Rect getContentCrop() const;
  503. /*
  504. * Returns if a frame is ready
  505. */
  506. virtual bool hasReadyFrame() const { return false; }
  507. virtual int32_t getQueuedFrameCount() const { return 0; }
  508. // -----------------------------------------------------------------------
  509. bool hasHwcLayer(const sp<const DisplayDevice>& displayDevice);
  510. HWC2::Layer* getHwcLayer(const sp<const DisplayDevice>& displayDevice);
  511. inline const State& getDrawingState() const { return mDrawingState; }
  512. inline const State& getCurrentState() const { return mCurrentState; }
  513. inline State& getCurrentState() { return mCurrentState; }
  514. LayerDebugInfo getLayerDebugInfo() const;
  515. /* always call base class first */
  516. static void miniDumpHeader(std::string& result);
  517. void miniDump(std::string& result, const sp<DisplayDevice>& display) const;
  518. void dumpFrameStats(std::string& result) const;
  519. void dumpFrameEvents(std::string& result);
  520. void clearFrameStats();
  521. void logFrameStats();
  522. void getFrameStats(FrameStats* outStats) const;
  523. virtual std::vector<OccupancyTracker::Segment> getOccupancyHistory(bool /*forceFlush*/) {
  524. return {};
  525. }
  526. void onDisconnect();
  527. void addAndGetFrameTimestamps(const NewFrameEventsEntry* newEntry,
  528. FrameEventHistoryDelta* outDelta);
  529. virtual bool getTransformToDisplayInverse() const { return false; }
  530. ui::Transform getTransform() const;
  531. // Returns the Alpha of the Surface, accounting for the Alpha
  532. // of parent Surfaces in the hierarchy (alpha's will be multiplied
  533. // down the hierarchy).
  534. half getAlpha() const;
  535. half4 getColor() const;
  536. // Returns how rounded corners should be drawn for this layer.
  537. // This will traverse the hierarchy until it reaches its root, finding topmost rounded
  538. // corner definition and converting it into current layer's coordinates.
  539. // As of now, only 1 corner radius per display list is supported. Subsequent ones will be
  540. // ignored.
  541. RoundedCornerState getRoundedCornerState() const;
  542. void traverseInReverseZOrder(LayerVector::StateSet stateSet,
  543. const LayerVector::Visitor& visitor);
  544. void traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor);
  545. /**
  546. * Traverse only children in z order, ignoring relative layers that are not children of the
  547. * parent.
  548. */
  549. void traverseChildrenInZOrder(LayerVector::StateSet stateSet,
  550. const LayerVector::Visitor& visitor);
  551. size_t getChildrenCount() const;
  552. void addChild(const sp<Layer>& layer);
  553. // Returns index if removed, or negative value otherwise
  554. // for symmetry with Vector::remove
  555. ssize_t removeChild(const sp<Layer>& layer);
  556. sp<Layer> getParent() const { return mCurrentParent.promote(); }
  557. bool hasParent() const { return getParent() != nullptr; }
  558. Rect getScreenBounds(bool reduceTransparentRegion = true) const;
  559. bool setChildLayer(const sp<Layer>& childLayer, int32_t z);
  560. bool setChildRelativeLayer(const sp<Layer>& childLayer,
  561. const sp<IBinder>& relativeToHandle, int32_t relativeZ);
  562. // Copy the current list of children to the drawing state. Called by
  563. // SurfaceFlinger to complete a transaction.
  564. void commitChildList();
  565. int32_t getZ() const;
  566. virtual void pushPendingState();
  567. /**
  568. * Returns active buffer size in the correct orientation. Buffer size is determined by undoing
  569. * any buffer transformations. If the layer has no buffer then return INVALID_RECT.
  570. */
  571. virtual Rect getBufferSize(const Layer::State&) const { return Rect::INVALID_RECT; }
  572. /**
  573. * Returns the source bounds. If the bounds are not defined, it is inferred from the
  574. * buffer size. Failing that, the bounds are determined from the passed in parent bounds.
  575. * For the root layer, this is the display viewport size.
  576. */
  577. virtual FloatRect computeSourceBounds(const FloatRect& parentBounds) const {
  578. return parentBounds;
  579. }
  580. compositionengine::OutputLayer* findOutputLayerForDisplay(
  581. const sp<const DisplayDevice>& display) const;
  582. protected:
  583. // constant
  584. sp<SurfaceFlinger> mFlinger;
  585. /*
  586. * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
  587. * is called.
  588. */
  589. class LayerCleaner {
  590. sp<SurfaceFlinger> mFlinger;
  591. sp<Layer> mLayer;
  592. protected:
  593. ~LayerCleaner() {
  594. // destroy client resources
  595. mFlinger->onHandleDestroyed(mLayer);
  596. }
  597. public:
  598. LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
  599. : mFlinger(flinger), mLayer(layer) {}
  600. };
  601. friend class impl::SurfaceInterceptor;
  602. // For unit tests
  603. friend class TestableSurfaceFlinger;
  604. virtual void commitTransaction(const State& stateToCommit);
  605. uint32_t getEffectiveUsage(uint32_t usage) const;
  606. /**
  607. * Setup rounded corners coordinates of this layer, taking into account the layer bounds and
  608. * crop coordinates, transforming them into layer space.
  609. */
  610. void setupRoundedCornersCropCoordinates(Rect win, const FloatRect& roundedCornersCrop) const;
  611. void setParent(const sp<Layer>& layer);
  612. LayerVector makeTraversalList(LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers);
  613. void addZOrderRelative(const wp<Layer>& relative);
  614. void removeZOrderRelative(const wp<Layer>& relative);
  615. class SyncPoint {
  616. public:
  617. explicit SyncPoint(uint64_t frameNumber, wp<Layer> requestedSyncLayer)
  618. : mFrameNumber(frameNumber),
  619. mFrameIsAvailable(false),
  620. mTransactionIsApplied(false),
  621. mRequestedSyncLayer(requestedSyncLayer) {}
  622. uint64_t getFrameNumber() const { return mFrameNumber; }
  623. bool frameIsAvailable() const { return mFrameIsAvailable; }
  624. void setFrameAvailable() { mFrameIsAvailable = true; }
  625. bool transactionIsApplied() const { return mTransactionIsApplied; }
  626. void setTransactionApplied() { mTransactionIsApplied = true; }
  627. sp<Layer> getRequestedSyncLayer() { return mRequestedSyncLayer.promote(); }
  628. private:
  629. const uint64_t mFrameNumber;
  630. std::atomic<bool> mFrameIsAvailable;
  631. std::atomic<bool> mTransactionIsApplied;
  632. wp<Layer> mRequestedSyncLayer;
  633. };
  634. // SyncPoints which will be signaled when the correct frame is at the head
  635. // of the queue and dropped after the frame has been latched. Protected by
  636. // mLocalSyncPointMutex.
  637. Mutex mLocalSyncPointMutex;
  638. std::list<std::shared_ptr<SyncPoint>> mLocalSyncPoints;
  639. // SyncPoints which will be signaled and then dropped when the transaction
  640. // is applied
  641. std::list<std::shared_ptr<SyncPoint>> mRemoteSyncPoints;
  642. // Returns false if the relevant frame has already been latched
  643. bool addSyncPoint(const std::shared_ptr<SyncPoint>& point);
  644. void popPendingState(State* stateToCommit);
  645. virtual bool applyPendingStates(State* stateToCommit);
  646. virtual uint32_t doTransactionResize(uint32_t flags, Layer::State* stateToCommit);
  647. // Returns mCurrentScaling mode (originating from the
  648. // Client) or mOverrideScalingMode mode (originating from
  649. // the Surface Controller) if set.
  650. virtual uint32_t getEffectiveScalingMode() const { return 0; }
  651. public:
  652. /*
  653. * The layer handle is just a BBinder object passed to the client
  654. * (remote process) -- we don't keep any reference on our side such that
  655. * the dtor is called when the remote side let go of its reference.
  656. *
  657. * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
  658. * this layer when the handle is destroyed.
  659. */
  660. class Handle : public BBinder, public LayerCleaner {
  661. public:
  662. Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
  663. : LayerCleaner(flinger, layer), owner(layer) {}
  664. wp<Layer> owner;
  665. };
  666. // Creates a new handle each time, so we only expect
  667. // this to be called once.
  668. sp<IBinder> getHandle();
  669. const String8& getName() const;
  670. const String8& getSystemName() const;
  671. const char* systemName() const;
  672. virtual void notifyAvailableFrames() {}
  673. virtual PixelFormat getPixelFormat() const { return PIXEL_FORMAT_NONE; }
  674. bool getPremultipledAlpha() const;
  675. bool mPendingHWCDestroy{false};
  676. void setInputInfo(const InputWindowInfo& info);
  677. InputWindowInfo fillInputInfo();
  678. bool hasInput() const;
  679. protected:
  680. // -----------------------------------------------------------------------
  681. bool usingRelativeZ(LayerVector::StateSet stateSet) const;
  682. bool mPremultipliedAlpha{true};
  683. String8 mName;
  684. String8 mSystemName;
  685. String8 mTransactionName; // A cached version of "TX - " + mName for systraces
  686. bool mPrimaryDisplayOnly = false;
  687. // these are protected by an external lock
  688. State mCurrentState;
  689. State mDrawingState;
  690. std::atomic<uint32_t> mTransactionFlags{0};
  691. // Accessed from main thread and binder threads
  692. Mutex mPendingStateMutex;
  693. Vector<State> mPendingStates;
  694. // Timestamp history for UIAutomation. Thread safe.
  695. FrameTracker mFrameTracker;
  696. // Timestamp history for the consumer to query.
  697. // Accessed by both consumer and producer on main and binder threads.
  698. Mutex mFrameEventHistoryMutex;
  699. ConsumerFrameEventHistory mFrameEventHistory;
  700. FenceTimeline mAcquireTimeline;
  701. FenceTimeline mReleaseTimeline;
  702. // main thread
  703. sp<NativeHandle> mSidebandStream;
  704. // Active buffer fields
  705. sp<GraphicBuffer> mActiveBuffer;
  706. sp<Fence> mActiveBufferFence;
  707. // False if the buffer and its contents have been previously used for GPU
  708. // composition, true otherwise.
  709. bool mIsActiveBufferUpdatedForGpu = true;
  710. ui::Dataspace mCurrentDataSpace = ui::Dataspace::UNKNOWN;
  711. Rect mCurrentCrop;
  712. uint32_t mCurrentTransform{0};
  713. // We encode unset as -1.
  714. int32_t mOverrideScalingMode{-1};
  715. std::atomic<uint64_t> mCurrentFrameNumber{0};
  716. bool mFrameLatencyNeeded{false};
  717. // Whether filtering is needed b/c of the drawingstate
  718. bool mNeedsFiltering{false};
  719. std::atomic<bool> mRemovedFromCurrentState{false};
  720. // page-flip thread (currently main thread)
  721. bool mProtectedByApp{false}; // application requires protected path to external sink
  722. // protected by mLock
  723. mutable Mutex mLock;
  724. const wp<Client> mClientRef;
  725. // This layer can be a cursor on some displays.
  726. bool mPotentialCursor{false};
  727. bool mFreezeGeometryUpdates{false};
  728. // Child list about to be committed/used for editing.
  729. LayerVector mCurrentChildren{LayerVector::StateSet::Current};
  730. // Child list used for rendering.
  731. LayerVector mDrawingChildren{LayerVector::StateSet::Drawing};
  732. wp<Layer> mCurrentParent;
  733. wp<Layer> mDrawingParent;
  734. // Can only be accessed with the SF state lock held.
  735. bool mLayerDetached{false};
  736. // Can only be accessed with the SF state lock held.
  737. bool mChildrenChanged{false};
  738. // Window types from WindowManager.LayoutParams
  739. const int mWindowType;
  740. // This is populated if the layer is registered with Scheduler for tracking purposes.
  741. std::unique_ptr<scheduler::LayerHistory::LayerHandle> mSchedulerLayerHandle;
  742. private:
  743. /**
  744. * Returns an unsorted vector of all layers that are part of this tree.
  745. * That includes the current layer and all its descendants.
  746. */
  747. std::vector<Layer*> getLayersInTree(LayerVector::StateSet stateSet);
  748. /**
  749. * Traverses layers that are part of this tree in the correct z order.
  750. * layersInTree must be sorted before calling this method.
  751. */
  752. void traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
  753. LayerVector::StateSet stateSet,
  754. const LayerVector::Visitor& visitor);
  755. LayerVector makeChildrenTraversalList(LayerVector::StateSet stateSet,
  756. const std::vector<Layer*>& layersInTree);
  757. /**
  758. * Returns the cropped buffer size or the layer crop if the layer has no buffer. Return
  759. * INVALID_RECT if the layer has no buffer and no crop.
  760. * A layer with an invalid buffer size and no crop is considered to be boundless. The layer
  761. * bounds are constrained by its parent bounds.
  762. */
  763. Rect getCroppedBufferSize(const Layer::State& s) const;
  764. // Cached properties computed from drawing state
  765. // Effective transform taking into account parent transforms and any parent scaling.
  766. ui::Transform mEffectiveTransform;
  767. // Bounds of the layer before any transformation is applied and before it has been cropped
  768. // by its parents.
  769. FloatRect mSourceBounds;
  770. // Bounds of the layer in layer space. This is the mSourceBounds cropped by its layer crop and
  771. // its parent bounds.
  772. FloatRect mBounds;
  773. // Layer bounds in screen space.
  774. FloatRect mScreenBounds;
  775. void setZOrderRelativeOf(const wp<Layer>& relativeOf);
  776. bool mGetHandleCalled = false;
  777. void removeRemoteSyncPoints();
  778. };
  779. } // namespace android
  780. #define RETURN_IF_NO_HWC_LAYER(displayDevice, ...) \
  781. do { \
  782. if (!hasHwcLayer(displayDevice)) { \
  783. ALOGE("[%s] %s failed: no HWC layer found for display %s", mName.string(), \
  784. __FUNCTION__, displayDevice->getDebugName().c_str()); \
  785. return __VA_ARGS__; \
  786. } \
  787. } while (false)
  788. #endif // ANDROID_LAYER_H