fake_window.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2014 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 ANDROID_WEBVIEW_BROWSER_GFX_TEST_FAKE_WINDOW_H_
  5. #define ANDROID_WEBVIEW_BROWSER_GFX_TEST_FAKE_WINDOW_H_
  6. #include <map>
  7. #include "android_webview/browser/gfx/hardware_renderer.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. #include "ui/gl/gl_context.h"
  14. #include "ui/gl/gl_surface.h"
  15. namespace base {
  16. class WaitableEvent;
  17. }
  18. namespace android_webview {
  19. class BrowserViewRenderer;
  20. class CompositorFrameConsumer;
  21. class FakeFunctor;
  22. class RenderThreadManager;
  23. class WindowHooks {
  24. public:
  25. virtual ~WindowHooks() {}
  26. virtual void WillOnDraw() = 0;
  27. virtual void DidOnDraw(bool success) = 0;
  28. virtual FakeFunctor* GetFunctor() = 0;
  29. virtual void WillSyncOnRT() = 0;
  30. virtual void DidSyncOnRT() = 0;
  31. virtual void WillProcessOnRT() = 0;
  32. virtual void DidProcessOnRT() = 0;
  33. virtual bool WillDrawOnRT(HardwareRendererDrawParams* params) = 0;
  34. virtual void DidDrawOnRT() = 0;
  35. };
  36. class FakeWindow {
  37. public:
  38. FakeWindow(BrowserViewRenderer* view, WindowHooks* hooks, gfx::Rect location);
  39. FakeWindow(const FakeWindow&) = delete;
  40. FakeWindow& operator=(const FakeWindow&) = delete;
  41. ~FakeWindow();
  42. void Detach();
  43. void RequestInvokeGL(FakeFunctor* functor, bool wait_for_completion);
  44. void PostInvalidate();
  45. const gfx::Size& surface_size() { return surface_size_; }
  46. void RequestDrawGL(FakeFunctor* functor);
  47. bool on_draw_hardware_pending() const { return on_draw_hardware_pending_; }
  48. scoped_refptr<base::SingleThreadTaskRunner> render_thread_task_runner() {
  49. return render_thread_loop_;
  50. }
  51. private:
  52. class ScopedMakeCurrent;
  53. void OnDrawHardware();
  54. void CheckCurrentlyOnUIThread();
  55. void CreateRenderThreadIfNeeded();
  56. void InitializeOnRT(base::WaitableEvent* sync);
  57. void DestroyOnRT(base::WaitableEvent* sync);
  58. void InvokeFunctorOnRT(FakeFunctor* functor, base::WaitableEvent* sync);
  59. void ProcessSyncOnRT(FakeFunctor* functor, base::WaitableEvent* sync);
  60. void ProcessDrawOnRT(FakeFunctor* functor);
  61. void DrawFunctorOnRT(FakeFunctor* functor, base::WaitableEvent* sync);
  62. void CheckCurrentlyOnRT();
  63. // const so can be used on both threads.
  64. const raw_ptr<BrowserViewRenderer> view_;
  65. const raw_ptr<WindowHooks> hooks_;
  66. const gfx::Size surface_size_;
  67. // UI thread members.
  68. gfx::Rect location_;
  69. bool on_draw_hardware_pending_;
  70. base::SequenceChecker ui_checker_;
  71. // Render thread members.
  72. base::SequenceChecker rt_checker_;
  73. scoped_refptr<base::SingleThreadTaskRunner> render_thread_loop_;
  74. scoped_refptr<gl::GLSurface> surface_;
  75. scoped_refptr<gl::GLContext> context_;
  76. bool context_current_ = false;
  77. base::WeakPtrFactory<FakeWindow> weak_ptr_factory_{this};
  78. };
  79. class FakeFunctor {
  80. public:
  81. FakeFunctor();
  82. ~FakeFunctor();
  83. void Init(FakeWindow* window,
  84. std::unique_ptr<RenderThreadManager> render_thread_manager);
  85. void Sync(const gfx::Rect& location, WindowHooks* hooks);
  86. void Draw(WindowHooks* hooks);
  87. void Invoke(WindowHooks* hooks);
  88. CompositorFrameConsumer* GetCompositorFrameConsumer();
  89. void ReleaseOnUIWithInvoke();
  90. void ReleaseOnUIWithoutInvoke(base::OnceClosure callback);
  91. private:
  92. bool RequestInvokeGL(bool wait_for_completion);
  93. void ReleaseOnRT(base::OnceClosure callback);
  94. raw_ptr<FakeWindow> window_;
  95. std::unique_ptr<RenderThreadManager> render_thread_manager_;
  96. gfx::Rect committed_location_;
  97. };
  98. } // namespace android_webview
  99. #endif // ANDROID_WEBVIEW_BROWSER_GFX_TEST_FAKE_WINDOW_H_