gles2_decoder_helper.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "media/gpu/gles2_decoder_helper.h"
  5. #include <memory>
  6. #include "base/check_op.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/threading/thread_checker.h"
  9. #include "gpu/command_buffer/common/gles2_cmd_utils.h"
  10. #include "gpu/command_buffer/common/mailbox.h"
  11. #include "gpu/command_buffer/service/context_group.h"
  12. #include "gpu/command_buffer/service/decoder_context.h"
  13. #include "gpu/command_buffer/service/mailbox_manager.h"
  14. #include "ui/gl/gl_context.h"
  15. #include "ui/gl/scoped_binders.h"
  16. using gpu::gles2::AbstractTexture;
  17. namespace media {
  18. class GLES2DecoderHelperImpl : public GLES2DecoderHelper {
  19. public:
  20. explicit GLES2DecoderHelperImpl(gpu::DecoderContext* decoder)
  21. : decoder_(decoder) {
  22. DCHECK(decoder_);
  23. gpu::gles2::ContextGroup* group = decoder_->GetContextGroup();
  24. mailbox_manager_ = group->mailbox_manager();
  25. DCHECK(mailbox_manager_);
  26. }
  27. GLES2DecoderHelperImpl(const GLES2DecoderHelperImpl&) = delete;
  28. GLES2DecoderHelperImpl& operator=(const GLES2DecoderHelperImpl&) = delete;
  29. bool MakeContextCurrent() override {
  30. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  31. return decoder_->MakeCurrent();
  32. }
  33. std::unique_ptr<AbstractTexture> CreateTexture(GLenum target,
  34. GLenum internal_format,
  35. GLsizei width,
  36. GLsizei height,
  37. GLenum format,
  38. GLenum type) override {
  39. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  40. DCHECK(decoder_->GetGLContext()->IsCurrent(nullptr));
  41. std::unique_ptr<AbstractTexture> texture =
  42. decoder_->CreateAbstractTexture(target, internal_format, width, height,
  43. 1, // depth
  44. 0, // border
  45. format, type);
  46. texture->SetParameteri(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  47. texture->SetParameteri(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  48. texture->SetParameteri(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  49. texture->SetParameteri(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  50. // TODO(sandersd): Do we always want to allocate for GL_TEXTURE_2D?
  51. if (target == GL_TEXTURE_2D) {
  52. gl::ScopedTextureBinder scoped_binder(target, texture->service_id());
  53. glTexImage2D(target, // target
  54. 0, // level
  55. internal_format, // internal_format
  56. width, // width
  57. height, // height
  58. 0, // border
  59. format, // format
  60. type, // type
  61. nullptr); // data
  62. }
  63. return texture;
  64. }
  65. gl::GLContext* GetGLContext() override {
  66. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  67. return decoder_->GetGLContext();
  68. }
  69. gpu::Mailbox CreateMailbox(AbstractTexture* texture) override {
  70. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  71. gpu::Mailbox mailbox = gpu::Mailbox::Generate();
  72. mailbox_manager_->ProduceTexture(mailbox, texture->GetTextureBase());
  73. return mailbox;
  74. }
  75. void ProduceTexture(const gpu::Mailbox& mailbox,
  76. AbstractTexture* texture) override {
  77. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  78. mailbox_manager_->ProduceTexture(mailbox, texture->GetTextureBase());
  79. }
  80. private:
  81. raw_ptr<gpu::DecoderContext> decoder_;
  82. raw_ptr<gpu::MailboxManager> mailbox_manager_;
  83. THREAD_CHECKER(thread_checker_);
  84. };
  85. // static
  86. std::unique_ptr<GLES2DecoderHelper> GLES2DecoderHelper::Create(
  87. gpu::DecoderContext* decoder) {
  88. return std::make_unique<GLES2DecoderHelperImpl>(decoder);
  89. }
  90. } // namespace media