in_process_context_provider.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2013 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 UI_COMPOSITOR_TEST_IN_PROCESS_CONTEXT_PROVIDER_H_
  5. #define UI_COMPOSITOR_TEST_IN_PROCESS_CONTEXT_PROVIDER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/observer_list.h"
  11. #include "base/synchronization/lock.h"
  12. #include "base/threading/thread_checker.h"
  13. #include "components/viz/common/gpu/context_lost_observer.h"
  14. #include "components/viz/common/gpu/context_provider.h"
  15. #include "components/viz/common/gpu/raster_context_provider.h"
  16. #include "gpu/command_buffer/common/context_creation_attribs.h"
  17. #include "gpu/ipc/common/surface_handle.h"
  18. #include "ipc/raster_in_process_context.h"
  19. #include "ui/gfx/native_widget_types.h"
  20. namespace gpu {
  21. class GLInProcessContext;
  22. class GpuMemoryBufferManager;
  23. class ImageFactory;
  24. class ImplementationBase;
  25. }
  26. namespace skia_bindings {
  27. class GrContextForGLES2Interface;
  28. }
  29. namespace ui {
  30. // TODO(crbug.com/1292507): Merge into viz::TestInProcessContextProvider once
  31. // on-screen context support is no longer needed.
  32. class InProcessContextProvider
  33. : public base::RefCountedThreadSafe<InProcessContextProvider>,
  34. public viz::ContextProvider,
  35. public viz::RasterContextProvider {
  36. public:
  37. // Uses default attributes for creating an offscreen context. If `is_worker`
  38. // is true then the context will support locking and OOP-R (through
  39. // RasterInterface) and won't support GLES2 or GrContext.
  40. static scoped_refptr<InProcessContextProvider> CreateOffscreen(
  41. gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
  42. gpu::ImageFactory* image_factory,
  43. bool is_worker);
  44. InProcessContextProvider(const InProcessContextProvider&) = delete;
  45. InProcessContextProvider& operator=(const InProcessContextProvider&) = delete;
  46. // viz::ContextProvider / viz::RasterContextProvider implementation.
  47. void AddRef() const override;
  48. void Release() const override;
  49. gpu::ContextResult BindToCurrentThread() override;
  50. const gpu::Capabilities& ContextCapabilities() const override;
  51. const gpu::GpuFeatureInfo& GetGpuFeatureInfo() const override;
  52. gpu::gles2::GLES2Interface* ContextGL() override;
  53. gpu::raster::RasterInterface* RasterInterface() override;
  54. gpu::ContextSupport* ContextSupport() override;
  55. class GrDirectContext* GrContext() override;
  56. gpu::SharedImageInterface* SharedImageInterface() override;
  57. viz::ContextCacheController* CacheController() override;
  58. base::Lock* GetLock() override;
  59. void AddObserver(viz::ContextLostObserver* obs) override;
  60. void RemoveObserver(viz::ContextLostObserver* obs) override;
  61. // Gives the GL internal format that should be used for calling CopyTexImage2D
  62. // on the default framebuffer.
  63. uint32_t GetCopyTextureInternalFormat();
  64. // Calls OnContextLost() on all observers. This doesn't modify the context.
  65. void SendOnContextLost();
  66. private:
  67. friend class base::RefCountedThreadSafe<InProcessContextProvider>;
  68. InProcessContextProvider(
  69. const gpu::ContextCreationAttribs& attribs,
  70. gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
  71. gpu::ImageFactory* image_factory,
  72. bool support_locking);
  73. ~InProcessContextProvider() override;
  74. void CheckValidThreadOrLockAcquired() const;
  75. base::ThreadChecker main_thread_checker_;
  76. base::ThreadChecker context_thread_checker_;
  77. std::unique_ptr<gpu::GLInProcessContext> gles2_context_;
  78. std::unique_ptr<gpu::RasterInProcessContext> raster_context_;
  79. raw_ptr<gpu::ImplementationBase> impl_base_ = nullptr;
  80. // Initialized only when `gles2_context_` is used.
  81. std::unique_ptr<skia_bindings::GrContextForGLES2Interface> gr_context_;
  82. std::unique_ptr<gpu::raster::RasterInterface> gles2_raster_impl_;
  83. std::unique_ptr<viz::ContextCacheController> cache_controller_;
  84. const bool support_locking_;
  85. bool bind_tried_ = false;
  86. gpu::ContextResult bind_result_;
  87. gpu::ContextCreationAttribs attribs_;
  88. raw_ptr<gpu::ImageFactory> image_factory_;
  89. base::Lock context_lock_;
  90. base::ObserverList<viz::ContextLostObserver>::Unchecked observers_;
  91. };
  92. } // namespace ui
  93. #endif // UI_COMPOSITOR_TEST_IN_PROCESS_CONTEXT_PROVIDER_H_