codec_output_buffer_renderer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2020 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_OUTPUT_BUFFER_RENDERER_H_
  5. #define MEDIA_GPU_ANDROID_CODEC_OUTPUT_BUFFER_RENDERER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "gpu/command_buffer/service/ref_counted_lock.h"
  9. #include "gpu/command_buffer/service/stream_texture_shared_image_interface.h"
  10. #include "media/gpu/android/codec_buffer_wait_coordinator.h"
  11. #include "media/gpu/android/codec_wrapper.h"
  12. #include "media/gpu/media_gpu_export.h"
  13. namespace media {
  14. // A class that holds CodecOutputBuffer and renders it to TextureOwner or
  15. // overlay as necessary. Unit tests for this class are part of CodecImage unit
  16. // tests. Note that when DrDc is enabled(kEnableDrDc),
  17. // a per codec dr-dc lock is expected to be held while calling methods of this
  18. // class. This is ensured by adding AssertAcquiredDrDcLock() to those methods.
  19. class MEDIA_GPU_EXPORT CodecOutputBufferRenderer
  20. : public gpu::RefCountedLockHelperDrDc {
  21. public:
  22. using BindingsMode = gpu::StreamTextureSharedImageInterface::BindingsMode;
  23. CodecOutputBufferRenderer(
  24. std::unique_ptr<CodecOutputBuffer> output_buffer,
  25. scoped_refptr<CodecBufferWaitCoordinator> codec_buffer_wait_coordinator,
  26. scoped_refptr<gpu::RefCountedLock> drdc_lock);
  27. ~CodecOutputBufferRenderer();
  28. CodecOutputBufferRenderer(const CodecOutputBufferRenderer&) = delete;
  29. CodecOutputBufferRenderer& operator=(const CodecOutputBufferRenderer&) =
  30. delete;
  31. // Renders this image to the overlay. Returns true if the buffer is in the
  32. // overlay front buffer. Returns false if the buffer was invalidated.
  33. bool RenderToOverlay();
  34. // Renders this image to the texture owner front buffer by first rendering
  35. // it to the back buffer if it's not already there, and then waiting for the
  36. // frame available event before calling UpdateTexImage().
  37. // Also bind the latest imagecto the provided |service_id| if TextureOwner
  38. // does not binds texture on update. If |bindings_mode| is other than
  39. // kEnsureTexImageBound, then |service_id| is not required.
  40. bool RenderToTextureOwnerFrontBuffer(BindingsMode bindings_mode,
  41. GLuint service_id);
  42. // Renders this image to the front buffer of its backing surface.
  43. // Returns true if the buffer is in the front buffer. Returns false if the
  44. // buffer was invalidated. After an image is invalidated it's no longer
  45. // possible to render it.
  46. bool RenderToFrontBuffer();
  47. // Renders this image to the back buffer of its texture owner. Only valid if
  48. // is_texture_owner_backed(). Returns true if the buffer is in the back
  49. // buffer. Returns false if the buffer was invalidated.
  50. // RenderToTextureOwnerBackBuffer() will not block if there is any previously
  51. // pending frame and will return false in this case.
  52. bool RenderToTextureOwnerBackBuffer();
  53. // Whether the codec buffer has been rendered to the front buffer.
  54. bool was_rendered_to_front_buffer() const {
  55. AssertAcquiredDrDcLock();
  56. return phase_ == Phase::kInFrontBuffer;
  57. }
  58. gfx::Size size() const { return output_buffer_->size(); }
  59. // Color space of the image.
  60. const gfx::ColorSpace& color_space() const {
  61. return output_buffer_->color_space();
  62. }
  63. bool was_tex_image_bound() const { return was_tex_image_bound_; }
  64. scoped_refptr<gpu::TextureOwner> texture_owner() const {
  65. return codec_buffer_wait_coordinator_
  66. ? codec_buffer_wait_coordinator_->texture_owner()
  67. : nullptr;
  68. }
  69. CodecOutputBuffer* get_codec_output_buffer_for_testing() const {
  70. return output_buffer_.get();
  71. }
  72. private:
  73. friend class FrameInfoHelperTest;
  74. // The lifecycle phases of an buffer.
  75. // The only possible transitions are from left to right. Both
  76. // kInFrontBuffer and kInvalidated are terminal.
  77. enum class Phase { kInCodec, kInBackBuffer, kInFrontBuffer, kInvalidated };
  78. // Ensure that the latest image is bound to the texture |service_id| if
  79. // TextureOwner does not binds texture on update. If TextureOwner binds
  80. // texture on update, then it will always be bound to the TextureOwners
  81. // texture and |service_id| will be ignored.
  82. void EnsureBoundIfNeeded(BindingsMode mode, GLuint service_id);
  83. void set_phase_for_testing(Phase phase) { phase_ = phase; }
  84. // The phase of the image buffer's lifecycle.
  85. Phase phase_ = Phase::kInCodec;
  86. // The buffer backing this image.
  87. std::unique_ptr<CodecOutputBuffer> output_buffer_;
  88. // The CodecBufferWaitCoordinator that |output_buffer_| will be rendered to.
  89. // Or null, if this image is backed by an overlay.
  90. scoped_refptr<CodecBufferWaitCoordinator> codec_buffer_wait_coordinator_;
  91. bool was_tex_image_bound_ = false;
  92. };
  93. } // namespace media
  94. #endif // MEDIA_GPU_ANDROID_CODEC_OUTPUT_BUFFER_RENDERER_H_