mailbox_to_surface_bridge_impl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 COMPONENTS_WEBXR_MAILBOX_TO_SURFACE_BRIDGE_IMPL_H_
  5. #define COMPONENTS_WEBXR_MAILBOX_TO_SURFACE_BRIDGE_IMPL_H_
  6. #include "base/callback.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "device/vr/android/mailbox_to_surface_bridge.h"
  11. #include "gpu/command_buffer/common/sync_token.h"
  12. #include "gpu/ipc/common/surface_handle.h"
  13. #include "ui/gfx/buffer_format_util.h"
  14. #include "ui/gfx/gpu_fence.h"
  15. #include "ui/gl/android/scoped_java_surface.h"
  16. namespace gpu {
  17. class ContextSupport;
  18. namespace gles2 {
  19. class GLES2Interface;
  20. }
  21. } // namespace gpu
  22. namespace viz {
  23. class ContextProvider;
  24. }
  25. namespace webxr {
  26. class MailboxToSurfaceBridgeImpl : public device::MailboxToSurfaceBridge {
  27. public:
  28. // It's OK to create an object instance and pass it to a different thread,
  29. // i.e. to enable dependency injection for a unit test, but all methods on it
  30. // must be called consistently on a single GL thread. This is verified by
  31. // DCHECKs.
  32. MailboxToSurfaceBridgeImpl();
  33. MailboxToSurfaceBridgeImpl(const MailboxToSurfaceBridgeImpl&) = delete;
  34. MailboxToSurfaceBridgeImpl& operator=(const MailboxToSurfaceBridgeImpl&) =
  35. delete;
  36. ~MailboxToSurfaceBridgeImpl() override;
  37. bool IsConnected() override;
  38. bool IsGpuWorkaroundEnabled(int32_t workaround) override;
  39. void CreateSurface(gl::SurfaceTexture*) override;
  40. void CreateAndBindContextProvider(base::OnceClosure callback) override;
  41. // All other public methods below must be called on the GL thread
  42. // (except when marked otherwise).
  43. void ResizeSurface(int width, int height) override;
  44. bool CopyMailboxToSurfaceAndSwap(const gpu::MailboxHolder& mailbox) override;
  45. bool CopyMailboxToSurfaceAndSwap(const gpu::MailboxHolder& mailbox,
  46. const gfx::Transform& uv_transform) override;
  47. void GenSyncToken(gpu::SyncToken* out_sync_token) override;
  48. void WaitSyncToken(const gpu::SyncToken& sync_token) override;
  49. void WaitForClientGpuFence(gfx::GpuFence*) override;
  50. void CreateGpuFence(const gpu::SyncToken& sync_token,
  51. base::OnceCallback<void(std::unique_ptr<gfx::GpuFence>)>
  52. callback) override;
  53. gpu::MailboxHolder CreateSharedImage(
  54. gpu::GpuMemoryBufferImplAndroidHardwareBuffer* buffer,
  55. const gfx::ColorSpace& color_space,
  56. uint32_t usage) override;
  57. void DestroySharedImage(const gpu::MailboxHolder& mailbox_holder) override;
  58. private:
  59. void BindContextProviderToCurrentThread();
  60. void OnContextAvailableOnUiThread(
  61. scoped_refptr<viz::ContextProvider> provider);
  62. void InitializeRenderer();
  63. void DestroyContext();
  64. void DrawQuad(unsigned int textureHandle, const gfx::Transform& uv_transform);
  65. scoped_refptr<viz::ContextProvider> context_provider_;
  66. std::unique_ptr<gl::ScopedJavaSurface> surface_;
  67. raw_ptr<gpu::gles2::GLES2Interface> gl_ = nullptr;
  68. raw_ptr<gpu::ContextSupport> context_support_ = nullptr;
  69. int surface_handle_ = gpu::kNullSurfaceHandle;
  70. // TODO(https://crbug.com/836524): shouldn't have both of these closures
  71. // in the same class like this.
  72. base::OnceClosure on_context_bound_;
  73. int surface_width_ = 0;
  74. int surface_height_ = 0;
  75. // If true, surface width/height is the intended size that should be applied
  76. // to the surface once it's ready for use.
  77. bool needs_resize_ = false;
  78. // A swap ID which is passed to GL swap. Incremented each call.
  79. uint64_t swap_id_ = 0;
  80. // Uniform handle for the UV transform used by DrawQuad.
  81. uint32_t uniform_uv_transform_handle_ = 0;
  82. // A task runner for the GL thread
  83. scoped_refptr<base::SingleThreadTaskRunner> gl_thread_task_runner_;
  84. // Must be last.
  85. base::WeakPtrFactory<MailboxToSurfaceBridgeImpl> weak_ptr_factory_{this};
  86. };
  87. class MailboxToSurfaceBridgeFactoryImpl
  88. : public device::MailboxToSurfaceBridgeFactory {
  89. public:
  90. std::unique_ptr<device::MailboxToSurfaceBridge> Create() const override;
  91. };
  92. } // namespace webxr
  93. #endif // COMPONENTS_WEBXR_MAILBOX_TO_SURFACE_BRIDGE_IMPL_H_