Layer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 <stdint.h>
  19. #include <sys/types.h>
  20. #include <EGL/egl.h>
  21. #include <EGL/eglext.h>
  22. #include <utils/RefBase.h>
  23. #include <utils/String8.h>
  24. #include <utils/Timers.h>
  25. #include <ui/FrameStats.h>
  26. #include <ui/GraphicBuffer.h>
  27. #include <ui/PixelFormat.h>
  28. #include <ui/Region.h>
  29. #include <gui/ISurfaceComposerClient.h>
  30. #include <private/gui/LayerState.h>
  31. #include "FrameTracker.h"
  32. #include "Client.h"
  33. #include "MonitoredProducer.h"
  34. #include "SurfaceFlinger.h"
  35. #include "SurfaceFlingerConsumer.h"
  36. #include "Transform.h"
  37. #include "DisplayHardware/HWComposer.h"
  38. #include "DisplayHardware/FloatRect.h"
  39. #include "RenderEngine/Mesh.h"
  40. #include "RenderEngine/Texture.h"
  41. namespace android {
  42. // ---------------------------------------------------------------------------
  43. class Client;
  44. class Colorizer;
  45. class DisplayDevice;
  46. class GraphicBuffer;
  47. class SurfaceFlinger;
  48. class LayerBlur;
  49. // ---------------------------------------------------------------------------
  50. /*
  51. * A new BufferQueue and a new SurfaceFlingerConsumer are created when the
  52. * Layer is first referenced.
  53. *
  54. * This also implements onFrameAvailable(), which notifies SurfaceFlinger
  55. * that new data has arrived.
  56. */
  57. class Layer : public SurfaceFlingerConsumer::ContentsChangedListener {
  58. static int32_t sSequence;
  59. friend class LayerBlur;
  60. public:
  61. #ifdef QTI_BSP
  62. friend class ExLayer;
  63. #endif
  64. mutable bool contentDirty;
  65. // regions below are in window-manager space
  66. Region visibleRegion;
  67. Region coveredRegion;
  68. Region visibleNonTransparentRegion;
  69. Region surfaceDamageRegion;
  70. // Layer serial number. This gives layers an explicit ordering, so we
  71. // have a stable sort order when their layer stack and Z-order are
  72. // the same.
  73. int32_t sequence;
  74. enum { // flags for doTransaction()
  75. eDontUpdateGeometryState = 0x00000001,
  76. eVisibleRegion = 0x00000002,
  77. };
  78. struct Geometry {
  79. float x;
  80. float y;
  81. uint32_t w;
  82. uint32_t h;
  83. bool isPositionPending;
  84. Rect crop;
  85. inline bool operator ==(const Geometry& rhs) const {
  86. return (w == rhs.w && h == rhs.h && crop == rhs.crop && x == rhs.x && y == rhs.y
  87. && isPositionPending == rhs.isPositionPending);
  88. }
  89. inline bool operator !=(const Geometry& rhs) const {
  90. return !operator ==(rhs);
  91. }
  92. };
  93. struct State {
  94. Geometry active;
  95. Geometry requested;
  96. uint32_t z;
  97. uint32_t layerStack;
  98. uint8_t blur;
  99. uint8_t alpha;
  100. uint8_t flags;
  101. uint8_t reserved[2];
  102. int32_t sequence; // changes when visible regions can change
  103. Transform transform;
  104. // the transparentRegion hint is a bit special, it's latched only
  105. // when we receive a buffer -- this is because it's "content"
  106. // dependent.
  107. Region activeTransparentRegion;
  108. Region requestedTransparentRegion;
  109. };
  110. // -----------------------------------------------------------------------
  111. Layer(SurfaceFlinger* flinger, const sp<Client>& client,
  112. const String8& name, uint32_t w, uint32_t h, uint32_t flags);
  113. virtual ~Layer();
  114. // the this layer's size and format
  115. status_t setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
  116. // modify current state
  117. bool setPosition(float x, float y);
  118. bool setLayer(uint32_t z);
  119. bool setBlur(uint8_t blur);
  120. virtual bool setBlurMaskLayer(sp<Layer>& /*maskLayer*/) { return false; }
  121. virtual bool setBlurMaskSampling(int32_t /*sampling*/) { return false; }
  122. virtual bool setBlurMaskAlphaThreshold(float /*alpha*/) { return false; }
  123. bool setSize(uint32_t w, uint32_t h);
  124. bool setAlpha(uint8_t alpha);
  125. bool setMatrix(const layer_state_t::matrix22_t& matrix);
  126. bool setTransparentRegionHint(const Region& transparent);
  127. bool setFlags(uint8_t flags, uint8_t mask);
  128. bool setCrop(const Rect& crop);
  129. bool setLayerStack(uint32_t layerStack);
  130. // If we have received a new buffer this frame, we will pass its surface
  131. // damage down to hardware composer. Otherwise, we must send a region with
  132. // one empty rect.
  133. void useSurfaceDamage();
  134. void useEmptyDamage();
  135. uint32_t getTransactionFlags(uint32_t flags);
  136. uint32_t setTransactionFlags(uint32_t flags);
  137. #ifdef QTI_BSP
  138. virtual void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
  139. bool useIdentityTransform) const;
  140. #else
  141. void computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh,
  142. bool useIdentityTransform) const;
  143. #endif
  144. Rect computeBounds(const Region& activeTransparentRegion) const;
  145. Rect computeBounds() const;
  146. sp<IBinder> getHandle();
  147. sp<IGraphicBufferProducer> getProducer() const;
  148. const String8& getName() const;
  149. // -----------------------------------------------------------------------
  150. // Virtuals
  151. virtual const char* getTypeId() const { return "Layer"; }
  152. /*
  153. * isOpaque - true if this surface is opaque
  154. *
  155. * This takes into account the buffer format (i.e. whether or not the
  156. * pixel format includes an alpha channel) and the "opaque" flag set
  157. * on the layer. It does not examine the current plane alpha value.
  158. */
  159. virtual bool isOpaque(const Layer::State& s) const;
  160. /*
  161. * isSecure - true if this surface is secure, that is if it prevents
  162. * screenshots or VNC servers.
  163. */
  164. virtual bool isSecure() const;
  165. /*
  166. * isProtected - true if the layer may contain protected content in the
  167. * GRALLOC_USAGE_PROTECTED sense.
  168. */
  169. virtual bool isProtected() const;
  170. /*
  171. * isVisible - true if this layer is visible, false otherwise
  172. */
  173. virtual bool isVisible() const;
  174. /*
  175. * isFixedSize - true if content has a fixed size
  176. */
  177. virtual bool isFixedSize() const;
  178. /*
  179. * isBlurLayer - true if this is a LayerBlur instance
  180. */
  181. virtual bool isBlurLayer() const { return false; }
  182. protected:
  183. /*
  184. * onDraw - draws the surface.
  185. */
  186. virtual void onDraw(const sp<const DisplayDevice>& hw, const Region& clip,
  187. bool useIdentityTransform);
  188. public:
  189. // -----------------------------------------------------------------------
  190. void setGeometry(const sp<const DisplayDevice>& hw,
  191. HWComposer::HWCLayerInterface& layer);
  192. virtual void setPerFrameData(const sp<const DisplayDevice>& hw,
  193. HWComposer::HWCLayerInterface& layer);
  194. void setAcquireFence(const sp<const DisplayDevice>& hw,
  195. HWComposer::HWCLayerInterface& layer);
  196. Rect getPosition(const sp<const DisplayDevice>& hw);
  197. /*
  198. * called after page-flip
  199. */
  200. void onLayerDisplayed(const sp<const DisplayDevice>& hw,
  201. HWComposer::HWCLayerInterface* layer);
  202. bool shouldPresentNow(const DispSync& dispSync) const;
  203. /*
  204. * called before composition.
  205. * returns true if the layer has pending updates.
  206. */
  207. bool onPreComposition();
  208. /*
  209. * called after composition.
  210. */
  211. void onPostComposition();
  212. /*
  213. * draw - performs some global clipping optimizations
  214. * and calls onDraw().
  215. */
  216. void draw(const sp<const DisplayDevice>& hw, const Region& clip);
  217. void draw(const sp<const DisplayDevice>& hw, bool useIdentityTransform);
  218. void draw(const sp<const DisplayDevice>& hw);
  219. /*
  220. * doTransaction - process the transaction. This is a good place to figure
  221. * out which attributes of the surface have changed.
  222. */
  223. uint32_t doTransaction(uint32_t transactionFlags);
  224. /*
  225. * setVisibleRegion - called to set the new visible region. This gives
  226. * a chance to update the new visible region or record the fact it changed.
  227. */
  228. void setVisibleRegion(const Region& visibleRegion);
  229. /*
  230. * setCoveredRegion - called when the covered region changes. The covered
  231. * region corresponds to any area of the surface that is covered
  232. * (transparently or not) by another surface.
  233. */
  234. void setCoveredRegion(const Region& coveredRegion);
  235. /*
  236. * setVisibleNonTransparentRegion - called when the visible and
  237. * non-transparent region changes.
  238. */
  239. void setVisibleNonTransparentRegion(const Region&
  240. visibleNonTransparentRegion);
  241. /*
  242. * latchBuffer - called each time the screen is redrawn and returns whether
  243. * the visible regions need to be recomputed (this is a fairly heavy
  244. * operation, so this should be set only if needed). Typically this is used
  245. * to figure out if the content or size of a surface has changed.
  246. */
  247. Region latchBuffer(bool& recomputeVisibleRegions);
  248. bool isPotentialCursor() const { return mPotentialCursor;}
  249. /*
  250. * called with the state lock when the surface is removed from the
  251. * current list
  252. */
  253. void onRemoved();
  254. // Updates the transform hint in our SurfaceFlingerConsumer to match
  255. // the current orientation of the display device.
  256. void updateTransformHint(const sp<const DisplayDevice>& hw) ;
  257. /* ------------------------------------------------------------------------
  258. * Extensions
  259. */
  260. virtual bool isExtOnly() const { return false; }
  261. virtual bool isIntOnly() const { return false; }
  262. virtual bool isSecureDisplay() const { return false; }
  263. virtual bool isYuvLayer() const { return false; }
  264. virtual void setPosition(const sp<const DisplayDevice>& /*hw*/,
  265. HWComposer::HWCLayerInterface& /*layer*/,
  266. const State& /*state*/) { }
  267. virtual void setAcquiredFenceIfBlit(int& /*fenceFd */,
  268. HWComposer::HWCLayerInterface& /*layer */) { }
  269. virtual bool canAllowGPUForProtected() const { return false; }
  270. /*
  271. * returns the rectangle that crops the content of the layer and scales it
  272. * to the layer's size.
  273. */
  274. Rect getContentCrop() const;
  275. /*
  276. * Returns if a frame is queued.
  277. */
  278. bool hasQueuedFrame() const { return mQueuedFrames > 0 || mSidebandStreamChanged; }
  279. // -----------------------------------------------------------------------
  280. void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip) const;
  281. void setFiltering(bool filtering);
  282. bool getFiltering() const;
  283. // only for debugging
  284. inline const sp<GraphicBuffer>& getActiveBuffer() const { return mActiveBuffer; }
  285. inline const State& getDrawingState() const { return mDrawingState; }
  286. inline const State& getCurrentState() const { return mCurrentState; }
  287. inline State& getCurrentState() { return mCurrentState; }
  288. /* always call base class first */
  289. void dump(String8& result, Colorizer& colorizer) const;
  290. void dumpFrameStats(String8& result) const;
  291. void clearFrameStats();
  292. void logFrameStats();
  293. void getFrameStats(FrameStats* outStats) const;
  294. protected:
  295. // constant
  296. sp<SurfaceFlinger> mFlinger;
  297. virtual void onFirstRef();
  298. /*
  299. * Trivial class, used to ensure that mFlinger->onLayerDestroyed(mLayer)
  300. * is called.
  301. */
  302. class LayerCleaner {
  303. sp<SurfaceFlinger> mFlinger;
  304. wp<Layer> mLayer;
  305. protected:
  306. ~LayerCleaner();
  307. public:
  308. LayerCleaner(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer);
  309. };
  310. Rect reduce(const Rect& win, const Region& exclude) const;
  311. private:
  312. // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
  313. virtual void onFrameAvailable(const BufferItem& item) override;
  314. virtual void onFrameReplaced(const BufferItem& item) override;
  315. virtual void onSidebandStreamChanged() override;
  316. void commitTransaction();
  317. // needsLinearFiltering - true if this surface's state requires filtering
  318. bool needsFiltering(const sp<const DisplayDevice>& hw) const;
  319. uint32_t getEffectiveUsage(uint32_t usage) const;
  320. FloatRect computeCrop(const sp<const DisplayDevice>& hw) const;
  321. bool isCropped() const;
  322. static bool getOpacityForFormat(uint32_t format);
  323. // drawing
  324. void clearWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
  325. float r, float g, float b, float alpha) const;
  326. #ifdef QTI_BSP
  327. virtual void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
  328. bool useIdentityTransform) const;
  329. #else
  330. void drawWithOpenGL(const sp<const DisplayDevice>& hw, const Region& clip,
  331. bool useIdentityTransform) const;
  332. #endif
  333. // Temporary - Used only for LEGACY camera mode.
  334. uint32_t getProducerStickyTransform() const;
  335. // -----------------------------------------------------------------------
  336. // constants
  337. sp<SurfaceFlingerConsumer> mSurfaceFlingerConsumer;
  338. sp<IGraphicBufferProducer> mProducer;
  339. uint32_t mTextureName; // from GLES
  340. bool mPremultipliedAlpha;
  341. String8 mName;
  342. PixelFormat mFormat;
  343. // these are protected by an external lock
  344. State mCurrentState;
  345. State mDrawingState;
  346. volatile int32_t mTransactionFlags;
  347. // thread-safe
  348. volatile int32_t mQueuedFrames;
  349. volatile int32_t mSidebandStreamChanged; // used like an atomic boolean
  350. FrameTracker mFrameTracker;
  351. // main thread
  352. sp<GraphicBuffer> mActiveBuffer;
  353. sp<NativeHandle> mSidebandStream;
  354. Rect mCurrentCrop;
  355. uint32_t mCurrentTransform;
  356. uint32_t mCurrentScalingMode;
  357. bool mCurrentOpacity;
  358. bool mRefreshPending;
  359. bool mFrameLatencyNeeded;
  360. // Whether filtering is forced on or not
  361. bool mFiltering;
  362. // Whether filtering is needed b/c of the drawingstate
  363. bool mNeedsFiltering;
  364. // The mesh used to draw the layer in GLES composition mode
  365. mutable Mesh mMesh;
  366. // The texture used to draw the layer in GLES composition mode
  367. mutable Texture mTexture;
  368. // page-flip thread (currently main thread)
  369. bool mProtectedByApp; // application requires protected path to external sink
  370. // protected by mLock
  371. mutable Mutex mLock;
  372. // Set to true once we've returned this surface's handle
  373. mutable bool mHasSurface;
  374. const wp<Client> mClientRef;
  375. // This layer can be a cursor on some displays.
  376. bool mPotentialCursor;
  377. // Local copy of the queued contents of the incoming BufferQueue
  378. mutable Mutex mQueueItemLock;
  379. Condition mQueueItemCondition;
  380. Vector<BufferItem> mQueueItems;
  381. uint64_t mLastFrameNumberReceived;
  382. bool mUpdateTexImageFailed; // This is only modified from the main thread
  383. uint32_t mTransformHint;
  384. };
  385. // ---------------------------------------------------------------------------
  386. }; // namespace android
  387. #endif // ANDROID_LAYER_H