command_buffer_helper.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2018 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_COMMAND_BUFFER_HELPER_H_
  5. #define MEDIA_GPU_COMMAND_BUFFER_HELPER_H_
  6. #include "base/callback_forward.h"
  7. #include "base/memory/ref_counted_delete_on_sequence.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "base/task/sequenced_task_runner.h"
  10. #include "base/task/sequenced_task_runner_helpers.h"
  11. #include "build/build_config.h"
  12. #include "gpu/command_buffer/common/mailbox.h"
  13. #include "gpu/command_buffer/common/sync_token.h"
  14. #include "media/gpu/media_gpu_export.h"
  15. #include "ui/gl/gl_bindings.h"
  16. namespace gpu {
  17. class CommandBufferStub;
  18. class DXGISharedHandleManager;
  19. class SharedImageBacking;
  20. class SharedImageRepresentationFactoryRef;
  21. class SharedImageStub;
  22. class TextureBase;
  23. } // namespace gpu
  24. namespace gl {
  25. class GLContext;
  26. class GLImage;
  27. } // namespace gl
  28. namespace media {
  29. // TODO(sandersd): CommandBufferHelper does not inherently need to be ref
  30. // counted, but some clients want that (VdaVideoDecoder and PictureBufferManager
  31. // both hold a ref to the same CommandBufferHelper). Consider making an owned
  32. // variant.
  33. class MEDIA_GPU_EXPORT CommandBufferHelper
  34. : public base::RefCountedDeleteOnSequence<CommandBufferHelper> {
  35. public:
  36. REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
  37. using WillDestroyStubCB = base::OnceCallback<void(bool have_context)>;
  38. // TODO(sandersd): Consider adding an Initialize(stub) method so that
  39. // CommandBufferHelpers can be created before a stub is available.
  40. static scoped_refptr<CommandBufferHelper> Create(
  41. gpu::CommandBufferStub* stub);
  42. CommandBufferHelper(const CommandBufferHelper&) = delete;
  43. CommandBufferHelper& operator=(const CommandBufferHelper&) = delete;
  44. // Gets the associated GLContext.
  45. //
  46. // Used by DXVAVDA to test for D3D11 support, and by V4L2VDA to create
  47. // EGLImages. New clients should use more specialized accessors instead.
  48. virtual gl::GLContext* GetGLContext() = 0;
  49. // Retrieve the interface through which to create shared images.
  50. virtual gpu::SharedImageStub* GetSharedImageStub() = 0;
  51. #if BUILDFLAG(IS_WIN)
  52. virtual gpu::DXGISharedHandleManager* GetDXGISharedHandleManager() = 0;
  53. #endif
  54. // Checks whether the stub has been destroyed.
  55. virtual bool HasStub() = 0;
  56. // Makes the GL context current.
  57. virtual bool MakeContextCurrent() = 0;
  58. // Register a shared image backing
  59. virtual std::unique_ptr<gpu::SharedImageRepresentationFactoryRef> Register(
  60. std::unique_ptr<gpu::SharedImageBacking> backing) = 0;
  61. virtual gpu::TextureBase* GetTexture(GLuint service_id) const = 0;
  62. // Creates a texture and returns its |service_id|.
  63. //
  64. // See glTexImage2D() for argument definitions.
  65. //
  66. // The texture will be configured as a video frame: linear filtering, clamp to
  67. // edge. If |target| is GL_TEXTURE_2D, storage will be allocated but not
  68. // initialized.
  69. //
  70. // It is up to the caller to initialize the texture before providing it to the
  71. // renderer, else the results are undefined.
  72. //
  73. // The context must be current.
  74. //
  75. // TODO(sandersd): Is really necessary to allocate storage? GpuVideoDecoder
  76. // does this, but it's not clear that any clients require it.
  77. virtual GLuint CreateTexture(GLenum target,
  78. GLenum internal_format,
  79. GLsizei width,
  80. GLsizei height,
  81. GLenum format,
  82. GLenum type) = 0;
  83. // Destroys a texture.
  84. //
  85. // The context must be current.
  86. virtual void DestroyTexture(GLuint service_id) = 0;
  87. // Sets the cleared flag on level 0 of the texture.
  88. virtual void SetCleared(GLuint service_id) = 0;
  89. // Binds level 0 of the texture to an image.
  90. //
  91. // If the sampler binding already exists, set |client_managed| to true.
  92. // Otherwise set it to false, and BindTexImage()/CopyTexImage() will be called
  93. // when the texture is used.
  94. virtual bool BindImage(GLuint service_id,
  95. gl::GLImage* image,
  96. bool client_managed) = 0;
  97. // Creates a mailbox for a texture.
  98. //
  99. // TODO(sandersd): Specify the behavior when the stub has been destroyed. The
  100. // current implementation returns an empty (zero) mailbox. One solution would
  101. // be to add a HasStub() method, and not define behavior when it is false.
  102. virtual gpu::Mailbox CreateMailbox(GLuint service_id) = 0;
  103. // Produce a texture into a mailbox. The context does not have to be current.
  104. // However, this will fail if the stub has been destroyed.
  105. virtual void ProduceTexture(const gpu::Mailbox& mailbox,
  106. GLuint service_id) = 0;
  107. // Waits for a SyncToken, then runs |done_cb|.
  108. //
  109. // |done_cb| may be destructed without running if the stub is destroyed.
  110. //
  111. // TODO(sandersd): Currently it is possible to lose the stub while
  112. // PictureBufferManager is waiting for all picture buffers, which results in a
  113. // decoding softlock. Notification of wait failure (or just context/stub lost)
  114. // is probably necessary.
  115. virtual void WaitForSyncToken(gpu::SyncToken sync_token,
  116. base::OnceClosure done_cb) = 0;
  117. // Set the callback to be called when our stub is destroyed. This callback
  118. // may not change the current context.
  119. virtual void SetWillDestroyStubCB(WillDestroyStubCB will_destroy_stub_cb) = 0;
  120. // Is the backing command buffer passthrough (versus validating).
  121. virtual bool IsPassthrough() const = 0;
  122. // Does this command buffer support ARB_texture_rectangle.
  123. virtual bool SupportsTextureRectangle() const = 0;
  124. protected:
  125. explicit CommandBufferHelper(
  126. scoped_refptr<base::SequencedTaskRunner> task_runner);
  127. // TODO(sandersd): Deleting remaining textures upon destruction requires
  128. // making the context current, which may be undesireable. Consider adding an
  129. // explicit DestroyWithContext() API.
  130. virtual ~CommandBufferHelper() = default;
  131. private:
  132. friend class base::DeleteHelper<CommandBufferHelper>;
  133. friend class base::RefCountedDeleteOnSequence<CommandBufferHelper>;
  134. };
  135. } // namespace media
  136. #endif // MEDIA_GPU_COMMAND_BUFFER_HELPER_H_