buffer.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_EXO_BUFFER_H_
  5. #define COMPONENTS_EXO_BUFFER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/cancelable_callback.h"
  9. #include "base/containers/flat_map.h"
  10. #include "base/files/file_descriptor_watcher_posix.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/time/time.h"
  13. #include "components/exo/protected_native_pixmap_query_delegate.h"
  14. #include "components/viz/common/resources/transferable_resource.h"
  15. #include "media/media_buildflags.h"
  16. #include "third_party/skia/include/core/SkColor.h"
  17. #include "ui/gfx/geometry/size.h"
  18. #include "ui/gfx/gpu_fence.h"
  19. #include "ui/gfx/gpu_memory_buffer.h"
  20. namespace exo {
  21. class FrameSinkResourceManager;
  22. // This class provides the content for a Surface. The mechanism by which a
  23. // client provides and updates the contents is the responsibility of the client
  24. // and not defined as part of this class.
  25. class Buffer : public base::SupportsWeakPtr<Buffer> {
  26. public:
  27. explicit Buffer(std::unique_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer);
  28. Buffer(std::unique_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer,
  29. unsigned texture_target,
  30. unsigned query_type,
  31. bool use_zero_copy,
  32. bool is_overlay_candidate,
  33. bool y_invert);
  34. Buffer(const Buffer&) = delete;
  35. Buffer& operator=(const Buffer&) = delete;
  36. virtual ~Buffer();
  37. const gfx::GpuMemoryBuffer* gfx_buffer() const {
  38. return gpu_memory_buffer_.get();
  39. }
  40. // Set the callback to run when the buffer is no longer used by the
  41. // compositor. The client is free to re-use or destroy this buffer and
  42. // its backing storage after this has been called.
  43. void set_release_callback(const base::RepeatingClosure& release_callback) {
  44. release_callback_ = release_callback;
  45. }
  46. // Returns if this buffer's contents are vertically inverted.
  47. bool y_invert() const { return y_invert_; }
  48. // This function can be used to acquire a texture mailbox for the contents of
  49. // buffer. |release_callback| will be called when the contents of the buffer
  50. // are no longer required.
  51. using PerCommitExplicitReleaseCallback =
  52. base::OnceCallback<void(gfx::GpuFenceHandle)>;
  53. virtual bool ProduceTransferableResource(
  54. FrameSinkResourceManager* resource_manager,
  55. std::unique_ptr<gfx::GpuFence> acquire_fence,
  56. bool secure_output_only,
  57. viz::TransferableResource* resource,
  58. ProtectedNativePixmapQueryDelegate* protected_native_pixmap_query,
  59. PerCommitExplicitReleaseCallback per_commit_explicit_release_callback);
  60. // This should be called when the buffer is attached to a Surface.
  61. void OnAttach();
  62. // This should be called when the buffer is detached from a surface.
  63. void OnDetach();
  64. // Returns the size of the buffer.
  65. virtual gfx::Size GetSize() const;
  66. // Returns the format of the buffer.
  67. gfx::BufferFormat GetFormat() const;
  68. // The default color to be used should transferable resource production fail.
  69. virtual SkColor4f GetColor() const;
  70. #if BUILDFLAG(USE_ARC_PROTECTED_MEDIA)
  71. // Returns true if the underlying buffer is hardware protected. This should
  72. // only be checked if the corresponding surface requires secure output,
  73. // otherwise it will yield false positives.
  74. bool NeedsHardwareProtection();
  75. #endif // BUILDFLAG(USE_ARC_PROTECTED_MEDIA)
  76. // Set the amount of time to wait for buffer release.
  77. void set_wait_for_release_delay_for_testing(
  78. base::TimeDelta wait_for_release_delay) {
  79. wait_for_release_delay_ = wait_for_release_delay;
  80. }
  81. private:
  82. class Texture;
  83. struct BufferRelease {
  84. BufferRelease(
  85. gfx::GpuFenceHandle release_fence,
  86. std::unique_ptr<base::FileDescriptorWatcher::Controller> controller,
  87. base::OnceClosure buffer_release_callback);
  88. ~BufferRelease();
  89. BufferRelease(const BufferRelease&) = delete;
  90. BufferRelease& operator=(const BufferRelease&) = delete;
  91. BufferRelease(BufferRelease&&);
  92. BufferRelease& operator=(BufferRelease&&);
  93. // |release_fence| must be kept above |controller| to keep the file
  94. // descriptor valid during destruction.
  95. gfx::GpuFenceHandle release_fence;
  96. std::unique_ptr<base::FileDescriptorWatcher::Controller> controller;
  97. base::OnceClosure buffer_release_callback;
  98. };
  99. #if BUILDFLAG(USE_ARC_PROTECTED_MEDIA)
  100. // For ARC protected content support this tracks the state of the
  101. // asynchronous query to determine if the GMB is using a protected buffer or
  102. // not.
  103. enum class ProtectedBufferState { UNKNOWN, QUERYING, PROTECTED, UNPROTECTED };
  104. #endif // BUILDFLAG(USE_ARC_PROTECTED_MEDIA)
  105. // This should be called when buffer is released and will notify the
  106. // client that buffer has been released.
  107. void Release();
  108. // This is used by ProduceTransferableResource() to produce a release callback
  109. // that releases a texture so it can be destroyed or reused.
  110. void ReleaseTexture(std::unique_ptr<Texture> texture,
  111. gfx::GpuFenceHandle release_fence);
  112. // This is used by ProduceTransferableResource() to produce a release callback
  113. // that releases the buffer contents referenced by a texture before the
  114. // texture is destroyed or reused.
  115. void ReleaseContentsTexture(std::unique_ptr<Texture> texture,
  116. base::OnceClosure callback,
  117. uint64_t commit_id,
  118. gfx::GpuFenceHandle release_fence);
  119. // Notifies the client that buffer has been released if no longer attached to
  120. // a surface.
  121. void ReleaseContents();
  122. void MaybeRunPerCommitRelease(uint64_t commit_id,
  123. gfx::GpuFenceHandle release_fence,
  124. base::OnceClosure buffer_release_callback);
  125. void FenceSignalled(uint64_t commit_id);
  126. #if BUILDFLAG(USE_ARC_PROTECTED_MEDIA)
  127. void OnIsProtectedNativePixmapHandle(bool is_protected);
  128. #endif // BUILDFLAG(USE_ARC_PROTECTED_MEDIA)
  129. // The GPU memory buffer that contains the contents of this buffer.
  130. std::unique_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer_;
  131. // Texture target that must be used when creating a texture for buffer.
  132. const unsigned texture_target_;
  133. // Query type that must be used when releasing buffer from a texture.
  134. const unsigned query_type_;
  135. // True if zero copy is used when producing a texture mailbox for buffer.
  136. const bool use_zero_copy_;
  137. // True if this buffer is an overlay candidate.
  138. const bool is_overlay_candidate_;
  139. // True if buffer content is vertically inverted.
  140. const bool y_invert_;
  141. // This keeps track of how many Surfaces the buffer is attached to.
  142. unsigned attach_count_ = 0;
  143. // The last used texture. ProduceTransferableResource() will use this
  144. // instead of creating a new texture when possible.
  145. std::unique_ptr<Texture> texture_;
  146. // The last used contents texture. ProduceTransferableResource() will use this
  147. // instead of creating a new texture when possible.
  148. std::unique_ptr<Texture> contents_texture_;
  149. // The client release callback.
  150. base::RepeatingClosure release_callback_;
  151. // Cancelable release contents callback. This is set when a release callback
  152. // is pending.
  153. base::CancelableOnceClosure release_contents_callback_;
  154. // The amount of time to wait for buffer release.
  155. base::TimeDelta wait_for_release_delay_;
  156. // Because viz can release buffers out of order, it's necessary to map
  157. // releases to specific commits. Identify commits via a incrementing counter.
  158. uint64_t next_commit_id_ = 0;
  159. // Maps commit count to the callback to call when we receive a release from
  160. // viz.
  161. base::flat_map<uint64_t, PerCommitExplicitReleaseCallback>
  162. pending_explicit_releases_;
  163. // Maps commit count to information required to send regular buffer releases.
  164. // Even if we send explicit synchronization release information, Wayland
  165. // protocol requires us to send regular buffer release events.
  166. base::flat_map<uint64_t, BufferRelease> buffer_releases_;
  167. #if BUILDFLAG(USE_ARC_PROTECTED_MEDIA)
  168. ProtectedBufferState protected_buffer_state_ = ProtectedBufferState::UNKNOWN;
  169. #endif // BUILDFLAG(USE_ARC_PROTECTED_MEDIA)
  170. };
  171. class SolidColorBuffer : public Buffer {
  172. public:
  173. SolidColorBuffer(const SkColor4f& color, const gfx::Size& size);
  174. SolidColorBuffer(const SolidColorBuffer& buffer) = delete;
  175. SolidColorBuffer& operator=(const SolidColorBuffer&) = delete;
  176. ~SolidColorBuffer() override;
  177. SkColor4f GetColor() const override;
  178. gfx::Size GetSize() const override;
  179. bool ProduceTransferableResource(
  180. FrameSinkResourceManager* resource_manager,
  181. std::unique_ptr<gfx::GpuFence> acquire_fence,
  182. bool secure_output_only,
  183. viz::TransferableResource* resource,
  184. ProtectedNativePixmapQueryDelegate* protected_native_pixmap_query,
  185. PerCommitExplicitReleaseCallback per_commit_explicit_release_callback)
  186. override;
  187. private:
  188. SkColor4f color_;
  189. gfx::Size size_;
  190. };
  191. } // namespace exo
  192. #endif // COMPONENTS_EXO_BUFFER_H_