codec_image.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2017 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 MEDIA_GPU_ANDROID_CODEC_IMAGE_H_
  5. #define MEDIA_GPU_ANDROID_CODEC_IMAGE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/gtest_prod_util.h"
  11. #include "base/memory/ref_counted_delete_on_sequence.h"
  12. #include "gpu/command_buffer/service/ref_counted_lock.h"
  13. #include "gpu/command_buffer/service/stream_texture_shared_image_interface.h"
  14. #include "media/gpu/android/codec_output_buffer_renderer.h"
  15. #include "media/gpu/android/promotion_hint_aggregator.h"
  16. #include "media/gpu/media_gpu_export.h"
  17. namespace base {
  18. namespace android {
  19. class ScopedHardwareBufferFenceSync;
  20. } // namespace android
  21. } // namespace base
  22. namespace media {
  23. // A GLImage that renders MediaCodec buffers to a TextureOwner or overlay
  24. // as needed in order to draw them. Note that when DrDc is enabled(kEnableDrDc),
  25. // a per codec dr-dc lock is expected to be held while calling methods of this
  26. // class. This is ensured by adding AssertAcquiredDrDcLock() to those methods.
  27. // We are not adding a Locked suffix on those methods since many of those
  28. // methods are either overrides or virtual.
  29. class MEDIA_GPU_EXPORT CodecImage
  30. : public gpu::StreamTextureSharedImageInterface,
  31. gpu::RefCountedLockHelperDrDc {
  32. public:
  33. // Callback to notify that a codec image is now unused in the sense of not
  34. // being out for display. This lets us signal interested folks once a video
  35. // frame is destroyed and the sync token clears, so that that CodecImage may
  36. // be re-used. Once legacy mailboxes go away, SharedImageVideo can manage all
  37. // of this instead.
  38. //
  39. // Also note that, presently, only destruction does this. However, with
  40. // pooling, there will be a way to mark a CodecImage as unused without
  41. // destroying it.
  42. using UnusedCB = base::OnceCallback<void(CodecImage*)>;
  43. CodecImage(const gfx::Size& coded_size,
  44. scoped_refptr<gpu::RefCountedLock> drdc_lock);
  45. CodecImage(const CodecImage&) = delete;
  46. CodecImage& operator=(const CodecImage&) = delete;
  47. // (Re-)Initialize this CodecImage to use |output_buffer| et. al.
  48. //
  49. // May be called on a random thread, but only if the CodecImage is otherwise
  50. // not in use.
  51. void Initialize(
  52. std::unique_ptr<CodecOutputBufferRenderer> output_buffer_renderer,
  53. bool is_texture_owner_backed,
  54. PromotionHintAggregator::NotifyPromotionHintCB promotion_hint_cb);
  55. // Add a callback that will be called when we're marked as unused. Does not
  56. // replace previous callbacks. Order of callbacks is not guaranteed.
  57. void AddUnusedCB(UnusedCB unused_cb);
  58. // gl::GLImage implementation
  59. gfx::Size GetSize() override;
  60. unsigned GetInternalFormat() override;
  61. unsigned GetDataType() override;
  62. BindOrCopy ShouldBindOrCopy() override;
  63. bool BindTexImage(unsigned target) override;
  64. void ReleaseTexImage(unsigned target) override;
  65. bool CopyTexImage(unsigned target) override;
  66. bool CopyTexSubImage(unsigned target,
  67. const gfx::Point& offset,
  68. const gfx::Rect& rect) override;
  69. void SetColorSpace(const gfx::ColorSpace& color_space) override {}
  70. void Flush() override {}
  71. void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
  72. uint64_t process_tracing_id,
  73. const std::string& dump_name) override;
  74. std::unique_ptr<base::android::ScopedHardwareBufferFenceSync>
  75. GetAHardwareBuffer() override;
  76. // If we re-use one CodecImage with different output buffers, then we must
  77. // not claim to have mutable state. Otherwise, CopyTexImage is only called
  78. // once. For pooled shared images, this must return false. For single-use
  79. // images, it works either way.
  80. bool HasMutableState() const override;
  81. // Notify us that we're no longer in-use for display, and may be pointed at
  82. // another output buffer via a call to Initialize.
  83. void NotifyUnused();
  84. // gpu::StreamTextureSharedImageInterface implementation.
  85. void ReleaseResources() override;
  86. bool IsUsingGpuMemory() const override;
  87. void UpdateAndBindTexImage(GLuint service_id) override;
  88. bool HasTextureOwner() const override;
  89. gpu::TextureBase* GetTextureBase() const override;
  90. void NotifyOverlayPromotion(bool promotion, const gfx::Rect& bounds) override;
  91. // Renders this image to the overlay. Returns true if the buffer is in the
  92. // overlay front buffer. Returns false if the buffer was invalidated.
  93. bool RenderToOverlay() override;
  94. bool TextureOwnerBindsTextureOnUpdate() override;
  95. // Whether the codec buffer has been rendered to the front buffer.
  96. bool was_rendered_to_front_buffer() const {
  97. return output_buffer_renderer_
  98. ? output_buffer_renderer_->was_rendered_to_front_buffer()
  99. : false;
  100. }
  101. // Whether this image is backed by a texture owner.
  102. bool is_texture_owner_backed() const { return is_texture_owner_backed_; }
  103. scoped_refptr<gpu::TextureOwner> texture_owner() const {
  104. return output_buffer_renderer_ ? output_buffer_renderer_->texture_owner()
  105. : nullptr;
  106. }
  107. // Renders this image to the front buffer of its backing surface.
  108. // Returns true if the buffer is in the front buffer. Returns false if the
  109. // buffer was invalidated. After an image is invalidated it's no longer
  110. // possible to render it.
  111. bool RenderToFrontBuffer();
  112. // Renders this image to the back buffer of its texture owner. Only valid if
  113. // is_texture_owner_backed(). Returns true if the buffer is in the back
  114. // buffer. Returns false if the buffer was invalidated.
  115. // RenderToTextureOwnerBackBuffer() will not block if there is any previously
  116. // pending frame and will return false in this case.
  117. bool RenderToTextureOwnerBackBuffer();
  118. // Release any codec buffer without rendering, if we have one.
  119. virtual void ReleaseCodecBuffer();
  120. CodecOutputBuffer* get_codec_output_buffer_for_testing() const {
  121. return output_buffer_renderer_
  122. ? output_buffer_renderer_->get_codec_output_buffer_for_testing()
  123. : nullptr;
  124. }
  125. protected:
  126. ~CodecImage() override;
  127. private:
  128. FRIEND_TEST_ALL_PREFIXES(CodecImageTest, RenderAfterUnusedDoesntCrash);
  129. std::unique_ptr<CodecOutputBufferRenderer> output_buffer_renderer_;
  130. // Renders this image to the texture owner front buffer by first rendering
  131. // it to the back buffer if it's not already there, and then waiting for the
  132. // frame available event before calling UpdateTexImage().
  133. // Also bind the latest image
  134. // to the provided |service_id| if TextureOwner does not binds texture on
  135. // update. If |bindings_mode| is other than kEnsureTexImageBound, then
  136. // |service_id| is not required.
  137. bool RenderToTextureOwnerFrontBuffer(BindingsMode bindings_mode,
  138. GLuint service_id);
  139. // Whether this image is texture_owner or overlay backed.
  140. bool is_texture_owner_backed_ = false;
  141. // The bounds last sent to the overlay.
  142. gfx::Rect most_recent_bounds_;
  143. // Coded size of the image.
  144. gfx::Size coded_size_;
  145. // Callback to notify about promotion hints and overlay position.
  146. PromotionHintAggregator::NotifyPromotionHintCB promotion_hint_cb_;
  147. std::vector<UnusedCB> unused_cbs_;
  148. // Bound to the gpu main thread on which this CodecImage is created. Some
  149. // methods can only be called on this thread.
  150. THREAD_CHECKER(gpu_main_thread_checker_);
  151. };
  152. // Temporary helper class to prevent touching a non-threadsafe-ref-counted
  153. // CodecImage off the gpu main thread, while still holding a reference to it.
  154. // Passing a raw pointer around isn't safe, since stub destruction could still
  155. // destroy the consumers of the codec image.
  156. class MEDIA_GPU_EXPORT CodecImageHolder
  157. : public base::RefCountedDeleteOnSequence<CodecImageHolder>,
  158. public gpu::RefCountedLockHelperDrDc {
  159. public:
  160. CodecImageHolder(scoped_refptr<base::SequencedTaskRunner> task_runner,
  161. scoped_refptr<CodecImage> codec_image,
  162. scoped_refptr<gpu::RefCountedLock> drdc_lock);
  163. CodecImageHolder(const CodecImageHolder&) = delete;
  164. CodecImageHolder& operator=(const CodecImageHolder&) = delete;
  165. // Safe from any thread.
  166. CodecImage* codec_image_raw() const { return codec_image_.get(); }
  167. private:
  168. virtual ~CodecImageHolder();
  169. friend class base::RefCountedDeleteOnSequence<CodecImageHolder>;
  170. friend class base::DeleteHelper<CodecImageHolder>;
  171. scoped_refptr<CodecImage> codec_image_;
  172. };
  173. } // namespace media
  174. #endif // MEDIA_GPU_ANDROID_CODEC_IMAGE_H_