BufferLayerConsumer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (C) 2010 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_BUFFERLAYERCONSUMER_H
  17. #define ANDROID_BUFFERLAYERCONSUMER_H
  18. #include <android-base/thread_annotations.h>
  19. #include <gui/BufferQueueDefs.h>
  20. #include <gui/ConsumerBase.h>
  21. #include <gui/HdrMetadata.h>
  22. #include <ui/FenceTime.h>
  23. #include <ui/GraphicBuffer.h>
  24. #include <ui/GraphicTypes.h>
  25. #include <ui/Region.h>
  26. #include <utils/String8.h>
  27. #include <utils/Vector.h>
  28. #include <utils/threads.h>
  29. namespace android {
  30. // ----------------------------------------------------------------------------
  31. class DispSync;
  32. class Layer;
  33. class String8;
  34. namespace renderengine {
  35. class RenderEngine;
  36. class Image;
  37. } // namespace renderengine
  38. /*
  39. * BufferLayerConsumer consumes buffers of graphics data from a BufferQueue,
  40. * and makes them available to RenderEngine as a texture.
  41. *
  42. * A typical usage pattern is to call updateTexImage() when a new frame is
  43. * desired. If a new frame is available, the frame is latched. If not, the
  44. * previous contents are retained. The texture is attached and updated after
  45. * bindTextureImage() is called.
  46. *
  47. * All calls to updateTexImage must be made with RenderEngine being current.
  48. * The texture is attached to the TEXTURE_EXTERNAL texture target.
  49. */
  50. class BufferLayerConsumer : public ConsumerBase {
  51. public:
  52. static const status_t BUFFER_REJECTED = UNKNOWN_ERROR + 8;
  53. class BufferRejecter {
  54. friend class BufferLayerConsumer;
  55. virtual bool reject(const sp<GraphicBuffer>& buf, const BufferItem& item) = 0;
  56. protected:
  57. virtual ~BufferRejecter() {}
  58. };
  59. struct ContentsChangedListener : public FrameAvailableListener {
  60. virtual void onSidebandStreamChanged() = 0;
  61. };
  62. // BufferLayerConsumer constructs a new BufferLayerConsumer object. The
  63. // tex parameter indicates the name of the RenderEngine texture to which
  64. // images are to be streamed.
  65. BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, renderengine::RenderEngine& engine,
  66. uint32_t tex, Layer* layer);
  67. // Sets the contents changed listener. This should be used instead of
  68. // ConsumerBase::setFrameAvailableListener().
  69. void setContentsChangedListener(const wp<ContentsChangedListener>& listener);
  70. // updateTexImage acquires the most recently queued buffer, and sets the
  71. // image contents of the target texture to it.
  72. //
  73. // This call may only be made while RenderEngine is current.
  74. //
  75. // This calls doFenceWait to ensure proper synchronization unless native
  76. // fence is supported.
  77. //
  78. // Unlike the GLConsumer version, this version takes a functor that may be
  79. // used to reject the newly acquired buffer. It also does not bind the
  80. // RenderEngine texture until bindTextureImage is called.
  81. status_t updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime,
  82. bool* autoRefresh, bool* queuedBuffer, uint64_t maxFrameNumber);
  83. // See BufferLayerConsumer::bindTextureImageLocked().
  84. status_t bindTextureImage();
  85. // setReleaseFence stores a fence that will signal when the current buffer
  86. // is no longer being read. This fence will be returned to the producer
  87. // when the current buffer is released by updateTexImage(). Multiple
  88. // fences can be set for a given buffer; they will be merged into a single
  89. // union fence.
  90. void setReleaseFence(const sp<Fence>& fence);
  91. bool releasePendingBuffer();
  92. sp<Fence> getPrevFinalReleaseFence() const;
  93. // See GLConsumer::getTransformMatrix.
  94. void getTransformMatrix(float mtx[16]);
  95. // getTimestamp retrieves the timestamp associated with the texture image
  96. // set by the most recent call to updateTexImage.
  97. //
  98. // The timestamp is in nanoseconds, and is monotonically increasing. Its
  99. // other semantics (zero point, etc) are source-dependent and should be
  100. // documented by the source.
  101. int64_t getTimestamp();
  102. // getDataSpace retrieves the DataSpace associated with the texture image
  103. // set by the most recent call to updateTexImage.
  104. ui::Dataspace getCurrentDataSpace();
  105. // getCurrentHdrMetadata retrieves the HDR metadata associated with the
  106. // texture image set by the most recent call to updateTexImage.
  107. const HdrMetadata& getCurrentHdrMetadata() const;
  108. // getFrameNumber retrieves the frame number associated with the texture
  109. // image set by the most recent call to updateTexImage.
  110. //
  111. // The frame number is an incrementing counter set to 0 at the creation of
  112. // the BufferQueue associated with this consumer.
  113. uint64_t getFrameNumber();
  114. bool getTransformToDisplayInverse() const;
  115. // must be called from SF main thread
  116. const Region& getSurfaceDamage() const;
  117. // Merge the given damage region into the current damage region value.
  118. void mergeSurfaceDamage(const Region& damage);
  119. // getCurrentApi retrieves the API which queues the current buffer.
  120. int getCurrentApi() const;
  121. // See GLConsumer::setDefaultBufferSize.
  122. status_t setDefaultBufferSize(uint32_t width, uint32_t height);
  123. // setFilteringEnabled sets whether the transform matrix should be computed
  124. // for use with bilinear filtering.
  125. void setFilteringEnabled(bool enabled);
  126. // getCurrentBuffer returns the buffer associated with the current image.
  127. // When outSlot is not nullptr, the current buffer slot index is also
  128. // returned. Simiarly, when outFence is not nullptr, the current output
  129. // fence is returned.
  130. sp<GraphicBuffer> getCurrentBuffer(int* outSlot = nullptr, sp<Fence>* outFence = nullptr) const;
  131. // getCurrentCrop returns the cropping rectangle of the current buffer.
  132. Rect getCurrentCrop() const;
  133. // getCurrentTransform returns the transform of the current buffer.
  134. uint32_t getCurrentTransform() const;
  135. // getCurrentScalingMode returns the scaling mode of the current buffer.
  136. uint32_t getCurrentScalingMode() const;
  137. // getCurrentFence returns the fence indicating when the current buffer is
  138. // ready to be read from.
  139. sp<Fence> getCurrentFence() const;
  140. // getCurrentFence returns the FenceTime indicating when the current
  141. // buffer is ready to be read from.
  142. std::shared_ptr<FenceTime> getCurrentFenceTime() const;
  143. // setConsumerUsageBits overrides the ConsumerBase method to OR
  144. // DEFAULT_USAGE_FLAGS to usage.
  145. status_t setConsumerUsageBits(uint64_t usage);
  146. void onBufferAvailable(const BufferItem& item) EXCLUDES(mImagesMutex);
  147. protected:
  148. // abandonLocked overrides the ConsumerBase method to clear
  149. // mCurrentTextureImage in addition to the ConsumerBase behavior.
  150. virtual void abandonLocked() EXCLUDES(mImagesMutex);
  151. // dumpLocked overrides the ConsumerBase method to dump BufferLayerConsumer-
  152. // specific info in addition to the ConsumerBase behavior.
  153. virtual void dumpLocked(String8& result, const char* prefix) const;
  154. // See ConsumerBase::acquireBufferLocked
  155. virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
  156. uint64_t maxFrameNumber = 0) override
  157. EXCLUDES(mImagesMutex);
  158. bool canUseImageCrop(const Rect& crop) const;
  159. struct PendingRelease {
  160. PendingRelease() : isPending(false), currentTexture(-1), graphicBuffer() {}
  161. bool isPending;
  162. int currentTexture;
  163. sp<GraphicBuffer> graphicBuffer;
  164. };
  165. // This releases the buffer in the slot referenced by mCurrentTexture,
  166. // then updates state to refer to the BufferItem, which must be a
  167. // newly-acquired buffer. If pendingRelease is not null, the parameters
  168. // which would have been passed to releaseBufferLocked upon the successful
  169. // completion of the method will instead be returned to the caller, so that
  170. // it may call releaseBufferLocked itself later.
  171. status_t updateAndReleaseLocked(const BufferItem& item,
  172. PendingRelease* pendingRelease = nullptr)
  173. EXCLUDES(mImagesMutex);
  174. // Binds mTexName and the current buffer to TEXTURE_EXTERNAL target.
  175. // If the bind succeeds, this calls doFenceWait.
  176. status_t bindTextureImageLocked();
  177. private:
  178. // Utility class for managing GraphicBuffer references into renderengine
  179. class Image {
  180. public:
  181. Image(const sp<GraphicBuffer>& graphicBuffer, renderengine::RenderEngine& engine);
  182. virtual ~Image();
  183. const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
  184. private:
  185. // mGraphicBuffer is the buffer that was used to create this image.
  186. sp<GraphicBuffer> mGraphicBuffer;
  187. // Back-reference into renderengine to initiate cleanup.
  188. renderengine::RenderEngine& mRE;
  189. DISALLOW_COPY_AND_ASSIGN(Image);
  190. };
  191. // freeBufferLocked frees up the given buffer slot. If the slot has been
  192. // initialized this will release the reference to the GraphicBuffer in
  193. // that slot. Otherwise it has no effect.
  194. //
  195. // This method must be called with mMutex locked.
  196. virtual void freeBufferLocked(int slotIndex) EXCLUDES(mImagesMutex);
  197. // IConsumerListener interface
  198. void onDisconnect() override;
  199. void onSidebandStreamChanged() override;
  200. void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
  201. FrameEventHistoryDelta* outDelta) override;
  202. // computeCurrentTransformMatrixLocked computes the transform matrix for the
  203. // current texture. It uses mCurrentTransform and the current GraphicBuffer
  204. // to compute this matrix and stores it in mCurrentTransformMatrix.
  205. // mCurrentTextureImage must not be nullptr.
  206. void computeCurrentTransformMatrixLocked();
  207. // doFenceWaitLocked inserts a wait command into the RenderEngine command
  208. // stream to ensure that it is safe for future RenderEngine commands to
  209. // access the current texture buffer.
  210. status_t doFenceWaitLocked() const;
  211. // getCurrentCropLocked returns the cropping rectangle of the current buffer.
  212. Rect getCurrentCropLocked() const;
  213. // The default consumer usage flags that BufferLayerConsumer always sets on its
  214. // BufferQueue instance; these will be OR:d with any additional flags passed
  215. // from the BufferLayerConsumer user. In particular, BufferLayerConsumer will always
  216. // consume buffers as hardware textures.
  217. static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
  218. // mCurrentTextureBuffer is the buffer containing the current texture. It's
  219. // possible that this buffer is not associated with any buffer slot, so we
  220. // must track it separately in order to support the getCurrentBuffer method.
  221. std::shared_ptr<Image> mCurrentTextureBuffer;
  222. // mCurrentCrop is the crop rectangle that applies to the current texture.
  223. // It gets set each time updateTexImage is called.
  224. Rect mCurrentCrop;
  225. // mCurrentTransform is the transform identifier for the current texture. It
  226. // gets set each time updateTexImage is called.
  227. uint32_t mCurrentTransform;
  228. // mCurrentScalingMode is the scaling mode for the current texture. It gets
  229. // set each time updateTexImage is called.
  230. uint32_t mCurrentScalingMode;
  231. // mCurrentFence is the fence received from BufferQueue in updateTexImage.
  232. sp<Fence> mCurrentFence;
  233. // The FenceTime wrapper around mCurrentFence.
  234. std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
  235. // mCurrentTransformMatrix is the transform matrix for the current texture.
  236. // It gets computed by computeTransformMatrix each time updateTexImage is
  237. // called.
  238. float mCurrentTransformMatrix[16];
  239. // mCurrentTimestamp is the timestamp for the current texture. It
  240. // gets set each time updateTexImage is called.
  241. int64_t mCurrentTimestamp;
  242. // mCurrentDataSpace is the dataspace for the current texture. It
  243. // gets set each time updateTexImage is called.
  244. ui::Dataspace mCurrentDataSpace;
  245. // mCurrentHdrMetadata is the HDR metadata for the current texture. It
  246. // gets set each time updateTexImage is called.
  247. HdrMetadata mCurrentHdrMetadata;
  248. // mCurrentFrameNumber is the frame counter for the current texture.
  249. // It gets set each time updateTexImage is called.
  250. uint64_t mCurrentFrameNumber;
  251. // Indicates this buffer must be transformed by the inverse transform of the screen
  252. // it is displayed onto. This is applied after BufferLayerConsumer::mCurrentTransform.
  253. // This must be set/read from SurfaceFlinger's main thread.
  254. bool mCurrentTransformToDisplayInverse;
  255. // The portion of this surface that has changed since the previous frame
  256. Region mCurrentSurfaceDamage;
  257. int mCurrentApi;
  258. uint32_t mDefaultWidth, mDefaultHeight;
  259. // mFilteringEnabled indicates whether the transform matrix is computed for
  260. // use with bilinear filtering. It defaults to true and is changed by
  261. // setFilteringEnabled().
  262. bool mFilteringEnabled;
  263. renderengine::RenderEngine& mRE;
  264. // mTexName is the name of the RenderEngine texture to which streamed
  265. // images will be bound when bindTexImage is called. It is set at
  266. // construction time.
  267. const uint32_t mTexName;
  268. // The layer for this BufferLayerConsumer
  269. const wp<Layer> mLayer;
  270. wp<ContentsChangedListener> mContentsChangedListener;
  271. // mCurrentTexture is the buffer slot index of the buffer that is currently
  272. // bound to the RenderEngine texture. It is initialized to INVALID_BUFFER_SLOT,
  273. // indicating that no buffer slot is currently bound to the texture. Note,
  274. // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
  275. // that no buffer is bound to the texture. A call to setBufferCount will
  276. // reset mCurrentTexture to INVALID_BUFFER_SLOT.
  277. int mCurrentTexture;
  278. // Shadow buffer cache for cleaning up renderengine references.
  279. std::shared_ptr<Image> mImages[BufferQueueDefs::NUM_BUFFER_SLOTS] GUARDED_BY(mImagesMutex);
  280. // Separate mutex guarding the shadow buffer cache.
  281. // mImagesMutex can be manipulated with binder threads (e.g. onBuffersAllocated)
  282. // which is contentious enough that we can't just use mMutex.
  283. mutable std::mutex mImagesMutex;
  284. // A release that is pending on the receipt of a new release fence from
  285. // presentDisplay
  286. PendingRelease mPendingRelease;
  287. };
  288. // ----------------------------------------------------------------------------
  289. }; // namespace android
  290. #endif // ANDROID_BUFFERLAYERCONSUMER_H